iVendNextDevelopers Request a sandbox

S27 — Stock alerts and a draft transfer request

What the shop does

The shop is told when a store's stock falls below its reorder level, or when a batch is close to its expiry date, and finds a draft stock transfer request already made from those alerts: one per store and day, for a person to check, change and submit. The app never submits anything. iVendNext's own automatic reorder makes a request too, but submits it at once, and it has no expiry alert; this app is for the shop that wants a person in between.

What happens When
A Low stock alert, and a line moving the reorder quantity (or the shortfall, when larger) from the source warehouse to the store a store's projected stock is below the level on the item's reorder row, and the row's type is Transfer
A Low stock alert with no line the reorder row's type is Purchase, Material Issue or Manufacture
A Near expiry alert for each store that holds the batch a batch expires within the settings' days, or has expired and is still in stock
A line moving the batch's item from the store to the clearance warehouse, its description naming the batch; a second batch of that item joins the same line (the quantities added, both batches named) the settings name a clearance warehouse
The alert, with no line the item is already on today's draft for another move (in from the source and out to clearance): a request holds an item once
The shop's bell tells the people with the role PP STK Alerts, and the user named in the settings an alert is written
Nothing new the same alert was already written today, or its line is already on an open request of the app for that store
Today's draft gains the new lines the app runs again the same day and finds new alerts
A request is left exactly as it is it was made by a person (it has no stamp), or a person has submitted it

Bounds, said plainly.

What the shop does to set it up

  1. Install manifest.json (the shop's administrator, on the install screen; on your sandbox, tools/deploy/deploy.py). It adds PP STK Settings, PP STK Alert, the notification PP STK New Alert (bell) and the roles PP STK Integration and PP STK Alerts (both empty).
  2. In Role Permissions, give PP STK Integration read on Warehouse, Item, Bin, Batch and Serial and Batch Bundle, and read, create and write on Material Request. A manifest grants rights only on its own record types (the integration user recipe). ⚠ With these rights the key can change any draft transfer request in the shop: give the role to this app's user alone.
  3. Make the app's integration user with that role and generate its API key.
  4. Fill PP STK Settings named Default: the source warehouse (the stock replenishment comes from), the days before expiry, an optional clearance warehouse, the stores watched (empty: every store but the source and the clearance), an optional user to tell; tick Enabled.
  5. Give PP STK Alerts to the people who act on the alerts.
  6. Put reorder rows on the items (the store, the level, the quantity, the type Transfer), as for the standard reorder. Leave the shop's automatic reorder off, or both will ask for the same stock.
  7. Run the service on a schedule with the user's key (below).

What leaves the shop. The shop calls nothing outside. The service reads the warehouses, the items' reorder rows, the stock of the watched stores, the batches and their records, and the open transfer requests. It writes alerts and draft transfer requests, and adds lines to its own drafts.

Run it

python3 -m unittest discover -s samples/s27_stock_alerts/tests -p 'test_*.py'
python3 samples/s27_stock_alerts/tests/canaries.py

On a sandbox, with the integration user's key in the environment (never on the command line, never committed):

STK_KEY=… STK_SECRET=… python3 samples/s27_stock_alerts/run.py --base https://your-sandbox.example

Once a day at 06:00, from any scheduler; with cron:

0 6 * * * cd /srv/partner-kit && STK_KEY=… STK_SECRET=… python3 samples/s27_stock_alerts/run.py --base https://shop.example >> stk.log 2>&1

Each run prints one line: the alerts written, the requests made, the lines added, the lines already on a request, the alerts with no line, and whether each batch's quantity per store could be read.

The files

File What
alerts.py the rules, pure: low stock against the reorder row, the expiry window, the alert's key, the request's stamp, the lines per store
run.py the service: the shop over the record API, one run (read, alerts, the draft per store and day, then the alert records)
manifest.json the settings, the alert, the stores table, the two roles, the bell notification
tests/test_s27.py 32 laptop tests: low stock and its quantity, a row with no level, a store not watched, a run twice, the next day, a run that stopped half-way, the stamp, a person's draft, a submitted request, the expiry window's edges, a batch per store, two batches of one item, one item for two moves, no clearance warehouse, a batch quantity that cannot be read, the per-store sum with an outward row, what an update sends, settings switched off
tests/test_manifest.py the manifest: valid, every field the service reads and writes declared, the rights, the notification on the app's own record
tests/canaries.py each guard removed in turn: its test must go RED (27 of 27)

Technical notes

What was proven where

Needs this serves

This page in the kit