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.
counter_hand_over.py · 5.2 KB · 114 lines
| 1 | """action_counter_hand_over -- the customer has their order: the goods leave |
| 2 | the shelf, then the order says collected. |
| 3 | |
| 4 | POST {"order_id": "..."} |
| 5 | |
| 6 | For a counter or pickup sale (orders.fulfillment_method). Corner Cup sold |
| 7 | all day at the counter and no stock moved, because only a shipment ever |
| 8 | moved stock. This is the hand-over: system_counter_stock's MOVE writes one |
| 9 | sale move per line for whatever has not already left (a shipment's move, an |
| 10 | earlier hand-over, a remake's waste all count), and only then is the order |
| 11 | walked up its ladder to `collected` with collected_at stamped. Moving first |
| 12 | is deliberate: the hold an online order keeps on the shelf is released by |
| 13 | the move, never by the status, so there is no moment where the goods are in |
| 14 | a bag and the shop still shows them available. |
| 15 | |
| 16 | Refuses, writing nothing: a sale that is not counter or pickup; a cancelled |
| 17 | one; one with money still owed on an issued bill unless it was booked to pay |
| 18 | at pickup (take the payment first: action_counter_pay); and anything MOVE |
| 19 | refuses (no shop.stock_location, an unreadable cost). Staff only (execute |
| 20 | grant: cashier, shift lead, manager, owner), for an order the caller may |
| 21 | read. A second hand-over of a collected order moves nothing and says so. |
| 22 | """ |
| 23 | |
| 24 | from __future__ import annotations |
| 25 | |
| 26 | import os |
| 27 | from datetime import datetime, timezone |
| 28 | |
| 29 | import object_access |
| 30 | import object_corrections |
| 31 | import object_execution |
| 32 | import object_record_changes |
| 33 | import object_records |
| 34 | import python_object_runtime |
| 35 | |
| 36 | ACTOR = "action_counter_hand_over" |
| 37 | LADDER = ("draft", "confirmed", "preparing", "ready", "collected") |
| 38 | |
| 39 | |
| 40 | def _base_dir(): |
| 41 | return os.environ.get("DBBASIC_DATA_DIR", "data") |
| 42 | |
| 43 | |
| 44 | def _move(order_id, actor): |
| 45 | try: |
| 46 | outcome = object_execution.execute_object( |
| 47 | python_object_runtime.PythonObjectRuntime(), |
| 48 | object_execution.ObjectExecutionRequest( |
| 49 | "system_counter_stock", method="MOVE", |
| 50 | payload={"order_id": order_id, "actor": actor})) |
| 51 | except Exception as exc: # noqa: BLE001 |
| 52 | return {"ok": False, "status": 503, "error": f"Stock could not be moved: {str(exc)[:160]}"} |
| 53 | if not outcome.ok: |
| 54 | message = getattr(outcome.error, "message", "") if outcome.error else "" |
| 55 | return {"ok": False, "status": 503, "error": message or "system_counter_stock failed"} |
| 56 | return outcome.result |
| 57 | |
| 58 | |
| 59 | def _post(request): |
| 60 | text = object_corrections.text |
| 61 | base = _base_dir() |
| 62 | access = object_access.Access(request.get("_identity"), base_dir=base) |
| 63 | if not access.signed_in: |
| 64 | return {"status": 401, "error": "Sign in to hand over an order."} |
| 65 | staff = object_corrections.staff_refusal(access, "action_counter_hand_over", doing="hand an order over") |
| 66 | if staff: |
| 67 | return staff |
| 68 | order = access.get("orders", text(request.get("order_id"))) |
| 69 | if order is None: |
| 70 | return {"status": 404, "error": "No such order."} |
| 71 | if text(order.get("fulfillment_method")) not in object_corrections.COUNTER_METHODS: |
| 72 | return {"status": 409, "error": "Only a counter or pickup sale is handed over here; " |
| 73 | "a shipped order leaves with its shipment."} |
| 74 | status = text(order.get("status") or "draft") |
| 75 | if status == "collected": |
| 76 | return {"status": 200, "ok": True, "already": True, "moved": [], |
| 77 | "note": "This order was already collected."} |
| 78 | if status not in LADDER: |
| 79 | return {"status": 409, "error": f"An order that is {status} is not handed over."} |
| 80 | # access: filtered to the order read through access.get above, and each bill asked about with access.can |
| 81 | invoices = object_records.read_collection_records("invoices", base_dir=base) |
| 82 | bills = [b for b in object_corrections.bills_for_order(order, invoices) |
| 83 | if object_corrections.issued(b) and access.can("read", "invoices", b)] |
| 84 | owed = sum(object_corrections.cents(b.get("balance_due_cents")) for b in bills) |
| 85 | if owed > 0 and not object_corrections.truthy(order.get("pay_at_pickup")): |
| 86 | return {"status": 409, "owed_cents": owed, |
| 87 | "error": f"{owed} is still owed on this order: take the payment first " |
| 88 | f"(action_counter_pay)."} |
| 89 | |
| 90 | actor = f"{ACTOR}:{access.user_id}" |
| 91 | moved = _move(order["id"], actor) |
| 92 | if not moved.get("ok"): |
| 93 | return {"status": moved.get("status") or 409, "error": moved.get("error")} |
| 94 | now = datetime.now(timezone.utc).isoformat(timespec="seconds").replace("+00:00", "Z") |
| 95 | for step in LADDER[LADDER.index(status) + 1:]: |
| 96 | changes = {"status": step} |
| 97 | if step == "collected": |
| 98 | changes["collected_at"] = now |
| 99 | # access: system write after access check above (access.get on the order) |
| 100 | object_records.update_collection_record("orders", order["id"], changes, |
| 101 | base_dir=base, actor=actor) |
| 102 | return {"status": 200, "ok": True, "order_id": order["id"], "moved": moved.get("moved", []), |
| 103 | "owed_cents": owed} |
| 104 | |
| 105 | |
| 106 | def _t(value): |
| 107 | return str(value if value is not None else "").strip() |
| 108 | |
| 109 | |
| 110 | def POST(request): |
| 111 | """Every record change this action writes carries why, as the history |
| 112 | entry's message (object_record_changes.change_reason).""" |
| 113 | with object_record_changes.change_reason("handed over at the counter"): |
| 114 | return _post(request) |