Metadata-Version: 2.4
Name: stapel-inventory
Version: 0.1.0
Summary: Stock per location, holds and a movement ledger for the Stapel framework
License: MIT
Project-URL: Homepage, https://github.com/usestapel/stapel-inventory
Project-URL: Repository, https://github.com/usestapel/stapel-inventory
Project-URL: Documentation, https://github.com/usestapel/stapel-inventory#readme
Project-URL: Changelog, https://github.com/usestapel/stapel-inventory/blob/main/CHANGELOG.md
Project-URL: Issues, https://github.com/usestapel/stapel-inventory/issues
Keywords: django,stapel,inventory
Classifier: Development Status :: 4 - Beta
Classifier: Framework :: Django
Classifier: Intended Audience :: Developers
Classifier: License :: OSI Approved :: MIT License
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3 :: Only
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Programming Language :: Python :: 3.14
Classifier: Typing :: Typed
Requires-Python: >=3.11
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: stapel-core<1.0,>=0.99.3
Provides-Extra: all
Provides-Extra: test
Requires-Dist: pytest; extra == "test"
Requires-Dist: pytest-django; extra == "test"
Requires-Dist: pytest-cov; extra == "test"
Requires-Dist: stapel-tools<1,>=0.84.0; extra == "test"
Requires-Dist: jsonschema>=4; extra == "test"
Provides-Extra: postgres
Requires-Dist: psycopg[binary]>=3.1; extra == "postgres"
Dynamic: license-file

