- services/sales.py: sold = active + paid lines; a line's value net of what
was refunded of it; system lines (the delivery fee) counted apart as fees;
cost/profit with has_gap. Products, categories, tables, traffic, revenue
trends, business days (list and current), per-waiter sales, order history
totals and the printed products/categories/tables reports all use it (they
used to differ: closed-only vs all orders, fee as a product, refunds never
subtracted)
- the overview's day summary values lines with their price change (discounts
were valued at full price)
- services/csv_export.py: UTF-8 BOM, times in venue time with the zone in the
header, ';' and decimal commas for a Greek venue (',' and '.' otherwise);
every report export uses it; orders export has products / fees / refunded
- fix: /api/reports/shift/orders with business_day_id crashed (no window)
- manager: src/pages/reports/kit (page with tabs, period picker, panels,
figures, sortable table, export button, order dialog reusing the order
page's parts, hour chart on tokens) and reports/today on it; v1 Today removed
- tests: test_sales_accounting.py (a day with a fee and a partial refund adds
up the same in every report), test_reports_export.py (Excel format),
reports.test.mjs, e2e manager.mjs (Today, order dialog, export, Greek)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
67 lines
3.1 KiB
Python
67 lines
3.1 KiB
Python
"""Report exports (KI-008; plan step 6.12a): CSV for the venue's Excel — a UTF-8 mark, venue
|
||
time with the zone in the header, the separators of the venue's language — and each order's
|
||
products, delivery fee and refunds counted by services/sales.py."""
|
||
import csv
|
||
import io
|
||
|
||
|
||
def _rows(r, sep=","):
|
||
assert r.status_code == 200, r.text
|
||
assert r.headers["content-type"].startswith("text/csv")
|
||
text = r.content.decode("utf-8")
|
||
assert text.startswith("\ufeff") # Excel reads it as UTF-8
|
||
return list(csv.DictReader(io.StringIO(text[1:]), delimiter=sep))
|
||
|
||
|
||
def _order(db, make_user):
|
||
from datetime import datetime, timezone
|
||
from models.order import Order, OrderItem
|
||
from models.product import Category, Product
|
||
waiter = make_user(role="waiter", full_name="Maria W")
|
||
cat = Category(name="Coffees")
|
||
db.add(cat)
|
||
db.flush()
|
||
prod = Product(name="Freddo", base_price=4.0, primary_category_id=cat.id)
|
||
db.add(prod)
|
||
db.flush()
|
||
order = Order(opened_by=waiter.id, order_type="takeaway", opened_at=datetime(2026, 11, 19, 19, 30, tzinfo=timezone.utc))
|
||
db.add(order)
|
||
db.flush()
|
||
db.add_all([OrderItem(order_id=order.id, product_id=prod.id, added_by=waiter.id, quantity=2, unit_price=4.0),
|
||
OrderItem(order_id=order.id, product_id=prod.id, added_by=waiter.id, quantity=1, unit_price=4.0,
|
||
price_adjustment=-1.0),
|
||
OrderItem(order_id=order.id, product_id=prod.id, added_by=waiter.id, quantity=1, unit_price=4.0,
|
||
status="cancelled")])
|
||
db.commit()
|
||
return order
|
||
|
||
|
||
def test_orders_export_for_excel_in_venue_time(client, db, make_user, login):
|
||
order = _order(db, make_user)
|
||
h = login(make_user(role="owner"))
|
||
rows = _rows(client.get("/api/reports/orders/export", headers=h))
|
||
assert len(rows) == 1
|
||
row = rows[0]
|
||
assert row["id"] == str(order.id) and row["waiter"] == "Maria W" and row["status"] == order.status
|
||
assert row["products"] == "11.00" and row["fees"] == "0.00" and row["total"] == "11.00" # 2×4 + 1×(4−1); not the cancelled one
|
||
assert row["opened_at (Europe/Athens)"] == "2026-11-19 21:30" # 19:30 UTC, venue time
|
||
# business_day_id is honoured (the Today page sends it): no order in day 999
|
||
assert _rows(client.get("/api/reports/orders/export", headers=h, params={"business_day_id": 999})) == []
|
||
|
||
|
||
def test_a_greek_venue_gets_semicolons_and_decimal_commas(client, db, make_user, login):
|
||
from models.settings import PosSettings
|
||
_order(db, make_user)
|
||
db.add(PosSettings(key="venue.language", value="el"))
|
||
db.commit()
|
||
r = client.get("/api/reports/orders/export", headers=login(make_user(role="owner")))
|
||
row = _rows(r, sep=";")[0]
|
||
assert row["total"] == "11,00"
|
||
|
||
|
||
def test_orders_export_empty_still_has_a_header(client, make_user, login):
|
||
r = client.get("/api/reports/orders/export", headers=login(make_user(role="owner")))
|
||
assert r.status_code == 200
|
||
assert r.content.decode("utf-8").lstrip("\ufeff").splitlines()[0] == \
|
||
"id,table,waiter,opened_at (Europe/Athens),closed_at (Europe/Athens),status,products,fees,refunded,total"
|