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_subscription_advance_billing.py · 7.8 KB · 164 lines
| 1 | """Decision 4 (owner, 2026-11-30): subscriptions bill the base fee in advance |
| 2 | and usage in arrears; cash basis recognises revenue on receipt, accrual |
| 3 | posts what is billed in advance to Deferred Revenue and releases it monthly; |
| 4 | cancellation runs to the period's end, and staff may instead end it now |
| 5 | with a pro-rata credit note; usage is bucketed by when it occurred. |
| 6 | |
| 7 | The biller's invoices and the rollup are pinned in tests/test_billing_runner.py, |
| 8 | tests/test_usage_metering.py and tests/test_findings_handlers.py (M10, M11). |
| 9 | This file holds the books and the staff credit. |
| 10 | """ |
| 11 | |
| 12 | import pytest |
| 13 | |
| 14 | from scripts.try_package import Sandbox |
| 15 | from test_findings_handlers import BILLING_PACKAGES, _plan, _subscribe, _usage_event |
| 16 | from test_findings_handlers import env # noqa: F401 -- the isolated install fixture |
| 17 | |
| 18 | import object_records |
| 19 | |
| 20 | |
| 21 | def _accrual(env): |
| 22 | env.install(*BILLING_PACKAGES) |
| 23 | _plan(env, "plan-pro") |
| 24 | for account_id, kind in (("acct-cash", "asset"), ("acct-ar", "asset"), |
| 25 | ("acct-rev", "income"), ("acct-deferred", "liability")): |
| 26 | env.create("fin_accounts", {"id": account_id, "name": account_id, "code": account_id, |
| 27 | "account_type": kind, "owner_id": "dan"}) |
| 28 | for key, value in (("payments.accounting_basis", "accrual"), |
| 29 | ("payments.journal.cash_account", "acct-cash"), |
| 30 | ("payments.journal.receivable_account", "acct-ar"), |
| 31 | ("payments.journal.revenue_account", "acct-rev"), |
| 32 | ("payments.journal.deferred_revenue_account", "acct-deferred")): |
| 33 | env.setting(key, value) |
| 34 | |
| 35 | |
| 36 | def _balances(env): |
| 37 | out = {} |
| 38 | for line in env.rows("fin_journal_lines"): |
| 39 | net = int(line["credit_cents"] or 0) - int(line["debit_cents"] or 0) |
| 40 | out[line["account_id"]] = out.get(line["account_id"], 0) + net |
| 41 | return {k: v for k, v in out.items() if v} |
| 42 | |
| 43 | |
| 44 | def test_accrual_books_a_fee_billed_in_advance_as_deferred_and_releases_it_when_earned(env): |
| 45 | """June's 49.00 billed on 1 June is a debt the customer owes and a |
| 46 | month of service the shop owes: Deferred Revenue, not revenue. It is |
| 47 | earned when June ends, and released then -- once, however often the |
| 48 | biller runs.""" |
| 49 | _accrual(env) |
| 50 | _subscribe(env, "sub-a") |
| 51 | env.start_change_dispatch() |
| 52 | env.run("system_billing_runner", today="2026-06-01") |
| 53 | env.replay() |
| 54 | assert _balances(env) == {"acct-ar": -4900, "acct-deferred": 4900} |
| 55 | |
| 56 | env.run("system_billing_runner", today="2026-06-15") |
| 57 | assert _balances(env) == {"acct-ar": -4900, "acct-deferred": 4900} |
| 58 | |
| 59 | env.run("system_billing_runner", today="2026-06-30") |
| 60 | env.replay() |
| 61 | # June released to revenue; July billed (on the June close) and deferred. |
| 62 | env.run("system_billing_runner", today="2026-06-30") |
| 63 | assert _balances(env) == {"acct-ar": -9800, "acct-rev": 4900, "acct-deferred": 4900} |
| 64 | |
| 65 | |
| 66 | def test_accrual_without_a_deferred_revenue_account_books_nothing_and_says_so(env): |
| 67 | _accrual(env) |
| 68 | object_records.update_collection_record( |
| 69 | "app_settings", next(r["id"] for r in env.rows("app_settings") |
| 70 | if r["key"] == "payments.journal.deferred_revenue_account"), |
| 71 | {"value": " "}, base_dir=env.data_dir) |
| 72 | _subscribe(env, "sub-a") |
| 73 | env.run("system_billing_runner", today="2026-06-01") |
| 74 | invoice = env.rows("invoices")[0] |
| 75 | result = env.run("system_books", "EVENT", event="invoices.record.created", |
| 76 | collection="invoices", record_id=invoice["id"], action="create") |
| 77 | assert result["ok"] is False |
| 78 | assert result["needs_settings"] == ["payments.journal.deferred_revenue_account"] |
| 79 | assert env.rows("fin_journals") == [] |
| 80 | |
| 81 | |
| 82 | def test_late_usage_for_a_period_already_billed_is_left_visible_not_moved(env): |
| 83 | """Bucketing by when usage occurred means a June event folded after |
| 84 | June was invoiced belongs to an invoice already sent. How to bill it is |
| 85 | not decided, so it is reported and left unrated -- never quietly added |
| 86 | to a closed bill or counted against July.""" |
| 87 | env.install(*BILLING_PACKAGES) |
| 88 | _plan(env, "plan-pro") |
| 89 | _subscribe(env, "sub-u", start="2026-06-01", end="2026-06-30") |
| 90 | _usage_event(env, "e1", 7, "2026-06-10T10:00:00Z") |
| 91 | env.run("system_usage_rollup", today="2026-06-20") |
| 92 | env.run("system_billing_runner", today="2026-06-30") |
| 93 | # June's usage was billed on its invoice (a priced plan marks the |
| 94 | # summary; this plan prices nothing, so it is marked as the biller would). |
| 95 | [june] = env.rows("usage_summaries") |
| 96 | env.update("usage_summaries", june["id"], {"invoiced": "true"}) |
| 97 | _usage_event(env, "late", 5, "2026-06-29T23:00:00Z") |
| 98 | folded = env.run("system_usage_rollup", today="2026-07-02") |
| 99 | assert folded["late_for_invoiced_period"] == [ |
| 100 | {"subscription": "sub-u", "metric": "api_calls", "period_start": "2026-06-01", |
| 101 | "events": 1}] |
| 102 | assert env.get("usage_events", "late")["rated"] == "false" |
| 103 | assert [(r["period_start"], r["quantity"]) for r in env.rows("usage_summaries")] == [ |
| 104 | ("2026-06-01", "7")] |
| 105 | |
| 106 | |
| 107 | # --- staff end a subscription early with a pro-rata credit ----------------------- |
| 108 | |
| 109 | |
| 110 | @pytest.fixture() |
| 111 | def billing_box(): |
| 112 | with Sandbox(["app-billing", "app-settings"]) as box: |
| 113 | box.add_user("mo", roles=["manager"]) |
| 114 | box.add_user("cust") |
| 115 | object_records.create_collection_record("billing_plans", { |
| 116 | "id": "plan-pro", "name": "Pro", "code": "pro", "period": "month", |
| 117 | "base_minor": "3000", "is_active": "true", "owner_id": "mo"}, |
| 118 | base_dir=box.data_dir) |
| 119 | object_records.create_collection_record("subscriptions", { |
| 120 | "id": "sub-c", "customer_name": "Cust", "plan_id": "plan-pro", |
| 121 | "billing_mode": "subscription", "status": "active", |
| 122 | "current_period_start": "2026-06-01", "current_period_end": "2026-07-01", |
| 123 | "owner_id": "cust"}, base_dir=box.data_dir) |
| 124 | yield box |
| 125 | |
| 126 | |
| 127 | def _credit(box, user, **payload): |
| 128 | return box.call(user, "POST", "/objects/action_subscription_credit_unused", |
| 129 | {"subscription_id": "sub-c", **payload}) |
| 130 | |
| 131 | |
| 132 | def test_staff_end_a_subscription_now_with_a_pro_rata_credit_note(billing_box): |
| 133 | """30.00 billed in advance for June (30 days); ended on the 21st, the |
| 134 | 10 unused days are credited: 10.00, against the June invoice, applied |
| 135 | at once. The subscriber's own cancel runs to the period's end and |
| 136 | credits nothing.""" |
| 137 | import object_execution |
| 138 | import python_object_runtime |
| 139 | object_execution.execute_object( |
| 140 | python_object_runtime.PythonObjectRuntime(base_dir=billing_box.data_dir), |
| 141 | object_execution.ObjectExecutionRequest( |
| 142 | "system_billing_runner", method="POST", payload={"today": "2026-06-01"}), |
| 143 | roots=[billing_box.objects_dir]) |
| 144 | [june] = object_records.read_collection_records("invoices", base_dir=billing_box.data_dir) |
| 145 | assert june["total_cents"] == "3000" |
| 146 | |
| 147 | status, body = _credit(billing_box, "cust", as_of="2026-06-21") |
| 148 | assert status == 403, body |
| 149 | status, body = _credit(billing_box, "mo", as_of="2026-06-21", reason="moved abroad") |
| 150 | assert status == 200, body |
| 151 | assert (body["amount_cents"], body["unused_days"], body["period_days"]) == (1000, 10, 30) |
| 152 | june = object_records.get_collection_record("invoices", june["id"], |
| 153 | base_dir=billing_box.data_dir) |
| 154 | assert (june["credited_cents"], june["balance_due_cents"]) == ("1000", "2000") |
| 155 | sub = object_records.get_collection_record("subscriptions", "sub-c", |
| 156 | base_dir=billing_box.data_dir) |
| 157 | assert sub["status"] == "canceled" |
| 158 | status, body = _credit(billing_box, "mo", as_of="2026-06-22") |
| 159 | assert status == 409, body |
| 160 | |
| 161 | |
| 162 | def test_a_period_not_billed_in_advance_has_nothing_to_credit(billing_box): |
| 163 | status, body = _credit(billing_box, "mo", as_of="2026-06-21") |
| 164 | assert status == 409 and "not billed in advance" in body["error"], body |