# Reports, Exports and Account Ledgers

## User Workflow

Run the report with the required branch, dates, times and other filters first.
Use Print, Export Excel or Download PDF in its document header. Exports run the
same authorised report request again and retain its filters. Print opens a
separate clean A4 landscape document rather than printing the application shell.
Excel is an actual XLSX workbook, not CSV with a different extension.

Business details come from Business Settings and the selected accessible branch.
When several branches are included, the accessible main branch supplies contact
details and the document lists the permitted branches included in the report.
The configured business tax PIN is included when available. A report does not
invent missing addresses or contacts.

The Sales end-of-day popup uses the sales period filters, including shift periods.
Its Summary/Detailed selector is retained in the exported document. The central
end-of-day report includes payment-method totals and expenses as well as sales.

## Access Controls

Existing module, report, own/all and branch authorisation still runs on the report
route. `reports.print` controls printing. `reports.export` controls Excel and PDF.
The Sales shift popup also requires `sales.end_day_report`.
Changing an export URL does not bypass these checks.

## Customer and Supplier Ledgers

- Customers: Action > Customer Statement > View Ledger.
- Suppliers: Action > Supplier Statement / Ledger.
- Select dates/times, branch and optionally Include Invoice Items.
- Opening balance is calculated from recorded entries before From.
- Transactions show debit, credit, running DR/CR balance, payment method and notes.
- Invoices paid immediately still show invoice and payment movements without
  double-counting their settlement.
- Returns and reversals remain dated movements, not silently removed values.
- Customer advances remain separately labelled as a current available balance;
  application/restoration movements enter the receivable ledger.
- If the role has only own-invoice access, its ledger is labelled as a restricted
  view rather than presented as the complete customer/supplier account.

Ledgers derive movements from existing accounting postings. If historical
invoices have no postings, a visible warning identifies that limitation. This
feature never fabricates or silently backfills accounting entries. Compare such
records with the invoice summary and arrange an accounting review/backfill.
Included item details are explicitly current invoice details, not a reconstructed
historical item snapshot. Invoice payment status is also the current status.

## Developer Map

| File | Responsibility |
| --- | --- |
| `app/Services/ReportDocumentService.php` | Report registry, business/period headings, safe extraction, PDF and XLSX generation |
| `app/Http/Middleware/ReportDocuments.php` | Add the shared toolbar to supported report views; authorise and deliver exports |
| `app/Http/Kernel.php` | Register the report-document web middleware |
| `resources/views/reports/document_tools.blade.php` | On-screen document header and three export links |
| `resources/views/reports/document.blade.php` | Clean printable/PDF document and table styling |
| `app/Services/PartyLedgerService.php` | Scoped journal movements, opening balance and running balances |
| `resources/views/accounts/party_ledger.blade.php` | Customer/supplier ledger filters, summary and transaction table |
| `app/Http/Controllers/CustomerController.php` | Customer invoice-summary/ledger selection |
| `app/Http/Controllers/SupplierController.php` | Supplier ledger entry point |
| `resources/views/partials/contact_index.blade.php` | Contact action menu links |
| `app/Http/Controllers/ReportController.php` | Main report queries; obsolete 100/200/300-row cutoffs removed |
| `resources/views/reports/index.blade.php` | Main report tables, end-of-day payments/expenses and totals |
| `resources/views/sales/index.blade.php` | End-of-day popup selector and export links |

Add a new report view to `ReportDocumentService::definition()` with its title and
the XPath of the data container. Do not select a settings form or a page that
displays one-time tokens. Export cleaning removes scripts, editing forms, closed
dialogs and application controls. Remote resources and PHP evaluation are disabled
in Dompdf. Spreadsheet values are explicit strings unless a numeric report cell
can be safely converted; barcodes and formula-like text remain strings.

Exports request all pagination entries. If a paginated report exceeds the
100,000-row export ceiling, the user is told to narrow the filters instead of
receiving a silently truncated document. Use shorter periods for large datasets.

## Deployment and Tests

No schema migration, database replacement or licence change is required.
Back up client files, apply the matching update in a quiet window, run
`php artisan optimize:clear`, and test printing/exports on that installation.
Do not replace client `.env`, APP_KEY, signing configuration or live database.

Tests run through `tools/test-split-bill-payments.php` against a randomly named
temporary database restored from the clean client template. They refuse the
live database and remove their temporary database on completion.

`tests/Feature/ReportDocumentsTest.php` covers report headers, real PDF/XLSX,
the report catalogue, permissions/branches, running balances, returns/reversals,
already-paid invoices, supplier returns, uncapped report rows, safe spreadsheet
strings and selected shift-report sections. `tools/verify-report-documents.cjs`
checks the rendered toolbar and JavaScript at desktop/mobile sizes.
