Files
xenia-pos-local/local_backend/tests/test_customers_waste_notes.py
T
bonaminandClaude Opus 5.5 bde125bf09 feat(manager): customers, throwaways, stock and notes on v2 (platform-v2 step 6.13b)
- CRM -> Customers: search ignoring case and Greek accents (and part of the phone), visits / spent / last visit,
  the card (contact, notes, open tab, every visit with what was on a tab), new/edit/remove
- Inventory -> Throwaways: log (product, quantity with a comma, reason, note) with its cost, today's list (delete today only),
  a period's totals by reason and product, every entry; Inventory -> Stock: an honest 'not available yet'
- Notes: notes (pinned first, edit in place) and to-dos (urgent first, done folded away, who did it kept)
- backend: crm.manage / inventory.manage / notes.use (the customer search also with the dashboard key, for orders);
  spending counted by services/sales.py (refunds off, tab lines in); null clears a field; names/notes not blank, priorities checked;
  waste ?date_from=&date_to= venue dates; the Greek reason labels left the API; live customers.changed / waste.changed / notes.changed
- the overview's notes card needs notes.use and refreshes live
- tests: test_customers_waste_notes.py (4), crm.test.mjs (3), e2e manager.mjs (customers, throwaways, notes, stock, Greek)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-19 18:36:27 +02:00

