Runs live, versioned Python application objects on one VM, with a suite of apps: projects, notes, tasks, files and more.

9 downloads

dbbasic-object-server/tests/test_tax_per_line.py
Install on DigitalOcean 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.

Daniel Quellhorst Changelog for this batch. 65ef78d 3 hours, 19 minutes ago History
test_tax_per_line.py · 7.8 KB · 177 lines
1"""Decision 2 (owner, 2026-11-30): the tax rate is stamped per line at issue;
2tax is computed once per rate group, half-up, the odd cent to the line with
3the largest remainder; it is never recalculated after issue; and it posts
4to Sales Tax Payable, not to revenue.
5
6The checkout and fold halves are driven end to end in
7tests/test_findings_handlers.py (M1, M2, M18, D2); the counter's void and
8credit notes in tests/test_corner_cup.py. This file holds the arithmetic and
9the books.
10"""
11
12import pytest
13
14import object_lines
15from test_findings_handlers import env # noqa: F401 -- the isolated install fixture
16
17
18def _line(price, rate, quantity="1"):
19 return {"quantity": quantity, "unit_price_cents": str(price), "tax_rate_bps": str(rate)}
20
21
22def test_four_fifty_cent_lines_at_five_percent_owe_ten_cents_not_twelve():
23 """The reason tax is rounded per rate group, not per line."""
24 lines = [_line(50, 500)] * 4
25 assert sum(object_lines.allocate_tax(lines)) == 10
26 assert object_lines.allocate_tax(lines) == [3, 3, 2, 2]
27 assert object_lines.totals(lines)["tax_cents"] == 10
28
29
30def test_the_odd_cent_goes_to_the_line_with_the_largest_remainder():
31 # 115.5 and 53.625: the sale's 169.125 is 169; floors make 168 and the
32 # cent goes to the .625.
33 assert object_lines.allocate_tax([_line(1400, 825), _line(650, 825)]) == [115, 54]
34 # Ties go to the earlier line.
35 assert object_lines.allocate_tax([_line(10, 500), _line(10, 500), _line(10, 500)]) == [1, 1, 0]
36
37
38def test_each_rate_group_is_rounded_once_on_its_own():
39 lines = [_line(50, 500), _line(50, 500), _line(999, 2000), _line(700, 0)]
40 shares = object_lines.allocate_tax(lines)
41 assert shares[:2] == [3, 2] # 5.0 for the 5% group
42 assert shares[2] == 200 # 199.8 for the 20% group
43 assert shares[3] == 0
44 assert object_lines.totals(lines)["tax_cents"] == 205
45
46
47def test_a_discount_line_takes_a_negative_share_of_its_rate_group():
48 assert object_lines.allocate_tax([_line(1400, 825), _line(-280, 825)]) == [115, -23]
49
50
51# --- tax posts to Sales Tax Payable ----------------------------------------------
52
53
54def _books(env, basis):
55 env.install("app-settings", "app-finance", "app-invoices", "app-payments")
56 for account_id, kind in (("acct-cash", "asset"), ("acct-ar", "asset"),
57 ("acct-rev", "income"), ("acct-tax", "liability")):
58 env.create("fin_accounts", {"id": account_id, "name": account_id, "code": account_id,
59 "account_type": kind, "owner_id": "dan"})
60 for key, value in (("payments.accounting_basis", basis),
61 ("payments.journal.cash_account", "acct-cash"),
62 ("payments.journal.receivable_account", "acct-ar"),
63 ("payments.journal.revenue_account", "acct-rev")):
64 env.setting(key, value)
65 env.enable_handlers()
66
67
68def _balances(env):
69 out = {}
70 for line in env.rows("fin_journal_lines"):
71 net = int(line["credit_cents"] or 0) - int(line["debit_cents"] or 0)
72 out[line["account_id"]] = out.get(line["account_id"], 0) + net
73 return {k: v for k, v in out.items() if v}
74
75
76def _taxed_invoice(env, status="sent"):
77 status_code, body = env.http("POST", "/collections/invoices/records", {
78 "id": "inv1", "number": "INV-1", "customer_name": "Ada", "status": status,
79 "issue_date": "2026-11-01", "due_date": "2026-12-31",
80 "subtotal_cents": "1000", "tax_cents": "100", "total_cents": "1100",
81 "owner_id": "dan"})
82 assert status_code == 201, body
83
84
85def _pay(env, payment_id, amount):
86 status, body = env.http("POST", "/collections/payments/records", {
87 "id": payment_id, "invoice_id": "inv1", "amount_cents": str(amount),
88 "method": "card", "received_on": "2026-11-02", "owner_id": "dan"})
89 assert status == 201, body
90
91
92def test_cash_basis_books_each_receipts_share_of_tax_to_sales_tax_payable(env):
93 """A payment of 1100 on a 1000 + 100 tax invoice used to credit revenue
94 1100: the shop's income overstated by the tax it owes the state."""
95 _books(env, "cash")
96 env.setting("payments.journal.tax_account", "acct-tax")
97 _taxed_invoice(env)
98 _pay(env, "p1", 550)
99 assert _balances(env) == {"acct-cash": -550, "acct-rev": 500, "acct-tax": 50}
100 _pay(env, "p2", 550)
101 assert _balances(env) == {"acct-cash": -1100, "acct-rev": 1000, "acct-tax": 100}
102
103
104def test_accrual_books_the_tax_at_issue(env):
105 _books(env, "accrual")
106 env.setting("payments.journal.tax_account", "acct-tax")
107 _taxed_invoice(env)
108 assert _balances(env) == {"acct-ar": -1100, "acct-rev": 1000, "acct-tax": 100}
109
110
111def test_a_taxed_sale_with_no_tax_account_is_not_booked_as_revenue(env):
112 """Fail closed: with no Sales Tax Payable account mapped, booking the
113 tax as revenue would be wrong in a way nobody notices; the entry is not
114 composed and the answer names the setting."""
115 _books(env, "cash")
116 _taxed_invoice(env)
117 _pay(env, "p1", 1100)
118 assert env.rows("fin_journals") == []
119 result = env.run("system_books", "EVENT", event="payments.record.created",
120 collection="payments", record_id="p1", action="create")
121 assert result["ok"] is False
122 assert result["needs_settings"] == ["payments.journal.tax_account"]
123
124
125def _credit_note(env, amount=550, tax=50):
126 status, body = env.http("POST", "/collections/invoices/records", {
127 "id": "cn1", "doc_type": "credit_note", "number": "CN-1", "customer_name": "Ada",
128 "status": "sent", "issue_date": "2026-11-05", "credits_invoice_id": "inv1",
129 "subtotal_cents": str(amount - tax), "tax_cents": str(tax),
130 "total_cents": str(amount), "owner_id": "dan"})
131 assert status == 201, body
132
133
134def test_an_accrual_credit_note_reverses_revenue_and_tax_at_its_own_rate(env):
135 """Credit notes were never journaled: on accrual the credited sale
136 stayed in revenue, its tax stayed owed to the state and the customer's
137 balance stayed receivable. The credit note reverses all three by what
138 it states (the original rate, never recalculated), and the refund that
139 pays it out moves receivable to cash."""
140 _books(env, "accrual")
141 env.setting("payments.journal.tax_account", "acct-tax")
142 _taxed_invoice(env)
143 _pay(env, "p1", 1100)
144 _credit_note(env)
145 assert _balances(env) == {"acct-cash": -1100, "acct-ar": 550, "acct-rev": 500,
146 "acct-tax": 50}
147 status, body = env.http("POST", "/collections/refunds/records", {
148 "payment_id": "p1", "amount_cents": "550", "refunded_on": "2026-11-05",
149 "credit_note_id": "cn1", "owner_id": "dan"})
150 assert status == 201, body
151 assert _balances(env) == {"acct-cash": -550, "acct-rev": 500, "acct-tax": 50}
152
153
154def test_a_cash_basis_refund_of_a_credit_note_takes_back_its_own_tax(env):
155 _books(env, "cash")
156 env.setting("payments.journal.tax_account", "acct-tax")
157 _taxed_invoice(env)
158 _pay(env, "p1", 1100)
159 _credit_note(env, amount=330, tax=30)
160 assert _balances(env) == {"acct-cash": -1100, "acct-rev": 1000, "acct-tax": 100}
161 status, body = env.http("POST", "/collections/refunds/records", {
162 "payment_id": "p1", "amount_cents": "330", "refunded_on": "2026-11-05",
163 "credit_note_id": "cn1", "owner_id": "dan"})
164 assert status == 201, body
165 assert _balances(env) == {"acct-cash": -770, "acct-rev": 700, "acct-tax": 70}
166
167
168@pytest.mark.parametrize("amount,share",[(1100, 100), (550, 50), (1, 0), (11, 1)])
169def test_a_receipts_tax_share_is_pro_rata_half_up(amount, share):
170 import importlib.util
171 from pathlib import Path
172 path = Path(__file__).resolve().parents[1] / "packages/app-payments/objects/system/books.py"
173 spec = importlib.util.spec_from_file_location("books_under_test", path)
174 module = importlib.util.module_from_spec(spec)
175 module._logger = None
176 spec.loader.exec_module(module)
177 assert module.tax_share(amount, {"total_cents": "1100", "tax_cents": "100"}) == share