From 3c5751025cdead623c6f734db711c6518b1c6b97 Mon Sep 17 00:00:00 2001 From: bonamin Date: Fri, 13 Nov 2026 12:28:13 +0200 Subject: [PATCH] refactor(backend): drop the legacy products.printer_zone_id and its print fallbacks Routing has been by prep zone only since v1, which nulled this column on every start, so the four fallbacks in printer_service.py never ran. Revision 0012 drops the column (its unnamed FK goes with the batch rebuild). The unused legacy GET /api/kds/items keeps its single 'no zone' group. Product create/update/export/import no longer mention it. Scheduled in plan step 1.0 for the catalogue rework (1.13). Co-Authored-By: Claude Opus 5.5 --- .../0012_drop_product_printer_zone.py | 35 ++++++++++++++ local_backend/models/printer.py | 1 - local_backend/models/product.py | 2 - local_backend/routers/data_transfer.py | 3 -- local_backend/routers/kds.py | 2 +- local_backend/routers/products.py | 2 - local_backend/schemas/product.py | 3 -- local_backend/services/printer_service.py | 46 +------------------ 8 files changed, 37 insertions(+), 57 deletions(-) create mode 100644 local_backend/migrations/versions/0012_drop_product_printer_zone.py diff --git a/local_backend/migrations/versions/0012_drop_product_printer_zone.py b/local_backend/migrations/versions/0012_drop_product_printer_zone.py new file mode 100644 index 0000000..0273558 --- /dev/null +++ b/local_backend/migrations/versions/0012_drop_product_printer_zone.py @@ -0,0 +1,35 @@ +"""drop legacy products.printer_zone_id (plan step 1.13; noted in 1.0) + +Hand-edited: the FK is unnamed, so it can't be dropped by name in SQLite batch mode; the +batch rebuild of `products` drops it together with the column. Routing has been by prep +zone only since v1 (v1 nulled this column on every start), so no data is lost. + +Revision ID: 0012_drop_product_printer_zone +Revises: 0011_order_item_names +Create Date: 2026-11-13 12:27:34.824101 +""" +from alembic import op +import sqlalchemy as sa + + +revision = '0012_drop_product_printer_zone' +down_revision = '0011_order_item_names' +branch_labels = None +depends_on = None + + +def upgrade() -> None: + # ### commands auto generated by Alembic - please adjust! ### + with op.batch_alter_table('products', schema=None) as batch_op: + batch_op.drop_column('printer_zone_id') + + # ### end Alembic commands ### + + +def downgrade() -> None: + # ### commands auto generated by Alembic - please adjust! ### + with op.batch_alter_table('products', schema=None) as batch_op: + batch_op.add_column(sa.Column('printer_zone_id', sa.INTEGER(), nullable=True)) + batch_op.create_foreign_key('fk_products_printer_zone_id', 'printers', ['printer_zone_id'], ['id']) + + # ### end Alembic commands ### diff --git a/local_backend/models/printer.py b/local_backend/models/printer.py index 6aac8de..d9e5bbc 100644 --- a/local_backend/models/printer.py +++ b/local_backend/models/printer.py @@ -16,7 +16,6 @@ class Printer(Base): duplicates = Column(Integer, default=0, nullable=False) codepage_n = Column(Integer, default=29, nullable=False) - products = relationship("Product", back_populates="printer_zone") print_logs = relationship("PrintLog", back_populates="printer") print_jobs = relationship("PrintJob", back_populates="printer") prep_zones = relationship("PrepZone", secondary="prep_zone_printers", back_populates="printers") diff --git a/local_backend/models/product.py b/local_backend/models/product.py index 1e58042..387ba96 100644 --- a/local_backend/models/product.py +++ b/local_backend/models/product.py @@ -33,7 +33,6 @@ class Product(Base): is_available = Column(Boolean, default=True, nullable=False) # "active" | "archived" — archived products are kept for order history but hidden from active use lifecycle_status = Column(String, default="active", nullable=False) - printer_zone_id = Column(Integer, ForeignKey("printers.id"), nullable=True) image_url = Column(String, nullable=True) sort_order = Column(Integer, default=0, nullable=False) @@ -70,7 +69,6 @@ class Product(Base): fiscal_vat_group_id = Column(Integer, nullable=True) # machine VAT group ID (1-based integer) category = relationship("Category", back_populates="products") - printer_zone = relationship("Printer", back_populates="products") 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") diff --git a/local_backend/routers/data_transfer.py b/local_backend/routers/data_transfer.py index 7fce957..9851932 100644 --- a/local_backend/routers/data_transfer.py +++ b/local_backend/routers/data_transfer.py @@ -56,7 +56,6 @@ def _serialize_product(p: Product) -> dict: return { "name": p.name, "base_price": p.base_price, "is_available": p.is_available, "lifecycle_status": p.lifecycle_status, "sort_order": p.sort_order, - "printer_zone_id": None, # always stripped on export "quick_options": quick_options, "options": options, "ingredients": ingredients, "preference_sets": preference_sets, } @@ -179,7 +178,6 @@ def import_catalog(payload: dict, db: Session = Depends(get_db), user: User = De existing.lifecycle_status = prod_data.get("lifecycle_status", "active") existing.sort_order = prod_data.get("sort_order", 0) existing.category_id = category_id - existing.printer_zone_id = None db.flush() product = existing else: @@ -190,7 +188,6 @@ def import_catalog(payload: dict, db: Session = Depends(get_db), user: User = De lifecycle_status=prod_data.get("lifecycle_status", "active"), sort_order=prod_data.get("sort_order", 0), category_id=category_id, - printer_zone_id=None, ) db.add(product) db.flush() diff --git a/local_backend/routers/kds.py b/local_backend/routers/kds.py index ac266f9..595754c 100644 --- a/local_backend/routers/kds.py +++ b/local_backend/routers/kds.py @@ -771,7 +771,7 @@ def kds_items( for item in items: product = item.product - zone_id = product.printer_zone_id if product else None + zone_id = None # legacy per-product printer is gone (routing is by prep zone); one group, as before zkey = _zone_key(zone_id) if zkey not in zones: zones[zkey] = {"zone_id": zone_id, "zone_name": _zone_name(zone_id), "items": []} diff --git a/local_backend/routers/products.py b/local_backend/routers/products.py index efe2041..b25ea39 100644 --- a/local_backend/routers/products.py +++ b/local_backend/routers/products.py @@ -360,8 +360,6 @@ def update_product(product_id: int, body: ProductUpdate, db: Session = Depends(g ) for field, value in scalar_fields.items(): setattr(product, field, value) - # Always clear legacy per-product printer — routing is now via prep zones only - product.printer_zone_id = None # Update prep zones if provided if body.prep_zone_ids is not None: zones = db.query(PrepZone).filter(PrepZone.id.in_(body.prep_zone_ids)).all() diff --git a/local_backend/schemas/product.py b/local_backend/schemas/product.py index 2cbb216..d3b29c0 100644 --- a/local_backend/schemas/product.py +++ b/local_backend/schemas/product.py @@ -317,7 +317,6 @@ class ProductBase(BaseModel): unit_type: str = "piece" is_available: bool = True lifecycle_status: str = "active" - printer_zone_id: Optional[int] = None # kept for backward compat; use prep_zone_ids instead prep_zone_ids: List[int] = [] sort_order: int = 0 # Xenia Connect — digital menu overrides @@ -359,7 +358,6 @@ class ProductUpdate(BaseModel): unit_type: Optional[str] = None is_available: Optional[bool] = None lifecycle_status: Optional[str] = None - printer_zone_id: Optional[int] = None prep_zone_ids: Optional[List[int]] = None sort_order: Optional[int] = None quick_options: Optional[List[ProductQuickOptionCreate]] = None @@ -426,7 +424,6 @@ class ProductOut(ProductBase): 'unit_type': getattr(data, 'unit_type', 'piece') or 'piece', 'is_available': data.is_available, 'lifecycle_status': data.lifecycle_status, - 'printer_zone_id': data.printer_zone_id, '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, diff --git a/local_backend/services/printer_service.py b/local_backend/services/printer_service.py index 70f2d28..726760e 100644 --- a/local_backend/services/printer_service.py +++ b/local_backend/services/printer_service.py @@ -447,7 +447,6 @@ def send_test_order_print(ip: str, port: int, db: Session, line_width: int = LIN name = _FAKE_NAMES[self._filter_id] unit_type = "piece" category_id = None - printer_zone_id = None return _P() return _orig_query(self._model).filter(self._model.id == self._filter_id).first() @@ -1409,17 +1408,6 @@ def _do_print_cancellation(order_id: int, item_ids: List[int], db: Session): printer_job[job_key][1].append(item) item_routed = True - # Fallback to legacy printer_zone_id - if not item_routed and product.printer_zone_id: - printer = db.query(Printer).filter(Printer.id == product.printer_zone_id, Printer.is_active == True).first() - if printer: - key = (printer.id, 0, item.id) - if key not in seen: - seen.add(key) - job_key = (printer.id, 0) - if job_key not in printer_job: - printer_job[job_key] = (printer, [], None) - printer_job[job_key][1].append(item) div = load_divider_style(db) @@ -1859,21 +1847,7 @@ def _do_route_and_print(order_id: int, item_ids: List[int], db: Session, ephemer if product.prep_zones and all((z.auto_print or 'none') == 'none' for z in product.prep_zones): printed_item_ids_no_print.add(item.id) continue - # Fall back to legacy printer_zone_id (single printer, 1 copy, no zone formatting) - if product.printer_zone_id: - printer = db.query(Printer).filter(Printer.id == product.printer_zone_id, Printer.is_active == True).first() - if printer: - key = (printer.id, 1, 0, item.id) - if key not in seen: - seen.add(key) - job_key = (printer.id, 1, 0) - if job_key not in printer_job: - printer_job[job_key] = ([], None) - printer_job[job_key][0].append(item) - else: - unzoned.append(item) - else: - unzoned.append(item) + unzoned.append(item) if unzoned: logger.warning("order %s has %d item(s) with no prep zone/printer — skipped", order_id, len(unzoned)) @@ -1915,16 +1889,6 @@ def _do_route_and_print(order_id: int, item_ids: List[int], db: Session, ephemer job_svc_entries[job_key] = [] job_svc_entries[job_key].append(entry) entry_routed = True - # Fallback: legacy printer_zone_id - if not entry_routed and getattr(product, 'printer_zone_id', None): - pr = db.query(Printer).filter(Printer.id == product.printer_zone_id, Printer.is_active == True).first() - if pr: - job_key = (pr.id, 1, 0) - if job_key not in printer_job: - printer_job[job_key] = ([], None) - if job_key not in job_svc_entries: - job_svc_entries[job_key] = [] - job_svc_entries[job_key].append(entry) printed_item_ids: set[int] = set() @@ -2071,14 +2035,6 @@ def print_service_items_sync(order: Order, service_entries: List[dict], db: Sess printer_job[job_key] = {'printer': pr, 'copies': copies, 'zone': z, 'entries': []} printer_job[job_key]['entries'].append({'product': product, 'quantity': quantity}) - # Fallback: legacy printer_zone_id - if not product.prep_zones and product.printer_zone_id: - pr = db.query(Printer).filter(Printer.id == product.printer_zone_id, Printer.is_active == True).first() - if pr: - job_key = (pr.id, 1, 0) - if job_key not in printer_job: - printer_job[job_key] = {'printer': pr, 'copies': 1, 'zone': None, 'entries': []} - printer_job[job_key]['entries'].append({'product': product, 'quantity': quantity}) for job_key, job in printer_job.items(): pr = job['printer']