feat: sales points, offerings and prices per sales point (platform-v2 phase 3S, step S.1)

- core/sales_points.py: in_store, in_store_takeaway, qr_table, online_takeaway,
  online_delivery, online_preorder (+ old channel names mapped).
- Alembic 0017: orders.sales_point (backfilled), products.sold_at (from digital_visible),
  digital_price/digital_discount -> item price modifiers for the QR menu and online,
  order_channel conditions -> sales points; drops the three digital columns.
- Every order records its sales point; the pricing engine's channel is it; items not sold
  there are refused (order.not_sold_here, pos.line_invalid not_sold_here).
- POS menu: only what the counter sells, prices per counter sales point; the screen shows
  the order type's products and prices. Waiter app asks for in-person products.
- Cloud snapshot: products sold by the customer apps, priced by the engine.
- Manager: product "Where it's sold" tab; pricing condition shows sales points.
- Products API, catalogue export/import carry sold_at.
- Tests: test_sales_points.py (8, incl. the migration), access +1, terminal +1.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-15 11:00:40 +02:00
co-authored by Claude Opus 5.5
parent f2182dcb37
commit 1b468fe44b
30 changed files with 700 additions and 156 deletions
+11 -4
View File
@@ -11,6 +11,7 @@ Lock behaviour (unchanged):
- cloud sets locked=true → lock_pending while a workday is open, locked once it closes
- expiry past grace / site deactivated → likewise never mid-service
"""
from core import sales_points
from core.access.queries import holders_filter
from core.timeutil import utcnow
import asyncio
@@ -269,23 +270,28 @@ async def _push_menu_snapshot():
.join(ProductCategory, ProductCategory.product_id == Product.id)
.filter(
ProductCategory.category_id == cat.id,
Product.digital_visible == 1,
Product.lifecycle_status == "active",
)
.order_by(ProductCategory.sort_order, Product.id)
.all()
)
# sold by the customer apps (phase 3S); priced by the engine for the QR menu
# (S.2 sends a price per sales point)
products = [p for p in products if set(sales_points.sold_at(p)) & set(sales_points.DIGITAL)]
from services.pos_cart import unit_prices
qr_prices = unit_prices(db, products, sales_points.QR_TABLE)
all_products.extend(p for p in products if p not in all_products) # once, even if listed twice
product_list = []
for p in products:
for p, qr_price in zip(products, qr_prices):
product_list.append({
"id": p.id,
"name": p.name,
"digital_name": p.digital_name,
"digital_description": p.digital_description,
"digital_price": p.digital_price,
"digital_price": round(qr_price, 2),
"base_price": p.base_price,
"digital_discount": p.digital_discount,
"digital_discount": 0,
"sold_at": [sp for sp in sales_points.sold_at(p) if sp in sales_points.DIGITAL],
"digital_available": bool(p.digital_available),
"digital_image_url": p.digital_image_url,
"image_url": p.image_url,
@@ -353,6 +359,7 @@ def store_online_orders(db, orders: list[dict]) -> dict:
status="open",
source="online",
order_type="delivery" if cloud_order.get("order_type") == "delivery" else "here",
sales_point=sales_points.for_order(None, "online", cloud_order.get("order_type")),
business_day_id=day.id if day else None,
online_order_ref=cloud_order["public_ref"],
online_order_cloud_id=cloud_order["id"],
+32 -6
View File
@@ -20,7 +20,8 @@ Rules (the same as the waiter's options dialog, waiter_pwa ItemOptionsModal.jsx)
- quantities: per-choice qty only on multi-select sets, per-add-on qty only on allow_multiple.
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.
choice_required | sub_required | unknown_ingredient | quantity | discount | not_sold_here
(the product isn't sold at this sales point, phase 3S).
A line may carry `discount_pct` (0–100, whole percent): the quote shows it; selling it needs
ordering.prices.modify or a manager's approval (routers/pos.py).
"""
@@ -30,9 +31,15 @@ import json
from dataclasses import dataclass, field
from typing import Optional
from core import sales_points
from core.errors import XeniaError
CHANNELS = {"here": "pos", "takeaway": "takeaway"}
POINTS = {"here": sales_points.IN_STORE, "takeaway": sales_points.IN_STORE_TAKEAWAY}
def point_for(order_type: str | None) -> str:
"""The counter's sales point for an order type (phase 3S)."""
return POINTS.get(order_type or "here", sales_points.IN_STORE)
def _subs(raw) -> list[dict]:
@@ -175,19 +182,22 @@ def resolve_line(db, raw: dict, n: int) -> Line:
return line
def price_lines(db, lines: list[Line], order_type: str, existing_cart: list | None = None):
def price_lines(db, lines: list[Line], order_type: str, existing_cart: list | None = None, point: str | None = None):
"""[(unit_price, [PriceEvent])] for each line, with the same context POST
/api/orders/{id}/items builds: the order's existing items, the lines before it in the
same batch (at their resolved price), and this line."""
same batch (at their resolved price), and this line. The sales point is the order type's
at the counter unless given; a product not sold there → pos.line_invalid not_sold_here."""
from core.timeutil import utcnow
from services.pricing import OrderContext, build_active_price_group_ids, resolve_price
now = utcnow()
active = build_active_price_group_ids(db, now)
channel = CHANNELS.get(order_type, "pos")
channel = point or point_for(order_type)
cart = list(existing_cart or [])
out = []
for line in lines:
for n, line in enumerate(lines):
p = line.product
if not sales_points.is_sold_at(p, channel):
raise _bad(n, "not_sold_here")
here = {"product_id": p.id, "category_id": p.primary_category_id, "category_ids": p.category_ids,
"quantity": line.quantity, "unit_price": p.base_price + line.extra}
ctx = OrderContext(channel=channel, now=now, cart_items=cart + [here], active_price_group_ids=active)
@@ -197,6 +207,22 @@ def price_lines(db, lines: list[Line], order_type: str, existing_cart: list | No
return out
def unit_prices(db, products, point: str) -> list[float]:
"""Each product's price for one unit without options at a sales point, right now, priced
on its own (an empty cart): what a menu shows."""
from core.timeutil import utcnow
from services.pricing import OrderContext, build_active_price_group_ids, resolve_price
now = utcnow()
active = build_active_price_group_ids(db, now)
out = []
for p in products:
here = {"product_id": p.id, "category_id": p.primary_category_id, "category_ids": p.category_ids,
"quantity": 1, "unit_price": p.base_price}
ctx = OrderContext(channel=point, now=now, cart_items=[here], active_price_group_ids=active)
out.append(resolve_price(p, 1, 0.0, ctx, db)[0])
return out
def order_cart(order) -> list[dict]:
"""An order's active items as the pricing engine's cart context (what POST
/api/orders/{id}/items starts from, and what a table's quote starts from, 3.3c)."""
+3 -2
View File
@@ -33,7 +33,7 @@ from sqlalchemy.orm import Session
@dataclass
class OrderContext:
"""Snapshot of order-level facts needed to evaluate conditions."""
channel: str # 'pos' | 'online' | 'qr' | 'takeaway'
channel: str # the order's sales point (core/sales_points.py)
now: datetime # current UTC time
cart_items: list # list of {product_id, category_id (primary), category_ids, quantity, unit_price}
active_price_group_ids: set # set of PriceGroup.id that are currently active
@@ -135,7 +135,8 @@ def _eval_condition(ctype: str, params: dict, ctx: OrderContext,
return passed, {"min_order_value": round(total, 2)}
if ctype == "order_channel":
passed = ctx.channel in params.get("channels", [])
from core.sales_points import normalize
passed = ctx.channel in normalize(params.get("channels", []))
return passed, {"order_channel": ctx.channel}
if ctype == "price_group_active":