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
expense_attention.py · 1.9 KB · 61 lines
| 1 | """system_expense_attention -- how much spending is waiting for approval. |
| 2 | |
| 3 | COUNT {} -> {count, detail} |
| 4 | |
| 5 | The same gate as time logs, over money instead of hours: an expense at |
| 6 | `submitted` has been claimed and not yet decided, and the person who has |
| 7 | to decide is by design not the person who claimed it. Until somebody |
| 8 | looks, the books do not have it and the claimant is out of pocket, which |
| 9 | is the kind of queue people find out about through a complaint rather |
| 10 | than through a screen. |
| 11 | |
| 12 | The detail is the total, in whole currency units, because approving four |
| 13 | coffees and approving a laptop are the same row count and very different |
| 14 | afternoons. |
| 15 | |
| 16 | COUNT rather than GET or POST: the verb is the contract with the daemon's |
| 17 | attention pass. Nothing here writes. A missing collection reads as zero; |
| 18 | a genuine failure raises, so the rollup records the error and keeps the |
| 19 | last count rather than reporting an empty queue nobody emptied. |
| 20 | """ |
| 21 | |
| 22 | import os |
| 23 | |
| 24 | import object_records |
| 25 | |
| 26 | ACTOR = "system_expense_attention" |
| 27 | |
| 28 | # draft is still being written, approved/rejected have had a decision, |
| 29 | # billed is already on an invoice. `submitted` is the waiting room. |
| 30 | WAITING_STATUS = "submitted" |
| 31 | |
| 32 | |
| 33 | def _base_dir(): |
| 34 | return os.environ.get("DBBASIC_DATA_DIR", "data") |
| 35 | |
| 36 | |
| 37 | def _text(value): |
| 38 | return str(value if value is not None else "").strip() |
| 39 | |
| 40 | |
| 41 | def _cents(value): |
| 42 | try: |
| 43 | return int(_text(value) or 0) |
| 44 | except ValueError: |
| 45 | # A hand-edited amount must not take the count down with it. |
| 46 | return 0 |
| 47 | |
| 48 | |
| 49 | def COUNT(request): |
| 50 | try: |
| 51 | rows = object_records.read_collection_records("expenses", base_dir=_base_dir()) |
| 52 | except Exception: |
| 53 | return {"count": 0} |
| 54 | |
| 55 | waiting = [row for row in rows if _text(row.get("status")) == WAITING_STATUS] |
| 56 | if not waiting: |
| 57 | return {"count": 0} |
| 58 | |
| 59 | total = sum(_cents(row.get("amount_cents")) for row in waiting) |
| 60 | detail = f"{total / 100:,.2f} awaiting approval" if total else "" |
| 61 | return {"count": len(waiting), "detail": detail} |