class PaymentsDemand(db.Model, SerializerMixin): __tablename__ = "payments_demands" serialize_rules = ( "-session", "-table", ) id = db.Column(db.Integer, primary_key=True) created_on = db.Column(db.DateTime, default=db.func.now()) table_id = db.Column(db.Integer, db.ForeignKey("customer_tables.id"), nullable=False) table = db.relationship("CustomerTable", back_populates="payments") pay_method = db.Column(db.Enum(PaymentType), nullable=False, default=PaymentType.money) session_id = db.Column(db.Integer, db.ForeignKey("tables_sessions.id"), nullable=False) session = db.relationship("TableSession", back_populates="payment") #demands = db.relationship("Demand", backref="paymentsDemands") # 1 to n #customer_tables = db.relationship("CustomerTable", backref="paymentsDemands") #payment = db.relationship("CustomerPayment", back_populates="paymentsDemand") def total_value(self): return self.session.get_total() def create(self): db.session.add(self) db.session.commit() return self
class TableSession(db.Model, SerializerMixin): __tablename__ = "tables_sessions" serialize_rules = ("-demands",) id = db.Column(db.Integer, primary_key=True) created_on = db.Column(db.DateTime, default=db.func.now()) url = db.Column(db.String(80), unique=True, nullable=False) demands = db.relationship("Demand") closed = db.Column(db.Boolean, default=False) table_id = db.Column( db.Integer, db.ForeignKey("customer_tables.id"), nullable=False ) table = db.relationship("CustomerTable", back_populates="sessions") payment = db.relationship("PaymentsDemand") def get_total(self): value = 0 for demand in self.demands: value += demand.total_value() return self.value def create(self): if self.url == None: self.url = self.generate_url() db.session.add(self) db.session.commit() return self def generate_url(self): part = str(uuid1()) return f"{self.table_id}-{part}"
class CustomerTable(db.Model, SerializerMixin): __tablename__ = "customer_tables" serialize_rules = ( "-sessions", "-qrcode", "-payment", ) id = db.Column(db.Integer, primary_key=True) qrcode = db.Column(db.Text, nullable=True) identifier = db.Column(db.String(80), unique=True, nullable=False) sessions = db.relationship("TableSession") payments = db.relationship("PaymentsDemand") def create(self): db.session.add(self) db.session.commit() url = qrcode_table_url_builder(self.id) self.qrcode = qrcode_builder(url) db.session.add(self) db.session.commit() return self
class Item(db.Model, SerializerMixin): __tablename__ = "items" serialize_rules = ( "-menus", "-demands", ) # prevent recursion error on to_dict id = db.Column(db.Integer, primary_key=True) menus = db.relationship("Menu", secondary=association_table, back_populates="items") name = db.Column(db.String(80), nullable=False, unique=True, index=True) description = db.Column(db.String(140), nullable=True) value = db.Column(db.Float(2), nullable=False) demands = db.relationship( "Demand", cascade="all, delete-orphan", ) def create(self): db.session.add(self) db.session.commit() return self
class Demand(db.Model, SerializerMixin): __tablename__ = "demands" id = db.Column(db.Integer, primary_key=True) quantity = db.Column(db.Integer, nullable=False, default=1) status = db.Column(db.Enum(DemandStatus), nullable=False, default=DemandStatus.waiting) item_id = db.Column(db.Integer, db.ForeignKey("items.id"), nullable=False) item = db.relationship("Item", back_populates="demands") customer = db.Column(db.String(80), unique=False, nullable=False) session_id = db.Column(db.Integer, db.ForeignKey("tables_sessions.id"), nullable=False) table_session = db.relationship("TableSession", back_populates="demands") def create(self): db.session.add(self) db.session.commit() return self def total_value(self): return self.item.value * self.quantity
class Menu(db.Model, SerializerMixin): __tablename__ = "menus" serialize_rules = ("-items",) # prevent recursion error on to_dict id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(60), nullable=False) description = db.Column(db.String(140), nullable=True) is_daily = db.Column(db.Boolean, default=False) items = db.relationship( "Item", secondary=association_table, back_populates="menus", ) def create(self): db.session.add(self) db.session.commit() return self