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_barter_and_contra_cogs.py · 4.8 KB · 92 lines
| 1 | """Decision 6 (owner, 2026-11-30): scrap proceeds and vendor rebates are |
| 2 | contra-COGS; a barter is a sale and a purchase at fair value, taxable, via a |
| 3 | clearing account. Driven over HTTP with permission enforcement on, the way |
| 4 | a bookkeeper reaches the box. |
| 5 | """ |
| 6 | |
| 7 | import pytest |
| 8 | |
| 9 | import object_records |
| 10 | from scripts.try_package import Sandbox |
| 11 | |
| 12 | ACCOUNTS = (("cash", "asset"), ("revenue", "income"), ("tax", "liability"), |
| 13 | ("clearing", "asset"), ("printing", "expense"), ("cogs", "expense"), |
| 14 | ("scrap", "expense"), ("rebates", "expense")) |
| 15 | |
| 16 | |
| 17 | @pytest.fixture() |
| 18 | def books(): |
| 19 | with Sandbox(["app-finance", "app-settings"]) as box: |
| 20 | box.add_user("bea", roles=["finance"]) |
| 21 | box.add_user("mo", roles=["manager"]) |
| 22 | for account_id, kind in ACCOUNTS: |
| 23 | object_records.create_collection_record( |
| 24 | "fin_accounts", {"id": account_id, "name": account_id, "code": account_id, |
| 25 | "account_type": kind, "owner_id": "bea"}, |
| 26 | base_dir=box.data_dir) |
| 27 | for key, value in (("payments.journal.cash_account", "cash"), |
| 28 | ("payments.journal.revenue_account", "revenue"), |
| 29 | ("payments.journal.tax_account", "tax"), |
| 30 | ("barter.journal.clearing_account", "clearing"), |
| 31 | ("inventory.journal.scrap_sales_account", "scrap"), |
| 32 | ("inventory.journal.vendor_rebates_account", "rebates")): |
| 33 | object_records.create_collection_record( |
| 34 | "app_settings", {"key": key, "value": value}, base_dir=box.data_dir) |
| 35 | yield box |
| 36 | |
| 37 | |
| 38 | def _balances(box): |
| 39 | out = {} |
| 40 | for line in object_records.read_collection_records("fin_journal_lines", base_dir=box.data_dir): |
| 41 | net = int(line["credit_cents"] or 0) - int(line["debit_cents"] or 0) |
| 42 | out[line["account_id"]] = out.get(line["account_id"], 0) + net |
| 43 | return {k: v for k, v in out.items() if v} |
| 44 | |
| 45 | |
| 46 | BARTER = {"reference": "BARTER-1", "date": "2026-11-30", "counterparty": "Joe's Print", |
| 47 | "given": "Catering for 20", "given_value_cents": "40000", "tax_rate_bps": "825", |
| 48 | "received": "500 menus", "received_value_cents": "40000", |
| 49 | "received_account": "printing"} |
| 50 | |
| 51 | |
| 52 | def test_a_barter_is_booked_as_a_taxed_sale_and_a_purchase_through_clearing(books): |
| 53 | """Catering for printing, no money changing hands. Booking nothing (or |
| 54 | netting the two) hides 400.00 of revenue, 400.00 of cost and the 33.00 |
| 55 | of sales tax owed on the sale. What is left in clearing is what the |
| 56 | two sides still owe each other: here the tax, which is paid in cash.""" |
| 57 | status, body = books.call("bea", "POST", "/objects/action_record_barter", BARTER) |
| 58 | assert status == 200, body |
| 59 | assert (body["tax_cents"], body["clearing_left_cents"]) == (3300, 3300) |
| 60 | assert _balances(books) == {"revenue": 40000, "tax": 3300, "printing": -40000, |
| 61 | "clearing": -3300} |
| 62 | status, again = books.call("bea", "POST", "/objects/action_record_barter", BARTER) |
| 63 | assert status == 200, again |
| 64 | assert _balances(books)["revenue"] == 40000 # booked once by reference |
| 65 | |
| 66 | |
| 67 | def test_only_the_books_keepers_record_a_barter(books): |
| 68 | status, body = books.call("mo", "POST", "/objects/action_record_barter", BARTER) |
| 69 | assert status == 403, body |
| 70 | assert object_records.read_collection_records("fin_journals", base_dir=books.data_dir) == [] |
| 71 | |
| 72 | |
| 73 | @pytest.mark.parametrize("kind,account", [("scrap", "scrap"), ("rebate", "rebates")]) |
| 74 | def test_scrap_and_rebates_reduce_cost_of_goods_not_income(books, kind, account): |
| 75 | status, body = books.call("bea", "POST", "/objects/action_record_contra_cogs", |
| 76 | {"kind": kind, "reference": "R-1", "date": "2026-11-30", |
| 77 | "amount_cents": "1800", "description": "fryer oil"}) |
| 78 | assert status == 200, body |
| 79 | assert _balances(books) == {"cash": -1800, account: 1800} |
| 80 | accounts = {row["id"]: row for row in object_records.read_collection_records( |
| 81 | "fin_accounts", base_dir=books.data_dir)} |
| 82 | assert accounts[account]["account_type"] == "expense" # contra-COGS, not income |
| 83 | |
| 84 | |
| 85 | def test_an_unmapped_contra_account_books_nothing_and_names_the_setting(books): |
| 86 | row = next(r for r in object_records.read_collection_records("app_settings", base_dir=books.data_dir) |
| 87 | if r["key"] == "inventory.journal.scrap_sales_account") |
| 88 | object_records.delete_collection_record("app_settings", row["id"], base_dir=books.data_dir) |
| 89 | status, body = books.call("bea", "POST", "/objects/action_record_contra_cogs", |
| 90 | {"kind": "scrap", "reference": "R-2", "date": "2026-11-30", |
| 91 | "amount_cents": "100"}) |
| 92 | assert status == 409 and body["needs_settings"] == ["inventory.journal.scrap_sales_account"] |