feat(connect): the site side of online orders: fee line, customer's time, price check
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>
This commit is contained in:
@@ -16,6 +16,10 @@ is mirrored to the cloud in the background so the customer's tracking page follo
|
||||
never waits for the cloud (Rule 8). Paying an online order is the counter's ordinary payment
|
||||
(POST /api/pos/orders/{id}/pay): online orders belong to whoever holds connect_orders.manage
|
||||
(routers/orders.py _can_access_order).
|
||||
|
||||
Since phase 3S (S.5) an order carries the customer's time (`requested_for`, None = as soon as
|
||||
possible), its delivery area, the delivery fee as its own line (`system_key` "delivery_fee",
|
||||
paid with the items) and the site's price check (`price_diff`, services/online_check.py).
|
||||
"""
|
||||
import logging
|
||||
from typing import Literal, Optional
|
||||
@@ -129,7 +133,11 @@ def _board_out(order: Order) -> dict:
|
||||
"name": order.online_customer_name, "phone": order.online_customer_phone,
|
||||
"address": order.online_customer_address, "notes": order.online_customer_notes,
|
||||
},
|
||||
"requested_for": _iso(order.requested_for), # None = as soon as possible (S.5)
|
||||
"area": order.online_delivery_area,
|
||||
"price_diff": OrderOut._diff(order.online_price_diff),
|
||||
"items": [OrderItemOut.model_validate(i).model_dump(mode="json") for i in items],
|
||||
"fee": round(sum(_item_total(i) for i in items if i.system_key == "delivery_fee"), 2),
|
||||
"total": round(sum(_item_total(i) for i in items), 2),
|
||||
"unpaid": round(sum(_item_total(i) for i in items if i.status in UNPAID), 2),
|
||||
}
|
||||
|
||||
@@ -84,7 +84,7 @@ def export_catalog(db: Session = Depends(get_db), user: User = Depends(require_m
|
||||
categories = db.query(Category).order_by(Category.sort_order).all()
|
||||
orphan_products = (
|
||||
db.query(Product)
|
||||
.filter(~Product.memberships.any(), Product.lifecycle_status != "archived")
|
||||
.filter(~Product.memberships.any(), Product.lifecycle_status != "archived", Product.system_key.is_(None))
|
||||
.order_by(Product.sort_order)
|
||||
.all()
|
||||
)
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
"""Delivery areas and fee rules (plan phase 3S, step S.3). Key: connect_orders.fees.
|
||||
|
||||
GET /api/connect/fees {areas, rules, ordering}
|
||||
GET /api/connect/fees {areas, rules, ordering, fee_product}
|
||||
PUT /api/connect/fees/fee-product the delivery fee line's name and VAT group (S.5)
|
||||
GET /api/connect/fees/ordering the online ordering settings PUT: save them
|
||||
POST /api/connect/fees/areas add an area PUT/DELETE /areas/{id}
|
||||
POST /api/connect/fees/rules add a rule PUT/DELETE /rules/{id}
|
||||
@@ -219,7 +220,53 @@ def _changed():
|
||||
|
||||
@router.get("")
|
||||
def list_fees(db: Session = Depends(get_db), user=Depends(require(KEY))):
|
||||
return {**fees_payload(db), "ordering": ordering_settings(db)}
|
||||
from services import system_products
|
||||
fee_product = system_products.get(db, system_products.DELIVERY_FEE)
|
||||
db.commit()
|
||||
return {**fees_payload(db), "ordering": ordering_settings(db),
|
||||
"fee_product": {**system_products.out(fee_product), "vat_groups": _vat_groups(db)}}
|
||||
|
||||
|
||||
def _vat_groups(db) -> list[dict]:
|
||||
"""The fiscal machine's VAT groups (setting fiscal.vat_groups): [{machine_id, friendly_name}]."""
|
||||
from models.settings import PosSettings
|
||||
row = db.query(PosSettings).filter(PosSettings.key == "fiscal.vat_groups").first()
|
||||
try:
|
||||
groups = json.loads(row.value or "[]") if row else []
|
||||
except (TypeError, ValueError):
|
||||
return []
|
||||
return [{"machine_id": g.get("machine_id"), "friendly_name": g.get("friendly_name")}
|
||||
for g in groups if isinstance(g, dict) and isinstance(g.get("machine_id"), int)]
|
||||
|
||||
|
||||
class FeeProductBody(BaseModel):
|
||||
"""How the delivery fee line is called (on screens and the receipt) and its VAT group."""
|
||||
name: str = Field(min_length=1, max_length=40)
|
||||
vat_group_id: Optional[int] = Field(default=None, ge=1, le=99)
|
||||
|
||||
@field_validator("name")
|
||||
@classmethod
|
||||
def _strip(cls, v: str) -> str:
|
||||
v = v.strip()
|
||||
if not v:
|
||||
raise ValueError("empty")
|
||||
return v
|
||||
|
||||
|
||||
@router.put("/fee-product")
|
||||
def put_fee_product(body: FeeProductBody, db: Session = Depends(get_db), user=Depends(require(KEY))):
|
||||
"""The delivery fee's system product (services/system_products.py). Lines already on orders
|
||||
keep the name they were added with; the VAT group counts from the next receipt."""
|
||||
from services import system_products
|
||||
if body.vat_group_id is not None:
|
||||
groups = [g["machine_id"] for g in _vat_groups(db)]
|
||||
if groups and body.vat_group_id not in groups:
|
||||
raise XeniaError("fees.vat_group_unknown", vat_group_id=body.vat_group_id)
|
||||
product = system_products.get(db, system_products.DELIVERY_FEE)
|
||||
product.name = product.fiscal_name = body.name
|
||||
product.fiscal_vat_group_id = body.vat_group_id
|
||||
db.commit()
|
||||
return system_products.out(product)
|
||||
|
||||
|
||||
@router.get("/ordering")
|
||||
|
||||
@@ -261,6 +261,9 @@ def _build_order_payload(order: Order, tables_map: dict, db: Session) -> dict:
|
||||
"opened_at": opened_at_out,
|
||||
"closed_at": _fmt_ts(order.closed_at) if order.closed_at else None,
|
||||
"notes": order.notes,
|
||||
# online orders (phase 3S, S.5): their reference and the customer's time (None = asap)
|
||||
"online_ref": order.online_order_ref if order.source == "online" else None,
|
||||
"requested_for": _fmt_ts(order.requested_for),
|
||||
"waiters": waiter_names,
|
||||
"items": items_out,
|
||||
}
|
||||
|
||||
@@ -382,6 +382,7 @@ def get_order(order_id: int, db: Session = Depends(get_db), user: User = Depends
|
||||
"paid_in_shift_id": i.paid_in_shift_id,
|
||||
"price_adjustment": getattr(i, 'price_adjustment', 0.0) or 0.0,
|
||||
"unit_type": i.unit_type,
|
||||
"system_key": i.system_key, # the delivery fee line (phase 3S, S.5)
|
||||
}
|
||||
|
||||
def fmt_log(l):
|
||||
@@ -430,6 +431,9 @@ def get_order(order_id: int, db: Session = Depends(get_db), user: User = Depends
|
||||
"kds_status": order.kds_status,
|
||||
"kds_status_changed_at": _iso(order.kds_status_changed_at),
|
||||
"customer_count": order.customer_count,
|
||||
# online orders (phase 3S, S.5): the customer's time (None = asap) and area
|
||||
"requested_for": _iso(order.requested_for),
|
||||
"online_delivery_area": order.online_delivery_area,
|
||||
"items": [fmt_item(i) for i in order.items],
|
||||
"waiters": [{"waiter_id": w.waiter_id} for w in order.waiters],
|
||||
"audit_logs": [fmt_log(l) for l in order.audit_logs],
|
||||
|
||||
@@ -271,7 +271,8 @@ def list_products(all: bool = False, category_id: int | None = None, sold_at: st
|
||||
`sold_at` (comma-separated): only products sold at one of those sales points (phase 3S; the
|
||||
waiter app asks for in_store,in_store_takeaway)."""
|
||||
from sqlalchemy.orm import selectinload
|
||||
q = db.query(Product).options(selectinload(Product.memberships), selectinload(Product.prep_zones))
|
||||
q = (db.query(Product).filter(Product.system_key.is_(None)) # system products: never listed (S.5)
|
||||
.options(selectinload(Product.memberships), selectinload(Product.prep_zones)))
|
||||
has_dashboard = holds(user, "core.dashboard.access")
|
||||
if not all or not has_dashboard:
|
||||
# Waiters only see active, available products
|
||||
@@ -395,7 +396,8 @@ def create_product(body: ProductCreate, db: Session = Depends(get_db), user: Use
|
||||
|
||||
@router.put("/{product_id}", response_model=ProductOut)
|
||||
def update_product(product_id: int, body: ProductUpdate, db: Session = Depends(get_db), user: User = Depends(require_menu_manager)):
|
||||
product = db.query(Product).filter(Product.id == product_id).first()
|
||||
# system products (the delivery fee line, S.5) are not part of the catalogue
|
||||
product = db.query(Product).filter(Product.id == product_id, Product.system_key.is_(None)).first()
|
||||
if not product:
|
||||
raise HTTPException(status_code=404, detail="Product not found")
|
||||
scalar_fields = body.model_dump(
|
||||
@@ -442,7 +444,8 @@ def update_product(product_id: int, body: ProductUpdate, db: Session = Depends(g
|
||||
|
||||
@router.post("/{product_id}/image", response_model=ProductOut)
|
||||
async def upload_product_image(product_id: int, file: UploadFile = File(...), db: Session = Depends(get_db), user: User = Depends(require_menu_manager)):
|
||||
product = db.query(Product).filter(Product.id == product_id).first()
|
||||
# system products (the delivery fee line, S.5) are not part of the catalogue
|
||||
product = db.query(Product).filter(Product.id == product_id, Product.system_key.is_(None)).first()
|
||||
if not product:
|
||||
raise HTTPException(status_code=404, detail="Product not found")
|
||||
|
||||
@@ -480,7 +483,8 @@ def list_modifier_groups(product_id: int, db: Session = Depends(get_db), user: U
|
||||
|
||||
@router.post("/{product_id}/modifier-groups", response_model=ModifierGroupOut, status_code=status.HTTP_201_CREATED)
|
||||
def create_modifier_group(product_id: int, body: ModifierGroupCreate, db: Session = Depends(get_db), user: User = Depends(require_menu_manager)):
|
||||
product = db.query(Product).filter(Product.id == product_id).first()
|
||||
# system products (the delivery fee line, S.5) are not part of the catalogue
|
||||
product = db.query(Product).filter(Product.id == product_id, Product.system_key.is_(None)).first()
|
||||
if not product:
|
||||
raise HTTPException(status_code=404, detail="Product not found")
|
||||
g = ProductModifierGroup(product_id=product_id, modifier_type=body.modifier_type, name=body.name, sort_order=body.sort_order)
|
||||
@@ -520,7 +524,8 @@ def delete_modifier_group(product_id: int, group_id: int, db: Session = Depends(
|
||||
|
||||
@router.delete("/{product_id}", status_code=status.HTTP_204_NO_CONTENT)
|
||||
def delete_product(product_id: int, hard: bool = False, db: Session = Depends(get_db), user: User = Depends(require_menu_manager)):
|
||||
product = db.query(Product).filter(Product.id == product_id).first()
|
||||
# system products (the delivery fee line, S.5) are not part of the catalogue
|
||||
product = db.query(Product).filter(Product.id == product_id, Product.system_key.is_(None)).first()
|
||||
if not product:
|
||||
raise HTTPException(status_code=404, detail="Product not found")
|
||||
if hard:
|
||||
|
||||
Reference in New Issue
Block a user