"""sales points: orders.sales_point, products.sold_at; digital prices become price modifiers (plan phase 3S, step S.1) - orders.sales_point, from source / online_order_type / order_type (core/sales_points.for_order) - products.sold_at (JSON list), from digital_visible (hidden online → sold in the venue only) - digital_price / digital_discount → item price modifiers with the condition "sales point is the QR menu or online" (a price difference, then a stackable % discount), so every place that prices asks the same engine - the order_channel condition's old values (pos, takeaway, online, qr) → sales points, in price modifiers and deals - drops products.digital_visible, digital_price, digital_discount Revision ID: 0017_sales_points Revises: 0016_refunds Create Date: 2026-11-15 21:00:00 """ import json from alembic import op import sqlalchemy as sa from core.timeutil import UTCDateTime, utcnow revision = '0017_sales_points' down_revision = '0016_refunds' branch_labels = None depends_on = None ALL = ["in_store", "in_store_takeaway", "qr_table", "online_takeaway", "online_delivery", "online_preorder"] IN_PERSON = ["in_store", "in_store_takeaway"] DIGITAL = ["qr_table", "online_takeaway", "online_delivery", "online_preorder"] LEGACY = {"pos": ["in_store"], "takeaway": ["in_store_takeaway"], "online": ["online_takeaway", "online_delivery", "online_preorder"], "qr": ["qr_table"]} def _remap(params: str) -> str: try: p = json.loads(params or "{}") except ValueError: return params out = [] for v in p.get("channels") or []: for sp in ([v] if v in ALL else LEGACY.get(v, [])): if sp not in out: out.append(sp) p["channels"] = out return json.dumps(p) def upgrade() -> None: conn = op.get_bind() with op.batch_alter_table('orders', schema=None) as batch_op: batch_op.add_column(sa.Column('sales_point', sa.String(), nullable=False, server_default='in_store')) conn.execute(sa.text(""" update orders set sales_point = case when source = 'online' then case online_order_type when 'delivery' then 'online_delivery' when 'takeaway' then 'online_takeaway' when 'preorder' then 'online_preorder' else 'qr_table' end when order_type in ('takeaway', 'delivery') then 'in_store_takeaway' else 'in_store' end""")) with op.batch_alter_table('products', schema=None) as batch_op: batch_op.add_column(sa.Column('sold_at', sa.Text(), nullable=False, server_default=json.dumps(ALL))) conn.execute(sa.text("update products set sold_at = :v where digital_visible = 0"), {"v": json.dumps(IN_PERSON)}) modifiers = sa.table('price_modifiers', sa.column('id', sa.Integer), sa.column('name', sa.String), sa.column('description', sa.Text), sa.column('is_active', sa.Integer), sa.column('is_favorite', sa.Integer), sa.column('allow_stack', sa.Integer), sa.column('sort_order', sa.Integer), sa.column('scope', sa.String), sa.column('item_id', sa.Integer), sa.column('action_type', sa.String), sa.column('action_value', sa.Float), sa.column('created_at', UTCDateTime())) conditions = sa.table('price_modifier_conditions', sa.column('modifier_id', sa.Integer), sa.column('condition_type', sa.String), sa.column('params', sa.Text)) now = utcnow() rows = conn.execute(sa.text("select id, name, base_price, digital_price, digital_discount from products " "where digital_price is not null or coalesce(digital_discount, 0) > 0")).fetchall() for pid, name, base, price, discount in rows: made = [] if price is not None and abs(price - (base or 0.0)) > 0.0001: made.append((f"Online price · {name}", "add_amount", round(price - (base or 0.0), 4), 0)) if discount and discount > 0: made.append((f"Online discount · {name}", "add_percent", -float(discount), 1)) for label, action, value, stack in made: mid = conn.execute(modifiers.insert().values( name=label, description="From the product's digital menu price (moved in phase 3S)", is_active=1, is_favorite=0, allow_stack=stack, sort_order=0, scope="item", item_id=pid, action_type=action, action_value=value, created_at=now)).lastrowid conn.execute(conditions.insert().values(modifier_id=mid, condition_type="order_channel", params=json.dumps({"channels": DIGITAL}))) for table in ("price_modifier_conditions", "deal_conditions"): for cid, params in conn.execute(sa.text(f"select id, params from {table} where condition_type = 'order_channel'")).fetchall(): conn.execute(sa.text(f"update {table} set params = :p where id = :i"), {"p": _remap(params), "i": cid}) with op.batch_alter_table('products', schema=None) as batch_op: batch_op.drop_column('digital_visible') batch_op.drop_column('digital_price') batch_op.drop_column('digital_discount') def downgrade() -> None: with op.batch_alter_table('products', schema=None) as batch_op: batch_op.add_column(sa.Column('digital_discount', sa.Float(), nullable=False, server_default='0')) batch_op.add_column(sa.Column('digital_price', sa.Float(), nullable=True)) batch_op.add_column(sa.Column('digital_visible', sa.Integer(), nullable=False, server_default='1')) op.get_bind().execute(sa.text("update products set digital_visible = 0 where sold_at not like '%qr_table%' " "and sold_at not like '%online_%'")) with op.batch_alter_table('products', schema=None) as batch_op: batch_op.drop_column('sold_at') with op.batch_alter_table('orders', schema=None) as batch_op: batch_op.drop_column('sales_point')