Skip to main content
Version: v1.0.0(int)

org.materials_inventory_ledger

Append-only event log for materials movement. The summary view ("on-hand by material × location") is derived from this table; it isn't stored.

ER diagram

Columns

ColumnTypeNotes
idUUID PK
org_idUUID FKplatform.organizations(id).
codeTEXTNOT NULL. Row's own human-readable ID (e.g. MIL-001). Auto-generated when not supplied. UNIQUE per (org_id, code). Distinct from refcode identifies this row; ref is the external reference shared across rows tied to the same originating event.
material_idUUID FKNOT NULL. → org.materials(id) ON DELETE RESTRICT.
location_idUUID FKorg.locations(id) ON DELETE RESTRICT. The "from" location for transfers; the source/destination for other events. RESTRICT preserves audit integrity.
event_typematerials_inventory_event_typeNOT NULL. ENUM. See Event types below.
quantityNUMERIC(18,6)NOT NULL. CHECK > 0. Direction is encoded in event_type; quantity itself is always positive.
unit_costNUMERIC(18,6)Cost basis. Set at write time (FIFO/weighted-avg-resolved for outflows, vendor cost for inflows). NULL when not applicable.
refTEXTExternal reference (PO number, TFR-MI-###, etc.). Multiple rows can share the same ref.
to_location_idUUID FKorg.locations(id) ON DELETE RESTRICT. Set only for TRANSFER events.
notesTEXT
event_dateTIMESTAMPTZNOT NULL. Default NOW(). The real-world time the movement happened — distinct from created_at. Lets bulk imports stamp historical dates.
source_typeorg.ledger_source_kindENUM. Originating record kind: po_receipt, wo_receipt, so_item, manual.
source_idUUIDOriginating row id. (source_type, source_id) is the idempotency key. No DB-level FK — application-enforced.
created_atTIMESTAMPTZNOT NULL. Default NOW().

Constraints:

  • CHECK ((source_type IS NULL AND source_id IS NULL) OR (source_type IS NOT NULL AND source_id IS NOT NULL)) — both NULL (manual entry) or both set.

Event types

materials_inventory_event_type ENUM:

ValueMeaning
ORDERA PO was placed — future inflow committed but not yet received.
RECEIVEMaterial physically received (against a PO, or completing a transfer).
DEMANDA WO was placed — future outflow committed.
ALLOCATESoft reservation against a specific WO before consumption.
CONSUMEActual consumption when the WO progresses.
TRANSFERMaterial leaves location_id for to_location_id. Paired with a later RECEIVE sharing the same ref.
ADJUSTManual correction (positive quantity — i.e., "I miscounted, there are more"). Reductions use DISPOSE.
DISPOSEManual removal — write-off, damage, loss.
REVERSE_ORDERCancels an ORDER (e.g. PO transitioned to Cancelled).
REVERSE_RECEIVECancels a RECEIVE (e.g. PO receipt deleted, or transfer reversed).
REVERSE_DEMANDCancels a DEMAND (e.g. WO transitioned to Cancelled).
REVERSE_ALLOCATEReleases a previously written ALLOCATE (was RELEASE in earlier drafts; renamed for consistency).
REVERSE_CONSUMECancels a CONSUME (e.g. WO receipt deleted).
REVERSE_TRANSFERCancels a TRANSFER (e.g. an in-transit transfer is voided).

REVERSE_* rows store positive quantities — the schema's quantity > 0 invariant holds. Direction is encoded entirely in event_type; the summary query interprets reverse rows as netting their original counterpart. Each reverse row carries source_type and source_id matching the original entry so audit pairs are easy to find.

ref conventions

Event typeref value
RECEIVE (from PO)PO number
ORDER (from PO)PO number
TRANSFERTFR-MI-xxx (auto-generated by createLedgerEntry)
RECEIVE (completing transfer)Same TFR-MI-xxx as the paired TRANSFER
ADJUST / DISPOSENULL

Cross-references

  • Receives written by PO receipts (when the PO targets materials) and the manual transfer flow.
  • Allocations / consumes written by work orders progressing through their lifecycle.
  • Adjusts / disposes written by the manual flow on the Materials Inventory page.