feat(backend): a product can be listed in several categories, with one primary category

Platform v2 step 1.13 (APPS_PLATFORM D-23).
- product_categories(product_id, category_id, sort_order); products.category_id becomes
  products.primary_category_id (revision 0013 moves the data: one listing per product at its
  old position). services/catalog.py keeps the rule "the primary is one of its categories"
  and fixes up products when a category is deleted.
- Products API: category_ids + primary_category_id in and out, plus categories
  [{category_id, sort_order}]; GET /api/products/?category_id= lists a category's products in
  that category's order; PUT /reorder with category_id orders within a category. category_id
  stays as dev scaffolding (section 3.3): output = primary, input = "move the primary".
- Primary category: ticket grouping and prep-zone category order, the order-item category
  snapshot (1.12), report filters for waste-only rows and /meta/products.
- Every listing: price modifier / deal / min-quantity category targets, deal free choices,
  the QR menu snapshot (product in each category it's listed in; images pushed once).
- Catalogue export v2: each category lists all its products with "primary" and
  "category_sort_order"; import restores listings and order. v1 files are refused (D-21).
- New errors catalog.unknown_category, catalog.primary_not_listed.
This commit is contained in:
2026-11-13 12:39:02 +02:00
parent 3c5751025c
commit 6d42be95f8
20 changed files with 445 additions and 56 deletions
+2 -2
View File
@@ -230,8 +230,8 @@ def _snapshot_item_names(session, flush_context, instances):
continue
product = session.get(Product, obj.product_id) if obj.product_id is not None else None
obj.product_name = product.name if product else f"#{obj.product_id}"
if obj.category_id is None and product is not None and product.category_id is not None:
obj.category_id = product.category_id
if obj.category_id is None and product is not None and product.primary_category_id is not None:
obj.category_id = product.primary_category_id # the PRIMARY category (D-23)
if obj.category_id is not None and obj.category_name is None:
cat = session.get(Category, obj.category_id)
obj.category_name = cat.name if cat else None
+33 -3
View File
@@ -17,7 +17,10 @@ class Category(Base):
# sub-categories only: if True, the accordion section is expanded by default on the PWA
auto_expanded = Column(Boolean, default=False, nullable=False)
products = relationship("Product", back_populates="category")
# 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")
@@ -28,7 +31,10 @@ class Product(Base):
id = Column(Integer, primary_key=True, index=True)
name = Column(String, nullable=False)
description = Column(Text, nullable=True)
category_id = Column(Integer, ForeignKey("categories.id"), 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
@@ -68,7 +74,18 @@ class Product(Base):
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)
category = relationship("Category", back_populates="products")
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")
@@ -186,3 +203,16 @@ class ProductPreferenceChoice(Base):
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")