feat(backend): expense lines (quantity, price before VAT, VAT %) and documents (PDF / photo)
An expense can list what it bought; the lines make its total (net, VAT half up per line), are
replaced on edit ([] removes them) and the total still can't drop below what was paid. Files kept
with an expense: POST/GET/DELETE /api/expenses/{id}/documents[/{doc}], PDF / JPG / PNG / WebP /
HEIC by their bytes, at most 15 MB, under MEDIA_DIR/expenses/<id>/ (removed with the expense).
Migration 0039.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -122,3 +122,47 @@ def test_tabs_need_their_own_key(client, db, make_user, login):
|
||||
no = login(make_user(role="staff_manager")) # has the dashboard, not tabs.manage
|
||||
assert client.get("/api/tabs/", headers=no).status_code == 403
|
||||
assert client.post("/api/orders/1/items/1/tab", headers=no).status_code == 403
|
||||
|
||||
|
||||
def test_expense_lines_make_the_total(client, make_user, login):
|
||||
"""2026-11-20: an expense can list what it bought (quantity, price before VAT, VAT %);
|
||||
the lines make its total, and editing them changes it (never below what was paid)."""
|
||||
h = login(make_user(role="owner"))
|
||||
r = client.post("/api/expenses/", headers=h, json={"description": "Market", "category": "supplier", "lines": [
|
||||
{"description": "Tomatoes", "quantity": 3, "unit_price": 2.5, "vat_rate": 13},
|
||||
{"description": "Bags", "quantity": 1, "unit_price": 10, "vat_rate": 24}]})
|
||||
assert r.status_code == 201, r.text
|
||||
e = r.json()
|
||||
assert [ln["total"] for ln in e["lines"]] == [8.48, 12.4]
|
||||
assert e["net_amount"] == 17.5 and e["vat_amount"] == 3.38 and e["total_amount"] == 20.88
|
||||
client.post(f"/api/expenses/{e['id']}/pay", headers=h, json={"amount": 15})
|
||||
# fewer lines than what was paid: refused
|
||||
r = client.put(f"/api/expenses/{e['id']}", headers=h, json={"lines": [{"description": "Bags", "quantity": 1, "unit_price": 10}]})
|
||||
assert r.status_code == 400 and r.json()["error"]["code"] == "expense.below_paid"
|
||||
# lines cleared: the total stays as it was and is typed again
|
||||
r = client.put(f"/api/expenses/{e['id']}", headers=h, json={"lines": [], "total_amount": 21})
|
||||
assert r.status_code == 200 and r.json()["lines"] == [] and r.json()["total_amount"] == 21
|
||||
# a line needs a description and a positive quantity
|
||||
bad = client.post("/api/expenses/", headers=h, json={"description": "X", "category": "other", "lines": [{"description": " ", "quantity": 1, "unit_price": 1}]})
|
||||
assert bad.json()["error"]["code"] == "validation.missing"
|
||||
# no lines and no total: refused
|
||||
assert client.post("/api/expenses/", headers=h, json={"description": "X", "category": "other"}).json()["error"]["code"] == "validation.positive"
|
||||
|
||||
|
||||
def test_expense_documents(client, make_user, login, tmp_path, monkeypatch):
|
||||
"""A PDF or a photo kept with an expense: uploaded, read back, removed; other files refused."""
|
||||
import services.expense_docs as docs
|
||||
monkeypatch.setattr(docs, "MEDIA_DIR", str(tmp_path))
|
||||
h = login(make_user(role="owner"))
|
||||
e = client.post("/api/expenses/", headers=h, json={"description": "Gas", "category": "utilities", "total_amount": 40}).json()
|
||||
pdf = b"%PDF-1.4\n%fake\n" + b"0" * 100
|
||||
r = client.post(f"/api/expenses/{e['id']}/documents", headers=h, files={"file": ("bill.pdf", pdf, "application/pdf")})
|
||||
assert r.status_code == 201, r.text
|
||||
doc = r.json()["documents"][0]
|
||||
assert doc["filename"] == "bill.pdf" and doc["content_type"] == "application/pdf" and doc["size"] == len(pdf)
|
||||
got = client.get(f"/api/expenses/{e['id']}/documents/{doc['id']}", headers=h)
|
||||
assert got.status_code == 200 and got.content == pdf
|
||||
assert client.post(f"/api/expenses/{e['id']}/documents", headers=h, files={"file": ("x.exe", b"MZ\x90\x00", "application/pdf")}).status_code == 422
|
||||
r = client.delete(f"/api/expenses/{e['id']}/documents/{doc['id']}", headers=h)
|
||||
assert r.status_code == 200 and r.json()["documents"] == []
|
||||
assert client.get(f"/api/expenses/{e['id']}/documents/{doc['id']}", headers=h).status_code == 404
|
||||
|
||||
Reference in New Issue
Block a user