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

9 downloads

Install on DigitalOcean 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.

Daniel Quellhorst Changelog for this batch. 65ef78d 3 hours, 49 minutes ago History
counter_hand_over.py · 5.2 KB · 114 lines
1"""action_counter_hand_over -- the customer has their order: the goods leave
2the shelf, then the order says collected.
3
4POST {"order_id": "..."}
5
6For a counter or pickup sale (orders.fulfillment_method). Corner Cup sold
7all day at the counter and no stock moved, because only a shipment ever
8moved stock. This is the hand-over: system_counter_stock's MOVE writes one
9sale move per line for whatever has not already left (a shipment's move, an
10earlier hand-over, a remake's waste all count), and only then is the order
11walked up its ladder to `collected` with collected_at stamped. Moving first
12is deliberate: the hold an online order keeps on the shelf is released by
13the move, never by the status, so there is no moment where the goods are in
14a bag and the shop still shows them available.
15
16Refuses, writing nothing: a sale that is not counter or pickup; a cancelled
17one; one with money still owed on an issued bill unless it was booked to pay
18at pickup (take the payment first: action_counter_pay); and anything MOVE
19refuses (no shop.stock_location, an unreadable cost). Staff only (execute
20grant: cashier, shift lead, manager, owner), for an order the caller may
21read. A second hand-over of a collected order moves nothing and says so.
22"""
23
24from __future__ import annotations
25
26import os
27from datetime import datetime, timezone
28
29import object_access
30import object_corrections
31import object_execution
32import object_record_changes
33import object_records
34import python_object_runtime
35
36ACTOR = "action_counter_hand_over"
37LADDER = ("draft", "confirmed", "preparing", "ready", "collected")
38
39
40def _base_dir():
41 return os.environ.get("DBBASIC_DATA_DIR", "data")
42
43
44def _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
59def _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
106def _t(value):
107 return str(value if value is not None else "").strip()
108
109
110def 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)