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.
test_status_follows_the_money.py · 5.3 KB · 107 lines
| 1 | """Decision 1 (owner, 2026-11-30): an issued invoice's total never changes, |
| 2 | and its status follows the money. |
| 3 | |
| 4 | object_corrections.money_status is the one rule system_invoice_status and |
| 5 | the aging pass both read. The table is the decision written down: a debt is |
| 6 | cancelled only by a credit note; a refund on its own gives money back and |
| 7 | cancels nothing, so the invoice reopens; refunding a sale is a credit note |
| 8 | plus a refund and reads refunded; a balance below zero is credit owed to |
| 9 | the customer. tests/test_findings_handlers.py (M3, M4) and |
| 10 | tests/test_corner_cup.py drive the same rule through the real handlers. |
| 11 | """ |
| 12 | |
| 13 | import json |
| 14 | |
| 15 | import pytest |
| 16 | |
| 17 | import object_corrections |
| 18 | from test_findings_handlers import env # noqa: F401 -- the isolated install fixture |
| 19 | |
| 20 | |
| 21 | @pytest.mark.parametrize("current,total,received,refunded,credited,expected", [ |
| 22 | ("sent", 2000, 0, 0, 0, "sent"), |
| 23 | ("sent", 2000, 500, 0, 0, "partial"), |
| 24 | ("partial", 2000, 2000, 0, 0, "paid"), |
| 25 | # A refund with no credit note: the money went back, the debt stands. |
| 26 | ("paid", 2000, 2000, 2000, 0, "sent"), |
| 27 | ("paid", 2000, 2000, 800, 0, "partial"), |
| 28 | # A bounced or deleted payment leaves nothing received. |
| 29 | ("paid", 2000, 0, 0, 0, "sent"), |
| 30 | # overdue is the aging pass's word and stays while anything is owed. |
| 31 | ("overdue", 2000, 500, 0, 0, "overdue"), |
| 32 | ("overdue", 2000, 2000, 0, 0, "paid"), |
| 33 | # Credit notes cancel debt. |
| 34 | ("sent", 950, 0, 0, 300, "sent"), |
| 35 | ("sent", 950, 650, 0, 300, "paid"), |
| 36 | ("sent", 950, 0, 0, 950, "credited"), |
| 37 | # A credit note on a paid bill: the shop owes the customer. |
| 38 | ("paid", 950, 950, 0, 300, "credit_owed"), |
| 39 | ("paid", 950, 950, 300, 300, "paid"), |
| 40 | # Refunding the whole sale: credit note plus refund. |
| 41 | ("paid", 950, 950, 0, 950, "credit_owed"), |
| 42 | ("credit_owed", 950, 950, 950, 950, "refunded"), |
| 43 | # Overpaid with no credit at all is also credit owed. |
| 44 | ("partial", 1000, 1200, 0, 0, "credit_owed"), |
| 45 | # Not the money's to move. |
| 46 | ("draft", 1000, 1000, 0, 0, "draft"), |
| 47 | ("void", 1000, 0, 0, 0, "void"), |
| 48 | ]) |
| 49 | def test_status_follows_the_money(current, total, received, refunded, credited, expected): |
| 50 | assert object_corrections.money_status( |
| 51 | current, total=str(total), received=str(received), refunded=str(refunded), |
| 52 | credited=str(credited)) == expected |
| 53 | |
| 54 | |
| 55 | def test_a_credited_invoice_reads_credited_and_takes_no_payment_for_the_credit(env): |
| 56 | """Through the real handler (HTTP, handlers on): an invoice of 2000 |
| 57 | credited 500 owes 1500, so a payment of 2000 is an overpayment (the |
| 58 | ceiling knew refunds and not credit notes), 1500 settles it, and a |
| 59 | credit for the rest of a fresh invoice reads credited, not paid.""" |
| 60 | from test_findings_handlers import _invoice, _paid_state |
| 61 | env.install("app-settings", "app-invoices", "app-payments") |
| 62 | env.enable_handlers() |
| 63 | _invoice(env, "inv1") |
| 64 | env.create("invoices", {"id": "cn1", "doc_type": "credit_note", "number": "CN-1", |
| 65 | "customer_name": "Ada", "status": "sent", |
| 66 | "credits_invoice_id": "inv1", "subtotal_cents": "500", |
| 67 | "total_cents": "500", "owner_id": "u1"}) |
| 68 | status, body = env.http("POST", "/collections/invoice_credits/records", { |
| 69 | "credit_note_id": "cn1", "invoice_id": "inv1", "amount_cents": "500", |
| 70 | "owner_id": "u1"}) |
| 71 | assert status == 201, body |
| 72 | assert _paid_state(env, "inv1") == ("sent", 0, 1500) |
| 73 | status, body = env.http("POST", "/collections/payments/records", { |
| 74 | "invoice_id": "inv1", "amount_cents": "2000", "method": "card", |
| 75 | "received_on": "2026-11-02", "owner_id": "u1"}) |
| 76 | assert status == 409 and "1500" in json.dumps(body), body |
| 77 | status, body = env.http("POST", "/collections/payments/records", { |
| 78 | "invoice_id": "inv1", "amount_cents": "1500", "method": "card", |
| 79 | "received_on": "2026-11-02", "owner_id": "u1"}) |
| 80 | assert status == 201, body |
| 81 | assert _paid_state(env, "inv1") == ("paid", 1500, 0) |
| 82 | |
| 83 | _invoice(env, "inv2") |
| 84 | env.create("invoices", {"id": "cn2", "doc_type": "credit_note", "number": "CN-2", |
| 85 | "customer_name": "Ada", "status": "sent", |
| 86 | "credits_invoice_id": "inv2", "subtotal_cents": "2000", |
| 87 | "total_cents": "2000", "owner_id": "u1"}) |
| 88 | status, body = env.http("POST", "/collections/invoice_credits/records", { |
| 89 | "credit_note_id": "cn2", "invoice_id": "inv2", "amount_cents": "2000", |
| 90 | "owner_id": "u1"}) |
| 91 | assert status == 201, body |
| 92 | assert _paid_state(env, "inv2") == ("credited", 0, 0) |
| 93 | |
| 94 | |
| 95 | def test_every_status_the_rule_can_answer_is_a_move_the_invoices_ladder_allows(): |
| 96 | """The rule and the schema must agree, or a move is refused by the |
| 97 | ladder and the refusal swallowed -- which is exactly how a fully |
| 98 | refunded invoice stayed paid (finding M3).""" |
| 99 | import json |
| 100 | from pathlib import Path |
| 101 | schema = json.loads((Path(__file__).resolve().parents[1] / "packages" / "app-invoices" |
| 102 | / "schemas" / "invoices.json").read_text()) |
| 103 | [status] = [f for f in schema["fields"] if f["name"] == "status"] |
| 104 | for state in object_corrections.MONEY_STATES: |
| 105 | assert state in status["enum"] |
| 106 | for other in object_corrections.MONEY_STATES - {state}: |
| 107 | assert other in status["transitions"][state], (state, other) |