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
invoice_attention.py · 2.9 KB · 87 lines
1"""system_invoice_attention -- how many invoices are past their due date.
2
3COUNT {} -> {count, detail}
4
5Time changes an invoice's meaning with no write happening
6(system_invoice_aging's whole premise), so "overdue" is a queue that
7appears out of the calendar rather than out of anybody's action. That is
8the kind of pile nothing draws attention to: no record changed, no event
9fired, and the only person who finds out is the one who happens to open
10the invoice list.
11
12Severity `warning` rather than `normal` in the manifest, because this
13queue is not work that arrived, it is work that is already late.
14
15Three deliberate exclusions, each of which would otherwise inflate the
16number 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
25The detail names the worst case rather than the total, because "45 days"
26is what decides whether this is a reminder email or a phone call.
27
28COUNT rather than GET or POST: the verb is the contract with the daemon's
29attention pass. Nothing here writes. A missing collection reads as zero;
30a genuine failure raises, so the rollup records it and keeps the last
31count rather than reporting that nobody owes anything.
32"""
33
34import os
35from datetime import date
36
37import object_records
38
39ACTOR = "system_invoice_attention"
40
41# Settled, or never sent. Everything else with a due date in the past is
42# money somebody owes today.
43SETTLED_STATUSES = {"paid", "void", "draft"}
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 _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
68def 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"}