feat(ordering): orders priced by the server (platform-v2 step 4.4)
The waiter app (and the manager's ordering tab) send what was chosen, by id; the
server prices it with the counter's code. No app sends a price any more.
- POST /api/orders/{id}/items takes `lines` (pos_cart's line + course_id,
price_adjustment, is_service_item): sales point checked first, refusals name the
product and line (order.line.*, order.not_sold_here); prices or selected_options in a
line -> order.line.client_price, the old `items` shape -> order.lines_required; a
by-hand price change needs ordering.prices.modify or a manager's approval (audited
PRICE_SET_BY_HAND). add_priced_items is the one writer of priced items (pos too).
- POST /api/orders/quote: the cart's total before sending; /api/pos/quote shares
pos_cart.quote().
- pos_cart: a preference set left out takes its default; a set at its free default
isn't stored; a default that costs is charged (the waiter never sent it: fixed);
an extra's sub-choice is charged per extra (the counter charged it once: fixed).
- @xenia/api lines.js: lineFromOptions / wireLine / estimate / LineError.
- Waiter: lines on send; cart total from the quote ("≈ estimate" offline); summary
shows the same prices; offline orders queued by id, priced on sync, refused lines
shown on a "Δεν στάλθηκαν" sheet (no empty order opened); dead ItemOptionsModal gone.
- Tests: test_order_lines.py (14), lines.test.mjs, waiter e2e +6.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -1,23 +1,29 @@
|
||||
"""The counter POS's cart, priced by the backend (plan step 3.3a, D-26).
|
||||
"""Order lines priced by the backend: the counter POS (plan step 3.3a, D-26) and, since
|
||||
step 4.4, the waiter app (POST /api/orders/{id}/items takes these lines).
|
||||
|
||||
The POS never computes a price. It sends what was chosen by id (and sub-choices by name, as
|
||||
they are stored), and this module turns that into the same `selected_options` entries the
|
||||
waiter app sends (types quick | extra | extra_sub | pref | pref_sub) with the prices taken
|
||||
from the database, then runs the pricing engine (services/pricing.py::resolve_price) exactly
|
||||
as POST /api/orders/{id}/items does. 3.3b sends the same lines to create the order.
|
||||
No app computes a price. It sends what was chosen by id (and sub-choices by name, as
|
||||
they are stored), and this module turns that into the `selected_options` entries an order
|
||||
item stores (types quick | extra | extra_sub | pref | pref_sub) with the prices taken
|
||||
from the database, then runs the pricing engine (services/pricing.py::resolve_price) through
|
||||
routers/orders.py::add_priced_items. 3.3b sends the same lines to create the order.
|
||||
|
||||
A line:
|
||||
{product_id, quantity, notes?,
|
||||
quick: [{id, qty?}], quick add-ons (qty only if allow_multiple)
|
||||
options: [{id, qty?, subs?: [name]}], extras; one sub unless multi_select
|
||||
options: [{id, qty?, subs?: [name]}], extras; one sub unless multi_select; the
|
||||
same extra may be listed more than once
|
||||
prefs: [{set_id, choices: [{id, qty?, sub?: name}], shared?: name}],
|
||||
removed: [ingredient_id]}
|
||||
|
||||
Rules (the same as the waiter's options dialog, waiter_pwa ItemOptionsModal.jsx):
|
||||
- every preference set needs a choice (one, or one or more for allow_multi_select sets);
|
||||
Rules (the same as the waiter's options drawer, waiter_pwa OrderDrawer.jsx):
|
||||
- every preference set needs a choice (one, or one or more for allow_multi_select sets); a set
|
||||
left out of the line takes its default choice (with the default sub-choices) when it has one;
|
||||
- a set at its default (default choice, default sub-choices) that costs nothing is not stored:
|
||||
the ticket shows only what differs from the usual (the waiter app always worked that way);
|
||||
- a choice/extra with sub-choices needs one of them (the default is pre-selected on screen);
|
||||
- a set's shared sub-choice applies unless the chosen choice `disables_subset`;
|
||||
- quantities: per-choice qty only on multi-select sets, per-add-on qty only on allow_multiple.
|
||||
- quantities: per-choice qty only on multi-select sets, per-add-on qty only on allow_multiple;
|
||||
an extra's sub-choice is charged once per extra (qty 2 of "sauce: ketchup" = 2 × both).
|
||||
Anything else → XeniaError("pos.line_invalid", line=n, reason=...), reason one of
|
||||
product_unavailable | unknown_quick | unknown_option | unknown_choice | unknown_sub |
|
||||
choice_required | sub_required | unknown_ingredient | quantity | discount | not_sold_here
|
||||
@@ -97,6 +103,28 @@ def _sub(subs: list[dict], name, n: int) -> Optional[dict]:
|
||||
return found
|
||||
|
||||
|
||||
def _default_sub(subs: list[dict]) -> Optional[str]:
|
||||
"""The sub-choice selected to start with: the one marked default, else the first."""
|
||||
return next((s for s in subs if s.get("is_default")), subs[0])["name"] if subs else None
|
||||
|
||||
|
||||
def _usual_pick(choice, shared) -> dict:
|
||||
"""A preference set at its default: the default choice, its default sub-choice and the
|
||||
set's default shared sub-choice."""
|
||||
return {"choices": [{"id": choice.id, "qty": 1, "sub": _default_sub(_subs(choice.sub_choices))}],
|
||||
"shared": _default_sub(_subs(shared.get("choices"))) if shared and not choice.disables_subset else None}
|
||||
|
||||
|
||||
def _same_pick(sel: dict, usual: dict, multi: bool) -> bool:
|
||||
picks = sel.get("choices") or []
|
||||
if len(picks) != 1 or not isinstance(picks[0], dict):
|
||||
return False
|
||||
p, u = picks[0], usual["choices"][0]
|
||||
if p.get("id") != u["id"] or (p.get("qty") or 1) != 1:
|
||||
return False
|
||||
return multi or (p.get("sub") == u["sub"] and (usual["shared"] is None or sel.get("shared") == usual["shared"]))
|
||||
|
||||
|
||||
def resolve_line(db, raw: dict, n: int) -> Line:
|
||||
from models.product import Product
|
||||
product = db.get(Product, raw.get("product_id")) if isinstance(raw.get("product_id"), int) else None
|
||||
@@ -136,40 +164,48 @@ def resolve_line(db, raw: dict, n: int) -> Line:
|
||||
"price_delta": round((o.extra_cost or 0.0) * qty, 2), "type": "extra"})
|
||||
for name in names:
|
||||
s = _sub(subs, name, n)
|
||||
line.entries.append({"id": None, "name": s["name"], "price_delta": round(float(s.get("extra_cost") or 0.0), 2),
|
||||
"type": "extra_sub"})
|
||||
line.entries.append({"id": None, "name": s["name"] if qty == 1 else f"{s['name']} x{qty}",
|
||||
"price_delta": round(float(s.get("extra_cost") or 0.0) * qty, 2), "type": "extra_sub"})
|
||||
|
||||
chosen = {p.get("set_id"): p for p in (raw.get("prefs") or [])}
|
||||
chosen = {p.get("set_id"): p for p in (raw.get("prefs") or []) if isinstance(p, dict)}
|
||||
for ps in product.preference_sets:
|
||||
by_id = {c.id: c for c in ps.choices}
|
||||
shared = _shared(ps.shared_subset)
|
||||
default = by_id.get(ps.default_choice_id)
|
||||
usual = _usual_pick(default, shared) if default is not None else None
|
||||
sel = chosen.pop(ps.id, None)
|
||||
if sel is None and usual is not None:
|
||||
sel = usual # left out: the set's default (4.4)
|
||||
picks = (sel or {}).get("choices") or []
|
||||
if not picks or (len(picks) > 1 and not ps.allow_multi_select):
|
||||
raise _bad(n, "choice_required")
|
||||
by_id = {c.id: c for c in ps.choices}
|
||||
shared = _shared(ps.shared_subset)
|
||||
entries = []
|
||||
for pick in picks:
|
||||
c = by_id.get(pick.get("id"))
|
||||
c = by_id.get(pick.get("id")) if isinstance(pick, dict) else None
|
||||
if c is None:
|
||||
raise _bad(n, "unknown_choice")
|
||||
if ps.allow_multi_select:
|
||||
qty = _qty(pick.get("qty"), n, True)
|
||||
line.entries.append({"id": c.id, "name": c.name if qty == 1 else f"{c.name} x{qty}",
|
||||
"price_delta": round((c.extra_cost or 0.0) * qty, 2), "type": "pref"})
|
||||
entries.append({"id": c.id, "name": c.name if qty == 1 else f"{c.name} x{qty}",
|
||||
"price_delta": round((c.extra_cost or 0.0) * qty, 2), "type": "pref"})
|
||||
continue
|
||||
line.entries.append({"id": c.id, "name": c.name, "price_delta": round(c.extra_cost or 0.0, 2), "type": "pref"})
|
||||
entries.append({"id": c.id, "name": c.name, "price_delta": round(c.extra_cost or 0.0, 2), "type": "pref"})
|
||||
subs = _subs(c.sub_choices)
|
||||
if subs:
|
||||
s = _sub(subs, pick.get("sub"), n) if pick.get("sub") is not None else None
|
||||
if s is None:
|
||||
raise _bad(n, "sub_required")
|
||||
line.entries.append({"id": None, "name": s["name"], "price_delta": round(float(s.get("extra_cost") or 0.0), 2),
|
||||
"type": "pref_sub"})
|
||||
entries.append({"id": None, "name": s["name"], "price_delta": round(float(s.get("extra_cost") or 0.0), 2),
|
||||
"type": "pref_sub"})
|
||||
if shared and not c.disables_subset:
|
||||
s = _sub(_subs(shared.get("choices")), sel.get("shared"), n) if sel.get("shared") is not None else None
|
||||
if s is None:
|
||||
raise _bad(n, "sub_required")
|
||||
line.entries.append({"id": None, "name": s["name"], "price_delta": round(float(s.get("extra_cost") or 0.0), 2),
|
||||
"type": "pref_sub"})
|
||||
entries.append({"id": None, "name": s["name"], "price_delta": round(float(s.get("extra_cost") or 0.0), 2),
|
||||
"type": "pref_sub"})
|
||||
at_default = usual is not None and _same_pick(sel, usual, ps.allow_multi_select)
|
||||
if not (at_default and not any(e["price_delta"] for e in entries)):
|
||||
line.entries.extend(entries)
|
||||
if chosen:
|
||||
raise _bad(n, "unknown_choice")
|
||||
|
||||
@@ -242,3 +278,40 @@ def discount_adjustment(unit_price: float, pct) -> float:
|
||||
"""A line's discount (percent of its unit price) as the order item's price_adjustment
|
||||
(negative, rounded to cents)."""
|
||||
return -round(unit_price * pct / 100.0, 2) if pct else 0.0
|
||||
|
||||
|
||||
def adjustments(db, events) -> list[dict]:
|
||||
"""A line's price-engine adjustments (happy hour, price groups) as {label, delta}."""
|
||||
from models.pricing import PriceModifier
|
||||
out = []
|
||||
for e in events:
|
||||
if not e.delta_amount:
|
||||
continue
|
||||
m = db.get(PriceModifier, e.modifier_id) if e.modifier_id else None
|
||||
out.append({"label": m.name if m is not None else e.event_type, "delta": round(e.delta_amount, 2)})
|
||||
return out
|
||||
|
||||
|
||||
def quote(db, lines: list[Line], order_type: str, existing_cart: list | None = None, point: str | None = None,
|
||||
manual: list | None = None) -> dict:
|
||||
"""The lines priced exactly as the order will be: per line the unit price (base + options,
|
||||
then price groups/modifiers, then a discount or a by-hand price change), its total, what
|
||||
was chosen (names) and any adjustment. `manual`: each line's by-hand change per unit (the
|
||||
waiter's price change, 4.4)."""
|
||||
priced = price_lines(db, lines, order_type, existing_cart, point) if lines else []
|
||||
out = []
|
||||
for i, (line, (unit, events)) in enumerate(zip(lines, priced)):
|
||||
adj = discount_adjustment(unit, line.discount_pct)
|
||||
hand = round(float((manual or [])[i] or 0.0), 2) if manual and i < len(manual) else 0.0
|
||||
row = {
|
||||
"product_id": line.product.id, "name": line.product.name, "quantity": line.quantity,
|
||||
"base_price": round(line.product.base_price, 2), "extra": line.extra,
|
||||
"discount_pct": line.discount_pct, "discount": adj,
|
||||
"unit_price": round(unit + adj + hand, 2), "total": round((unit + adj + hand) * line.quantity, 2),
|
||||
"entries": [{"name": e["name"], "price": e["price_delta"], "type": e["type"]} for e in line.entries],
|
||||
"removed": line.removed, "notes": line.notes, "adjustments": adjustments(db, events),
|
||||
}
|
||||
if manual is not None:
|
||||
row["price_adjustment"] = hand
|
||||
out.append(row)
|
||||
return {"order_type": order_type, "lines": out, "total": round(sum(x["total"] for x in out), 2)}
|
||||
|
||||
Reference in New Issue
Block a user