"""The menu board's content (plan step 5.4b): which products a menu slide lists, in its own order, with live prices and availability. A menu slide picks products three ways, combined (routers/signage.py MenuContent): select = "all" every active product category_ids whole categories (and their sub-categories): products added to them later join product_ids single products, from any category minus `exclude_ids`. System products (the delivery fee line) and archived products never show. With a `sales_point`, products not sold there are left out and prices are that sales point's, right now (services/pos_cart.unit_prices, the same rules the counter uses); without one there are no prices. The order is the board's own (`category_order`, `product_order`), never the waiters' or the counter's: listed ids first in that order, the rest after them in the catalogue's order. """ from __future__ import annotations from core import menu_languages, sales_points def _descendants(cats_by_parent: dict, roots: list[int]) -> set[int]: out, stack = set(), list(roots) while stack: c = stack.pop() if c in out: continue out.add(c) stack.extend(cats_by_parent.get(c, [])) return out def _rank(order: list[int]): pos = {v: i for i, v in enumerate(order or [])} return lambda v: (0, pos[v]) if v in pos else (1, 0) def resolve(db, content: dict, now=None, keep_hidden: bool = False) -> dict: """{"groups": [{"category": {id, name} | None, "items": [...]}], "count": n} for a menu slide. keep_hidden (the editor's list, 2026-11-21): products hidden from the board (`exclude_ids`) stay in the list marked `"hidden": true` (greyed there, shown again with a click); `count` and the TV leave them out.""" from models.product import Category, Product, ProductCategory from services import media as media_store from services.pos_cart import unit_prices cats = db.query(Category).all() by_id = {c.id: c for c in cats} by_parent: dict = {} for c in cats: by_parent.setdefault(c.parent_id, []).append(c.id) q = db.query(Product).filter(Product.lifecycle_status == "active", Product.system_key.is_(None)) if content.get("select") == "all": products = q.all() else: wanted_cats = _descendants(by_parent, [i for i in content.get("category_ids") or [] if i in by_id]) in_cats = {pid for (pid,) in db.query(ProductCategory.product_id).filter(ProductCategory.category_id.in_(wanted_cats))} if wanted_cats else set() ids = in_cats | set(content.get("product_ids") or []) products = q.filter(Product.id.in_(ids)).all() if ids else [] excluded = set(content.get("exclude_ids") or []) if not keep_hidden: products = [p for p in products if p.id not in excluded] point = content.get("sales_point") if point: products = [p for p in products if sales_points.is_sold_at(p, point)] if content.get("sold_out") == "hide": products = [p for p in products if p.is_available] # the catalogue's order inside each product's main category (the fallback after the board's own) member_order = {(m.category_id, m.product_id): m.sort_order for m in db.query(ProductCategory).all()} cat_rank = _rank(content.get("category_order")) prod_rank = _rank(content.get("product_order")) def cat_key(cid): c = by_id.get(cid) return (cat_rank(cid), c.sort_order if c else 0, c.name.lower() if c else "") def prod_key(p): return (prod_rank(p.id), member_order.get((p.primary_category_id, p.id), p.sort_order or 0), (p.name or "").lower()) prices = dict(zip([p.id for p in products], unit_prices(db, products, point, now=now))) if point and products else {} badges = {int(k): v for k, v in (content.get("badges") or {}).items() if str(k).isdigit()} lang = content.get("language") # 6.4: the board's language; None = the base texts def text(row, base, field): return menu_languages.texts(base, menu_languages.of(row), field).get(lang, base) if lang else base def item(p): image = media_store.image_of(p.media) if content.get("show_photos") else None return {"id": p.id, "name": text(p, p.digital_name or p.name, "name"), "description": text(p, p.digital_description or p.description or None, "description") if content.get("show_descriptions") else None, "price": round(prices[p.id], 2) if p.id in prices else None, "available": bool(p.is_available), "image": image["card"] if image else None, "badge": badges.get(p.id), **({"hidden": True} if p.id in excluded else {})} if content.get("group_by_category", True): groups: dict = {} for p in products: groups.setdefault(p.primary_category_id, []).append(p) out = [{"category": {"id": cid, "name": text(by_id[cid], by_id[cid].name, "name")} if cid in by_id else None, "items": [item(p) for p in sorted(ps, key=prod_key)]} for cid, ps in sorted(groups.items(), key=lambda kv: cat_key(kv[0]))] else: ordered = sorted(products, key=lambda p: (prod_rank(p.id), cat_key(p.primary_category_id), prod_key(p))) out = [{"category": None, "items": [item(p) for p in ordered]}] if ordered else [] return {"groups": out, "count": len([p for p in products if p.id not in excluded])}