# ═══════════════════════════════════════════════════════════════
# PHASE 10: ADVANCED FINANCE MODULE
# Module directory: /modules/advanced_finance/
# ═══════════════════════════════════════════════════════════════
# READ THE MASTER PROMPT FIRST.

## YOUR TABLES
installment_plan_templates, installment_plans, installment_plan_schedules,
installment_payments, credit_notes, credit_note_items, refunds, write_offs,
checks, check_status_history, bank_accounts, bank_deposits, bank_deposit_items,
gl_accounts, gl_journal_entries, gl_journal_entry_lines, gl_account_balances,
budget_heads

## PAGES TO BUILD

### 1. Installment Plans (`GET /advanced-finance/installments`)
- DataTable with vw_installment_overview data
- Create plan: member, invoice, template or custom, down payment, schedule generation
- Call `sp_create_installment_plan`
- View plan with payment schedule
- Record installment payment
- Mark defaulted

### 2. Installment Templates (`GET /advanced-finance/installment-templates`)
- CRUD for installment_plan_templates

### 3. Checks (`GET /advanced-finance/checks`)
- DataTable: Check#, Bank, Drawer, Amount, Date, Status
- Receive check registration
- Status workflow: received → deposited → cleared/bounced
- Bounce handling with fee
- Replacement check linking
- Scan upload (front/back)

### 4. Credit Notes (`GET /advanced-finance/credit-notes`)
- Create credit note for member
- Link to original invoice
- Application to outstanding invoices

### 5. Refunds (`GET /advanced-finance/refunds`)
- Create refund request
- Approval workflow
- Processing with bank reference

### 6. Write-offs (`GET /advanced-finance/write-offs`)
- Create write-off (requires board resolution)
- Approval and posting

### 7. Bank Accounts (`GET /advanced-finance/bank-accounts`)
- CRUD for bank_accounts

### 8. Bank Deposits (`GET /advanced-finance/deposits`)
- Create deposit slip (group cash + checks)
- Mark as deposited/confirmed
- Bank reference recording

### 9. General Ledger (`GET /advanced-finance/gl`)
- Chart of Accounts (gl_accounts tree view)
- Journal Entries CRUD
- Post/reverse entries
- Account balance view per period

### 10. Budgets (`GET /advanced-finance/budgets`)
- Budget heads per fiscal year per account
- Quarterly breakdown
- Actual vs budget comparison
- Variance reporting

## BUSINESS LOGIC
- Installment payment updates schedule and plan (DB trigger handles this)
- Check status changes tracked in history
- Credit note application reduces invoice balance
- Write-off posts journal entry
- GL entries must balance (total debit = total credit)
- Budget variance calculated on-the-fly

## DASHBOARD WIDGETS
1. **Overdue Installments** — Count
2. **Pending Checks** — Deposited but not cleared
3. **GL Balance** — Cash account balance

## PERMISSIONS
installments.view, installments.create,
refunds.create, refunds.approve,
gl.view, gl.manage, budget.view, budget.manage