# Wallet-ledger integration plan — services that move money without wallets

Scope note: this is the design-first deliverable for audit category C. Nothing here is
coded yet; each section proposes schema/service changes that need a sign-off before
implementation, because they decide *which wallet* real cash flows through.

Conventions assumed (matching every already-integrated module):

- Movement goes through `WalletService::credit/debit` with a tagged `reference_type`
  (e.g. `salary_payout`) and the record id as `reference_id`.
- The moving dialog carries a `wallet_id` picker (default-cash fallback); short-pay
  rollbacks abort atomically; reversals pay back into the same wallet.
- Every feed surfaces the landed wallet *name* via the ledger leg.

## 1. Salary payouts (`SalaryController::markSalaryPaid`)

Today: a payslip is flagged paid; no cash leg anywhere.

Proposal:

- Accepted `wallet_id` on the pay action (`nullable|exists:wallets,id`), resolved
  against `Wallet::defaultCash()`.
- On the transition `paid`: `WalletService::debit($wallet, $salary->net_amount,
  'salary_payout', $salary->id, "Salary payout — {$period} ({$employee->name})")`,
  stamped `wallet_id` + `wallet_transaction_id` on the salary row (needs two nullable
  columns on `salaries`, same as `loans`/`vendor_payments`).
- Un-marking/reversal (admin): credit back into the same wallet with
  `salary_payout_reversal`.
- A drained wallet aborts the mark-paid at 422 — same atomic rollback the vendor
  approvals use.

## 2. Employee advances (`AdvanceController`)

Two legs, both milestone-tagged:

- `advance_granted` — debit the wallet when the advance is approved/issued.
- `advance_recovered` — credit the wallet for each payroll recovery (partial recovery
  rows may each move money).
- Rejections bounce back with `advance_granted_reversal`.
- `advances` table gains `wallet_id` + `wallet_transaction_id`; recovery rows carry
  their own leg where the ledger row ids matter (`advance_recoveries.wallet_transaction_id`).

## 3. Investor module (investments / withdrawals / distributions)

Investor cash is custody money — it should sit in a *fund* wallet, not the operating
cash wallet. Proposal:

- One wallet per investor fund marker OR reuse the company-fund wallets
  (`wallets.is_fund` already exists). Recommended: a dedicated "Investor custody"
  wallet per investor pool, resolved from a new `fund_wallet_id` on `investors`
  (nullable → company fund wallet fallback).
- `InvestmentController::store` → credit `investment_made`, stamped on the investment.
- `InvestmentWithdrawalController` → debit `investment_withdrawal` when approved.
- `ProfitDistributionController` → per-investor `distribution_paid` debits (or credits,
  depending on direction) with the distribution row id as reference; batch runs wrap
  the whole distribution in one transaction so a partially starving pool aborts atomically.
- `investments`, `investment_withdrawals`, `profit_distributions` gain
  `wallet_id`/`wallet_transaction_id` columns (same migration pattern).

## 4. Inventory-for-cash sales (`InventorySaleController`)

Sale proceeds bypass the shared ledger today. Proposal:

- Mirror the POS checkout money leg: on sale completion, `WalletService::credit`
  `inventory_sale` per tender; the sale row keeps `wallet_id` +
  `wallet_transaction_id` (columns already exist on the shared `payments` pattern —
  `inventory_sales` needs them itself).
- A later POS-style due-collection endpoint would reuse the contractor-collection
  pattern; refunds use `inventory_sale_reversal` into the wallet the leg came from.
- `sales` feed/detail surfaces the wallet name the same way the earnings feeds do.

## Open decisions (need a yes before coding)

1. Payroll/advances debit **wages wallet** (separate petty pool) or the operating cash
   wallet by default?2. Investor custody: one shared custody wallet vs per-investor wallets (affects the
   custody NAV reporting and the finance-module reconciliation diff).
3. Should inventory-sale proceeds respect a "designated sales wallet" setting rather
   than default cash?

## Verification plan per section (once approved)

- Feature tests mirroring `WalletIntegrationLoansTest` / `WalletIntegrationVendorSupplierPaymentTest`
  conventions: movement + tagging + rollebacks + reversal + feed surfacing.
- Extend `WalletReconciliationService` expectations: every new `reference_type` must
  reconcile `wallets.balance` against its ledger sum zero-diff.
