# Wallet Money Sources & Reconciliation Check

## The invariant

Every change to `wallets.balance` flows through `WalletService::apply()`, which
updates the balance and mints one `wallet_transactions` row in a single locked
transaction. Wallet creation writes any opening balance as a `manual` ledger
row, and `adjust()` is tagged `manual` too — so there is no balance mutation
outside the ledger. Therefore, for every wallet:

```
balance == Σ(direction='in') − Σ(direction='out')      [over wallet_transactions]
```

`GET /api/wallets/reconciliation` and `php artisan wallet:reconcile` check that
equation per wallet. A difference other than ±0.01 means money changed hands
outside the ledger (direct SQL, schema tooling, tampering) and must be
investigated.

## The full source trace (every credit/debit site in the backend)

| Reference tag | Ledger direction | Source / flow | Code path |
| --- | --- | --- | --- |
| `income` | in | Income entries recorded | `IncomeController::store` |
| `expense` | out | Approved expenses paying out | `ExpenseController` (approve) |
| `manual` | in / out | Wallet opening balances + manual adjustments | `WalletController::store`, `WalletController::adjust` |
| `reversal` | in / out | Automatic reversal of any reference (`reverseFor`) | `WalletService` |
| `contractor_collection` | in | Contractor billings collected | `ContractorServiceController::collect` |
| `contractor_payout` | out | Contractor payables paid | `ContractorServiceController` (pay) |
| `delivery_handover` | in / out | Courier cash-in-bag handovers | `DeliveryAssignmentController` |
| `delivery_bank_deposit` | in (bank) / out (cash) | Cash deposited to the bank | `DeliveryAssignmentController::deposit` |
| `booking_payment` | in / out | Storefront booking wallet legs | `Support/BookingEngine.php` |
| `customer_payment` | in | Customer due collections | `CustomerPaymentController::store` |
| `customer_payment_reversal` | out | Collections deleted — cash back out | `CustomerPaymentController::destroy` |
| `pos_due_payment` | in | Inventory POS due collections | `PosController::collectPosDue` |
| `pharmacy_due_payment` | in | Pharmacy POS due collections | `PharmacyPosController::collectDue` |
| `pharmacy_supplier_payment` | out | Pharmacy supplier payments | `PharmacyPurchaseController::storeSupplierPayment` |
| `property_due_payment` | in | Property POS due collections | `PropertyPosController::collectDue` |
| `vendor_payment` | out | Approved vendor payments | `VendorPaymentController::applyEffects` |
| `vendor_payment_reversal` | in | Applied vendor payments deleted | `VendorPaymentController::reversePayment` |
| `fleet_payment` | in | Fleet invoice collections | `FleetBillingController::collect` |
| `fleet_refund` | out | Fleet refunds | `FleetBillingController::refund` |
| `parking_payment` | in | Parking collections | `ParkingRentController::collect` |
| `parking_refund` | out | Parking refunds | `ParkingRentController::refund` |
| `rent_payment` | in | Property rent collections | `RentCollectionController::recordPayment` |
| `loan_received` | in | Money borrowed (taken loan) | `LoanController::store` |
| `loan_received_reversal` | out | Taken loan deleted | `LoanController::destroy` |
| `loan_disbursement` | out | Given loans disbursed | `LoanController::approve` |
| `loan_disbursement_reversal` | in | Given loan deleted | `LoanController::destroy` |
| `loan_repayment` | in (given) / out (taken) | Loan installments settled | `LoanPaymentController::store` |
| `loan_repayment_reversal` | in / out | Repayment rows deleted | `LoanPaymentController::destroy` |
| `property_loan_disbursement` | out | Property loans disbursed | `PropertyLoanController::disburse` |
| `property_loan_payment` | in | Property loan receipts | `PropertyLoanController::payInstallment` / `payMultiple` |
| `property_loan_settlement` | in | Early settlement payoff | `PropertyLoanController::settleEarly` |
| `property_loan_top_up` | out | Refinance top-ups | `PropertyLoanController::refinance` |
| `fund_payment` | in | Money added to a fund wallet | `FundController::storePayment` |
| `fund_withdrawal` | out | Approved fund withdrawals | `FundController` (approve) |

Anything found in the ledger that is not on this map is surfaced by the
reconciliation report as **UNKNOWN — untraced wallet movement source** so new
flows can be added to the map deliberately.

## The check

- `GET /api/wallets/reconciliation` (`perm:wallets.view`) — full report:
  `reconciliation` (per-wallet balance vs ledger net + drift list) and
  `source_breakdowns` (per-wallet, per-source in/out sums, each row pointing at
  its source label and file). Optional `wallet_id` narrows to one wallet.
- `php artisan wallet:reconcile [--wallet=ID]` — same per-wallet check as a
  table; **exits non-zero** when any wallet drifted, so a scheduled run can
  raise a bell/email.

Components: `App\Services\WalletReconciliationService` (the `$sources` map, the
`reconcile()` check, `sourceBreakdown()`), `WalletController::reconciliation`,
and the `wallet:reconcile` command.
