# stapel-inventory 0.1.0

Stock per location for opaque purchasables (the orders purchasable pair), an append-only movement ledger whose sum is on_hand, and checkout holds with a TTL. Plugs into stapel-orders as a checkout hook (reserve / commit / release, idempotent on the checkout attempt key): a reserve locks the stock rows in ascending id order and re-counts availability under the lock, all-or-nothing across lines, so two shoppers racing for the last unit end as one order and one error.409.insufficient_stock. Allocation across locations is a strategy (single_default, priority); oversell is an axis (deny, backorder) with a per-item override; untracked purchasables pass through. A public availability endpoint discloses in-stock / few-left / out-of-stock per a policy; staff receive, adjust with a reason, transfer and import CSV. Low-stock, out-of-stock, back-in-stock and oversold facts fire once per transition. Beat tasks expire holds of crashed checkouts and reconcile the cache with the ledger.

Contract: axes 6 · surface 5 · extension points 6 · operations 11 · error codes 54 · flows 2.
Generated from docs/capabilities.json by `stapel-llms-txt` — do not edit; drift-gated by `make contract-check`.

## Configuration axes — what a product switches on
Settings keys; `default` is what you get by saying nothing. Turning an axis off unmounts the operations it gates.
- ALLOCATION [enum, default "single_default"] — Which warehouse sells
  single_default (default): only the default location's stock is sold; other locations are storage you transfer from. priority: every active location sells, highest priority first, and one order line may be split across locations. Values: single_default, priority.
- MAX_AVAILABILITY_KEYS [enum, default 100] — Items per availability request
  How many keys one availability request (GET availability/ or the inventory.availability Function) may ask for; more is refused with error.400.availability_too_many_keys, whose params.max carries this limit. A client pages its keys by it.
- OVERSELL [enum, default "deny"] — Selling beyond stock
  deny (default): checkout refuses more units than are available. backorder: the order goes through and the missing units are backordered (stock goes negative). A single item can override it. Values: deny, backorder.
- PUBLIC_AVAILABILITY [enum, default "few_left"] — What shoppers see about stock
  few_left (default): in stock, a few left, out of stock or on backorder, never a number. exact: the number of units too. in_stock: only in or out of stock. Values: few_left, exact, in_stock.
- RESTOCK_ON_CANCEL [bool, default true] — Cancelled orders return stock
  true (default): when an order is cancelled its units go back on the shelf. false: staff return them by hand. Values: true, false.
- TRACK_UNKNOWN [enum, default "allow"] — Items without stock records
  allow (default): a product with no stock record is not stock-managed and always sells. deny: it cannot be sold until stock is recorded. Values: allow, deny.

## Usage surface — call these before writing your own
This is the answer to "does Stapel already have something for X?". `instead of` names the outside symbol this one displaces.
### gate_function
- resolve_allocation — stapel_inventory.allocation.resolve_allocation
  Turn an ALLOCATION value (short name or dotted path) into a strategy instance; raises ImproperlyConfigured (check E001) for an unknown name.
### predicate
- active_allocation — stapel_inventory.allocation.active_allocation
  The strategy the current settings select; ask it instead of reading the setting.
- allocation_names — stapel_inventory.allocation.allocation_names
  The short names ALLOCATION accepts (built-ins plus registered); also the env vocabulary.
### factory
- register_allocation — stapel_inventory.allocation.register_allocation
  Register an allocation strategy under a short name ALLOCATION (and the environment) may choose; call it from an AppConfig.ready().
- reset_allocations — stapel_inventory.allocation.reset_allocations
  Drop runtime registrations — a test lever.

## Extension points — what a product replaces, fork-free
- ALLOCATION [strategy]
  Which locations a reserve takes stock from: single_default (default; only the default location sells), priority (active locations by priority, a line may split), a register_allocation name, or a dotted path to an AllocationStrategy. The strategy is pure: it sees the locked, counted candidates and answers allocations.
- STAPEL_INVENTORY_AVAILABILITY_PRESENTER / STAPEL_INVENTORY_STOCK_PRESENTER [presenter]
  Swap keys (STAPEL_SWAP) for the public availability presenter (where the disclosure policy is applied) and the staff stock/ledger presenter.
- STAPEL_ORDERS['CHECKOUT_HOOKS']['inventory'] [setting]
  The orders checkout hook entry shipped in stapel_inventory.preset.SETTINGS_DEFAULTS: reserve / commit / release Function names and order 50 (stock first: before promotions 100 and delivery 200). Merge it under the project's STAPEL_ORDERS; nothing else wires stock into checkout.
- inventory.availability [comm_function]
  Exact availability for a keyed batch of one purchasable type (on_hand, held, available, policy, per location) for a listing page or another module; the public endpoint applies PUBLIC_AVAILABILITY on top.
