class Contract(db.Model): __table_args__ = {'extend_existing': True} id = db.Column(db.Integer, primary_key=True) contract_no = db.Column(db.String(80), nullable=False) contract_year = db.Column(db.Integer, nullable=False) contract_date = db.Column(db.DateTime, nullable=False) validity_date = db.Column(db.DateTime, nullable=False) fruitVeg_products = db.Column(db.Integer, nullable=False) dairy_products = db.Column(db.Integer, nullable=False) is_annex = db.Column(db.Boolean, nullable=False, default=0) school_id = db.Column(db.Integer, db.ForeignKey('school.id'), nullable=False) school = db.relationship('School', backref=db.backref( 'contracts', lazy=True, order_by='Contract.validity_date.desc()')) program_id = db.Column(db.Integer, db.ForeignKey('program.id'), nullable=False) program = db.relationship('Program', backref=db.backref('contracts', lazy=True)) def update(self, contract_date, validity_date=None, fruitVeg_products=None, dairy_products=None): self.contract_date = DateConverter.to_date(contract_date) if validity_date: self.validity_date = validity_date if fruitVeg_products: self.fruitVeg_products = fruitVeg_products if dairy_products: self.dairy_products = dairy_products db.session.commit() def convert_date_to_string(self): return DateConverter.to_string(self.contract_date) def convert_validity_date_to_string(self): return DateConverter.to_string(self.validity_date) def __str__(self): return f"Contract_{self.contract_no}_{self.contract_year}" __table_args__ = (db.UniqueConstraint('validity_date', 'school_id'), )
class Record(db.Model): __table_args__ = {'extend_existing': True} id = db.Column(db.Integer, primary_key=True) date = db.Column(db.DateTime, nullable=False) generation_date = db.Column(db.DateTime) state = db.Column(db.Enum(RecordState), nullable=False, default=RecordState.NOT_DELIVERED) product_id = db.Column(db.Integer, db.ForeignKey('product.id'), nullable=False) product = db.relationship('Product', backref=db.backref('records', lazy=True)) contract_id = db.Column(db.Integer, db.ForeignKey('contract.id'), nullable=False) contract = db.relationship('Contract', backref=db.backref( 'records', lazy=True, order_by='Contract.validity_date.desc()', cascade="all, delete-orphan")) week_id = db.Column(db.Integer, db.ForeignKey('week.id'), nullable=False) week = db.relationship('Week', backref=db.backref('records', lazy=True)) def __str__(self): return f"Week no {self.week.week_no} school {self.contract.school.nick} productType {self.product.type}" __table_args__ = (db.UniqueConstraint('date', 'product_id', 'contract_id'), ) def set_to_delivered(self): self.state = RecordState.DELIVERED db.session.commit() def update_product(self, product_id): self.product_id = product_id db.session.commit() def is_dairy(self): return self.product.type == ProductType.DAIRY def is_fruitVeg(self): return self.product.type == ProductType.FRUIT_VEG
class Application(db.Model): __table_args__ = {'extend_existing': True} id = db.Column(db.Integer, primary_key=True) apple = db.Column(db.Integer, default=0) pear = db.Column(db.Integer, default=0) plum = db.Column(db.Integer, default=0) strawberry = db.Column(db.Integer, default=0) carrot = db.Column(db.Integer, default=0) radish = db.Column(db.Integer, default=0) pepper = db.Column(db.Integer, default=0) tomato = db.Column(db.Integer, default=0) kohlrabi = db.Column(db.Integer, default=0) milk = db.Column(db.Integer, default=0) yoghurt = db.Column(db.Integer, default=0) kefir = db.Column(db.Integer, default=0) cheese = db.Column(db.Integer, default=0) fruit_all = db.Column(db.Integer, default=0) veg_all = db.Column(db.Integer, default=0) dairy_all = db.Column(db.Integer, default=0) max_kids_perWeeks_fruitVeg = db.Column(db.Integer, default=0) max_kids_perWeeks_milk = db.Column(db.Integer, default=0) juice = db.Column(db.Integer, default=0) summary_id = db.Column(db.Integer, db.ForeignKey('summary.id'), nullable=False) summary = db.relationship('Summary', backref=db.backref( 'application', lazy=True, order_by='Contract.validity_date.desc()')) school_id = db.Column(db.Integer, db.ForeignKey('school.id'), nullable=False) school = db.relationship('School', backref=db.backref( 'application', lazy=True, order_by='Contract.validity_date.desc()')) __table_args__ = (db.UniqueConstraint('summary_id', 'school_id'), )
class Week(db.Model): id = db.Column(db.Integer, primary_key=True) week_no = db.Column(db.Integer, nullable=False) start_date = db.Column(db.DateTime, nullable=False) end_date = db.Column(db.DateTime, nullable=False) program_id = db.Column(db.Integer, db.ForeignKey('program.id'), nullable=False) program = db.relationship('Program', backref=db.backref('weeks', lazy=True)) def convert_start_date_to_string(self): return DateConverter.to_string(self.start_date) def convert_start_end_to_string(self): return DateConverter.to_string(self.end_date) db.UniqueConstraint('week_no', 'program_id') __table_args__ = {'extend_existing': True}
class Product(db.Model): __table_args__ = {'extend_existing': True} id = db.Column(db.Integer, primary_key=True) name = db.Column(db.Enum(ProductName), nullable=False) type = db.Column(db.Enum(ProductType), nullable=False) min_amount = db.Column(db.Integer, nullable=False) program_id = db.Column(db.Integer, db.ForeignKey('program.id'), nullable=False) program = db.relationship('Program', backref=db.backref('products', lazy=True)) #@TODO remove language dependency def get_name_mapping(self): name = fruit_veg_mapping.get(self.name, None) if name: return name else: name = dairy_mapping.get(self.name, "") return name def get_record_title_mapping(self): if self.type == ProductType.DAIRY: return "Mleko i przetwory mleczne" if self.type == ProductType.FRUIT_VEG: return "Warzywa i owoce " def get_type_mapping(self): if self.type == ProductType.DAIRY: return "nb" if self.type == ProductType.FRUIT_VEG: return "wo" def is_dairy(self): return self.type == ProductType.DAIRY def is_fruit_veg(self): return self.type == ProductType.FRUIT_VEG
class Summary(db.Model): __table_args__ = {'extend_existing': True} id = db.Column(db.Integer, primary_key=True) no = db.Column(db.Integer, nullable=False) year = db.Column(db.Integer, nullable=False) kids_no = db.Column(db.Integer, default=0) school_no = db.Column(db.Integer, default=0) apple = db.Column(db.Integer, default=0) pear = db.Column(db.Integer, default=0) plum = db.Column(db.Integer, default=0) strawberry = db.Column(db.Integer, default=0) juice = db.Column(db.Integer, default=0) carrot = db.Column(db.Integer, default=0) radish = db.Column(db.Integer, default=0) pepper = db.Column(db.Integer, default=0) tomato = db.Column(db.Integer, default=0) kohlrabi = db.Column(db.Integer, default=0) school_no_milk = db.Column(db.Integer, default=0) kids_no_milk = db.Column(db.Integer, default=0) milk = db.Column(db.Integer, default=0) yoghurt = db.Column(db.Integer, default=0) kefir = db.Column(db.Integer, default=0) cheese = db.Column(db.Integer, default=0) fruitVeg_income = db.Column(db.Float, default=0) milk_income = db.Column(db.Float, default=0) is_first = db.Column(db.Boolean) weeks = db.Column(db.PickleType) program_id = db.Column(db.Integer, db.ForeignKey('program.id'), nullable=False) program = db.relationship('Program', backref=db.backref('summary', lazy=True)) number_of_weeks = 0 def set_number_of_weeks(self, weeks_no): if self.number_of_weeks != 0 and self.number_of_weeks != len(weeks_no): raise ValueError( f"weeks_no: {weeks_no} != {self.number_of_weeks}. " f"For one summary all schools must have the same number of weeks." ) self.weeks = weeks_no self.number_of_weeks = len(weeks_no) return True def __fruit_list(self): return [ ProductHandler(self.apple, 5, float(self.program.fruitVeg_price)), ProductHandler(self.plum, 5, float(self.program.fruitVeg_price)), ProductHandler(self.pear, 5, float(self.program.fruitVeg_price)), ProductHandler(self.strawberry, 5, float(self.program.fruitVeg_price)), ProductHandler(self.juice, 5, float(self.program.fruitVeg_price)) ] def __veg_list(self): return [ ProductHandler(self.carrot, 8, float(self.program.fruitVeg_price)), ProductHandler(self.tomato, 5, float(self.program.fruitVeg_price)), ProductHandler(self.radish, 8, float(self.program.fruitVeg_price)), ProductHandler(self.kohlrabi, 8, float(self.program.fruitVeg_price)), ProductHandler(self.pepper, 8, float(self.program.fruitVeg_price)) ] def __dairy_list(self): return [ ProductHandler(self.milk, 5, float(self.program.dairy_price)), ProductHandler(self.yoghurt, 5, float(self.program.dairy_price)), ProductHandler(self.kefir, 5, float(self.program.dairy_price)), ProductHandler(self.cheese, 5, float(self.program.dairy_price)) ] def get_from_fruit_list(self, product: ProductName): if product == ProductName.APPLE: return self.__fruit_list()[0] if product == ProductName.PLUM: return self.__fruit_list()[1] if product == ProductName.PEAR: return self.__fruit_list()[2] if product == ProductName.STRAWBERRY: return self.__fruit_list()[3] if product == ProductName.JUICE: return self.__fruit_list()[4] if product == ProductName.CARROT: return self.__veg_list()[0] if product == ProductName.TOMATO: return self.__veg_list()[1] if product == ProductName.RADISH: return self.__veg_list()[2] if product == ProductName.KOHLRABI: return self.__veg_list()[3] if product == ProductName.PEPPER: return self.__veg_list()[4] def get_from_diary_list(self, product: ProductName): if product == ProductName.MILK: return self.__dairy_list()[0] if product == ProductName.YOGHURT: return self.__dairy_list()[1] if product == ProductName.KEFIR: return self.__dairy_list()[2] if product == ProductName.CHEESE: return self.__dairy_list()[3] @staticmethod def calculate_income(products_list: List[ProductHandler]): return sum([product.calculate_brutto() for product in products_list]) @staticmethod def calculate_netto(products_list: List[ProductHandler]): return sum([product.calculate_netto() for product in products_list]) @staticmethod def calculate_vat(products_list: List[ProductHandler]): return sum([product.calculate_vat() for product in products_list]) def get_fruit_amount(self): return sum([product.amount for product in self.__fruit_list()]) @convert_to_float def get_fruit_vat(self): return Summary.calculate_vat(self.__fruit_list()) @convert_to_float def get_fruit_netto(self): return Summary.calculate_netto(self.__fruit_list()) @convert_to_float def get_fruit_income(self): return self.calculate_income(self.__fruit_list()) def get_veg_amount(self): return sum([product.amount for product in self.__veg_list()]) def get_veg_vat(self): return Summary.calculate_vat(self.__veg_list()) @convert_to_float def get_veg_netto(self): return Summary.calculate_netto(self.__veg_list()) @convert_to_float def get_veg_income(self): return self.calculate_income(self.__veg_list()) @convert_to_float def get_fruit_veg_income(self): return self.calculate_income(self.__fruit_list() + self.__veg_list()) def get_dairy_amount(self): return sum([product.amount for product in self.__dairy_list()]) @convert_to_float def get_dairy_vat(self): return Summary.calculate_vat(self.__dairy_list()) @convert_to_float def get_dairy_netto(self): return Summary.calculate_netto(self.__dairy_list()) @convert_to_float def get_dairy_income(self): return self.calculate_income(self.__dairy_list()) def __str__(self): return f"[{self.id}]Summary {self.get_application_no()} program {self.program_id}" def get_application_no(self): return "{0}/{1}/{2}".format(self.program.semester_no, self.no, self.program.school_year) __table_args__ = (db.UniqueConstraint('no', 'year', 'program_id'), )