def create(self, contract_date=None): self.contract_date = DateConverter.to_date(contract_date, "%d.%m.%Y") contract = DatabaseManager.get_contract(self.school.id, self.program.id) if contract: self.contract_no = contract.contract_no self.contract_year = contract.contract_year app.logger.warning( "[%s] Contract already exists [%s, %s]. Only update contract date and regenerate", __class__.__name__, self.school.nick, self.program.id) contract.update(self.contract_date) self.generate() else: self.contract_no = str( DatabaseManager.get_next_contract_no(self.program.id)) self.contract_year = DateConverter.get_year() app.logger.info( "[%s] Adding new contract: school_nick %s: city %s | current_date %s, | contract_no %s" "| contract_year %s", __class__.__name__, self.school.nick, self.school.city, self.contract_date, self.contract_no, self.contract_year) if self.create_new(): self.generate() else: app.logger.error( "[%s] Something went wrong when creating a contract", __class__.__name__)
def fill_data_in_table(self, contract, no, is_annex): record_dict = dict() if not is_annex: record_dict['no'] = str(no) record_dict['contract_info'] = "{}/{}".format( contract.contract_no, contract.contract_year) record_dict['school_name'] = contract.school.name record_dict['school_nip'] = contract.school.nip record_dict['school_address'] = contract.school.address record_dict['school_city'] = contract.school.city record_dict['school_regon'] = contract.school.regon record_dict['school_phone'] = contract.school.phone record_dict['school_email'] = contract.school.email else: record_dict['no'] = RegisterCreator.CELL_TO_MERGE_MARK record_dict['school_name'] = RegisterCreator.CELL_TO_MERGE_MARK record_dict['school_nip'] = RegisterCreator.CELL_TO_MERGE_MARK record_dict['school_address'] = RegisterCreator.CELL_TO_MERGE_MARK record_dict['school_city'] = RegisterCreator.CELL_TO_MERGE_MARK record_dict['school_regon'] = RegisterCreator.CELL_TO_MERGE_MARK record_dict['school_phone'] = RegisterCreator.CELL_TO_MERGE_MARK record_dict['school_email'] = RegisterCreator.CELL_TO_MERGE_MARK validity_date = DateConverter.to_date(contract.validity_date) record_dict['contract_info'] = "{}/{}".format( contract.contract_no.split("_")[0], contract.contract_year) record_dict['annex_info'] = "{}*".format( DateConverter.to_string(validity_date, "%d-%m-%Y")) record_dict['kids_milk'] = str(contract.dairy_products) record_dict['kids_fruitveg'] = str(contract.fruitVeg_products) self.records_to_merge.append(record_dict)
def _preapre_str_from_weeks(weeks): return ",".join([ "{0}-{1}".format( DateConverter.to_string(week.start_date, "%d.%m"), DateConverter.to_string(week.end_date, "%d.%m.%Y")) for week in weeks ])
def prepare(self): self.dict_data['school_name'] = self.school.name self.dict_data['school_nip'] = self.school.nip self.dict_data['school_regon'] = self.school.regon self.dict_data['school_address'] = self.school.address self.dict_data['city'] = self.school.city self.dict_data['date_day'] = DateConverter.two_digits( self.sign_date.day) self.dict_data['date_month'] = DateConverter.two_digits( self.sign_date.month) self.dict_data['date_year'] = str(self.sign_date.year)
def program_form(program_id=INVALID_ID): config_parser.read(config_file) if program_id == INVALID_ID: id_of_program_being_added = DatabaseManager.id_of_program_being_added(FILL_STR) if not id_of_program_being_added: default_date = DateConverter.to_string(datetime.datetime.now()) new_program = Program(semester_no=FILL_STR, school_year=FILL_STR, fruitVeg_price=FILL_STR, dairy_price=FILL_STR, start_date=DateConverter.to_date(default_date), end_date=DateConverter.to_date(default_date), dairy_min_per_week=FILL_STR, fruitVeg_min_per_week=FILL_STR, dairy_amount=FILL_STR, fruitVeg_amount=FILL_STR) if DatabaseManager.add_row(new_program): return redirect(url_for('program_form', program_id=new_program.id)) else: return redirect(url_for('program_form', program_id=INVALID_ID)) else: return redirect(url_for('program_form', program_id=id_of_program_being_added.id)) current_program = DatabaseManager.get_program(program_id) school_without_summary = None summaries_data = None if is_current_program_set(current_program, config_parser): schools_with_contract = DatabaseManager.get_all_schools_with_contract(current_program.id) available_summary = get_current_summaries(current_program.id) summaries_data = dict() if not available_summary: school_without_summary = schools_with_contract else: school_without_summary = [school for school in schools_with_contract if not any( [DatabaseManager.get_application(school.id, summary.id) for summary in available_summary])] summaries_data = { k: ", ".join( [application.school.nick for application in DatabaseManager.get_school_with_summary(k.id) if application.school]) for k in available_summary} if request.method == 'POST': data_to_update = {"semester_no": empty_if_none(request.form["semester_no"]), "school_year": empty_if_none(request.form["school_year"]), "fruitVeg_price": empty_if_none(request.form["fruitVeg_price"]), "dairy_price": empty_if_none(request.form["dairy_price"]), "start_date": empty_if_none(DateConverter.to_date(request.form["start_date"])), "end_date": empty_if_none(DateConverter.to_date(request.form["end_date"])), "dairy_min_per_week": empty_if_none(request.form["dairy_min_per_week"]), "fruitVeg_min_per_week": empty_if_none(request.form["fruitVeg_min_per_week"]), "dairy_amount": empty_if_none(request.form["dairy_amount"]), "fruitVeg_amount": empty_if_none(request.form["fruitVeg_amount"])} program_id = DatabaseManager.update_program_data(current_program, **data_to_update) return redirect(url_for('program_form', program_id=program_id)) return render_template("program_form.html", Program=current_program, Schools=school_without_summary, Summary=summaries_data)
def add_week(program_id): current_program = request.args.get('program') if not current_program: current_program = DatabaseManager.get_program(program_id) if request.method == 'POST': if not request.form['week_no'] or not request.form['start_date'] or not request.form['end_date']: flash('Uzupełnij wszystkie dane', 'error') else: new_week = Week(week_no=request.form['week_no'], start_date=DateConverter.to_date(request.form['start_date']), end_date=DateConverter.to_date(request.form['end_date']), program_id=current_program.id) if DatabaseManager.add_row(new_week): return redirect(url_for("program_form", program_id=current_program.id)) return render_template("add_week.html", program_id=program_id, program=current_program)
def school_form_edit_annex(school_id, annex_id): if not session.get('program_id'): return redirect(url_for('program')) school = DatabaseManager.get_school(school_id) annex: Contract = DatabaseManager.get_existing_annex(annex_id) if request.method == 'POST': date = DateConverter.to_date(request.form['contract_date']) date_valid = DateConverter.to_date(request.form['validity_date']) fruitVeg_products = request.form['fruitVeg_products'] dairy_products = request.form['dairy_products'] if annex: annex.update(date, date_valid, fruitVeg_products, dairy_products) AnnexCreator(annex.school_id, annex.program_id, annex).generate() return redirect(url_for('school_form', school_id=school_id)) return render_template("add_annex_form.html", school=school, annex=annex)
def get_existing_record(current_date, school_id, product_id): if not product_id: return None product_type = Product.query.filter(Product.id == product_id).one().type cdate = DateConverter.to_date(current_date, pattern=DATABASE_DATE_PATTERN) assert(product_type == ProductType.DAIRY or product_type == ProductType.FRUIT_VEG) return Record.query.join(Contract).join(Product).filter(Record.date.like(cdate)).filter(Contract.school_id == school_id)\ .filter(Product.type.like(product_type)).first()
def generate_many(records_to_merge, file_prefix=None): if not records_to_merge: return out_dir = config_parser.get('Directories', 'record_all') doc = DocumentCreator.start_doc_gen( config_parser.get('DocTemplates', 'record'), out_dir) records_to_merge_list = [ record.doc_data for record in records_to_merge if (isinstance(record, RecordCreator) and record.doc_data) ] if not records_to_merge_list: return date = records_to_merge[0].date if not isinstance(date, datetime.date): date = DateConverter.to_date(date) gen_date = records_to_merge[0].generation_date if not isinstance(gen_date, datetime.date): gen_date = DateConverter.to_date(gen_date) assert (gen_date and date) # @TODO remove later if not gen_date or not date: return out_dir = path.join(out_dir, DateConverter.to_string(gen_date, '%Y_%m_%d'), DateConverter.to_string(date)) if not path.exists(out_dir): makedirs(out_dir) out_file_name = ALL_RECORDS_DOC_NAME if file_prefix and file_prefix != ALL_RECORDS_DOC_NAME: counter = 1 for filename in listdir(out_dir): if file_prefix in filename: counter += 1 out_file_name = "{}_{}{}".format(out_file_name, file_prefix, counter) out_doc = path.join(out_dir, "{}.docx".format(out_file_name)) doc.merge_pages(records_to_merge_list) app.logger.info("[%s] Created merge docx of records in dir: [%s]", RecordCreator.__qualname__, out_doc) DocumentCreator.end_doc_gen(doc, out_doc, out_dir, gen_pdf=True)
def _prepare_data_for_doc(self): self.doc_data['city'] = self.contract.school.city self.doc_data['current_date'] = DateConverter.to_string(self.date) self.doc_data['name'] = self.contract.school.name self.doc_data['address'] = self.contract.school.address self.doc_data['nip'] = self.contract.school.nip self.doc_data['regon'] = self.contract.school.regon self.doc_data['email'] = self.contract.school.email self.doc_data['kids_no'] = str(self._get_kids_no()) self.doc_data['product_name'] = self.product.get_name_mapping() self.doc_data['record_title'] = self.product.get_record_title_mapping()
def generate(self): self.document.merge( city=self.school.city, date=DateConverter.to_string(self.contract_date, "%d.%m.%Y"), no=str(self.contract_no), year=str(self.contract_year), semester=self.program.get_current_semester(), name=self.school.name, address=self.school.address, nip=self.school.nip, regon=self.school.regon, representant=self.school.responsible_person if not self.ommit_representat else FILL_BY_SCHOOL, email=self.school.email, program_start_date=DateConverter.to_string(self.program.start_date, "%d.%m.%Y"), program_end_date=DateConverter.to_string(self.program.end_date, "%d.%m.%Y"), nip_additional=self.school.representative_nip if self.school.representative_nip else "-", name_additional=self.school.representative if self.school.representative else "-", regon_additional=self.school.representative_regon if self.school.representative_regon else "-", giving_weeks=ContractCreator._preapre_str_from_weeks( DatabaseManager.get_weeks(self.program.id))) doc_contract_name = "Umowa_{0}_{1}.docx".format( self.contract_no, self.contract_year) created_doc_name = DocumentCreator.generate(self, doc_contract_name) doc_contract_name_copy = path.join( self.main_contract_dir, "{0}_Umowa_{1}_{2}.docx".format(self.school.nick, self.contract_no, self.contract_year)) try: DocumentCreator.copy_to_path(created_doc_name, doc_contract_name_copy) # DocumentCreator.copy_to_path(created_doc_name.replace("docx", "pdf"), doc_contract_name_copy.replace("docx", "pdf")) #TODO problem with generating pdf numbering is wrong except: app.logger.error("Could not copy files [%s]", created_doc_name)
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 __init__(self, program_id, current_date, school_id, product_id, generation_date=""): self.program_id = program_id self.date = DateConverter.to_date(current_date, pattern=DATABASE_DATE_PATTERN) self.state = RecordState.NOT_DELIVERED self.contract = DatabaseManager.get_current_contract( school_id, self.program_id, self.date) self.product = DatabaseManager.get_product(product_id) self.doc_data = dict() self.generation_date = DateConverter.to_date( generation_date) if generation_date else datetime.date.today() output_directory = self.contract.school.generate_directory_name( config_parser.get('Directories', 'record')) DocumentCreator.__init__(self, config_parser.get('DocTemplates', 'record'), output_directory) DatabaseManager.__init__(self) self._prepare_data_for_doc()
def generate(self): self.document.merge(city=self.doc_data['city'], current_date=self.doc_data['current_date'], name=self.doc_data['name'], address=self.doc_data['address'], nip=self.doc_data['nip'], regon=self.doc_data['regon'], email=self.doc_data['email'], kids_no=self.doc_data['kids_no'], product_name=self.doc_data['product_name'], record_title=self.doc_data['record_title']) DocumentCreator.generate(self, "WZ_{0}_{1}.docx".format( DateConverter.to_string(self.date), self.product.get_type_mapping()), gen_pdf=True)
def generate_contracts(program_id): current_program = DatabaseManager.get_program(program_id) contract_date = request.form["contract_date"] if not is_current_program_set(current_program, config_parser): flash('Żeby wygenerować umowy ustaw program jako aktulany', 'error') elif not contract_date or contract_date == "dd.mm.rrrr": flash('Uzupełnij datę zawarcia umów', 'error') else: if session.get('program_id') == program_id: all_schols = DatabaseManager.get_all_schools() for school in all_schols: if school.nick != FILL_STR_SCHOOL: # Dont create contract for school with not full date filled new_contract = ContractCreator(school, session.get('program_id')) new_contract.create(DateConverter.to_date(contract_date)) flash("Umowy zostały wygenerowane pomyślnie", 'success') else: flash('Możesz wygnerować umowy tylko dla akutalnie wybranego programu', 'error') return render_template("program_form.html", Program=current_program)
def school_form_add_contract(school_id=INVALID_ID): if not session.get('program_id'): return redirect(url_for('program')) school = DatabaseManager.get_school(school_id) school_contract = DatabaseManager.get_contract(school_id, session.get('program_id')) if request.method == 'POST': date = DateConverter.to_date(request.form['contract_date']) fruitVeg_products = request.form['fruitVeg_products'] dairy_products = request.form['dairy_products'] if not school_contract: if not date: flash('Uzupełnij datę zawarcia umowy', 'error') else: new_contract = ContractCreator(school, session.get('program_id')) new_contract.create(date) return redirect(url_for('school_form', school_id=school_id)) if school_contract: school_contract.update(date, date, fruitVeg_products, dairy_products) return redirect(url_for('school_form', school_id=school_id)) return render_template("add_contract_form.html", school=school, contract=school_contract)
def __prepare_data(self): try: self.__are_records_inconsistent() except InconsistentAnnexWzError as e: self.inconsistent_records_error = f"Szkoła: {self.school.nick}.\n{e}", e.get_log_level( ) for data in self.tmp_data_to_rename: data.prepare() self.product_data.update(self.fruits.get()) self.product_data.update(self.vegs.get()) self.product_data.update(self.dairy.get()) self.product_data[ 'max_kids_perWeeks_fruitVeg'] = self.frutiVeg.get_kids() self.product_data[ 'max_kids_perWeeks_milk'] = self.dairySummary.get_kids() self.__prepare_fruit_data() self.__prepare_dairy_data() for record in DatabaseManager.get_records(self.program_id, self.school.id, ProductType.FRUIT_VEG, self.summary.weeks): record_dict = dict() record_dict['date_vegFruit'] = DateConverter.to_string( record.date, "%d.%m.%Y") record_dict['kids_vegFruit'] = str(record.product_no) record_dict['vegFruit'] = record.Product.get_name_mapping() self.records_to_merge_vegFruit.append(record_dict) for record in DatabaseManager.get_records(self.program_id, self.school.id, ProductType.DAIRY, self.summary.weeks): record_dict = dict() record_dict['date_milk'] = DateConverter.to_string( record.date, "%d.%m.%Y") record_dict['kids_milk'] = str(record.product_no) record_dict['milk'] = record.Product.get_name_mapping() self.records_to_merge_milk.append(record_dict) self.sum_product_vegFruit = self.__sum_product( self.records_to_merge_vegFruit, 'kids_vegFruit') self.sum_product_milk = self.__sum_product(self.records_to_merge_milk, 'kids_milk') fruit_veg = self.product_data['veg_all'] + self.product_data[ 'fruit_all'] if self.sum_product_vegFruit != fruit_veg: app.logger.error( "Value of fruitVeg product from 5 and 5A does not match! School: {0} 5: {1} " " 5A: {2} -- ABORT generating".format( self.school.nick, fruit_veg, self.sum_product_vegFruit)) return False if self.sum_product_milk != self.product_data['dairy_all']: app.logger.error( "Value of dairy product from 5 and 5A does not match! School: {0} 5: {1} " " 5A: {2} -- ABORT generating".format( self.school.nick, self.product_data['dairy_all'], self.sum_product_milk)) return False return True
def get_week_by_date(date): rdate = date if isinstance(date, datetime.datetime) else DateConverter.to_date(date, pattern="%Y-%m-%d %H:%M:%S") return Week.query.filter(Week.start_date <= rdate).filter(Week.end_date >= rdate).first()
def get_daily_records(program_id, current_date, generation_date=None): cdate = DateConverter.to_date(current_date, pattern=DATABASE_DATE_PATTERN) gen_date = DateConverter.to_date(generation_date, pattern=DATABASE_DATE_PATTERN) if generation_date else None if gen_date: return Record.query.filter(Product.program_id.like(program_id)).filter(Record.date.like(cdate)).filter(Record.generation_date.like(gen_date)).all() return Record.query.filter(Product.program_id.like(program_id)).filter(Record.date.like(cdate)).all()
def get_dates(program_id, week_no): week = Week.query.filter(Week.program_id==program_id).filter(Week.week_no==week_no).one() return "{0}-{1}\n{2}".format(DateConverter.to_string(week.start_date, "%d.%m"), DateConverter.to_string(week.end_date, "%d.%m"), DateConverter.to_string(week.end_date, "%Y"))
def convert_start_end_to_string(self): return DateConverter.to_string(self.end_date)
def convert_start_date_to_string(self): return DateConverter.to_string(self.start_date)
def convert_validity_date_to_string(self): return DateConverter.to_string(self.validity_date)
def is_annex(validity_date, school_id, program_id): rdate = validity_date if not isinstance(validity_date, datetime.datetime) else DateConverter.to_string(validity_date) return Contract.query.join(Contract.school).filter(Contract.program_id == program_id).filter(School.id==school_id)\ .filter(Contract.validity_date == DateConverter.to_date(rdate)).all()
def contract_date(self): if self.contracts: return DateConverter.to_string(self.contracts[0].contract_date) else: return DateConverter.to_string(datetime.datetime.now())
def convert_date_to_string(self): return DateConverter.to_string(self.contract_date)
def __init__(self, school, sign_date): super().__init__() self.school = school self.sign_date = DateConverter.to_date(sign_date)