# Loan & Property-Loan Wallet Movement Policy

Applies to the personal loans module (`loans` + `LoanPaymentController`) and the
property loans module (`LoanApplication` / `LoanAccount`). Every rule follows the
house contractor pattern: the physical cash moves on a real wallet through the
locked, transactional `WalletService`, each ledger row is tagged with the
money-side row id, and a wallet that cannot fund a pay-out aborts the whole
operation atomically with a 422.

## Direction map (cash in vs cash out)

| Module | Event | Money | Wallet leg | Ledger reference type |
| --- | --- | --- | --- | --- |
| Personal loan | `store` of a `taken` loan (borrows from someone) | cash **in** | credit | `loan_received` |
| Personal loan | `approve` of a `given` loan (lends money out) | cash **out** | debit | `loan_disbursement` |
| Personal loan | `store` repayment on a **given** loan | cash **in** | credit | `loan_repayment` |
| Personal loan | `store` repayment on a **taken** loan | cash **out** | debit | `loan_repayment` |
| Personal loan | `destroy` a repayment mirror row | opposite of the original leg | debit (returns credit) / credit (returns debit) | `loan_repayment_reversal` |
| Personal loan | `destroy` an unpaid loan that already moved | opposite of the original leg | debit / credit | `loan_disbursement_reversal` / `loan_received_reversal` |
| Property loan | `disburse` an approved application | cash **out** | debit | `property_loan_disbursement` |
| Property loan | `pay-installment` / `pay-multiple` receipts | cash **in** | credit | `property_loan_payment` |
| Property loan | `settle-early` (payoff received) | cash **in** | credit | `property_loan_settlement` |
| Property loan | `refinance` with a top-up amount | extra cash **out** for the top-up | debit | `property_loan_top_up` |

## Core rules

1. **One wallet movement per physical cash event.** Repayment receipts that are
   allocated across several installments make ONE wallet move; the installment
   rows only split bookkeeping.
2. **Wallet choice.** Callers may pass `wallet_id`; otherwise `Wallet::defaultCash()`
   is used. The choice and the transaction id are stamped on the money-side row.
3. **Pay-outs cannot overdraw.** A debit from a wallet short of funds throws
   inside the same DB transaction as the business write, so the loan status,
   schedules, mirrors and ledger all roll back; the caller sees a 422 with the
   explicit insufficient-balance message.
4. **Backfilled reference ids.** The ledger row is minted before its money-side
   payment row exists; the `reference_id` is stamped right after creation so
   `WalletService::reverseFor` and audits can trace it.
5. **Both directions are reversible.** Any endpoint that deletes a money row
   reverses its exact wallet leg (the ledger tells the direction), OCR'd by tag,
   and respects the same overdraw guard — deletes cannot conjure cash that was
   already spent elsewhere (422).
6. **Loans with recorded payments cannot be edited or deleted** (existing rule);
   reversal legs only run for rows with no dept history over them.
