- 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>
71 lines
2.8 KiB
Python
71 lines
2.8 KiB
Python
"""CSV exports that open right in the venue's Excel (plan step 6.12a, from 2.99e).
|
|
|
|
- Times in **venue time** ("2026-11-19 21:30"), the zone named in the column header
|
|
("Opened (Europe/Athens)"), instead of UTC ISO.
|
|
- A UTF-8 byte-order mark, so Excel shows Greek names instead of mojibake.
|
|
- The venue's language picks the separators Excel expects: Greek → columns split by `;`
|
|
and decimal commas (12,50); otherwise `,` and decimal points.
|
|
"""
|
|
from __future__ import annotations
|
|
|
|
import csv
|
|
import io
|
|
from datetime import date, datetime
|
|
|
|
from fastapi.responses import StreamingResponse
|
|
|
|
BOM = chr(0xFEFF) # tells Excel the file is UTF-8
|
|
|
|
|
|
def _language(db) -> str:
|
|
from models.settings import PosSettings
|
|
row = db.query(PosSettings).filter(PosSettings.key == "venue.language").first() if db is not None else None
|
|
return (row.value if row and row.value else "en").lower()
|
|
|
|
|
|
def dialect(db) -> tuple[str, str]:
|
|
"""(column separator, decimal mark) for the venue's Excel."""
|
|
return (";", ",") if _language(db) == "el" else (",", ".")
|
|
|
|
|
|
def venue_time(value) -> str:
|
|
"""An ISO string or a datetime (UTC) → "YYYY-MM-DD HH:MM" in venue time; "" for nothing."""
|
|
from core.timeutil import parse_api_datetime, to_venue
|
|
if value in (None, ""):
|
|
return ""
|
|
dt = value if isinstance(value, datetime) else parse_api_datetime(str(value))
|
|
if dt is None:
|
|
return str(value)
|
|
return to_venue(dt).strftime("%Y-%m-%d %H:%M")
|
|
|
|
|
|
def response(db, rows: list[dict], fields: list[str], filename: str, *, times=(), labels: dict | None = None) -> StreamingResponse:
|
|
"""`fields` in order (the header even with no rows); `times` are converted to venue time and
|
|
their header names the zone; `labels` renames headers."""
|
|
from core.timeutil import venue_tz
|
|
sep, dec = dialect(db)
|
|
zone = venue_tz().key
|
|
labels = labels or {}
|
|
header = [f"{labels.get(f, f)} ({zone})" if f in times else labels.get(f, f) for f in fields]
|
|
|
|
def cell(field, v):
|
|
if field in times:
|
|
return venue_time(v)
|
|
if isinstance(v, bool):
|
|
return "1" if v else "0"
|
|
if isinstance(v, float):
|
|
text = f"{v:.2f}" if abs(v - round(v, 2)) < 1e-9 else repr(v)
|
|
return text.replace(".", dec) if dec != "." else text
|
|
if isinstance(v, (date, datetime)):
|
|
return v.isoformat()
|
|
return "" if v is None else v
|
|
|
|
out = io.StringIO()
|
|
w = csv.writer(out, delimiter=sep, lineterminator="\r\n")
|
|
w.writerow(header)
|
|
for r in rows:
|
|
w.writerow([cell(f, r.get(f)) for f in fields])
|
|
body = BOM + out.getvalue()
|
|
return StreamingResponse(iter([body.encode("utf-8")]), media_type="text/csv; charset=utf-8",
|
|
headers={"Content-Disposition": f'attachment; filename="{filename}"'})
|