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
receiving_attention.py · 3.5 KB · 99 lines
| 1 | """system_receiving_attention -- purchase orders somebody shorted us on. |
| 2 | |
| 3 | COUNT {} -> {count, detail} |
| 4 | |
| 5 | A purchase order at `partial` is a supplier who sent some of it. Nobody |
| 6 | typed that status: `system_receipt_posting` derives it by counting |
| 7 | receipt lines against order lines, which means the fact is already |
| 8 | computed, already correct, and already thrown away -- the PO sits at |
| 9 | `partial` and no screen on this server ever mentions it again. Somebody |
| 10 | has to chase the balance or close the order, and neither happens until |
| 11 | somebody remembers. |
| 12 | |
| 13 | The detail names the outstanding QUANTITY, because that is the question |
| 14 | the buyer actually asks. Three POs short by one unit each is a phone call |
| 15 | worth making on Friday; three short by two hundred is one worth making |
| 16 | now, and the row count cannot tell those apart. The arithmetic is the |
| 17 | same as `action_receive_goods._outstanding_by_line` -- ordered minus |
| 18 | everything received on a receipt that was not cancelled -- read here |
| 19 | rather than shared, because these two files must be free to disagree |
| 20 | about nothing else. |
| 21 | |
| 22 | COUNT rather than GET or POST: the verb is the contract with the daemon's |
| 23 | attention pass. Nothing here writes. Missing collections read as zero; |
| 24 | a genuine failure raises, so the rollup records it and keeps the last |
| 25 | count rather than reporting a warehouse with nothing outstanding. |
| 26 | """ |
| 27 | |
| 28 | import os |
| 29 | from decimal import Decimal, InvalidOperation |
| 30 | |
| 31 | import object_records |
| 32 | |
| 33 | ACTOR = "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. |
| 38 | SHORT_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). |
| 43 | NOT_RECEIVED_STATUSES = {"cancelled"} |
| 44 | |
| 45 | |
| 46 | def _base_dir(): |
| 47 | return os.environ.get("DBBASIC_DATA_DIR", "data") |
| 48 | |
| 49 | |
| 50 | def _text(value): |
| 51 | return str(value if value is not None else "").strip() |
| 52 | |
| 53 | |
| 54 | def _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 | |
| 63 | def _rows(collection): |
| 64 | try: |
| 65 | return object_records.read_collection_records(collection, base_dir=_base_dir()) |
| 66 | except Exception: |
| 67 | return [] |
| 68 | |
| 69 | |
| 70 | def _number(value): |
| 71 | return format(value.normalize(), "f") |
| 72 | |
| 73 | |
| 74 | def 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} |