# Corporate Phase 6 Invoicing Audit

## Existing Finance / Payment Modules

### Individual Payments

`App\Models\Payment` and related receipt/review flows are designed around individual registrations, pre-enrollments, deposits, balances, receipts, and review states.

Decision: Phase 6 must not reuse `payments` as the corporate invoice/payment ledger. Corporate payments need separate records to avoid breaking individual enrollment flows.

### Financial Transactions

`App\Models\FinancialTransaction` already supports:

- `type`
- `source_type`
- `source_id`
- `amount_dzd`
- `occurred_at`
- `category`
- `organization_id`
- `corporate_agreement_id`
- `cohort_id`

Decision: Reuse `financial_transactions` only as the accounting/revenue summary layer after a corporate payment allocation is confirmed. Use `source_type = corporate_payment_allocation` and `source_id = allocation.id` to keep idempotency and avoid double counting.

### Finance Reports

Existing finance report pages aggregate general revenue/expenses and should remain separate from corporate receivables. Phase 6 adds a scoped Corporate Receivables report instead of mixing corporate invoices into student finance reports.

## Existing Corporate Data

The following canonical corporate models are ready for invoice linking:

- `Organization`
- `CorporateAgreement`
- `Session` where `cohort_type = corporate`
- `CorporateReport`

Invoices should use `Organization` as the customer account and may link to `CorporateAgreement`, a corporate cohort in `course_sessions`, and a final delivery report.

## What Phase 6 Creates

- `corporate_invoices`
- `corporate_invoice_items`
- `corporate_payments`
- `corporate_payment_allocations`
- `corporate_invoice_activities`
- `corporate_payment_activities`
- Invoice and payment reference generators.
- Invoice total calculation.
- Payment allocation/reconciliation service.
- Receivables and aging summary service.
- Private invoice PDF generation.
- Admin invoice/payment/receivables pages.
- Organization portal invoice list/download.
- Policies and permissions.
- Overdue command.

## What Phase 6 Must Not Touch

- Individual `payments` table and review workflow.
- Student deposit/balance logic.
- Public enrollment/payment routes.
- Legacy `companies`, `users.company_id`, or `agreement_path`.

## Security / IDOR Risks

1. Invoice PDF download must always go through a controller and `CorporateInvoicePolicy`.
2. Organization portal invoice queries must be scoped to `selected_organization_id`.
3. Payment receipts must be private files.
4. Payment allocation must verify invoice and payment belong to the same organization.
5. Support users may view invoices only if granted, but cannot review or allocate payments without explicit permissions.
6. Financial/receivable dashboards are finance-sensitive and require explicit corporate permissions.

## Implementation Direction

Build a corporate-only invoicing subsystem that publishes summarized revenue into `financial_transactions` after confirmed allocations. This keeps Phase 6 useful operationally without creating a full ERP or touching individual learner payments.
