Orders | Stock Counting (mobile)

stocktake

Mobile stock counting endpoint (v3.164 / #753, engine shipped as #752): the counting half of the CMS Inventory > Stocktake tool. Shop and warehouse staff count shelves on handheld devices while the shop stays open and POS keeps selling — the reconciliation that makes this safe lives in the CMS (a per-session diary window anchored at a forced CR absorb), not in the app. The app's job is exactly one thing: feed counted lines to the session, never write stock.

  • list open counting sessions (per location),
  • lookup a product by scan (multirow barcode / product barcode / prdNumber / pid) or by name — the answer carries the expected shelves + best-before dates + the old quantities from the session snapshot, plus the already-counted line(s),
  • count a quantity for (product, shelf, optional best-before date) — repeatable, it upserts,
  • count_bulk a whole list in one call,
  • summary with the live gap list (supervisor view).

Important notes

  • The app NEVER writes stockcurrent or stockdiary. Counting only writes tbl_stocktake_lines. The CMS applies the results: shops get a restock transfer (warehouse → shop, quantities grossed back to the session anchor so POS sales during the count never double-deduct); the warehouse gets finalize (absolute set-to per shelf/batch).
  • Empty bbd = whole-shelf count. A count line without a best-before date means "this is the TOTAL for this shelf" — the CMS finalize gives the line the entire shelf and zeroes sibling batches on it. A line with a date counts just that batch. Choose deliberately.
  • counted_at matters. The server stamps each line; the reconciliation windows key on it. Do not pre-load a day-old list — re-count instead.
  • Sessions in status retiring are not countable yet (the retire batches are still moving the old quantities out); count answers not_counting.

Authentication

Two layers, exactly like picking:

  1. Account: the standard api_verifier lanes — Authorization1 API key + username + password + account, or the Inventory Manager token lane (token + username + account).
  2. User: user_token — an ACTIVE mobile-app token of a CMS user of the SAME account (log in via verify_user and reuse the token). The user needs the order_picker or inventory permit; account administrators (super_admin / reseller / administrator) always pass.

Common parameters

  • action — the action name (required)
  • session_id — the counting session (all write actions + info)
  • user_token — the mobile-app user token (required)

Response envelope

{"status":"success","action":"count","data":{...}}
{"status":"error","action":"count","error_code":"not_counting","data":{...}}

HTTP 200 carries counting-flow outcomes (unknown_code, not_counting, session_not_found, …); 4xx/5xx are auth/parameter failures.

Actions

sessions

Lists counting sessions (status counting or restocked; include_done=1 adds everything). Optional location_id filter.

  • Request: {action:"sessions", location_id?:11} → response rows carry session_id, location_id, location_name, kind (shop|warehouse), status, anchor_time, counted_lines.

session_info

One session + the live summary (counted vs snapshot product counts, gap count, movement totals since the anchor). Heavy gap lists are omitted here — use summary.

lookup

  • Request: {action:"lookup", session_id, code:"6401234567890"} (or pid instead of code; free text with letters triggers a title search).
  • Response products[] (max 10): pid, title, barcode, prdNumber, price, expected[] = snapshot rows {shelf_id, best_before_date, old_stock, default_location} (default bin first, biggest stock first), counted[] = existing lines {line_id, shelf_id, best_before_date, qty, counted_at}.
  • Errors: unknown_code.

count

Upserts one count line. An existing line with the same (session, pid, shelf, bbd) is replaced (qty + counted_at refreshed).

  • Request: {action:"count", session_id, pid, qty, shelf_id?, bbd?}
  • qty may be 0 (explicitly counted zero). shelf_id 0 = the product's snapshot default shelf. bbd empty = whole-shelf semantics.
  • Errors: not_counting, product_not_found, shelf_not_in_location.

count_bulk

  • Request: {action:"count_bulk", session_id, lines:[{pid, qty, shelf_id?, bbd?}, ...]} → {lines_ok, lines_failed, errors[]} per line.

line_delete

Removes one counted line ({action:"line_delete", session_id, line_id}) — only while the session is counting.

summary

Full session summary including the gap list (gaps[]: snapshot products with no count line) and the last 200 count lines. The supervisor view.

Quick reference

action key params writes
sessions location_id?, include_done? —
session_info session_id —
lookup session_id, code pid —
count session_id, pid, qty, shelf_id?, bbd? session line
count_bulk session_id, lines[] session lines
line_delete session_id, line_id session line
summary session_id —

Call examples

curl -s -X POST https://shop.example.com/public_api/stocktake/ \
  -H "Authorization1: <api key>" \
  -d "account=1&username=api&password=***" \
  -d "user_token=7f2d…" \
  -d "action=lookup&session_id=3&code=6401234567890"
// Android/Kotlin — count 24 units on shelf 68, whole shelf (no bbd)
val body = FormBody.Builder()
    .add("account", account).add("username", user).add("password", pass)
    .add("user_token", token)
    .add("action", "count").add("session_id", "3")
    .add("pid", "1017").add("qty", "24").add("shelf_id", "68")
    .build()

The counting flow, the CMS tool and the diary balance fix are documented in blueprints/stocktake-blueprint.md (repo) — the app only needs this page.