- inventory.stock.low / inventory.stock.out / inventory.stock.back_in_stock [comm_event]
  Emitted once per alert-state transition of a stock item (never per sale), in the transaction of the movement that caused it; a back-in-stock notification is a host subscriber.
- inventory.stock.oversold [comm_event]
  A commit drove a deny item below zero because its hold had expired before the order's commit arrived; the order is the fact and staff must act.

## Fits with — fleet dependencies
- stapel-auth (optional) — a guest checkout's hold is keyed on a stapel-auth anonymous account; user.merged moves it to the account
- stapel-core (required) — comm bus (inventory.* facts, availability and checkout hook Functions, user.merged consumer), AnchorPagination, GDPR owner protocol, AppSettings
- stapel-orders (optional) — calls the checkout hooks registered by stapel_inventory.preset.SETTINGS_DEFAULTS; without it the stock API, availability and facts still work

## HTTP operations (11) — call by operationId, never by a typed path
Paths are relative to `/inventory/api/v1/`.
### Inventory
- GET /availability/ — inventory_availability_retrieve
- POST /locations/ — inventory_locations_create
- GET /locations/ — inventory_locations_list
- PATCH /locations/{code}/ — inventory_locations_partial_update
- GET /movements/ — inventory_movements_list
- POST /stock/adjust/ — inventory_stock_adjust
- GET /stock/ — inventory_stock_list
- PATCH /stock/{item_id}/ — inventory_stock_partial_update
- POST /stock/receive/ — inventory_stock_receive
- GET /stock/{item_id}/ — inventory_stock_retrieve
- POST /stock/transfer/ — inventory_stock_transfer

## Error codes (54) — the StapelError envelope
Render `t(code, params)`; branch UX on the remediation. Localized text lives in docs/errors.<lang>.md, not here.
- error.400.availability_too_many_keys [400] fix_input {max,field}
- error.400.bad_request [400] fix_input
- error.400.captcha_invalid [400] retry
- error.400.captcha_required [400] retry
- error.400.expected_list [400] fix_input
- error.400.field.blank [400] fix_input {field}
- error.400.field.does_not_exist [400] fix_input {field}
- error.400.field.invalid [400] fix_input {field}
- error.400.field.invalid_choice [400] fix_input {field}
- error.400.field.max_length [400] fix_input {field,max_length}
- error.400.field.max_value [400] fix_input {field,max_value}
- error.400.field.min_length [400] fix_input {field,min_length}
- error.400.field.min_value [400] fix_input {field,min_value}
- error.400.field.null [400] fix_input {field}
- error.400.field.required [400] fix_input {field}
- error.400.field.unique [400] fix_input {field}
- error.400.invalid_ad_id [400] fix_input
- error.400.stock_adjust_ambiguous [400] fix_input {field}
- error.400.stock_transfer_same_location [400] fix_input {field}
- error.400.too_many_ids [400] fix_input {requested,limit}
- error.400.validation_error [400] fix_input
- error.400.verification_failed [400] verify
- error.400.verification_invalid_factor [400] verify
- error.401.unauthorized [401] reauthenticate
- error.402.payment_required [402] retry
- error.403.forbidden [403] retry
- error.403.network_blocked [403] contact_support
- error.403.verification_enrollment_required [403] verify
- error.403.verification_required [403] verify
- error.404.ad_not_found [404] retry
- error.404.location_not_found [404] retry {code,field}
- error.404.not_found [404] retry
- error.404.stock_item_not_found [404] retry
- error.404.verification_challenge_not_found [404] verify
- error.405.method_not_allowed [405] retry
- error.406.not_acceptable [406] retry
- error.408.request_timeout [408] retry
- error.409.conflict [409] fix_input
- error.409.default_location_required [409] fix_input {code,field}
- error.409.insufficient_stock [409] retry {available,index,purchasable_type,purchasable_key,requested,field}
- error.409.location_exists [409] fix_input {code,field}
- error.409.location_inactive [409] fix_input {code,field}
- error.409.stock_not_tracked [409] fix_input {index,purchasable_type,purchasable_key,field}
- error.409.stock_would_go_negative [409] fix_input {on_hand,field}
- error.410.gone [410] retry
- error.413.payload_too_large [413] retry
- error.415.unsupported_media_type [415] retry
- error.422.unprocessable_entity [422] wait_and_retry
- error.423.locked [423] wait_and_retry
- error.423.verification_locked [423] wait_and_retry
- error.429.rate_limit [429] wait_and_retry {retry_after_display,retry_after,retry_after_minutes}
- error.429.too_many_requests [429] wait_and_retry
- error.500.internal [500] contact_support
- error.503.mandate_unavailable [503] retry

## Documented flows (2) — full steps in docs/flows.json
- inventory.shop — See whether an item is in stock
- inventory.stock — Keep stock right
