Runs live, versioned Python application objects on one VM, with a suite of apps: projects, notes, tasks, files and more.

0 downloads

Download zip
Clone
Clone with git
git clone https://gitstable.com/git/askrobots/dbbasic-object-server.git
HTTPS
https://gitstable.com/git/askrobots/dbbasic-object-server.git
SSH
ssh://git@gitstable.com:2222/askrobots/dbbasic-object-server.git

Cloning needs no sign-in. Pushing over HTTPS asks for your username and an access token. SSH needs your key under SSH keys.

danq9 signup: the person chooses the password and name on the confirm page 83c7aa6 2 weeks, 5 days ago History
order_attention.py · 4.0 KB · 111 lines
1"""system_order_attention -- orders with goods still owed to a customer.
2
3COUNT {} -> {count, detail}
4
5The pick queue, as a number. An order that is committed and not yet fully
6out the door is a promise this business has made and not kept, and unlike
7almost everything else on this list it is not a status somebody sets --
8it is ordered quantity minus shipped quantity, folded from four
9collections. `site_pick_list` folds exactly that, live, per render, and
10is right to: it is a working screen somebody opens on purpose. A home
11page cannot afford the same fold a dozen times over, which is why this
12object exists and why the daemon runs it on an interval instead.
13
14Counted per ORDER rather than per unit, because "4 orders waiting" is the
15sentence a person acts on; the units go in the detail, where they answer
16the follow-up question (one order short by a hundred mugs is a different
17morning from four short by one each).
18
19The `confirmed | processing | partial` set and the "lost and
20returned_to_sender do not count as shipped" rule are read from
21`site_pick_list`, deliberately, so the badge and the page it links to
22cannot disagree about what is owed. app-shipping's shipment/shipment_line
23schemas are the source of the shipped half; nothing here writes to them,
24and when they are absent every committed order is simply owed in full,
25which is the correct answer for a deployment with no shipping app.
26
27COUNT rather than GET or POST: the verb is the contract with the daemon's
28attention pass. A missing orders collection reads as zero; a genuine
29failure raises, so the rollup records it and keeps the last count rather
30than reporting an empty warehouse.
31"""
32
33import os
34from decimal import Decimal, InvalidOperation
35
36import object_records
37
38ACTOR = "system_order_attention"
39
40# Committed and not yet fully out the door. draft is not a commitment;
41# shipped and delivered have nothing left; cancelled must never be picked.
42PICKABLE_ORDER_STATUSES = {"confirmed", "processing", "partial"}
43
44# Shipments in these never reached anybody, so their lines do not reduce
45# what is still owed -- the goods have to be picked again.
46NOT_DELIVERED_STATUSES = {"lost", "returned_to_sender"}
47
48
49def _base_dir():
50 return os.environ.get("DBBASIC_DATA_DIR", "data")
51
52
53def _text(value):
54 return str(value if value is not None else "").strip()
55
56
57def _quantity(value):
58 try:
59 return Decimal(_text(value) or "0")
60 except InvalidOperation:
61 return Decimal(0)
62
63
64def _rows(collection):
65 try:
66 return object_records.read_collection_records(collection, base_dir=_base_dir())
67 except Exception:
68 # An absent shipping app means nothing has shipped, which is a
69 # real answer and not a failure.
70 return []
71
72
73def _number(value):
74 return format(value.normalize(), "f")
75
76
77def COUNT(request):
78 try:
79 orders = object_records.read_collection_records("orders", base_dir=_base_dir())
80 except Exception:
81 return {"count": 0}
82
83 open_orders = {order["id"] for order in orders
84 if _text(order.get("status")) in PICKABLE_ORDER_STATUSES
85 and _text(order.get("doc_type") or "sale") == "sale"}
86 if not open_orders:
87 return {"count": 0}
88
89 counted = {row["id"] for row in _rows("shipments")
90 if _text(row.get("status")) not in NOT_DELIVERED_STATUSES}
91 shipped = {}
92 for line in _rows("shipment_lines"):
93 if _text(line.get("shipment_id")) not in counted:
94 continue
95 key = _text(line.get("order_line_id"))
96 shipped[key] = shipped.get(key, Decimal(0)) + _quantity(line.get("quantity"))
97
98 owing_orders = set()
99 units = Decimal(0)
100 for line in _rows("order_lines"):
101 order_id = _text(line.get("order_id"))
102 if order_id not in open_orders:
103 continue
104 remaining = _quantity(line.get("quantity")) - shipped.get(line["id"], Decimal(0))
105 if remaining > 0:
106 owing_orders.add(order_id)
107 units += remaining
108
109 if not owing_orders:
110 return {"count": 0}
111 return {"count": len(owing_orders), "detail": f"{_number(units)} units to pick"}