# UC-08 Retrieve Specific PDF Report Or PDF Templet (Module 3) - Architectural Mapping

This document maps the visual components from the **UC-08 Retrieve Specific PDF Report Or PDF Templet** PlantUML diagram to the Laravel backend implementation, emphasizing the robust **Template Method** design pattern and **SOLID/DRY** principles.

## 1. Actors & Routing
* **Actors**: PRHeader, Quality, Admin.
* **Routing**: The endpoint `GET /reports/{code}/pdf` is protected by `role:PRHeader,Quality,Admin` middleware. The PR Employee role is strictly excluded at the routing layer. 
* Additionally, there is a dedicated endpoint for single responses `GET /responses/{id}/pdf` for the specific survey sheets.

## 2. "Check CanExportPdf Permission" (<<include>>) & "Show Permission Denied Error"
* **Database-Driven Logic**: Just because an actor can *view* a report doesn't mean they can *export* it. The `ReportController::exportPdf` method relies on the `ReportPermissionService::canExportPdf($user, $code)` service.
* **Denial (HTTP 403)**: If the database record indicates `CanExportPdf = false` for the user's role and requested report code, the application immediately aborts with a 403 Permission Denied exception.

## 3. "Select Report Code & Apply Filters Or specific Survey"
* **Query Parameters**: Filters like `?branchId=3&cycleId=5&from=...` are passed directly into shared Data Services (e.g., `EmployeeAuditDataService`, `ResponseSheetDataService`).
* **Dual MVC/API (DRY)**: These identical Data Services are used by both the Flutter API to get JSON and the Web Controller to render the PDF. Data aggregation logic is completely abstracted from the controller.

## 4. "Server Generates PDF (FPDF/DomPDF)" 
This implements two major design patterns to satisfy the **SOLID** requirement:
* **Strategy / Registry Pattern**: The `PdfRenderService` acts as a dispatcher. It uses a registry array mapping string codes (like `RPT-RESPONSE` or `RPTCYCLEANALYSIS`) to specific PDF generator classes. This allows adding new report types without modifying the core renderer logic (**Open/Closed Principle**).
* **Template Method Pattern**: All PDF reports extend an abstract base class `AbstractPdfReport`. 
    * The base class defines the fixed document skeleton in a `generate()` method (Header -> Filters Bar -> Body -> Footer -> toPdf).
    * Subclasses (like `ResponseSheetPdf`, `CycleAnalysisPdf`) only override the specific methods that change, primarily `renderBody()`. This prevents huge blocks of repetitive HTML/PDF scaffolding across different report types (**DRY Principle**).

## 5. "Download PDF File"
* The `PdfRenderService` finalizes the document and streams it back via Laravel's HTTP response object, applying `Content-Type: application/pdf` and the appropriate `Content-Disposition` header (e.g., attachment with dynamic date-stamped filenames) so the Flutter client can seamlessly download it.
