# CloudAudit accounting posting rules

## Posting policy

All amounts use decimal database values, never floating-point arithmetic. A journal is posted only when total debits equal total credits. Posted transactions and journals are immutable; correction creates a linked reversal or credit document. Accounts are configurable per organisation but posting rule templates are centrally versioned.

## Required default accounts

- Cash on hand, bank, accounts receivable, inventory asset, inventory adjustment gain/loss.
- Accounts payable, tax payable/receivable.
- Sales revenue, sales returns, discounts allowed, cost of goods sold.
- Purchase clearing/GRNI where used, operating expense accounts.

## Core posting matrix

| Event | Debit | Credit | Inventory movement |
|---|---|---|---|
| Cash sale | Cash/bank | Sales revenue + tax payable | decrease at cost |
| Credit sale | Accounts receivable | Sales revenue + tax payable | decrease at cost |
| COGS for sale | COGS | Inventory asset | decrease at valuation cost |
| Customer payment | Cash/bank | Accounts receivable | none |
| Sales return (resalable) | Sales returns / tax adjustment | Cash/bank or AR | increase at original cost |
| Goods receipt on credit | Inventory asset | Accounts payable or GRNI | increase at landed/unit cost |
| Cash purchase | Inventory asset | Cash/bank | increase at landed/unit cost |
| Supplier payment | Accounts payable | Cash/bank | none |
| Purchase return | Accounts payable / cash receivable | Inventory asset | decrease at carried cost |
| Stock shortage | Inventory adjustment loss | Inventory asset | decrease |
| Stock surplus | Inventory asset | Inventory adjustment gain | increase |
| Operating expense paid | Expense account | Cash/bank | none |

## Workflow states

```text
Draft → Submitted (optional) → Approved → Posted
                                  ↓
                             Reversed / Credited
```

- Draft data may be edited by an authorised creator.
- Approval validates permissions, period status, balance, stock policy, tax, and idempotency key.
- Posting is performed in one MySQL transaction: business document, stock movements, journals, balances/materialised summaries, audit event.
- Reversal creates a new posted document/journal with inverse lines and a link to the original; the original remains visible.

## Inventory valuation

MVP uses **weighted average cost** by product and warehouse. Each receipt updates the weighted cost; each outbound movement records the cost actually applied, so historic COGS is stable. FIFO, batches, expiry, serialisation, and landed-cost allocation are V1+ decisions and must not be partly implemented in MVP.

## Period controls and reconciliation

- Accounting periods can be open, soft-closed, or locked.
- Soft-close permits designated accountant adjustments and logs them; lock requires a reversal workflow or owner-authorised reopen.
- Daily controls: cash drawer total vs recorded cash payments; stock movement total vs stock on hand.
- Month-end controls: AR/AP ageing, bank reconciliation, inventory valuation vs ledger inventory, trial balance, retained earnings process.

