Invoices: business details and logo, saved customers, numbering, GST per line, PDF download (fpdf2), and marking paid, which records the income in the book (split into Incl and Excl GST rows when an invoice has both) or uses an already-imported bank deposit. Statement import treats a split payment as the one bank line it came from. Mileage: vehicles and trips with odometer readings, business or private use, IRD kilometre rates entered per income year, a per-vehicle tier 1/2 claim and a CSV logbook export. Report only: nothing is added to the cashbook or IR10. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
82 lines
3.8 KiB
Python
82 lines
3.8 KiB
Python
import pytest
|
|
|
|
|
|
@pytest.fixture
|
|
def cars(admin):
|
|
admin.ute = admin.post("/api/vehicles", json={"name": "Ute", "rego": "abc123"}).json()
|
|
admin.van = admin.post("/api/vehicles", json={"name": "Van"}).json()
|
|
return admin
|
|
|
|
|
|
def trip(client, vehicle, day, **kw):
|
|
r = client.post("/api/trips", json={"vehicle_id": vehicle["id"], "date": day, "purpose": "Site visit", **kw})
|
|
assert r.status_code == 200, r.text
|
|
return r.json()
|
|
|
|
|
|
def test_odometer_km(cars):
|
|
t = trip(cars, cars.ute, "2026-05-01", odo_start=10000, odo_end=10042)
|
|
assert (t["km"], t["vehicle"], t["warnings"]) == (42, "Ute", [])
|
|
assert cars.ute["rego"] == "ABC123"
|
|
bad = {"vehicle_id": cars.ute["id"], "date": "2026-05-02"}
|
|
assert cars.post("/api/trips", json={**bad, "odo_start": 10100, "odo_end": 10050}).status_code == 400
|
|
assert cars.post("/api/trips", json=bad).status_code == 400 # no distance at all
|
|
assert cars.post("/api/trips", json={**bad, "km": 5, "vehicle_id": 999}).status_code == 404
|
|
# overlapping readings are kept, with a warning
|
|
assert trip(cars, cars.ute, "2026-05-02", odo_start=10030, odo_end=10060)["warnings"]
|
|
# km typed directly
|
|
assert trip(cars, cars.van, "2026-05-03", km=12.5)["km"] == 12.5
|
|
|
|
|
|
def test_report_tiers_per_vehicle(cars):
|
|
r = cars.get("/api/mileage/report?year=2026").json()
|
|
assert (r["claim"], r["vehicles"], r["rate"]) == (None, [], None)
|
|
|
|
trip(cars, cars.ute, "2026-04-01", km=15000)
|
|
trip(cars, cars.ute, "2026-06-01", km=1000, business=False)
|
|
trip(cars, cars.van, "2027-03-31", km=10000)
|
|
trip(cars, cars.van, "2027-04-01", km=999) # next year
|
|
|
|
r = cars.get("/api/mileage/report?year=2026").json()
|
|
assert r["claim"] is None and r["vehicles"][0]["claim"] is None # no rate yet: no claim, not zero
|
|
|
|
assert cars.put("/api/mileage/rates/2026", json={"tier1": 0, "tier2": 0.3}).status_code == 400
|
|
cars.put("/api/mileage/rates/2026", json={"tier1": 1.17, "tier2": 0.37})
|
|
r = cars.get("/api/mileage/report?year=2026").json()
|
|
ute, van = r["vehicles"]
|
|
assert (ute["business_km"], ute["private_km"], ute["business_percent"]) == (15000, 1000, 93.8)
|
|
assert (ute["tier1_km"], ute["tier2_km"], ute["claim"]) == (14000, 1000, 16380 + 370)
|
|
# the 14,000 km limit is per vehicle
|
|
assert (van["tier1_km"], van["tier2_km"], van["claim"]) == (10000, 0, 11700)
|
|
assert r["claim"] == 16750 + 11700
|
|
assert r["business_km"] == 25000
|
|
|
|
|
|
def test_export_and_vehicle_delete(cars):
|
|
trip(cars, cars.ute, "2026-05-01", odo_start=100, odo_end=150, to_place="Hamilton", customer="Client Ltd")
|
|
trip(cars, cars.ute, "2026-05-02", km=7, business=False)
|
|
r = cars.get("/api/mileage/export?year=2026")
|
|
assert r.status_code == 200 and "logbook-2026-27.csv" in r.headers["content-disposition"]
|
|
lines = r.content.decode("utf-8-sig").splitlines()
|
|
assert lines[1] == "2026-05-01,Ute,,Hamilton,Site visit,Client Ltd,100,150,50,Business"
|
|
assert lines[2].endswith(",,,7,Private")
|
|
|
|
assert cars.delete(f"/api/vehicles/{cars.ute['id']}").status_code == 400
|
|
assert cars.put(f"/api/vehicles/{cars.ute['id']}", json={"active": False}).json()["active"] == 0
|
|
assert cars.delete(f"/api/vehicles/{cars.van['id']}").status_code == 200
|
|
assert cars.post("/api/vehicles", json={"name": "Ute"}).status_code == 400
|
|
|
|
|
|
def test_edit_and_delete_trip(cars):
|
|
t = trip(cars, cars.ute, "2026-05-01", km=10)
|
|
r = cars.put(f"/api/trips/{t['id']}", json={"vehicle_id": cars.ute["id"], "date": "2026-05-01",
|
|
"odo_start": 5, "odo_end": 25})
|
|
assert r.json()["km"] == 20
|
|
cars.delete(f"/api/trips/{t['id']}")
|
|
assert cars.get("/api/trips?year=2026").json() == []
|
|
|
|
|
|
def test_needs_sign_in(client):
|
|
assert client.get("/api/trips").status_code == 401
|
|
assert client.get("/api/mileage/report?year=2026").status_code == 401
|