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_status.py · 3.2 KB · 87 lines
| 1 | """system_invoice_status -- HANDLES payment/refund writes; flips invoice |
| 2 | paid/partial state the moment money moves. |
| 3 | |
| 4 | The event-driven half of invoice aging (plan/payments-spec.md slice 2): |
| 5 | paid/partial react to payments IMMEDIATELY (a customer who just paid must not |
| 6 | stay "overdue" until tomorrow's daemon pass); the time-driven half (sent -> |
| 7 | overdue, dunning escalation) lives in system_invoice_aging, per |
| 8 | docs/logic-decisions.md #2. |
| 9 | |
| 10 | Reads the invoice's DERIVED truth (amount_paid_cents -- the payments/refunds |
| 11 | rollup chain) after the write that changed it; the storage layer has already |
| 12 | recomputed it by dispatch time. Only touches workflow states (sent, partial, |
| 13 | overdue, paid); draft and void are never auto-flipped. |
| 14 | """ |
| 15 | |
| 16 | import os |
| 17 | |
| 18 | import object_records |
| 19 | |
| 20 | HANDLES = [ |
| 21 | "payments.record.created", |
| 22 | "payments.record.updated", |
| 23 | "payments.record.deleted", |
| 24 | "refunds.record.created", |
| 25 | ] |
| 26 | |
| 27 | ACTOR = "system_invoice_status" |
| 28 | _FLIPPABLE = {"sent", "partial", "overdue", "paid"} |
| 29 | |
| 30 | |
| 31 | def _base_dir(): |
| 32 | return os.environ.get("DBBASIC_DATA_DIR", "data") |
| 33 | |
| 34 | |
| 35 | def EVENT(request): |
| 36 | collection = str(request.get("collection") or "") |
| 37 | record_id = str(request.get("record_id") or "") |
| 38 | if not record_id: |
| 39 | return {"ok": True, "skipped": "no record id"} |
| 40 | base = _base_dir() |
| 41 | |
| 42 | invoice_id = "" |
| 43 | try: |
| 44 | if collection in ("payments", "refunds"): |
| 45 | try: |
| 46 | row = object_records.get_collection_record(collection, record_id, base_dir=base) |
| 47 | invoice_id = row.get("invoice_id", "") |
| 48 | except Exception: |
| 49 | # deleted payment: the change log's before-image isn't in the |
| 50 | # payload; recompute every invoice is overkill -- skip, the |
| 51 | # daily aging pass trues it up. |
| 52 | return {"ok": True, "skipped": "source row gone"} |
| 53 | if not invoice_id: |
| 54 | return {"ok": True, "skipped": "no invoice"} |
| 55 | invoice = object_records.get_collection_record("invoices", invoice_id, base_dir=base) |
| 56 | except Exception as exc: |
| 57 | return {"ok": False, "error": str(exc)[:200]} |
| 58 | |
| 59 | status = invoice.get("status") or "" |
| 60 | if status not in _FLIPPABLE: |
| 61 | return {"ok": True, "skipped": f"status {status!r} not auto-flipped"} |
| 62 | try: |
| 63 | total = int(invoice.get("total_cents") or 0) |
| 64 | paid = int(invoice.get("amount_paid_cents") or 0) |
| 65 | except ValueError: |
| 66 | return {"ok": True, "skipped": "non-numeric amounts"} |
| 67 | if total <= 0: |
| 68 | return {"ok": True, "skipped": "zero-total invoice"} |
| 69 | |
| 70 | if paid >= total: |
| 71 | new_status = "paid" |
| 72 | elif paid > 0: |
| 73 | # partially paid; keep overdue sticky (time cleared only by payment |
| 74 | # in full or the aging pass re-evaluating) |
| 75 | new_status = "overdue" if status == "overdue" else "partial" |
| 76 | else: |
| 77 | new_status = "overdue" if status == "overdue" else "sent" |
| 78 | |
| 79 | if new_status == status: |
| 80 | return {"ok": True, "skipped": "status already correct"} |
| 81 | try: |
| 82 | object_records.update_collection_record( |
| 83 | "invoices", invoice_id, {"status": new_status}, base_dir=base, actor=ACTOR |
| 84 | ) |
| 85 | except Exception as exc: |
| 86 | return {"ok": False, "error": str(exc)[:200]} |
| 87 | return {"ok": True, "invoice": invoice_id, "status": new_status} |