Files
bonaminandClaude Opus 5.5 72e175ab73 feat(backend,waiter,terminal,manager): +1 on a sent line and changing a line's quantity
Owner 2026-11-23 (plan 6F.5). Product flag one_more_enabled (off by default; product editor).
POST /api/orders/{id}/items/{item}/again: a new line like the old one (choices, removed, note,
course), priced now, sent to the kitchen like any new line. Waiter app and the counter's table
panel: +1 beside such lines; the first tap arms it, more taps count up, 'Send +N' sends, 4 s idle
disarms (no stray sends).
POST /api/orders/{id}/items/{item}/quantity: venue setting orders.quantity_change (off | add_only |
add_and_remove, default off), key ordering.items.change_quantity (owner, store manager; Alembic 0049
grants it); up only changes the line, down cuts a cancelled part ('quantity lowered', split_from_id)
and prints a cancellation only when asked (the dialog presets yes while the kitchen hasn't finished).
Waiter: 'Change quantity' in a line's actions (now -> new, -/+). cut_part() now shared with split.
GET /api/orders/{id} items carry the product's one_more_enabled / unit_type, deal ids, split_from_id,
cancel_reason.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-23 08:26:46 +02:00

529 lines
20 KiB
Python

import json
from pydantic import BaseModel, model_validator, field_validator
from typing import Optional, List, Any, Literal, Dict
from core.sales_points import ALL as _SALES_POINTS, sold_at as _sold_at
from core.allergens import of as _allergens_of
from core.menu_languages import of as _menu_tr_of, _load as _menu_tr, names_of as _choice_names
SalesPoint = Literal["in_store", "in_store_takeaway", "qr_table", "online_takeaway", "online_delivery", "online_preorder"]
class CategoryCreate(BaseModel):
name: str
color: Optional[str] = None
sort_order: int = 0
parent_id: Optional[int] = None
general_sort_order: int = 0
auto_expanded: bool = False
media_id: Optional[int] = None # its picture in the media library (5.2)
translations: Dict[str, Dict[str, str]] = {} # 6.4: {"en": {"name": "…"}}
class CategoryUpdate(BaseModel):
name: Optional[str] = None
color: Optional[str] = None
sort_order: Optional[int] = None
parent_id: Optional[int] = None
general_sort_order: Optional[int] = None
auto_expanded: Optional[bool] = None
media_id: Optional[int] = None # null clears it (sent explicitly)
translations: Optional[Dict[str, Dict[str, str]]] = None # None = unchanged
class CategoryOut(BaseModel):
id: int
name: str
color: Optional[str] = None
sort_order: int = 0
parent_id: Optional[int] = None
general_sort_order: int = 0
auto_expanded: bool = False
media_id: Optional[int] = None
image: Optional[dict] = None # {id, name, thumb, card, full} (services/media.image_of)
translations: Dict[str, Dict[str, str]] = {}
model_config = {"from_attributes": True}
@field_validator("translations", mode="before")
@classmethod
def _tr(cls, v):
return _menu_tr(v)
class CategoryReparentRequest(BaseModel):
parent_id: Optional[int] = None
class CategoryReorderItem(BaseModel):
id: int
sort_order: int
class SubcategoryReorderItem(BaseModel):
id: int
sort_order: int # position among subcategories within the parent
class ParentGeneralReorderItem(BaseModel):
id: int # parent category id
general_sort_order: int
# ── Modifier Groups ───────────────────────────────────────────────────────────
class ModifierGroupCreate(BaseModel):
modifier_type: str # "option" | "ingredient" | "preference"
name: str
sort_order: int = 0
translations: Dict[str, str] = {} # its name per language (0042)
class ModifierGroupOut(BaseModel):
id: int
product_id: int
modifier_type: str
name: str
sort_order: int = 0
translations: Dict[str, str] = {} # its name per language (0042)
model_config = {"from_attributes": True}
@field_validator("translations", mode="before")
@classmethod
def _names(cls, v):
return {k: x for k, x in _menu_tr(v).items() if isinstance(x, str)}
# ── Quick Options ─────────────────────────────────────────────────────────────
class ProductQuickOptionCreate(BaseModel):
name: str
price: float = 0.0
allow_multiple: bool = False
sort_order: int = 0
is_favorite: bool = False
favorite_sort_order: int = 0
is_compact: bool = False
translations: Dict[str, str] = {} # its name per language (0042)
class ProductQuickOptionOut(BaseModel):
id: int
product_id: int
name: str
price: float = 0.0
allow_multiple: bool = False
sort_order: int = 0
is_favorite: bool = False
favorite_sort_order: int = 0
is_compact: bool = False
translations: Dict[str, str] = {} # its name per language (0042)
model_config = {"from_attributes": True}
@field_validator("translations", mode="before")
@classmethod
def _names(cls, v):
return {k: x for k, x in _menu_tr(v).items() if isinstance(x, str)}
# ── Options ──────────────────────────────────────────────────────────────────
class OptionSubChoice(BaseModel):
name: str
extra_cost: float = 0.0
is_default: bool = False
# When True waiter can add more than 1 of this sub-choice
allow_multiple: bool = False
# When True renders as half-width card on the PWA
is_compact: bool = False
translations: Dict[str, str] = {} # its name per language (0042)
class ProductOptionBase(BaseModel):
name: str
extra_cost: float = 0.0
allow_multiple: bool = False
# When True waiter can select more than one sub-choice at once
multi_select: bool = False
is_favorite: bool = False
favorite_sort_order: int = 0
# When True renders the option itself as half-width card on the PWA
is_compact: bool = False
group_id: Optional[int] = None
translations: Dict[str, str] = {} # its name per language (0042)
class ProductOptionCreate(ProductOptionBase):
sub_choices: List[OptionSubChoice] = []
class ProductOptionOut(ProductOptionBase):
id: int
product_id: int
sub_choices: List[OptionSubChoice] = []
model_config = {"from_attributes": True}
@model_validator(mode='before')
@classmethod
def parse_option_sub_choices(cls, data: Any) -> Any:
if hasattr(data, 'sub_choices'):
raw = data.sub_choices
parsed = json.loads(raw) if isinstance(raw, str) else []
return {
'id': data.id,
'product_id': data.product_id,
'name': data.name,
'extra_cost': data.extra_cost,
'allow_multiple': getattr(data, 'allow_multiple', False) or False,
'multi_select': getattr(data, 'multi_select', False) or False,
'sub_choices': parsed,
'is_favorite': getattr(data, 'is_favorite', False) or False,
'favorite_sort_order': getattr(data, 'favorite_sort_order', 0) or 0,
'is_compact': getattr(data, 'is_compact', False) or False,
'group_id': getattr(data, 'group_id', None),
'translations': _choice_names(data),
}
return data
# ── Ingredients ───────────────────────────────────────────────────────────────
class ProductIngredientBase(BaseModel):
name: str
extra_cost: float = 0.0
is_favorite: bool = False
favorite_sort_order: int = 0
# When True renders as half-width card on the PWA
is_compact: bool = False
group_id: Optional[int] = None
translations: Dict[str, str] = {} # its name per language (0042)
class ProductIngredientCreate(ProductIngredientBase):
pass
class ProductIngredientOut(ProductIngredientBase):
id: int
product_id: int
model_config = {"from_attributes": True}
@field_validator("translations", mode="before")
@classmethod
def _names(cls, v):
return {k: x for k, x in _menu_tr(v).items() if isinstance(x, str)}
# ── Sub-choices (nested under a preference choice) ────────────────────────────
class SubChoice(BaseModel):
name: str
extra_cost: float = 0.0
is_default: bool = False
is_compact: bool = False
translations: Dict[str, str] = {} # its name per language (0042)
# ── Shared subset (set-level, shown for all non-disabling choices) ─────────────
class SharedSubsetChoice(BaseModel):
name: str
extra_cost: float = 0.0
is_default: bool = False
is_compact: bool = False
translations: Dict[str, str] = {} # its name per language (0042)
class SharedSubset(BaseModel):
name: str
translations: Dict[str, str] = {} # its name per language (0042)
choices: List[SharedSubsetChoice] = []
# ── Preferences ───────────────────────────────────────────────────────────────
class PreferenceChoiceCreate(BaseModel):
name: str
extra_cost: float = 0.0
sub_choices: List[SubChoice] = []
disables_subset: bool = False
# When True renders as half-width card on the PWA
is_compact: bool = False
translations: Dict[str, str] = {} # its name per language (0042)
class PreferenceChoiceOut(BaseModel):
id: int
set_id: int
name: str
extra_cost: float = 0.0
sub_choices: List[SubChoice] = []
disables_subset: bool = False
is_compact: bool = False
translations: Dict[str, str] = {} # its name per language (0042)
model_config = {"from_attributes": True}
@model_validator(mode='before')
@classmethod
def parse_sub_choices(cls, data: Any) -> Any:
if hasattr(data, 'sub_choices'):
raw = data.sub_choices
if isinstance(raw, str):
try:
parsed = json.loads(raw)
except Exception:
parsed = []
else:
parsed = []
return {
'id': data.id,
'set_id': data.set_id,
'name': data.name,
'extra_cost': data.extra_cost,
'sub_choices': parsed,
'disables_subset': data.disables_subset or False,
'is_compact': getattr(data, 'is_compact', False) or False,
'translations': _choice_names(data),
}
return data
class PreferenceSetCreate(BaseModel):
name: str
choices: List[PreferenceChoiceCreate] = []
default_choice_index: Optional[int] = None # index into choices (0-based)
shared_subset: Optional[SharedSubset] = None
is_favorite: bool = False
favorite_sort_order: int = 0
group_id: Optional[int] = None
allow_multi_select: bool = False
allow_choice_quantity: bool = False
translations: Dict[str, str] = {} # its name per language (0042)
class PreferenceSetOut(BaseModel):
id: int
product_id: int
name: str
choices: List[PreferenceChoiceOut] = []
default_choice_id: Optional[int] = None
shared_subset: Optional[SharedSubset] = None
is_favorite: bool = False
favorite_sort_order: int = 0
group_id: Optional[int] = None
allow_multi_select: bool = False
allow_choice_quantity: bool = False
translations: Dict[str, str] = {} # its name per language (0042)
model_config = {"from_attributes": True}
@model_validator(mode='before')
@classmethod
def parse_shared_subset(cls, data: Any) -> Any:
if hasattr(data, 'shared_subset'):
raw = data.shared_subset
if isinstance(raw, str):
try:
parsed = json.loads(raw)
except Exception:
parsed = None
else:
parsed = None
return {
'id': data.id,
'product_id': data.product_id,
'name': data.name,
'choices': list(data.choices),
'default_choice_id': data.default_choice_id,
'shared_subset': parsed,
'is_favorite': getattr(data, 'is_favorite', False) or False,
'favorite_sort_order': getattr(data, 'favorite_sort_order', 0) or 0,
'group_id': getattr(data, 'group_id', None),
'allow_multi_select': getattr(data, 'allow_multi_select', False) or False,
'allow_choice_quantity': getattr(data, 'allow_choice_quantity', False) or False,
'translations': _choice_names(data),
}
return data
# ── Products ──────────────────────────────────────────────────────────────────
class CostBreakdownItem(BaseModel):
label: str
amount: float
class ProductBase(BaseModel):
name: str
description: Optional[str] = None
# D-23: listed in category_ids (primary first in output); primary_category_id decides
# tickets/reports. category_id is DEV SCAFFOLDING (§3.3) for the current manager and
# waiter screens: in output = the primary; as input = "move the product's primary to".
category_id: Optional[int] = None
primary_category_id: Optional[int] = None
category_ids: Optional[List[int]] = None
base_price: float
# piece | portion | kg | liter | gram | ml
unit_type: str = "piece"
is_available: bool = True
lifecycle_status: str = "active"
prep_zone_ids: List[int] = []
sort_order: int = 0
# Where it's sold (phase 3S, core/sales_points.py); prices per sales point are price modifiers
sold_at: List[SalesPoint] = list(_SALES_POINTS)
# Xenia Connect — what the customer apps show
digital_available: bool = True
digital_name: Optional[str] = None
digital_description: Optional[str] = None
digital_image_url: Optional[str] = None
# Phase 2A — cost tracking
cost_simple: Optional[float] = None
cost_breakdown: Optional[List[CostBreakdownItem]] = None
# Quick Add
quick_add_enabled: bool = True
one_more_enabled: bool = False # 6F.5
# Tags
tags: Optional[List[str]] = None
# The EU allergens (plan step 6.3): {key: "contains" | "may_contain"}, core/allergens.py
allergens: Dict[str, str] = {}
# The menu in other languages (6.4, core/menu_languages.py)
translations: Dict[str, Dict[str, Any]] = {}
# Service item flag
is_service_item: bool = False
# Fiscal printer (ΦΗΜ)
fiscal_name: Optional[str] = None
fiscal_vat_group_id: Optional[int] = None
# Its photo in the media library (plan step 5.2)
media_id: Optional[int] = None
class ProductCreate(ProductBase):
quick_options: List[ProductQuickOptionCreate] = []
options: List[ProductOptionCreate] = []
ingredients: List[ProductIngredientCreate] = []
preference_sets: List[PreferenceSetCreate] = []
# group_id on options/ingredients/preference_sets is an index into this list
modifier_groups: List[ModifierGroupCreate] = []
class ProductUpdate(BaseModel):
name: Optional[str] = None
description: Optional[str] = None
category_id: Optional[int] = None # scaffolding, see ProductBase
primary_category_id: Optional[int] = None
category_ids: Optional[List[int]] = None # None = memberships unchanged
base_price: Optional[float] = None
unit_type: Optional[str] = None
is_available: Optional[bool] = None
lifecycle_status: Optional[str] = None
prep_zone_ids: Optional[List[int]] = None
sort_order: Optional[int] = None
quick_options: Optional[List[ProductQuickOptionCreate]] = None
options: Optional[List[ProductOptionCreate]] = None
ingredients: Optional[List[ProductIngredientCreate]] = None
preference_sets: Optional[List[PreferenceSetCreate]] = None
modifier_groups: Optional[List[ModifierGroupCreate]] = None
sold_at: Optional[List[SalesPoint]] = None
digital_available: Optional[bool] = None
digital_name: Optional[str] = None
digital_description: Optional[str] = None
digital_image_url: Optional[str] = None
# Phase 2A — cost tracking
cost_simple: Optional[float] = None
cost_breakdown: Optional[List[CostBreakdownItem]] = None
# Quick Add
quick_add_enabled: Optional[bool] = None
one_more_enabled: Optional[bool] = None # 6F.5
# Tags
tags: Optional[List[str]] = None
allergens: Optional[Dict[str, str]] = None # None = unchanged, {} = none
translations: Optional[Dict[str, Dict[str, Any]]] = None # None = unchanged, {} = none
# Service item flag
is_service_item: Optional[bool] = None
# Fiscal printer (ΦΗΜ)
fiscal_name: Optional[str] = None
fiscal_vat_group_id: Optional[int] = None
media_id: Optional[int] = None # null clears it (sent explicitly)
class ProductReorderItem(BaseModel):
id: int
sort_order: int
category_id: Optional[int] = None # set: position within that category (D-23); unset: global order (v1)
class ProductCategoryOut(BaseModel):
category_id: int
sort_order: int
class ProductOut(ProductBase):
id: int
categories: List[ProductCategoryOut] = [] # every listing with its position in that category
quick_options: List[ProductQuickOptionOut] = []
options: List[ProductOptionOut] = []
ingredients: List[ProductIngredientOut] = []
preference_sets: List[PreferenceSetOut] = []
modifier_groups: List[ModifierGroupOut] = []
image_url: Optional[str] = None # the card size of `image` (phones, counter)
image: Optional[dict] = None # {id, name, thumb, card, full} (services/media.image_of)
model_config = {"from_attributes": True}
@model_validator(mode='before')
@classmethod
def parse_cost_breakdown(cls, data: Any) -> Any:
if hasattr(data, 'cost_breakdown'):
raw = data.cost_breakdown
parsed = None
if isinstance(raw, str):
try:
parsed = json.loads(raw)
except Exception:
parsed = None
# Return a plain dict so other validators still see all fields
return {
'id': data.id,
'name': data.name,
'description': data.description,
'category_id': data.primary_category_id, # scaffolding (§3.3)
'primary_category_id': data.primary_category_id,
'category_ids': data.category_ids,
'categories': [{'category_id': m.category_id, 'sort_order': m.sort_order} for m in data.memberships],
'base_price': data.base_price,
'unit_type': getattr(data, 'unit_type', 'piece') or 'piece',
'is_available': data.is_available,
'lifecycle_status': data.lifecycle_status,
'prep_zone_ids': [z.id for z in data.prep_zones] if hasattr(data, 'prep_zones') else [],
'sort_order': data.sort_order,
'image_url': data.image_url,
'image': data.image,
'media_id': data.media_id,
'sold_at': _sold_at(data),
'digital_available': bool(data.digital_available),
'digital_name': data.digital_name,
'digital_description': data.digital_description,
'digital_image_url': data.digital_image_url,
'cost_simple': data.cost_simple,
'cost_breakdown': parsed,
'quick_add_enabled': getattr(data, 'quick_add_enabled', True),
'one_more_enabled': bool(getattr(data, 'one_more_enabled', False)),
'tags': json.loads(data.tags) if isinstance(getattr(data, 'tags', None), str) else (getattr(data, 'tags', None) or []),
'allergens': _allergens_of(data),
'translations': _menu_tr_of(data),
'is_service_item': bool(getattr(data, 'is_service_item', False)),
'fiscal_name': getattr(data, 'fiscal_name', None),
'fiscal_vat_group_id': getattr(data, 'fiscal_vat_group_id', None),
'quick_options': list(data.quick_options),
'options': list(data.options),
'ingredients': list(data.ingredients),
'preference_sets': list(data.preference_sets),
'modifier_groups': list(data.modifier_groups) if hasattr(data, 'modifier_groups') else [],
}
return data