# Phase 44 — Financial Period Close & Month-End Control

## Purpose
Phase 44 adds a professional accounting close framework to the existing PHP/MySQLi microfinance system. It does not replace the General Ledger; it controls when the ledger may accept postings and preserves audit-ready close evidence.

## Workspace
Open `/financial-close` under **Finance & Control → Accounting → Financial Period Close**.

Tabs:
- Control Centre
- Periods
- Close Checklist
- Adjustments
- Reversals
- Close Snapshots

## Upgrade
1. Back up the database.
2. Import `sql/migration_044_financial_period_close.sql` after Phase 43.
3. Replace the application files.
4. Open `/financial-close` and create accounting periods for each branch.

## Posting-lock design
`AccountingService` now calls `PeriodCloseService::assertPostingAllowed()` before automatic and manual GL posting. Journal approval also re-checks the journal entry date before changing a manual journal to posted.

- Open/normal close-preparation period: posting allowed.
- Pending close approval: posting frozen.
- Closed period: posting blocked.
- Period not yet configured: posting remains allowed for backward compatibility.

The last rule prevents an upgrade from unexpectedly stopping live operations before accounting periods have been configured.

## Close checklist
Each period receives the standard checklist automatically:
1. Cash counts and cash-register reconciliation
2. Bank reconciliation
3. Loan control reconciliation
4. Savings control reconciliation
5. AP/AR review
6. Payroll/statutory review
7. Fixed assets/depreciation review
8. ECL/provisioning review
9. Inter-branch clearing review
10. Suspense/exception review
11. Trial-balance review
12. Management close review

Required items must be completed or explicitly marked Not Applicable with documented evidence before close submission.

## Close workflow
1. Finance completes the checklist and close adjustments.
2. Maker submits the period for close.
3. The period is immediately frozen against new postings.
4. An independent checker approves or rejects the close.
5. Approval freezes a trial-balance snapshot and marks the period closed.
6. Rejection returns the period to open status.

The close requester cannot approve the same close request.

## Close blockers
A period cannot be submitted/approved while it has:
- incomplete required checklist controls;
- draft/pending manual journals in the period;
- pending period adjustments;
- due scheduled reversals that have not posted;
- earlier open periods in the same fiscal year when closing a year-end period.

## Close adjustments
Supported types:
- accrual;
- prepayment;
- deferral;
- reclassification;
- estimate;
- other.

Every adjustment uses separate debit and credit GL accounts, must fall inside the selected accounting period, and requires independent approval before posting.

## Scheduled reversals
An approved adjustment can carry a future reversal date. When due:
1. A user prepares due reversals.
2. The schedule enters independent approval.
3. A different checker approves the reversing journal.
4. The original adjustment maker cannot approve its reversal.

The reversal also respects the posting status of the reversal date's accounting period.

## Close snapshots
Every approved close creates a versioned snapshot containing, by GL account:
- opening balance;
- period debits;
- period credits;
- closing balance.

The snapshot also stores total debits, total credits, period profit/loss, who generated it, timestamp, and a SHA-256 integrity hash. If a period is later reopened and closed again, the old snapshot is retained and a new version is created.

## Year-end close
Periods marked `Year-End` transfer the branch's income and expense balances into GL account `3100 Retained Earnings` when the close is approved.

The fiscal-year start is derived from the earliest configured period for that branch/fiscal year rather than assuming every institution uses a January start.

## Controlled reopening
Reopening a closed period requires:
- a mandatory documented reason;
- a separate control request;
- independent approval.

For a year-end period, approval of the reopen request first posts an exact reversal of the prior year-end closing journal, then unlocks the period. The existing close snapshot remains available as historical audit evidence.

## Files added/updated
- `sql/migration_044_financial_period_close.sql`
- `src/PeriodCloseService.php`
- `src/AccountingService.php`
- `views/financial_close.php`
- `views/accounting.php`
- `views/layout_top.php`
- `public/index.php`
- `public/assets/app.css`
- `public/service-worker.js`
- `sql/schema.sql`

## Validation
- 126 PHP files passed `php -l`.
- Service-worker and application JavaScript passed Node syntax validation.
- The cumulative fresh-install schema contains 507 named foreign-key constraints with no duplicate constraint names.
- Phase 44 table presence was validated in the cumulative schema.
- ZIP integrity is checked before release.
