from sqlalchemy import Column, Integer, String, Text, Float, DateTime, ForeignKey, UniqueConstraint from sqlalchemy.sql import func from database import Base class OnlineOrder(Base): __tablename__ = "online_orders" __table_args__ = ( UniqueConstraint("site_id", "public_ref", name="uq_online_orders_site_ref"), # S.2: per site UniqueConstraint("tracking_token", name="uq_online_orders_tracking_token"), ) id = Column(Integer, primary_key=True, index=True) site_id = Column(Integer, ForeignKey("sites.id"), nullable=False, index=True) # Human-readable reference, unique per site (e.g. "ORD-0042") public_ref = Column(String, nullable=False) # the customer's tracking page: random, unguessable (S.2; the ref alone was ambiguous across sites) tracking_token = Column(String, nullable=True) order_type = Column(String, nullable=False) # dine_in | takeaway | delivery | preorder # Customer info customer_name = Column(String, nullable=False) customer_phone = Column(String, nullable=True) customer_address = Column(Text, nullable=True) # required for delivery customer_notes = Column(Text, nullable=True) table_label = Column(String, nullable=True) # the QR menu's table, as the customer gave it sales_point = Column(String, nullable=True) # the site's sales point (phase 3S, S.2) # Order content — JSON array of ordered items items_json = Column(Text, nullable=False) # Financials subtotal = Column(Float, nullable=False) delivery_fee = Column(Float, default=0.0, nullable=False) total = Column(Float, nullable=False) # Status lifecycle: # pending_acceptance → accepted | rejected # accepted → preparing → ready | out_for_delivery → delivered status = Column(String, default="pending_acceptance", nullable=False, index=True) rejection_reason = Column(String, nullable=True) # Sync state — local backend polls for these synced_to_local = Column(Integer, default=0, nullable=False) # 0=no, 1=yes local_order_id = Column(Integer, nullable=True) # set after local creates it created_at = Column(DateTime(timezone=True), server_default=func.now()) updated_at = Column(DateTime(timezone=True), server_default=func.now(), onupdate=func.now())