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
receiving_attention.py · 3.5 KB · 99 lines
1"""system_receiving_attention -- purchase orders somebody shorted us on.
2
3COUNT {} -> {count, detail}
4
5A purchase order at `partial` is a supplier who sent some of it. Nobody
6typed that status: `system_receipt_posting` derives it by counting
7receipt lines against order lines, which means the fact is already
8computed, already correct, and already thrown away -- the PO sits at
9`partial` and no screen on this server ever mentions it again. Somebody
10has to chase the balance or close the order, and neither happens until
11somebody remembers.
12
13The detail names the outstanding QUANTITY, because that is the question
14the buyer actually asks. Three POs short by one unit each is a phone call
15worth making on Friday; three short by two hundred is one worth making
16now, and the row count cannot tell those apart. The arithmetic is the
17same as `action_receive_goods._outstanding_by_line` -- ordered minus
18everything received on a receipt that was not cancelled -- read here
19rather than shared, because these two files must be free to disagree
20about nothing else.
21
22COUNT rather than GET or POST: the verb is the contract with the daemon's
23attention pass. Nothing here writes. Missing collections read as zero;
24a genuine failure raises, so the rollup records it and keeps the last
25count rather than reporting a warehouse with nothing outstanding.
26"""
27
28import os
29from decimal import Decimal, InvalidOperation
30
31import object_records
32
33ACTOR = "system_receiving_attention"
34
35# Some of it arrived and some of it did not. `open`/`confirmed` are still
36# entirely owed and nobody has been shorted yet; `received` is complete;
37# `cancelled` is off the table.
38SHORT_STATUS = "partial"
39
40# A receipt abandoned before anything was counted describes no goods, so
41# its lines do not consume the ordered quantity (the same rule
42# action_receive_goods applies).
43NOT_RECEIVED_STATUSES = {"cancelled"}
44
45
46def _base_dir():
47 return os.environ.get("DBBASIC_DATA_DIR", "data")
48
49
50def _text(value):
51 return str(value if value is not None else "").strip()
52
53
54def _quantity(value):
55 try:
56 return Decimal(_text(value) or "0")
57 except InvalidOperation:
58 # A hand-edited quantity contributes nothing rather than taking
59 # the whole count down with it.
60 return Decimal(0)
61
62
63def _rows(collection):
64 try:
65 return object_records.read_collection_records(collection, base_dir=_base_dir())
66 except Exception:
67 return []
68
69
70def _number(value):
71 return format(value.normalize(), "f")
72
73
74def COUNT(request):
75 short = {row["id"]: row for row in _rows("orders")
76 if _text(row.get("doc_type") or "sale") == "purchase"
77 and _text(row.get("status")) == SHORT_STATUS}
78 if not short:
79 return {"count": 0}
80
81 counted = {row["id"] for row in _rows("receipts")
82 if _text(row.get("status")) not in NOT_RECEIVED_STATUSES}
83 received = {}
84 for line in _rows("receipt_lines"):
85 if _text(line.get("receipt_id")) not in counted:
86 continue
87 key = _text(line.get("order_line_id"))
88 received[key] = received.get(key, Decimal(0)) + _quantity(line.get("quantity_received"))
89
90 outstanding = Decimal(0)
91 for line in _rows("order_lines"):
92 if _text(line.get("order_id")) not in short:
93 continue
94 remaining = _quantity(line.get("quantity")) - received.get(line["id"], Decimal(0))
95 if remaining > 0:
96 outstanding += remaining
97
98 detail = f"{_number(outstanding)} units still owed" if outstanding > 0 else ""
99 return {"count": len(short), "detail": detail}