# CostLayer

## Description

CostLayer represents a record of inventory cost for FIFO (First In, First Out) valuation. Each cost layer is created when a GOODS_RECEIPT InventoryLedger entry is recorded for an item under a FIFO valuation policy, capturing the quantity received and its unit cost. Cost layers are generated by the receiveStock command (both normal and `toBlocked=true` modes create cost layers the same way). When goods are issued, cost layers are consumed in oldest-first order, with the remainingQuantity field tracking the unconsumed portion.

Cost layer fields are immutable after creation, with the sole exception of remainingQuantity, which is decremented as stock is consumed.

## Domain Model Definitions

### Model type

Standard

### Command Definitions

- Cost layers are created internally when a GOODS_RECEIPT InventoryLedger is recorded under a FIFO valuation policy (via [receiveStock](../commands/ReceiveStock.md)), and consumed when a GOODS_ISSUE InventoryLedger is recorded (via [issueStock](../commands/IssueStock.md))

### Query Definitions

- [listCostLayers](../queries/ListCostLayers.md) - List cost layers by item or valuation policy for FIFO auditing

### Models

- CostLayer

### Invariants

- Created on GOODS_RECEIPT InventoryLedger for items under a FIFO valuation policy
- Consumed in oldest-first order on GOODS_ISSUE InventoryLedger
- Remaining quantity tracks the unconsumed portion of the original layer
- All fields except remainingQuantity are immutable after creation
- remainingQuantity cannot go below zero

### Relationships

- **References Item**: Each cost layer belongs to an Item from the item-management module
- **References ValuationPolicy**: Each cost layer is associated with a FIFO valuation policy
- **References InventoryLedger**: Each cost layer is linked to the InventoryLedger entry that created it (via inventoryLedgerId)