119 lines
7.2 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""Customers, throwaways and notes (plan step 6.13b): each needs its module's own key (crm.manage,
inventory.manage, notes.use; the customer search also with the dashboard key, for orders), a
customer's spending is counted by services/sales.py, search ignores case and Greek accents, and
every change tells open pages (live events)."""
from datetime import datetime, timedelta, timezone
import pytest
@pytest.fixture
def events(monkeypatch):
sent = []
import routers.customers as c
import routers.notes as n
import routers.waste as w
for mod in (c, n, w):
monkeypatch.setattr(mod, "broadcast_sync", lambda t, d=None, **kw: sent.append(t))
return sent
def test_each_needs_its_own_key(client, make_user, login):
dash = login(make_user(role="store_manager", keys=("core.dashboard.access",)))
assert client.get("/api/customers/", headers=dash).status_code == 200 # attaching a customer to an order
assert client.post("/api/customers/", headers=dash, json={"name": "X"}).status_code == 403
assert client.get("/api/waste/", headers=dash).status_code == 403
assert client.get("/api/notes/", headers=dash).status_code == 403
assert client.get("/api/notes/todos", headers=dash).status_code == 403
def test_a_customer_found_without_accents_and_their_spending(client, db, make_user, login, events):
from models.order import Order, OrderItem
from models.product import Category, Product
from models.refund import Refund, RefundItem
h = login(make_user(role="owner"))
r = client.post("/api/customers/", headers=h, json={"name": " Γιώργος Παπαδόπουλος ", "nickname": "Ο μεγάλος", "phone": "+30 697 111 2222"})
assert r.status_code == 201 and r.json()["name"] == "Γιώργος Παπαδόπουλος"
cid = r.json()["id"]
client.post("/api/customers/", headers=h, json={"name": "Anna"})
find = lambda q: [c["name"] for c in client.get("/api/customers/", headers=h, params={"search": q}).json()] # noqa: E731
assert find("γιωργος") == ["Γιώργος Παπαδόπουλος"]
assert find("ΜΕΓΑΛΟΣ") == ["Γιώργος Παπαδόπουλος"]
assert find("6971112222") == ["Γιώργος Παπαδόπουλος"] and find("111 2222") == ["Γιώργος Παπαδόπουλος"]
assert find("nobody") == []
assert "validation.missing" in client.post("/api/customers/", headers=h, json={"name": " "}).text
# a finished order: 2 × 5 paid, 1 refunded; a line of 4 on a tab (sold, 6.13c); a cancelled line
u = make_user(role="waiter")
cat = Category(name="Food")
db.add(cat)
db.flush()
p = Product(name="Pita", base_price=5.0, primary_category_id=cat.id)
db.add(p)
db.flush()
o = Order(opened_by=u.id, order_type="takeaway", customer_id=cid, status="closed", opened_at=datetime(2026, 11, 18, 19, tzinfo=timezone.utc))
db.add(o)
db.flush()
paid = OrderItem(order_id=o.id, product_id=p.id, added_by=u.id, quantity=2, unit_price=5.0, status="paid", payment_method="cash")
db.add_all([paid, OrderItem(order_id=o.id, product_id=p.id, added_by=u.id, quantity=1, unit_price=4.0, status="tabbed"),
OrderItem(order_id=o.id, product_id=p.id, added_by=u.id, quantity=1, unit_price=5.0, status="cancelled")])
db.flush()
ref = Refund(order_id=o.id, refunded_by=u.id, method="cash", amount=5.0)
db.add(ref)
db.flush()
db.add(RefundItem(refund_id=ref.id, order_item_id=paid.id, quantity=1, amount=5.0))
db.commit()
c = client.get(f"/api/customers/{cid}", headers=h).json()
assert (c["visit_count"], c["total_spent"]) == (1, 9.0) and c["last_visit"] # 10 − 5 refunded + 4 on the tab
visits = client.get(f"/api/customers/{cid}/orders", headers=h).json()["orders"]
assert (visits[0]["total"], visits[0]["on_tab"], visits[0]["item_count"]) == (9.0, 4.0, 2)
# a field sent empty is cleared; the name stays
r = client.put(f"/api/customers/{cid}", headers=h, json={"nickname": None, "notes": "nut allergy"})
assert r.json()["nickname"] is None and r.json()["notes"] == "nut allergy" and r.json()["name"] == "Γιώργος Παπαδόπουλος"
assert client.delete(f"/api/customers/{cid}", headers=h).status_code == 204
assert events.count("customers.changed") == 4 # 2 added, 1 edited, 1 removed
def test_throwaways_cost_period_and_deleting(client, db, make_user, login, events):
from core.timeutil import business_date_of, utcnow
from models.business_day import BusinessDay
from models.product import Category, Product
from models.waste import WasteLog
owner = make_user(role="owner")
h = login(owner)
cat = Category(name="Food")
db.add(cat)
db.flush()
p = Product(name="Souvlaki", base_price=3.0, primary_category_id=cat.id, cost_simple=1.25)
db.add(p)
day = BusinessDay(status="open", opened_by_id=owner.id, opened_at=utcnow() - timedelta(hours=1), business_date=business_date_of(utcnow()))
db.add(day)
db.commit()
r = client.post("/api/waste/", headers=h, json={"product_id": p.id, "quantity": 2, "reason": "dropped", "reason_notes": "tray"})
assert r.status_code == 201, r.text
w = r.json()
assert (w["unit_cost_snapshot"], w["total_cost"], w["business_day_id"]) == (1.25, 2.5, day.id) and "reason_label" not in w
assert "validation.choice" in client.post("/api/waste/", headers=h, json={"product_id": p.id, "quantity": 1, "reason": "stolen"}).text
# an old entry (another day): listed by venue date, can't be deleted
old = WasteLog(product_id=p.id, quantity=1, reason="expired", logged_by_id=owner.id, logged_at=datetime(2026, 11, 1, 22, 30, tzinfo=timezone.utc))
db.add(old)
db.commit()
assert [e["id"] for e in client.get("/api/waste/", headers=h, params={"date_from": "2026-11-02", "date_to": "2026-11-02"}).json()["entries"]] == [old.id] # 00:30 in Athens
assert "waste.not_today" in client.delete(f"/api/waste/{old.id}", headers=h).text
s = client.get("/api/waste/summary", headers=h, params={"business_day_id": day.id}).json()
assert (s["total_items"], s["total_cost"], s["by_reason"][0]) == (1, 2.5, {"reason": "dropped", "count": 1, "total_cost": 2.5})
assert client.delete(f"/api/waste/{w['id']}", headers=h).status_code == 204
assert events.count("waste.changed") == 2
def test_notes_and_todos(client, make_user, login, events):
h = login(make_user(role="owner"))
n = client.post("/api/notes/", headers=h, json={"body": " Fridge 2 is noisy ", "is_pinned": False}).json()
assert n["body"] == "Fridge 2 is noisy"
assert "validation.missing" in client.post("/api/notes/", headers=h, json={"body": " "}).text
assert client.put(f"/api/notes/{n['id']}", headers=h, json={"is_pinned": True}).json()["is_pinned"] is True
t = client.post("/api/notes/todos", headers=h, json={"body": "Call the plumber", "priority": "high"}).json()
assert "validation.choice" in client.post("/api/notes/todos", headers=h, json={"body": "x", "priority": "urgent"}).text
done = client.put(f"/api/notes/todos/{t['id']}", headers=h, json={"is_done": True}).json()
assert done["is_done"] and done["done_by_name"]
assert events.count("notes.changed") == 4 # the note, its pin, the to-do, done