class Transaction(db.Model): __tablename__ = 'transaction' transaction_id = db.Column(db.Integer, primary_key=True, autoincrement=True) date = db.Column(db.DateTime) loyalty_id = db.Column(db.Integer, db.ForeignKey('customer.loyalty_id')) store_id = db.Column(db.Integer, db.ForeignKey('store.store_id')) tax_year = db.Column(db.Integer, nullable=False) def __init__(self, date, loyalty_id, store_id, tax_year): self.date = date self.loyalty_id = loyalty_id self.store_id = store_id self.tax_year = tax_year
class RawMaterialStock(db.Model): id = db.Column(db.Integer, primary_key=True) location_id = db.Column(db.Integer, db.ForeignKey(Location.id)) raw_material_id = db.Column(db.Integer, db.ForeignKey(RawMaterial.id)) available_stock = db.Column(db.Integer, db.CheckConstraint("available_stock>=0"), nullable=False) location = db.relationship(Location, foreign_keys=[location_id]) raw_material = db.relationship(RawMaterial, foreign_keys=[raw_material_id]) time_created = db.Column(db.TIMESTAMP, server_default=db.func.now()) time_updated = db.Column(db.TIMESTAMP, onupdate=db.func.now(), server_default=db.func.now()) db.UniqueConstraint( "location_id", "raw_material_id", name="raw_material_stock_location_id_raw_material_id_uindex", )
class Ticket(db.Model): __tablename__ = "ticket" id = db.Column(db.Integer, primary_key=True, autoincrement=True) registered_on = db.Column(db.TIMESTAMP, nullable=False, default=datetime.datetime.utcnow) changed_on = db.Column(db.TIMESTAMP, nullable=False, default=datetime.datetime.utcnow) text = db.Column(db.String(200)) theme = db.Column(db.String(50)) email = db.Column(db.String(255), nullable=False) status_id = db.Column(db.Integer, db.ForeignKey(TicketStatus.id)) status = db.relationship('TicketStatus', foreign_keys='Ticket.status_id') comments = db.relationship('Comment', back_populates="ticket", lazy=True) def __init__(self, theme, email, text=''): self.email = email self.text = text self.theme = theme self.status_id = 1 def __repr__(self): return str({"id": str(self.id), "text": self.text, "theme": self.theme}) def json(self): """ JSONify object :return: """ return json.dumps( { "id": self.id, "registered_on": str(self.registered_on), "changed_on": str(self.changed_on), "text": self.text, "theme": self.theme, "email": self.email, "status": TicketStatus.query.filter_by(id=self.status_id).first().json(), "comments": [json.loads(c.json()) for c in self.comments] }) def change_status_check(self, new_status_id): """ Тикет создается в статусе “открыт”, может перейти в “отвечен” или “закрыт”, из отвечен в “ожидает ответа” или TODO(? - как из "отвечен" в "ожидает ответа"). На что может поменяться “ожидает ответа” “закрыт”, статус “закрыт” финальный (нельзя изменить статус или добавить комментарий) :param new_status_id: :return: """ new_status = TicketStatus.query.filter_by(id=new_status_id).first() if self.status.name == "opened" and new_status.name in ["answered", "closed"]: return True elif self.status.name == "answered" and new_status.name in ["waiting", "closed"]: return True return False
class ProductManufacturing(db.Model): id = db.Column(db.Integer, primary_key=True) to_location_id = db.Column(db.Integer(), db.ForeignKey(Location.id), nullable=False) product_id = db.Column(db.Integer(), db.ForeignKey(Product.id), nullable=False) description = db.Column(db.TEXT) to_location = db.relationship(Location, foreign_keys=[to_location_id]) product = db.relationship(Product, foreign_keys=[product_id]) batch_size = db.Column(db.Integer(), db.CheckConstraint("batch_size >= 0"), nullable=False) time_created = db.Column(db.TIMESTAMP, server_default=db.func.now()) time_updated = db.Column(db.TIMESTAMP, onupdate=db.func.now(), server_default=db.func.now()) def __str__(self): return "{}".format(self.id)
class TransactionLine(db.Model): __tablename__ = 'transaction_line' transaction_line_id = db.Column(db.Integer, primary_key=True, autoincrement=True) item_type_id = db.Column(db.Integer, db.ForeignKey('item_type.item_type_id')) unit_type_id = db.Column(db.Integer, db.ForeignKey('unit_type.unit_type_id')) quantity = db.Column(db.Integer, nullable=False) description = db.Column(db.String(500)) transaction_id = db.Column(db.Integer, db.ForeignKey('transaction.transaction_id')) def __init__(self, item_type_id, unit_type_id, quantity, description, transaction_id): self.item_type_id = item_type_id self.unit_type_id = unit_type_id self.quantity = quantity self.description = description self.transaction_id = transaction_id
class RawMaterialMovement(db.Model): id = db.Column(db.Integer, primary_key=True) movement_date = db.Column(db.Date, server_default=db.func.now()) from_location_id = db.Column(db.Integer(), db.ForeignKey(Location.id)) to_location_id = db.Column(db.Integer(), db.ForeignKey(Location.id)) raw_material_id = db.Column(db.Integer(), db.ForeignKey(RawMaterial.id), nullable=False) description = db.Column(db.TEXT) from_location = db.relationship(Location, foreign_keys=[from_location_id]) to_location = db.relationship(Location, foreign_keys=[to_location_id]) raw_material = db.relationship(RawMaterial, foreign_keys=[raw_material_id]) qty = db.Column(db.Integer(), db.CheckConstraint("qty >= 0"), nullable=False) time_created = db.Column(db.TIMESTAMP, server_default=db.func.now()) time_updated = db.Column(db.TIMESTAMP, onupdate=db.func.now(), server_default=db.func.now()) def __str__(self): return "{}".format(self.id)
class ProductRawMaterial(db.Model): id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(100), unique=True, nullable=False) raw_material_id = db.Column(db.Integer(), db.ForeignKey(RawMaterial.id), nullable=False) raw_material = db.relationship(RawMaterial, foreign_keys=[raw_material_id]) product_id = db.Column(db.Integer(), db.ForeignKey(Product.id), nullable=False) product = db.relationship(Product, foreign_keys=[product_id]) raw_material_quantity = db.Column(db.Integer(), nullable=False) description = db.Column(db.TEXT) time_created = db.Column(db.TIMESTAMP, server_default=db.func.now()) time_updated = db.Column(db.TIMESTAMP, onupdate=db.func.now(), server_default=db.func.now()) def __str__(self): return "{}".format(self.name) def __repr__(self): return "{}: {}".format(self.id, self.__str__())
class Customer(User): __tablename__ = "customer" __mapper_args__ = {"polymorphic_identity": "CUST"} user_id = db.Column(db.Integer, db.ForeignKey("user.user_id"), primary_key=True) loyalty_id = db.Column(db.Integer, nullable=False, unique=True) def __init__(self, loyalty_id, password, first_name, last_name): super().__init__("CUST", password, first_name, last_name) self.loyalty_id = loyalty_id @staticmethod def find_and_authenticate(loyalty_id, password): user = Customer.query.filter_by(loyalty_id=loyalty_id).first() if not user: return None elif user.is_authentic(password): return user else: return None
class Employee(User): __tablename__ = "employee" __mapper_args__ = {"polymorphic_identity": "EMPL"} user_id = db.Column(db.Integer, db.ForeignKey('user.user_id'), primary_key=True) employee_id = db.Column(db.Integer, nullable=False, unique=True) def __init__(self, employee_id, password, first_name, last_name): super().__init__("EMPL", password, first_name, last_name) self.employee_id = employee_id @staticmethod def find_and_authenticate(employee_id, password): user = Employee.query.filter_by(employee_id=employee_id).first() if not user: return None elif user.is_authentic(password): return user else: return None
class Comment(db.Model): __tablename__ = "comment" id = db.Column(db.Integer, primary_key=True, autoincrement=True) registered_on = db.Column(db.TIMESTAMP, nullable=False, default=datetime.datetime.utcnow) text = db.Column(db.String(200)) email = db.Column(db.String(255), nullable=False) ticket_id = db.Column(db.Integer, db.ForeignKey(Ticket.id)) ticket = db.relationship('Ticket', foreign_keys='Comment.ticket_id') def json(self): """ JSONify object :return: """ return json.dumps({ "id": self.id, "registered_on": str(self.registered_on), "text": self.text, "email": self.email })