SR-020 — Warehouse Stock Entry: Ledger & Approval Control System Requirements
| Metadata | Details |
|---|---|
| System Name | inventory-service — Admin Stock Entry (StockEntryService) |
| Document ID | SR-2026-INV-002 |
| Author | Engineering Team |
| Status | Approved |
Traces back to BRD-019 — Warehouse Stock Entry: Auditable Movement Recording & Approval Control.
See it first: Admin UI Walkthrough — Warehouse Stock Entry Console — the page-by-page, screenshot-anchored walkthrough of the admin UI that drives every endpoint this spec defines. SR-021 is that UI’s own requirement spec.
This document specifies inventory-service’s admin REST surface for recording, correcting, approving, rejecting, and cloning a warehouse stock movement (transport/api/stockentry/, service/stockentry/, domain/stockentry/), plus the bulk opening-entry initialization endpoint (domain/stockentryprocess/) — not the checkout-driven reservation/consumption path, which SR-019 already specifies and which this feature’s balance-mutation routine is shared with.
Functional Requirements
Recording a Movement — Six Operator-Facing Transaction Modes
- FR-01 (Six Named Transaction Modes, Each With Its Own UI Label): a stock entry MUST be recorded under exactly one of six operator-facing transaction modes — Purchase, Returns, Adjustment, Opening Entry, Internal Loss, Damage Stock — each independently selectable, each with its own human-readable label. A seventh mode, Sale, exists in the same enumeration but MUST NOT be selectable from the admin mode-selector — it is reachable only through the checkout-driven consumption path (SR-019), never through this admin surface.
- FR-02 (A Mode Enforces the Direction of Its Own Quantity Change): the quantity change recorded on a line item MUST be validated against its transaction mode’s required sign before the entry can be created or updated — Purchase, Returns, and Opening Entry each require a strictly positive quantity change; Internal Loss, Damage Stock, and Sale each require a strictly negative quantity change; Adjustment is the only mode that allows either sign, left to the operator to supply correctly. This sign rule MUST be enforced twice: once on the input at creation/update time, and again immediately before the balance itself is mutated at approval time — the second check exists so the balance can never be moved by a value that bypassed the first.
- FR-03 (A New Movement Is Recorded in a Waiting State, With No Immediate Effect on Stock): creating a stock entry MUST insert its header and line items in a
PENDINGstatus and MUST NOT change any item’s recorded stock count at the moment of creation — the recorded count only changes once the movement is explicitly approved (see FR-05).
Correcting or Withdrawing an Unapproved Movement
- FR-04 (A Pending Movement Can Be Corrected, an Already-Decided One Cannot): a stock entry’s header and line items MUST be editable only while its status is
PENDING— an update attempt against a movement that has already been approved or rejected MUST be rejected as a validation error, since an approved movement’s ledger effect is already permanent (see FR-06) and a rejected one is already closed.
Approval — Deferred, Transactional Balance Mutation
- FR-05 (Approval Is the Only Action That Changes Stock on Hand): approving a
PENDINGstock entry MUST, in a single database transaction, apply every line item’s signed quantity change to the affected item’s recorded stock balance, write one permanent ledger entry per line item capturing the balance immediately before and immediately after that change, and flip the movement’s own status toAPPROVEDtogether with who approved it and when — all three effects MUST succeed together or none of them MUST take effect. - FR-06 (Approval Reuses the Same Balance-Mutation Routine as Checkout Consumption): the balance mutation performed at approval time MUST be the identical warehouse-scoped stock-mutation routine SR-019 FR-06 already specifies for checkout’s consumption step, not a second, independently-written mutation path — an admin-recorded movement and a customer sale get the identical row-locking, sign-application, and ledger-write guarantee by construction.
- FR-07 (Approving an Opening-Entry Movement Marks That Item x Warehouse Pair as Initialized): approving a movement recorded under the Opening Entry mode MUST additionally mark every affected item x warehouse pair as initialized in a dedicated tracking record, in the same database transaction as the balance mutation — this mark is what prevents a later opening-entry initialization attempt for the same pair from being accepted a second time (see FR-11).
- FR-08 (Rejection Changes Only the Movement’s Status, Never Stock on Hand): rejecting a
PENDINGstock entry MUST change only that movement’s own status toREJECTED, together with who rejected it and when — it MUST have zero effect on any item’s recorded stock balance and MUST write no ledger entry, since the movement never took effect.
Retrieval, History, and Starting a New Movement From a Previous One
- FR-09 (Movements Are Listable and Filterable, and Individually Retrievable): the full set of recorded movements MUST be listable, paginated, and filterable by warehouse, by product, by transaction mode, and by status; any single movement MUST be individually retrievable by its own identifier.
- FR-10 (An Approved Movement’s Line-Item Balances Are Frozen, Never Recalculated on Read): retrieving a single movement that has reached
APPROVEDstatus MUST include, for each of its line items, the exact stock balance immediately before and immediately after that line’s change, read back from the permanent ledger entry written at approval time (FR-05) — these values MUST NOT be recalculated at read time; a movement that has not yet been approved has no such before/after values to show. (Gap — the feature intended to let an operator quickly start a new movement from a previous one does not yet copy the source movement’s line items; tracked as technical debt, see TD-00014.)
Opening-Entry Initialization — Bulk Setup and Its Guard
- FR-11 (An Item x Warehouse Pair Can Only Be Opening-Entry-Initialized Once): a bulk opening-entry initialization request for a given item x warehouse pair MUST be rejected if that pair has already been marked initialized (FR-07) — enforced as a database-level uniqueness guarantee on the underlying ledger record for that movement type, not merely an application-level check, so a retried or duplicated initialization request can never record a second opening balance for the same pair.
Immutability and Balance Invariants — Enforced at the Database
- FR-12 (A Written Ledger Entry Can Never Be Changed or Removed, Enforced by the Database Itself): once a ledger entry has been written, no update or delete against it MUST be able to succeed, under any circumstance, including an operator with full administrative access or a direct database connection — this MUST be enforced by the database itself rejecting the attempt, not solely by every application code path choosing never to attempt one.
- FR-13 (An Item’s Recorded Balance Can Never Go Negative or Become Internally Inconsistent): an item’s physical count, its reserved count, and its available count MUST each always be zero or greater, and the available count MUST always equal the physical count minus the reserved count — the database itself MUST reject any mutation that would leave these relationships untrue, not solely rely on service-layer logic never attempting one.
Named Gaps — Approval Control and Clone Completeness
- FR-14 (A Movement Flagged as Requiring Supervisor Review Is Not Yet Gated by Who Approves It): a stock entry can be flagged, at creation, as requiring supervisor review before approval — but today, approving or rejecting that flagged movement is not yet restricted to a distinct supervisor role or permission; any caller able to reach the approve/reject endpoint can act on a flagged movement the same as an unflagged one. (Gap — tracked as technical debt, see TD-00014.)
- FR-15 (Automatic, Publish-Triggered Opening-Entry Initialization Is Not Yet Wired — Though a Different Automated Caller Already Exists): the opening-entry initialization capability is documented, at the code level, as intended to trigger automatically and synchronously when a product is published from the catalog admin flow — but today no such automatic trigger exists anywhere between the catalog and inventory services. The bulk REST endpoint does have two real callers today: an operator acting directly in the admin UI, and the bulk tenant-catalog-migration pipeline, which dispatches CSV-sourced opening-entry records to this same endpoint as one stage of importing a tenant’s catalog — neither is the publish-triggered, synchronous integration the code-level doc-comment describes. (Gap — tracked as technical debt, see TD-00014.)
Non-Functional Requirements
Correctness Under Concurrent Approval
- NFR-01 (Approval’s Balance Mutation Inherits Checkout’s Own Concurrency Guarantee): because FR-06 requires approval to reuse checkout consumption’s own row-locking balance-mutation routine, an approval running concurrently with a checkout-driven consumption against the same item x warehouse pair MUST be safe under the identical row-lock and deterministic-ordering guarantees SR-019 NFR-01/NFR-02 already specify, without a separate concurrency mechanism maintained for this feature alone.
Multi-Tenancy & Auth Propagation
- NFR-02 (Who Recorded and Who Approved a Movement Comes From the Caller’s Own Auth Context, Never the Request Body): the identity recorded as having created, updated, approved, or rejected a movement MUST always be sourced from the authenticated caller’s own auth context, per the platform’s security standards — never trusted from a value supplied in the request payload itself.
Composition & Reuse
- NFR-03 (DTO-Based Entry Points): every Stock Entry service method MUST take a single input DTO rather than positional parameters, per the platform’s coding standards.
- NFR-04 (No Per-Mode Negation Logic Inside the Shared Balance Routine): the shared balance-mutation routine (FR-06) MUST apply a line item’s quantity change exactly as signed by FR-02’s validation — it MUST NOT contain mode-specific branching that re-derives or re-negates a value by transaction mode; the sign contract is enforced entirely at the boundaries (creation/update input, and immediately pre-mutation), never inside the mutation routine itself.
- NFR-05 (The Permanent Ledger Deliberately Omits Update Attribution): because a ledger entry can never be updated after being written (FR-12), its record MUST NOT carry an “updated by” / “updated at” pair the way the platform’s standard five-column audit contract otherwise requires everywhere else — an append-only record has nothing to attribute an update to. This is a deliberate, documented exception to the audit-column contract, not an oversight.
Related Documents
- BRD-019 — Warehouse Stock Entry: Auditable Movement Recording & Approval Control
- SR-021 — Warehouse Stock Entry Administration Console System Requirements — the admin-UI client of this spec’s sign contract and approval mechanics
- Admin UI Walkthrough — Warehouse Stock Entry Console — the page-by-page, screenshot-anchored walkthrough of the console built against this spec
- SR-019 — Concurrent Demand: Stock Integrity Under Simultaneous Orders System Requirements — the shared balance-mutation routine (FR-06) and row-lock concurrency guarantee (NFR-01) this spec builds directly on top of
- ADR-00036 — Deferred, Approval-Gated Balance Mutation and a Database-Enforced Immutable Ledger for Stock Entry
- Technical Debt — Warehouse Stock Entry Gaps — FR-10/FR-14/FR-15’s gaps, in full
- Architecture — Warehouse Stock Entry: Creation, Approval-Gated Balance Mutation, and Immutable Ledger — the component topology and end-to-end flow implementing every FR/NFR in this spec
- Architecture — Inventory Stock Reservation, Consumption, and Concurrency Control
- Microservices: inventory-service, product-catalog