<!-- Generated by stapel-readme from docs/readme.md + docs/*.json. Do not edit this file; edit docs/readme.md and re-run `make readme`. -->

# stapel-inventory

[![CI](https://img.shields.io/github/actions/workflow/status/usestapel/stapel-inventory/ci.yml?branch=main&logo=github&label=CI)](https://github.com/usestapel/stapel-inventory/actions/workflows/ci.yml?query=branch%3Amain)
[![coverage](https://img.shields.io/codecov/c/github/usestapel/stapel-inventory?branch=main&logo=codecov&label=coverage)](https://app.codecov.io/gh/usestapel/stapel-inventory)
[![pypi](https://img.shields.io/pypi/v/stapel-inventory?logo=pypi&logoColor=white&label=pypi)](https://pypi.org/project/stapel-inventory/)
[![downloads](https://static.pepy.tech/badge/stapel-inventory/month)](https://pepy.tech/project/stapel-inventory)
[![python](https://img.shields.io/pypi/pyversions/stapel-inventory?logo=python&logoColor=white)](https://pypi.org/project/stapel-inventory/)
[![license](https://img.shields.io/github/license/usestapel/stapel-inventory)](https://github.com/usestapel/stapel-inventory/blob/main/LICENSE)
[![llms.txt](https://img.shields.io/badge/llms.txt-blue)](https://github.com/usestapel/stapel-inventory/blob/main/docs/llms.txt)

> 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.

Part of the [Stapel framework](https://github.com/usestapel) — composable Django apps that deploy as a monolith or as microservices without changing module code.

## Install

```bash
pip install stapel-inventory
```

## At a glance

| Fact | Value |
|---|---|
| Version | `0.1.0` |
| Python | `>=3.11` (3.11, 3.12, 3.13, 3.14) |
| HTTP operations | 11 |
| Config axes | 6 |
| Usage surface | 5 |
| Extension points | 6 |
| Error codes | 54 |
| Documented flows | 2 |
| Fleet dependencies | [`stapel-auth`](https://github.com/usestapel/stapel-auth) (optional) · [`stapel-core`](https://github.com/usestapel/stapel-core) · [`stapel-orders`](https://github.com/usestapel/stapel-orders) (optional) |

## Documentation

[OpenAPI](https://github.com/usestapel/stapel-inventory/blob/main/docs/schema.json) · [capabilities.json](https://github.com/usestapel/stapel-inventory/blob/main/docs/capabilities.json) · [llms.txt (for agents)](https://github.com/usestapel/stapel-inventory/blob/main/docs/llms.txt)

## What this is

Stock for a Stapel shop — per location, with checkout holds and an
append-only movement ledger — over the same **opaque purchasables** orders
sells (`purchasable_type`, `purchasable_key`). Inventory never imports the
module that owns a product. Installed next to `stapel-orders` it becomes a
checkout hook: a checkout reserves stock, the placed order commits it, a
failed checkout or a cancel releases it. Without orders its staff API,
availability Function and facts work on their own.

## Quick start

```python
INSTALLED_APPS = [
    # ...
    "stapel_inventory",
]

# urls.py
path("inventory/api/", include("stapel_inventory.urls"))   # -> /inventory/api/v1/...

# hold expiry (every minute) and ledger reconciliation (hourly); Celery optional
from stapel_inventory.beat import get_inventory_beat_schedule
CELERY_BEAT_SCHEDULE = {**get_inventory_beat_schedule()}
```

Wire it into the orders checkout. A scaffolded project already calls the
merge helper in `settings.py`: add this brick's preset to that list (the
helper merges two levels deep, so other presets and your own keys stay):

<!-- orders-wiring:scaffold -->
```python
_stapel_merge_preset_defaults(globals(), [
    # ... the presets already listed (stapel_shop.preset, other companions),
    "stapel_inventory.preset",
])
```

A hand-wired project merges per key instead — **never assign**
`STAPEL_ORDERS = {...}`: pasted after another fragment it would drop that
fragment and the host's `PURCHASABLE_TYPES`:

<!-- orders-wiring:hand -->
```python
from stapel_inventory.preset import SETTINGS_DEFAULTS as _INVENTORY

STAPEL_ORDERS = globals().get("STAPEL_ORDERS") or {}
STAPEL_ORDERS = {
    **STAPEL_ORDERS,
    "CHECKOUT_HOOKS": {**_INVENTORY["STAPEL_ORDERS"]["CHECKOUT_HOOKS"], **STAPEL_ORDERS.get("CHECKOUT_HOOKS", {})},
}
```

Load stock and ask for it:

```bash
python manage.py inventory_import stock.csv --dry-run   # purchasable_type,purchasable_key,quantity[,location][,low_stock_threshold]
python manage.py inventory_import stock.csv
```

```python
from stapel_core.comm import call

call("inventory.availability", {"purchasable_type": "listing", "keys": ["42", "43"]})
# -> {"items": {"42": {"tracked": true, "available": 3, "in_stock": true, ...}, ...}}
```

## Concepts

- **Location** — where stock sits. One default location (`main`) is
  created on migrate. `ALLOCATION="single_default"` sells only from it;
  `"priority"` sells from every active location, highest priority first,
  splitting a line when it must.
- **Stock item** — one purchasable at one location. `on_hand` is a cache of
  the ledger; `available = on_hand − active, unexpired holds`.
- **Ledger** — every change is a `StockMovement` (receipt, adjust, hold,
  commit, release, transfer, restock) written in the same transaction under
  the item's row lock; rows are never edited or deleted. The beat proves
  `on_hand = Σ movements` hourly and repairs a drifted cache from the ledger.
- **Hold** — a checkout's claim, keyed on the checkout attempt key, with a
  TTL (`HOLD_TTL_SECONDS`, 30 min). Reserve locks the stock rows in
  ascending id order and re-counts under the lock, all-or-nothing across
  lines: two shoppers racing for the last unit end as one order and one
  `error.409.insufficient_stock`. Replays are idempotent; a released key
  reserves anew. A crashed checkout's hold is released by the beat.
- **Oversell** — `deny` (default) refuses; `backorder` holds the remainder
  and lets stock go negative. A purchasable with no stock record is not
  stock-managed and always sells, unless `TRACK_UNKNOWN="deny"`.
- **Availability** — `inventory.availability` answers numbers to other
  modules; `GET availability/` answers visitors what `PUBLIC_AVAILABILITY`
  allows (`few_left` by default: in stock / a few left / out / backorder).
- **Facts** — `inventory.stock.low`, `.out`, `.back_in_stock` fire once per
  transition, never per sale; `.oversold` when an expired hold's order
  still had to be decremented.
- **GDPR** — erasure pseudonymizes the shopper on holds and the staff
  member on the ledger; the numbers stay.

## License

MIT — see [LICENSE](https://github.com/usestapel/stapel-inventory/blob/main/LICENSE).

---

<sub>This page is assembled by `stapel-readme` from `docs/readme.md` plus the contract artifacts in `docs/`. Edit the prose in `docs/readme.md`; the badges, facts and links above and below it are generated — do not hand-edit `README.md`.</sub>
