Plan phase 3S, step S.5. - Pull: an online order keeps the customer's time (orders.requested_for, None = as soon as possible) and delivery area; quick options are stored in the site's entry shape; items get their cost snapshot. - The delivery fee is its own order line: a system product (products.system_key "delivery_fee", lifecycle "system", sold nowhere; services/system_products.py), created on first use in the venue's language with the VAT group most products use. Added printed and served, so kitchen tickets and the KDS never see it; paid at the counter with the items, counted in the shift, on the fiscal receipt with its VAT group. Never listed in the catalogue (products list, export, edit endpoints → 404) and never added by hand. - Price check (services/online_check.py): each line priced as the snapshot prices it, at the order's time, and the fee through the same engine with the site's rules and the cloud's area id; differences kept on orders.online_price_diff and shown. The customer pays what the cloud charged. - Counter: time chip (or as soon as possible), area, fee line, price warning; takeaway is its own kind (was shown as "at the venue") and finishes as "collected". KDS cards show the online reference and the time; kitchen tickets print the reference, the type and the time. - Manager: Online ordering → "The fee on the receipt" (name, VAT group; PUT /api/connect/fees/fee-product, fees.vat_group_unknown); the online orders page shows the time, area and price warning. - Migration 0019. Tests: tests/test_online_site_side.py (6), terminal unit test, e2e (terminal: fee, time, warning, paid in full; manager: fee line settings). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
91 lines
4.4 KiB
Python
91 lines
4.4 KiB
Python
"""The site checks the cloud's prices when it pulls an online order (plan phase 3S, step S.5).
|
|
|
|
The cloud prices online orders from the menu the site last pushed (S.2), so its prices may be
|
|
up to one push old (a change pushes about 5 s later; the 5-minute push is the fallback). The
|
|
customer was promised the cloud's prices and is charged them; this check only tells the venue
|
|
when the site's own engine says something else, so nobody is surprised at the counter.
|
|
|
|
diff(db, cloud_order, sales_point) -> None (all matched) | {
|
|
"lines": [{product_id, name, quantity, cloud, site, reason}], reason: price | not_sold | option
|
|
"fee": {cloud, site, reason} | None, reason: price | area | minimum
|
|
"cloud_total", "site_total"}
|
|
|
|
Each line is priced as the snapshot prices it (services/cloud_sync.menu_snapshot): the product
|
|
on its own at the order's sales point, plus its quick options, at the time the customer
|
|
ordered. The fee goes through the same engine (services/fees.py) with the site's rules, the
|
|
area the cloud matched (by id) and the order's time at the venue.
|
|
"""
|
|
from __future__ import annotations
|
|
|
|
from datetime import datetime
|
|
|
|
EPS = 0.005
|
|
|
|
|
|
def _when(cloud_order: dict):
|
|
from core.timeutil import utcnow
|
|
raw = cloud_order.get("created_at")
|
|
if isinstance(raw, str):
|
|
try:
|
|
when = datetime.fromisoformat(raw.replace("Z", "+00:00"))
|
|
if when.tzinfo is not None:
|
|
return when
|
|
except ValueError:
|
|
pass
|
|
return utcnow()
|
|
|
|
|
|
def diff(db, cloud_order: dict, sales_point: str) -> dict | None:
|
|
import json
|
|
from core import sales_points
|
|
from core.timeutil import to_venue
|
|
from models.product import Product
|
|
from routers.fees import fees_payload
|
|
from services import fees
|
|
from services.pos_cart import unit_prices
|
|
|
|
when = _when(cloud_order)
|
|
lines, site_subtotal = [], 0.0
|
|
items = json.loads(cloud_order.get("items_json") or "[]")
|
|
for item in items:
|
|
qty = item.get("quantity", 1) or 1
|
|
cloud = round(float(item.get("unit_price") or 0.0), 2)
|
|
entry = {"product_id": item.get("product_id"), "name": item.get("name"), "quantity": qty, "cloud": cloud}
|
|
product = db.get(Product, item.get("product_id")) if isinstance(item.get("product_id"), int) else None
|
|
if product is None or product.lifecycle_status != "active" or not sales_points.is_sold_at(product, sales_point):
|
|
lines.append({**entry, "site": None, "reason": "not_sold"})
|
|
site_subtotal += cloud * qty
|
|
continue
|
|
quick = {q.id: q for q in product.quick_options}
|
|
chosen = [o.get("id") if isinstance(o, dict) else o for o in item.get("options") or []]
|
|
if any(oid not in quick for oid in chosen):
|
|
lines.append({**entry, "site": None, "reason": "option"})
|
|
site_subtotal += cloud * qty
|
|
continue
|
|
site = round(unit_prices(db, [product], sales_point, now=when)[0] + sum(quick[o].price or 0.0 for o in chosen), 2)
|
|
site_subtotal += site * qty
|
|
if abs(site - cloud) > EPS:
|
|
lines.append({**entry, "site": site, "reason": "price"})
|
|
|
|
fee = None
|
|
cloud_fee = round(float(cloud_order.get("delivery_fee") or 0.0), 2)
|
|
if sales_point == sales_points.ONLINE_DELIVERY or cloud_fee:
|
|
data = fees_payload(db)
|
|
local = to_venue(when)
|
|
result = fees.compute(data["rules"], data["areas"], sales_point=sales_point,
|
|
subtotal=round(float(cloud_order.get("subtotal") or 0.0), 2),
|
|
items=sum(int(i.get("quantity", 1) or 1) for i in items),
|
|
area_id=cloud_order.get("delivery_area_id"), postcode=None,
|
|
weekday=local.weekday(), hhmm=local.strftime("%H:%M"))
|
|
if result["refused"]:
|
|
fee = {"cloud": cloud_fee, "site": None,
|
|
"reason": "area" if result["refused"] == "area_not_served" else "minimum"}
|
|
elif abs(result["fee"] - cloud_fee) > EPS:
|
|
fee = {"cloud": cloud_fee, "site": result["fee"], "reason": "price"}
|
|
if not lines and fee is None:
|
|
return None
|
|
site_fee = fee["site"] if fee and fee["site"] is not None else cloud_fee
|
|
return {"lines": lines, "fee": fee,
|
|
"cloud_total": round(float(cloud_order.get("total") or 0.0), 2),
|
|
"site_total": round(site_subtotal + site_fee, 2)}
|