askrobots / dbbasic-object-server
Public
Runs live, versioned Python application objects on one VM, with a suite of apps: projects, notes, tasks, files and more.
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 | |
| 3 | COUNT {} -> {count, detail} |
| 4 | |
| 5 | The pick queue, as a number. An order that is committed and not yet fully |
| 6 | out the door is a promise this business has made and not kept, and unlike |
| 7 | almost everything else on this list it is not a status somebody sets -- |
| 8 | it is ordered quantity minus shipped quantity, folded from four |
| 9 | collections. `site_pick_list` folds exactly that, live, per render, and |
| 10 | is right to: it is a working screen somebody opens on purpose. A home |
| 11 | page cannot afford the same fold a dozen times over, which is why this |
| 12 | object exists and why the daemon runs it on an interval instead. |
| 13 | |
| 14 | Counted per ORDER rather than per unit, because "4 orders waiting" is the |
| 15 | sentence a person acts on; the units go in the detail, where they answer |
| 16 | the follow-up question (one order short by a hundred mugs is a different |
| 17 | morning from four short by one each). |
| 18 | |
| 19 | The `confirmed | processing | partial` set and the "lost and |
| 20 | returned_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 |
| 22 | cannot disagree about what is owed. app-shipping's shipment/shipment_line |
| 23 | schemas are the source of the shipped half; nothing here writes to them, |
| 24 | and when they are absent every committed order is simply owed in full, |
| 25 | which is the correct answer for a deployment with no shipping app. |
| 26 | |
| 27 | COUNT rather than GET or POST: the verb is the contract with the daemon's |
| 28 | attention pass. A missing orders collection reads as zero; a genuine |
| 29 | failure raises, so the rollup records it and keeps the last count rather |
| 30 | than reporting an empty warehouse. |
| 31 | """ |
| 32 | |
| 33 | import os |
| 34 | from decimal import Decimal, InvalidOperation |
| 35 | |
| 36 | import object_records |
| 37 | |
| 38 | ACTOR = "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. |
| 42 | PICKABLE_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. |
| 46 | NOT_DELIVERED_STATUSES = {"lost", "returned_to_sender"} |
| 47 | |
| 48 | |
| 49 | def _base_dir(): |
| 50 | return os.environ.get("DBBASIC_DATA_DIR", "data") |
| 51 | |
| 52 | |
| 53 | def _text(value): |
| 54 | return str(value if value is not None else "").strip() |
| 55 | |
| 56 | |
| 57 | def _quantity(value): |
| 58 | try: |
| 59 | return Decimal(_text(value) or "0") |
| 60 | except InvalidOperation: |
| 61 | return Decimal(0) |
| 62 | |
| 63 | |
| 64 | def _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 | |
| 73 | def _number(value): |
| 74 | return format(value.normalize(), "f") |
| 75 | |
| 76 | |
| 77 | def 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"} |