# AcquisitionCostAdjustment

## Description

AcquisitionCostAdjustment is the register of acquisition-cost variance events for ordering document lines, and the audit record of how the cumulative variance was distributed over receipt cost layers. One header row is created per non-zero line the `postAcquisitionCostAdjustment` call carries — each with the generic cause reference (`sourceType`/`sourceId`/`sourceLineId` — the AP document line or the purchase order revision, never a typed foreign key), the purchase order and line it concerns (`purchaseOrderId`/`purchaseOrderLineId`), the signed `amount` the source adds to the line's cumulative variance, the call's variance kind as its `kind`, and the effective date — and one zero-amount REDISTRIBUTION row per receipt posting that triggered a redistribution (source type INVENTORY_LEDGER, referencing the receipt ledger entry). The sum of `amount` over an order line is the variance its receipts must carry; events recorded before the first receipt simply wait in the register until a receipt posts. The record carries no item or company of its own — both are reached through the receipt cost layers its allocations reference.

AcquisitionCostAdjustmentAllocation records how much of the variance each posting distributed to each cost layer: up to one row per affected [CostLayer](./CostLayer.md) and `varianceKind`, carrying the signed `amount` of that kind's variance the adjustment put on that layer. The sum of allocation amounts per cost layer is the amount that layer already carries, which is the baseline the next redistribution posts against; the sum per kind is how much of that kind's register total has been distributed, which decides how the posting's wash splits between the two clearing accounts. How the layer's amount splits across debit accounts (on-hand vs consumed, and by consumption kind) is decided when the journal is posted and is not persisted here.

Adjustments carry no lifecycle: they are posted once, immutable, and corrected by posting further events (a credit memo or PRICE or QUANTITY correction produces a new record with a negative amount, and the redistribution self-corrects the layers).

## Domain Model Definitions

### Model type

AppendOnly

### Command Definitions

- Created internally by [postAcquisitionCostAdjustment](../command/PostAcquisitionCostAdjustment.md) and by purchase receipt postings via [postInventoryLedger](../command/PostInventoryLedger.md); no direct command

### Query Definitions

- Adjustments are an audit record; no read query yet

### Models

- AcquisitionCostAdjustment
- AcquisitionCostAdjustmentAllocation

### Invariants

- Rows are append-only; a posted adjustment is never modified or deleted, and corrections are expressed as new events
- Each adjustment carries a generic cause reference (`sourceType`, `sourceId`, optional `sourceLineId`): the AP document line that declared an invoice price variance change, the purchase order revision that declared an order price change, or the receipt ledger entry that triggered a redistribution; these are generic references, not typed foreign keys
- Each adjustment carries the purchase order and line it concerns (`purchaseOrderId`/`purchaseOrderLineId`); these are generic references, not typed foreign keys into purchase
- `amount` is the signed change of the order line's cumulative acquisition-cost variance, computed by the declaring module; zero only on receipt-triggered redistribution rows; a zero-amount declaration creates no record
- `kind` names the clearing account the amount washes against — INVOICE_PRICE (the invoice price variance account) or ORDER_PRICE (the accrual) — or is REDISTRIBUTION on the zero-amount receipt-triggered rows
- The sum of `amount` over an order line equals the cumulative variance its resolved layers' allocations must sum to once receipts exist
- The record holds no item or company; both are reached through the cost layers its allocations reference
- Each allocation belongs to one adjustment, references one CostLayer, and names the variance kind it distributes as its `varianceKind`
- A posting writes up to one allocation per touched cost layer and kind; a layer's allocations sum to its signed delta — its received-quantity share of the cumulative variance minus what its allocations already carry — and a kind's allocations sum to its register total once distributed

### Relationships

- **References cause document (generic)**: sourceType/sourceId/sourceLineId reach the AP document line, the purchase order revision, or the receipt ledger entry that triggered the posting, without a typed foreign key
- **References purchase order line (generic)**: `purchaseOrderId`/`purchaseOrderLineId` reach the purchase order line whose receipt layers are adjusted, without a typed foreign key
- **Has many AcquisitionCostAdjustmentAllocation**: Every cost layer a distribution touches is recorded as an allocation
- **AcquisitionCostAdjustmentAllocation References CostLayer**: Each allocation adjusts exactly one cost layer
