from conftest import SAMPLE_CSV def setup_book(admin): admin.post("/api/books", json={"name": "b", "year": 2026, "opening_balance": "100"}) path = admin.post("/api/upload?filename=april.csv", content=SAMPLE_CSV.encode()).json()["path"] admin.post("/api/import", json={"book": "b", "statements": [path]}) rows = admin.get("/api/month?book=b&sheet=APR").json()["rows"] sale = next(r for r in rows if r["income"] == 1150) jaycar = next(r for r in rows if r["expense"] == 115) admin.post("/api/row", json={"book": "b", "sheet": "APR", "row": sale["row"], "gst": "Incl GST", "category": "Sales"}) admin.post("/api/row", json={"book": "b", "sheet": "APR", "row": jaycar["row"], "category": "Purchases"}) def values(data, form): return {r["box"]: r["value"] for r in data[form.lower()]["rows"] if r["box"]} def test_ir10_from_cashbook(admin): setup_book(admin) data = admin.get("/api/tax?book=b").json() v = values(data, "IR10") assert v["2"] == 1000.0 # sales excluding GST assert v["4"] == 100.0 # purchases excluding GST assert v["6"] == 900.0 assert v["24"] == 8.5 # bank fees assert v["27"] == 891.5 assert v["31"] == round(100 - 8.5 - 115 + 1150 + 500 - 5.53, 2) # bank balance at year end assert v["57"] == 0.0 assert any("isn't allocated" in n for n in data["ir10"]["notes"]) # the Hetzner payment assert any("funds introduced" in n for n in data["ir10"]["notes"]) def test_adjustments_and_ir4(admin): setup_book(admin) # depreciation, closing stock and RWT; tax to pay at 28% admin.post("/api/tax", json={"book": "b", "form": "IR10", "box": "13", "value": 91.5}) admin.post("/api/tax", json={"book": "b", "form": "IR10", "box": "5", "value": 50}) data = admin.post("/api/tax", json={"book": "b", "form": "IR4", "box": "30K", "value": 100}).json() v10, v4 = values(data, "IR10"), values(data, "IR4") assert (v10["6"], v10["25"], v10["29"]) == (950.0, 100.0, 850.0) assert (v4["20B"], v4["29"], v4["30A"], v4["30B"]) == (850.0, 850.0, 850.0, 238.0) assert v4["30L"] == 138.0 assert data["ir4"]["result"] == {"amount": 138.0, "kind": "to pay"} # adjustments add to the cashbook figure, and clearing one removes it data = admin.post("/api/tax", json={"book": "b", "form": "IR10", "box": "2", "value": -1000}).json() assert values(data, "IR10")["2"] == 0.0 assert data["ir4"]["result"]["kind"] == "refund" # a loss: no tax, provisional tax comes back data = admin.post("/api/tax", json={"book": "b", "form": "IR10", "box": "2", "value": None}).json() assert values(data, "IR10")["2"] == 1000.0 def test_tax_inputs_are_checked(admin): setup_book(admin) assert admin.post("/api/tax", json={"book": "b", "form": "IR10", "box": "29", "value": 1}).status_code == 400 assert admin.post("/api/tax", json={"book": "b", "form": "IR3", "box": "1", "value": 1}).status_code == 400 assert admin.get("/api/tax?book=nope").status_code == 404 def test_tax_needs_sign_in(client): assert client.get("/api/tax?book=b").status_code == 401