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>
226 lines
12 KiB
Python
226 lines
12 KiB
Python
from sqlalchemy import Column, Integer, String, Boolean, Float, ForeignKey, Text
|
|
from sqlalchemy.orm import relationship
|
|
from database import Base
|
|
from core.sales_points import ALL as _SALES_POINTS, dump as _dump_sales_points
|
|
|
|
_ALL_SALES_POINTS = _dump_sales_points(_SALES_POINTS)
|
|
|
|
|
|
class Category(Base):
|
|
__tablename__ = "categories"
|
|
|
|
id = Column(Integer, primary_key=True, index=True)
|
|
name = Column(String, nullable=False)
|
|
color = Column(String, nullable=True)
|
|
sort_order = Column(Integer, default=0)
|
|
# self-referential: null = top-level, non-null = sub-category of parent
|
|
parent_id = Column(Integer, ForeignKey("categories.id"), nullable=True)
|
|
# position of the "General" group (direct products) among sub-categories
|
|
general_sort_order = Column(Integer, default=0, nullable=False)
|
|
# sub-categories only: if True, the accordion section is expanded by default on the PWA
|
|
auto_expanded = Column(Boolean, default=False, nullable=False)
|
|
|
|
# Products whose PRIMARY category this is (tickets, reports). Every product listed in
|
|
# this category, primary or not: `memberships` (ProductCategory, D-23).
|
|
primary_products = relationship("Product", back_populates="primary_category")
|
|
memberships = relationship("ProductCategory", back_populates="category", cascade="all, delete-orphan")
|
|
subcategories = relationship("Category", back_populates="parent", foreign_keys="Category.parent_id")
|
|
parent = relationship("Category", back_populates="subcategories", remote_side="Category.id", foreign_keys="Category.parent_id")
|
|
|
|
|
|
class Product(Base):
|
|
__tablename__ = "products"
|
|
|
|
id = Column(Integer, primary_key=True, index=True)
|
|
name = Column(String, nullable=False)
|
|
description = Column(Text, nullable=True)
|
|
# D-23: a product can be listed in several categories (product_categories). The primary
|
|
# one decides ticket grouping, prep-zone category order and the category order items
|
|
# snapshot (D-18). It is always one of the product's categories (routers/products.py).
|
|
primary_category_id = Column(Integer, ForeignKey("categories.id"), nullable=True, index=True)
|
|
base_price = Column(Float, nullable=False)
|
|
is_available = Column(Boolean, default=True, nullable=False)
|
|
# "active" | "archived" — archived products are kept for order history but hidden from active use;
|
|
# "system" — a line the system adds itself (services/system_products.py), never on a menu
|
|
lifecycle_status = Column(String, default="active", nullable=False)
|
|
# system products only (phase 3S, S.5): what the line is, e.g. "delivery_fee"
|
|
system_key = Column(String, nullable=True, unique=True, index=True)
|
|
image_url = Column(String, nullable=True)
|
|
sort_order = Column(Integer, default=0, nullable=False)
|
|
|
|
# Where it's sold (phase 3S): JSON list of sales points (core/sales_points.py). Prices
|
|
# per sales point are price modifiers (condition order_channel), not columns.
|
|
sold_at = Column(Text, nullable=False, default=_ALL_SALES_POINTS)
|
|
# Xenia Connect — what the customer apps show (name, text, picture); digital_available =
|
|
# orderable online right now (a temporary switch, like is_available)
|
|
digital_available = Column(Integer, default=1, nullable=False)
|
|
digital_name = Column(String, nullable=True)
|
|
digital_description = Column(String, nullable=True)
|
|
digital_image_url = Column(String, nullable=True)
|
|
# sha256 of the image_url file last successfully pushed to the cloud — lets
|
|
# cloud_sync skip re-uploading unchanged images on every sync tick
|
|
cloud_image_hash = Column(String, nullable=True)
|
|
|
|
# Unit of measure: "piece" | "portion" | "kg" | "liter" | "gram" | "ml"
|
|
unit_type = Column(String, default="piece", nullable=False)
|
|
|
|
# Phase 2A — cost tracking
|
|
cost_simple = Column(Float, nullable=True)
|
|
cost_breakdown = Column(Text, nullable=True) # JSON: [{"label": str, "amount": float}]
|
|
|
|
# Quick Add: allow adding 1 unit directly from product list without opening config modal
|
|
quick_add_enabled = Column(Boolean, default=True, nullable=False)
|
|
|
|
# Service item: non-inventoried table-service items (cutlery, glasses, etc.)
|
|
is_service_item = Column(Boolean, default=False, nullable=False)
|
|
|
|
# Tags: JSON array of strings e.g. ["vegan", "gluten-free"]
|
|
tags = Column(Text, nullable=True)
|
|
|
|
# Fiscal printer (ΦΗΜ) fields
|
|
fiscal_name = Column(String, nullable=True) # short name for receipt (falls back to name)
|
|
fiscal_vat_group_id = Column(Integer, nullable=True) # machine VAT group ID (1-based integer)
|
|
|
|
primary_category = relationship("Category", back_populates="primary_products")
|
|
memberships = relationship("ProductCategory", back_populates="product", cascade="all, delete-orphan",
|
|
order_by="ProductCategory.category_id")
|
|
|
|
@property
|
|
def category_ids(self) -> list[int]:
|
|
"""The categories it's listed in, primary first."""
|
|
ids = [m.category_id for m in self.memberships]
|
|
if self.primary_category_id in ids:
|
|
ids.remove(self.primary_category_id)
|
|
ids.insert(0, self.primary_category_id)
|
|
return ids
|
|
prep_zones = relationship("PrepZone", secondary="product_prep_zones", back_populates="products")
|
|
quick_options = relationship("ProductQuickOption", back_populates="product", cascade="all, delete-orphan")
|
|
options = relationship("ProductOption", back_populates="product", cascade="all, delete-orphan")
|
|
ingredients = relationship("ProductIngredient", back_populates="product", cascade="all, delete-orphan")
|
|
preference_sets = relationship("ProductPreferenceSet", back_populates="product", cascade="all, delete-orphan")
|
|
modifier_groups = relationship("ProductModifierGroup", back_populates="product", cascade="all, delete-orphan")
|
|
order_items = relationship("OrderItem", back_populates="product")
|
|
|
|
|
|
class ProductModifierGroup(Base):
|
|
"""A named folder/group that can contain options, ingredients, or preference sets."""
|
|
__tablename__ = "product_modifier_groups"
|
|
|
|
id = Column(Integer, primary_key=True, index=True)
|
|
product_id = Column(Integer, ForeignKey("products.id"), nullable=False)
|
|
# "option" | "ingredient" | "preference"
|
|
modifier_type = Column(String, nullable=False)
|
|
name = Column(String, nullable=False)
|
|
sort_order = Column(Integer, default=0, nullable=False)
|
|
|
|
product = relationship("Product", back_populates="modifier_groups")
|
|
|
|
|
|
class ProductOption(Base):
|
|
__tablename__ = "product_options"
|
|
|
|
id = Column(Integer, primary_key=True, index=True)
|
|
product_id = Column(Integer, ForeignKey("products.id"), nullable=False)
|
|
name = Column(String, nullable=False)
|
|
extra_cost = Column(Float, default=0.0)
|
|
allow_multiple = Column(Boolean, default=False, nullable=False)
|
|
# When True, waiter can select more than one sub-choice simultaneously
|
|
multi_select = Column(Boolean, default=False, nullable=False)
|
|
# JSON array [{name, extra_cost, is_default, allow_multiple}] — sub-options shown when this option is checked
|
|
sub_choices = Column(Text, nullable=True)
|
|
is_favorite = Column(Boolean, default=False, nullable=False)
|
|
favorite_sort_order = Column(Integer, default=0, nullable=False)
|
|
# When True renders as half-width card on the PWA
|
|
is_compact = Column(Boolean, default=False, nullable=False)
|
|
# Optional group/folder this option belongs to
|
|
group_id = Column(Integer, ForeignKey("product_modifier_groups.id"), nullable=True)
|
|
|
|
product = relationship("Product", back_populates="options")
|
|
|
|
|
|
class ProductQuickOption(Base):
|
|
__tablename__ = "product_quick_options"
|
|
|
|
id = Column(Integer, primary_key=True, index=True)
|
|
product_id = Column(Integer, ForeignKey("products.id"), nullable=False)
|
|
name = Column(String, nullable=False)
|
|
price = Column(Float, default=0.0, nullable=False)
|
|
allow_multiple = Column(Boolean, default=False, nullable=False)
|
|
sort_order = Column(Integer, default=0, nullable=False)
|
|
is_favorite = Column(Boolean, default=False, nullable=False)
|
|
favorite_sort_order = Column(Integer, default=0, nullable=False)
|
|
is_compact = Column(Boolean, default=False, nullable=False)
|
|
|
|
product = relationship("Product", back_populates="quick_options")
|
|
|
|
|
|
class ProductIngredient(Base):
|
|
__tablename__ = "product_ingredients"
|
|
|
|
id = Column(Integer, primary_key=True, index=True)
|
|
product_id = Column(Integer, ForeignKey("products.id"), nullable=False)
|
|
name = Column(String, nullable=False)
|
|
extra_cost = Column(Float, default=0.0)
|
|
is_favorite = Column(Boolean, default=False, nullable=False)
|
|
favorite_sort_order = Column(Integer, default=0, nullable=False)
|
|
# When True renders as half-width card on the PWA
|
|
is_compact = Column(Boolean, default=False, nullable=False)
|
|
# Optional group/folder this ingredient belongs to
|
|
group_id = Column(Integer, ForeignKey("product_modifier_groups.id"), nullable=True)
|
|
|
|
product = relationship("Product", back_populates="ingredients")
|
|
|
|
|
|
class ProductPreferenceSet(Base):
|
|
__tablename__ = "product_preference_sets"
|
|
|
|
id = Column(Integer, primary_key=True, index=True)
|
|
product_id = Column(Integer, ForeignKey("products.id"), nullable=False)
|
|
name = Column(String, nullable=False)
|
|
default_choice_id = Column(Integer, nullable=True)
|
|
# JSON: {name, default_choice_index, choices:[{name,extra_cost,is_default}]}
|
|
# Shared sub-set shown for all choices that don't have disables_subset=True
|
|
shared_subset = Column(Text, nullable=True)
|
|
is_favorite = Column(Boolean, default=False, nullable=False)
|
|
favorite_sort_order = Column(Integer, default=0, nullable=False)
|
|
# Optional group/folder this preference set belongs to
|
|
group_id = Column(Integer, ForeignKey("product_modifier_groups.id"), nullable=True)
|
|
# When True, waiter can select multiple choices instead of just one
|
|
allow_multi_select = Column(Boolean, default=False, nullable=False)
|
|
# When True, each selected choice can have a quantity (x2, x3, …)
|
|
allow_choice_quantity = Column(Boolean, default=False, nullable=False)
|
|
|
|
product = relationship("Product", back_populates="preference_sets")
|
|
choices = relationship("ProductPreferenceChoice", back_populates="set", cascade="all, delete-orphan")
|
|
|
|
|
|
class ProductPreferenceChoice(Base):
|
|
__tablename__ = "product_preference_choices"
|
|
|
|
id = Column(Integer, primary_key=True, index=True)
|
|
set_id = Column(Integer, ForeignKey("product_preference_sets.id"), nullable=False)
|
|
name = Column(String, nullable=False)
|
|
extra_cost = Column(Float, default=0.0)
|
|
# JSON array of sub-choice objects: [{name, extra_cost, is_default, is_compact}]
|
|
# Per-choice inline sub-preference shown only when this choice is selected.
|
|
sub_choices = Column(Text, nullable=True)
|
|
# When True this choice hides the set-level shared_subset on the PWA.
|
|
disables_subset = Column(Boolean, default=False, nullable=False)
|
|
# When True renders as half-width card on the PWA
|
|
is_compact = Column(Boolean, default=False, nullable=False)
|
|
|
|
set = relationship("ProductPreferenceSet", back_populates="choices")
|
|
|
|
|
|
class ProductCategory(Base):
|
|
"""A product listed in a category, with its position in that category's list (D-23)."""
|
|
__tablename__ = "product_categories"
|
|
|
|
product_id = Column(Integer, ForeignKey("products.id", ondelete="CASCADE"), primary_key=True)
|
|
category_id = Column(Integer, ForeignKey("categories.id", ondelete="CASCADE"), primary_key=True, index=True)
|
|
sort_order = Column(Integer, nullable=False, default=0)
|
|
|
|
product = relationship("Product", back_populates="memberships")
|
|
category = relationship("Category", back_populates="memberships")
|
|
|