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
invoice_attention.py · 2.9 KB · 87 lines
| 1 | """system_invoice_attention -- how many invoices are past their due date. |
| 2 | |
| 3 | COUNT {} -> {count, detail} |
| 4 | |
| 5 | Time changes an invoice's meaning with no write happening |
| 6 | (system_invoice_aging's whole premise), so "overdue" is a queue that |
| 7 | appears out of the calendar rather than out of anybody's action. That is |
| 8 | the kind of pile nothing draws attention to: no record changed, no event |
| 9 | fired, and the only person who finds out is the one who happens to open |
| 10 | the invoice list. |
| 11 | |
| 12 | Severity `warning` rather than `normal` in the manifest, because this |
| 13 | queue is not work that arrived, it is work that is already late. |
| 14 | |
| 15 | Three deliberate exclusions, each of which would otherwise inflate the |
| 16 | number into one nobody trusts: |
| 17 | |
| 18 | - `paid` and `void` are settled, which is the obvious half. |
| 19 | - `draft` is NOT overdue however old it is. An invoice nobody sent cannot |
| 20 | be late; counting it would mean chasing a customer who was never |
| 21 | billed, and the fix is to send it, not to dun it. |
| 22 | - `doc_type` other than `invoice`. A quote's date is an expiry and a |
| 23 | credit note's is an issue date; neither is money somebody owes. |
| 24 | |
| 25 | The detail names the worst case rather than the total, because "45 days" |
| 26 | is what decides whether this is a reminder email or a phone call. |
| 27 | |
| 28 | COUNT rather than GET or POST: the verb is the contract with the daemon's |
| 29 | attention pass. Nothing here writes. A missing collection reads as zero; |
| 30 | a genuine failure raises, so the rollup records it and keeps the last |
| 31 | count rather than reporting that nobody owes anything. |
| 32 | """ |
| 33 | |
| 34 | import os |
| 35 | from datetime import date |
| 36 | |
| 37 | import object_records |
| 38 | |
| 39 | ACTOR = "system_invoice_attention" |
| 40 | |
| 41 | # Settled, or never sent. Everything else with a due date in the past is |
| 42 | # money somebody owes today. |
| 43 | SETTLED_STATUSES = {"paid", "void", "draft"} |
| 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 _day(value): |
| 55 | """A YYYY-MM-DD cell as a date, or None when it will not parse. |
| 56 | |
| 57 | An invoice with no due date is not overdue -- it has no promise to |
| 58 | break -- and a hand-edited one that will not parse must not take the |
| 59 | whole count down with it. |
| 60 | """ |
| 61 | try: |
| 62 | year, month, day = (int(part) for part in _text(value).split("-")) |
| 63 | return date(year, month, day) |
| 64 | except (ValueError, AttributeError): |
| 65 | return None |
| 66 | |
| 67 | |
| 68 | def COUNT(request): |
| 69 | try: |
| 70 | rows = object_records.read_collection_records("invoices", base_dir=_base_dir()) |
| 71 | except Exception: |
| 72 | return {"count": 0} |
| 73 | |
| 74 | today = date.today() |
| 75 | late = [] |
| 76 | for row in rows: |
| 77 | if _text(row.get("doc_type") or "invoice") != "invoice": |
| 78 | continue |
| 79 | if _text(row.get("status")) in SETTLED_STATUSES: |
| 80 | continue |
| 81 | due = _day(row.get("due_date")) |
| 82 | if due is not None and due < today: |
| 83 | late.append((today - due).days) |
| 84 | |
| 85 | if not late: |
| 86 | return {"count": 0} |
| 87 | return {"count": len(late), "detail": f"oldest {max(late)} days late"} |