Receipts and invoices: attach photos and PDFs to transactions, read the supplier, date, total, GST and line items with Tesseract OCR, and match them to bank transactions by amount and date. Receipts can have several pages, and hand corrections survive re-reading. Suppliers: saved suppliers that uploads are linked to by GST number or name. The sections (cashbook, receipts, invoices, customers, suppliers, mileage, statements) move to a top bar, with the book controls in a row beneath it, and the layout works on phones. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
85 lines
4.1 KiB
Python
85 lines
4.1 KiB
Python
import sqlite3
|
|
|
|
import pytest
|
|
|
|
import ledger
|
|
from cashbook_app.services import receipts
|
|
from test_receipts import png
|
|
|
|
|
|
@pytest.fixture
|
|
def ocr(admin, monkeypatch):
|
|
"""OCR replaced by whatever `fake_ocr` holds."""
|
|
admin.fake_ocr = {"status": "done", "text": "", "items": []}
|
|
monkeypatch.setattr(receipts, "read_document", lambda p, mime: dict(admin.fake_ocr))
|
|
return admin
|
|
|
|
|
|
def test_supplier_crud(admin):
|
|
s = admin.post("/api/suppliers", json={"name": " Bunnings ", "gst_number": "123-456-789"}).json()
|
|
assert (s["name"], s["gst_number"], s["documents"], s["total"]) == ("Bunnings", "123-456-789", 0, 0)
|
|
assert admin.post("/api/suppliers", json={"name": ""}).status_code == 422
|
|
s = admin.put(f"/api/suppliers/{s['id']}", json={"name": "Bunnings Ltd", "email": "a@b.nz"}).json()
|
|
assert (s["name"], s["email"], s["gst_number"]) == ("Bunnings Ltd", "a@b.nz", "")
|
|
assert [x["name"] for x in admin.get("/api/suppliers").json()] == ["Bunnings Ltd"]
|
|
assert admin.delete(f"/api/suppliers/{s['id']}").json() == {"ok": True}
|
|
assert admin.get(f"/api/suppliers/{s['id']}").status_code == 404
|
|
|
|
|
|
def test_receipts_link_to_suppliers(ocr):
|
|
sup = ocr.post("/api/suppliers", json={"name": "Jaycar Electronics Ltd", "gst_number": "098-765-432"}).json()
|
|
|
|
# the same name (ignoring Ltd and case) links on upload
|
|
ocr.fake_ocr.update(vendor="JAYCAR ELECTRONICS", total_cents=11500)
|
|
a = ocr.post("/api/attachments?filename=a.png", content=png(b"a")).json()
|
|
assert a["supplier"] == {"id": sup["id"], "name": "Jaycar Electronics Ltd"}
|
|
|
|
# so does the GST number, whatever the name says
|
|
ocr.fake_ocr.update(vendor="J'car", gst_number="098-765-432", total_cents=500)
|
|
b = ocr.post("/api/attachments?filename=b.png", content=png(b"b")).json()
|
|
assert b["supplier_id"] == sup["id"]
|
|
|
|
# an unknown vendor is left for the user
|
|
ocr.fake_ocr.update(vendor="Somewhere Else", gst_number=None)
|
|
c = ocr.post("/api/attachments?filename=c.png", content=png(b"c")).json()
|
|
assert c["supplier"] is None
|
|
|
|
s = ocr.get(f"/api/suppliers/{sup['id']}").json()
|
|
assert (s["documents"], s["total"]) == (2, 120.0)
|
|
assert {x["id"] for x in ocr.get(f"/api/attachments?supplier={sup['id']}").json()} == {a["id"], b["id"]}
|
|
|
|
# link by hand, and unlink
|
|
c = ocr.patch(f"/api/attachments/{c['id']}", json={"supplier_id": sup["id"]}).json()
|
|
assert c["supplier"]["name"] == "Jaycar Electronics Ltd"
|
|
assert c["vendor"] == "Somewhere Else" # what was read is kept
|
|
assert ocr.patch(f"/api/attachments/{c['id']}", json={"supplier_id": None}).json()["supplier"] is None
|
|
assert ocr.patch(f"/api/attachments/{c['id']}", json={"supplier_id": 999}).status_code == 404
|
|
|
|
# a supplier with documents can't be deleted until they're unlinked
|
|
assert ocr.delete(f"/api/suppliers/{sup['id']}").status_code == 400
|
|
|
|
|
|
def test_customer_invoices_filter(admin):
|
|
admin.post("/api/books", json={"name": "b", "year": 2026})
|
|
ids = [admin.post("/api/customers", json={"name": n}).json()["id"] for n in ("A", "B")]
|
|
for cid in (ids[0], ids[1], ids[1]):
|
|
r = admin.post("/api/invoices", json={"customer_id": cid, "issue_date": "2026-04-10",
|
|
"lines": [{"description": "x", "unit": 10}]})
|
|
assert r.status_code == 200, r.text
|
|
assert len(admin.get(f"/api/invoices?customer={ids[1]}").json()) == 2
|
|
assert len(admin.get("/api/invoices").json()) == 3
|
|
|
|
|
|
def test_upgrade_adds_supplier_link(tmp_path):
|
|
"""Databases from before suppliers get the new column and keep their receipts."""
|
|
path = tmp_path / "old.db"
|
|
con = sqlite3.connect(path)
|
|
con.executescript(ledger.SCHEMA.replace(" supplier_id INTEGER REFERENCES suppliers(id), -- who the receipt or bill is from\n", ""))
|
|
con.execute("INSERT INTO attachments (vendor) VALUES ('Old receipt')")
|
|
con.execute("PRAGMA user_version = 4")
|
|
con.commit()
|
|
con.close()
|
|
conn = ledger.connect(path)
|
|
assert "supplier_id" in {c["name"] for c in conn.execute("PRAGMA table_info(attachments)")}
|
|
assert ledger.list_attachments(conn)[0]["supplier"] is None
|