feat(backend): order items keep the product and category they were sold under; reports use them
Platform v2 step 1.12 (APPS_PLATFORM D-18, CS-4). - order_items.product_name (NOT NULL), category_id, category_name (revision 0011, back-filled from the current catalogue). A before_flush hook in models/order.py fills them for every new item, whoever creates it; an item created with product_name set is left alone (importer). - Reports read the snapshots: order history, shift/printer/prep-zone breakdowns, cancellations, product and category performance (grouped by the category as sold, newest name in the period wins), and the printed product/category reports. Catalogue listings (unsold products in 'full' mode, meta endpoints, waste-only rows) keep live names on purpose. - Order detail and OrderItemOut expose product_name/category_id/category_name next to the live . Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -75,6 +75,13 @@ class OrderItem(Base):
|
||||
added_by = Column(Integer, ForeignKey("users.id"), nullable=False)
|
||||
quantity = Column(Float, nullable=False)
|
||||
unit_price = Column(Float, nullable=False) # price snapshot at time of order
|
||||
# Name snapshots (D-18, CS-4): what the item was called, and its category, when it was
|
||||
# ordered. Reports read these, so renaming or moving a product never rewrites history.
|
||||
# Filled automatically for every new item by the before_flush hook at the end of this
|
||||
# file; never updated afterwards. category_id is a plain id (categories can be deleted).
|
||||
product_name = Column(String, nullable=False)
|
||||
category_id = Column(Integer, nullable=True, index=True)
|
||||
category_name = Column(String, nullable=True)
|
||||
selected_options = Column(Text, nullable=True) # JSON array of option ids
|
||||
removed_ingredients = Column(Text, nullable=True) # JSON array of ingredient ids
|
||||
notes = Column(Text, nullable=True)
|
||||
@@ -204,3 +211,28 @@ class OrderDiscount(Base):
|
||||
order = relationship("Order", back_populates="discounts")
|
||||
item = relationship("OrderItem")
|
||||
applied_by_user = relationship("User")
|
||||
|
||||
|
||||
# ── Name snapshots for new order items (D-18) ────────────────────────────────
|
||||
from sqlalchemy import event as _event # noqa: E402
|
||||
from sqlalchemy.orm import Session as _Session # noqa: E402
|
||||
|
||||
|
||||
@_event.listens_for(_Session, "before_flush")
|
||||
def _snapshot_item_names(session, flush_context, instances):
|
||||
"""Every new OrderItem gets the product's current name and category, whoever creates
|
||||
it (waiter, counter, online orders, deals, demo seed). An item created with a
|
||||
product_name already set is left alone: the caller snapshots everything itself
|
||||
(the v1 importer, plan 7.1)."""
|
||||
from models.product import Category, Product
|
||||
for obj in session.new:
|
||||
if not isinstance(obj, OrderItem) or obj.product_name:
|
||||
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 not None and obj.category_name is None:
|
||||
cat = session.get(Category, obj.category_id)
|
||||
obj.category_name = cat.name if cat else None
|
||||
|
||||
|
||||
Reference in New Issue
Block a user