Order lines had no index on their order, orders none on their day/customer/status, and each line's product was loaded to tell a fee apart: with ~3000 orders Customers took ~2 s and Reports -> Days ~15 s. Migration 0038 adds the indexes (models carry index=True), Order.items loads with selectin, sales.lines looks the fee products up once, cancellations and prep-zones load their orders with the lines. Days 15 s -> 0.2 s (a month), customers 2 s -> 0.1 s. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
63 lines
3.0 KiB
Python
63 lines
3.0 KiB
Python
from sqlalchemy import Column, Integer, String, Boolean, Float, ForeignKey, Text
|
|
from core.timeutil import UTCDateTime, utcnow
|
|
from sqlalchemy.orm import relationship
|
|
from database import Base
|
|
|
|
|
|
|
|
|
|
class Tab(Base):
|
|
__tablename__ = "tabs"
|
|
|
|
id = Column(Integer, primary_key=True, index=True)
|
|
customer_id = Column(Integer, ForeignKey("customers.id"), nullable=False, index=True)
|
|
status = Column(String, default="open", nullable=False) # open | closed | forgiven
|
|
opened_at = Column(UTCDateTime, default=utcnow)
|
|
closed_at = Column(UTCDateTime, nullable=True)
|
|
closed_by_id = Column(Integer, ForeignKey("users.id"), nullable=True)
|
|
notes = Column(Text, nullable=True)
|
|
|
|
customer = relationship("Customer", foreign_keys=[customer_id])
|
|
closed_by = relationship("User", foreign_keys=[closed_by_id])
|
|
entries = relationship("TabEntry", back_populates="tab", cascade="all, delete-orphan")
|
|
payments = relationship("TabPayment", back_populates="tab", cascade="all, delete-orphan")
|
|
|
|
|
|
class TabEntry(Base):
|
|
"""What went ON the tab — a deferred charge."""
|
|
__tablename__ = "tab_entries"
|
|
|
|
id = Column(Integer, primary_key=True, index=True)
|
|
tab_id = Column(Integer, ForeignKey("tabs.id"), nullable=False, index=True)
|
|
order_id = Column(Integer, ForeignKey("orders.id"), nullable=True, index=True)
|
|
order_item_id = Column(Integer, ForeignKey("order_items.id"), nullable=True, index=True)
|
|
amount = Column(Float, nullable=False) # snapshot at time of tab entry
|
|
description = Column(Text, nullable=True) # auto-generated summary
|
|
created_by_id = Column(Integer, ForeignKey("users.id"), nullable=False)
|
|
created_at = Column(UTCDateTime, default=utcnow)
|
|
# 6.13c: the business day and the shift (of whoever did it, if on one) it happened in
|
|
business_day_id = Column(Integer, ForeignKey("business_days.id"), nullable=True, index=True)
|
|
shift_id = Column(Integer, ForeignKey("waiter_shifts.id"), nullable=True, index=True)
|
|
|
|
tab = relationship("Tab", back_populates="entries")
|
|
created_by = relationship("User", foreign_keys=[created_by_id])
|
|
|
|
|
|
class TabPayment(Base):
|
|
"""What came OFF the tab — a payment reducing the balance."""
|
|
__tablename__ = "tab_payments"
|
|
|
|
id = Column(Integer, primary_key=True, index=True)
|
|
tab_id = Column(Integer, ForeignKey("tabs.id"), nullable=False, index=True)
|
|
amount = Column(Float, nullable=False)
|
|
payment_method = Column(String, nullable=True) # cash | card | other
|
|
received_by_id = Column(Integer, ForeignKey("users.id"), nullable=False)
|
|
notes = Column(Text, nullable=True)
|
|
created_at = Column(UTCDateTime, default=utcnow)
|
|
# 6.13c: the business day and the shift (of whoever did it, if on one) it happened in
|
|
business_day_id = Column(Integer, ForeignKey("business_days.id"), nullable=True, index=True)
|
|
shift_id = Column(Integer, ForeignKey("waiter_shifts.id"), nullable=True, index=True)
|
|
|
|
tab = relationship("Tab", back_populates="payments")
|
|
received_by = relationship("User", foreign_keys=[received_by_id])
|