# MAYA OS ENTERPRISE — TREASURY, BANKING, CHEQUES & LOANS LOGIC

## 1. Unified Cash, Bank & Voucher Pipeline

- **Bank Accounts**: Encrypted account numbers + blind SHA-256 duplicate index (`BankAccount`), 1:1 link to distinct Balance Sheet GL accounts, atomic inter-bank/cash transfers (`transfer_ref` idempotency), and direct bank charges/interest postings.
- **Cash & Imprest Custody**: Multi-location cash boxes (HO, Site Petty Cash, Employee Imprest) tracking NPR denomination physical counts, custody handovers, and 3-step `ImprestAdvance -> ImprestSettlement` clearing.

## 2. Cheque Book & Issued/Received Register Architecture

- Issued cheque uniqueness on MySQL uses the explicit nullable unique column `ChequeLeaf.issued_unique_key` (`uniq_issued_cheque_per_company_account`), preserving uniqueness for `ISSUED` cheques while allowing multiple blank/cancelled/received rows (`NULL`) with zero partial-index warnings.
- Cheque reservation uses `SELECT ... FOR UPDATE` row locking to prevent two concurrent cashiers from reserving or issuing the same leaf.
- Received post-dated cheques post through `Cheques-in-Hand` clearing -> Bank Deposit -> bounced reversal workflow.

## 3. Bank Reconciliation & NRB Loans / FX

- **Reconciliation Formula**:
  $$\text{Bank Statement Balance} + \text{Deposits in Transit} - \text{Unpresented Cheques} = \text{GL Book Balance}$$
- **Loans / OD / CC**: Tracks principal, sanctioned limit, Cash Credit drawing power haircuts, contract day-count (`ACT_365`, `ACT_360`, `30_360`) daily/monthly accrued interest, bank-debited variance settlement, and Bank Guarantees.
- **Multi-Currency**: Fetches official Nepal Rastra Bank rates (`https://www.nrb.org.np/api/forex/v1/rates`) normalized per unit (`INR=100`, `JPY=10`, `USD=1`) for transaction-date conversion and period-end unrealized FX revaluation.

