def pdf_file(invoname, download): if not invoname: return redirect('/home') fdao = InvoiceDAO() if not fdao.exist(fdao.where('name', invoname)): return redirect('/home') invoice = fdao.get(fdao.where('name', invoname))[0] def date(dat): return '/'.join(reversed(dat.split('/'))) prestamonth = '/'.join(invoice.date_sent.split('/')[1:]) client = Client() cdao = ClientDAO() client = cdao.get(cdao.where('id', invoice.id_client))[0] total = float(invoice.total) if invoice.tax: total *= (1 + (TAX / 100)) profile = get_profile_from_session() adao = InsuranceDAO() insurance = adao.get( [adao.where('id_profile', profile.id), adao.where('sel', 'True')]) html_render = render_template('template/pdf_template.html', profile=profile, prestamonth=prestamonth, date=date, invoice=invoice, convert_date=convert_date, Page_title='Facture', client=client, total=total, insurance=insurance, len=len, url="invoice") pdf = pdfkit.from_string(html_render, False) response = make_response(pdf) response.headers['Content-Type'] = 'application/pdf' if download: response.headers[ 'Content-Disposition'] = 'attachment; filename={}_{}.pdf'.format( _('Invoice'), invoname) else: response.headers[ 'Content-Disposition'] = 'inline; filename={}_{}.pdf'.format( _('Invoice'), invoname) logging.info('Invoice create pdf download: %s', str(download)) return response
def remove_invoice(invoicename): profileSession = get_profile_from_session() if profileSession.id: id_profile = profileSession.id else: logging.warning('(Invoice) Session closed: ' + profileSession.id) return 1 fdao = InvoiceDAO() if fdao.delete(fdao.where('name', invoicename)): logging.info('remove invoice %s OK', invoicename) if id_profile in CACHE_INVOICE.keys(): del CACHE_INVOICE[id_profile] return 2 else: logging.info('remove invoice %s Failed', invoicename) return 3
def bill(invoicename, is_sold): profileSession = get_profile_from_session() if profileSession.id: id_profile = profileSession.id else: logging.warning('(Invoice) Session closed: ' + profileSession.id) return 1 fdao = InvoiceDAO() invo = fdao.get(fdao.where('name', invoicename))[0] invo.sold = is_sold if hasattr(invo, 'total_tax'): del invo.total_tax if fdao.update(invo): logging.info('bill invoice sold %s %s OK', is_sold, invoicename) if id_profile in CACHE_INVOICE.keys(): del CACHE_INVOICE[id_profile] return 2 else: logging.info('bill invoice sold %s %s FAILED', is_sold, invoicename) return 3
def get_new_invoice(): fdao = InvoiceDAO() now = datetime.datetime.now() year = now.year profile = get_profile_from_session() l_invs = fdao.get(fdao.where('id_profile', profile.id)) last_i = '' for invoice in l_invs: if last_i: tmp_invo = int(''.join(invoice.name.split('-'))) tmp_last = int(''.join(last_i.split('-'))) if tmp_invo < tmp_last: continue last_i = invoice.name if last_i: last_i.split('-')[0] if year == int(last_i.split('-')[0]): nb = last_i.split('-')[1] nb = int(nb) + 1 return '{}-{:04d}'.format(str(year), nb) logging.info('new invoice name: %s', '{}-0001'.format(year)) return '{}-0001'.format(year)
def get_list_invoice(id_profile): fdao = InvoiceDAO() l_invoices = fdao.get(fdao.where('id_profile', id_profile)) sold_en = 0 last_i = '' waiting_i = 0 for invoice in l_invoices: if invoice.sold: sold_en += float(invoice.total) else: waiting_i += float(invoice.total) if invoice.tax: invoice.total_tax = str(float(invoice.total) * (1 + (TAX / 100))) if last_i: tmp1 = invoice.date_sent.split('/') tmp1.reverse() tmp2 = last_i.split('/') tmp2.reverse() tmp_invo = int(''.join(tmp1)) tmp_last = int(''.join(tmp2)) if tmp_invo < tmp_last: continue last_i = invoice.date_sent return l_invoices, sold_en, last_i, waiting_i
class InvoiceTestCase(unittest.TestCase): def setUp(self): self.fdao = InvoiceDAO(DB_PATH) self.invoice = Invoice() self.invoice.date_expiry = '01/01/2020' self.invoice.date_sent = '01/01/2020' self.invoice.days = 10 self.invoice.max_delay = '01/01/2020' self.invoice.id_client = 1 self.invoice.id_profile = 1 self.invoice.name = 'FACTURE TEST 1' self.invoice.sold = False self.invoice.project = 'TEST 1 2 3' self.invoice.day_rate = 5000 self.invoice.total = '50000' self.invoice.tax = False def test_invoice_obj(self): self.assertIsNotNone(self.fdao, msg="Impossible to instance InvoiceDAO") self.assertTrue(self.fdao.create_table(), msg="Impossible to create invoice table in db") self.assertFalse(self.fdao.exist(self.invoice), msg="Impossible to check Invoice exist in db") self.assertTrue(self.fdao.insert(self.invoice), msg="Impossible to insert Invoice in db") list_invoice = self.fdao.get(self.fdao.where('name', self.invoice.name)) self.assertIsInstance(list_invoice, list, msg="dao.get function not return a list") self.assertGreater(len(list_invoice), 0, msg="No Invoice in db") invce = list_invoice[0] self.assertIsInstance( invce, Invoice, msg="1st element of dao.get is not a Invoice obj") self.assertEqual( self.invoice.date_expiry, invce.date_expiry, msg="Invoice get, has no same attribute 'date_expiry'") self.assertEqual(self.invoice.date_sent, invce.date_sent, msg="Invoice get, has no same attribute 'date_sent'") self.assertEqual(self.invoice.days, invce.days, msg="Invoice get, has no same attribute 'days'") self.assertEqual(self.invoice.max_delay, invce.max_delay, msg="Invoice get, has no same attribute 'max_delay'") self.assertEqual(self.invoice.id_client, invce.id_client, msg="Invoice get, has no same attribute 'id_client'") self.assertEqual(self.invoice.id_profile, invce.id_profile, msg="Invoice get, has no same attribute 'id_profile'") self.assertEqual(self.invoice.name, invce.name, msg="Invoice get, has no same attribute 'name'") self.assertEqual(self.invoice.sold, invce.sold, msg="Invoice get, has no same attribute 'sold'") self.assertEqual(self.invoice.project, invce.project, msg="Invoice get, has no same attribute 'project'") self.assertEqual(self.invoice.day_rate, invce.day_rate, msg="Invoice get, has no same attribute 'day_rate'") self.assertEqual(self.invoice.total, invce.total, msg="Invoice get, has no same attribute 'total'") self.assertEqual(self.invoice.tax, invce.tax, msg="Invoice get, has no same attribute 'tax'") self.assertTrue(hasattr(invce, 'id'), msg="Invoice get, has no attribute 'id'") invce.sold = True self.assertTrue(self.fdao.update(invce)) list_invoice2 = self.fdao.get(self.fdao.where('name', invce.name)) self.assertIsInstance(list_invoice2, list, msg="dao.get function not return a list 2") self.assertGreater(len(list_invoice2), 0, msg="No Invoice 2 in db") invce2 = list_invoice2[0] self.assertEqual( invce2.date_expiry, invce.date_expiry, msg="Invoice get 2, has no same attribute 'date_expiry'") self.assertEqual( invce2.date_sent, invce.date_sent, msg="Invoice get 2, has no same attribute 'date_sent'") self.assertEqual(invce2.days, invce.days, msg="Invoice get 2, has no same attribute 'days'") self.assertEqual( invce2.max_delay, invce.max_delay, msg="Invoice get 2, has no same attribute 'max_delay'") self.assertEqual( invce2.id_client, invce.id_client, msg="Invoice get 2, has no same attribute 'id_client'") self.assertEqual( invce2.id_profile, invce.id_profile, msg="Invoice get 2, has no same attribute 'id_profile'") self.assertEqual(invce2.name, invce.name, msg="Invoice get 2, has no same attribute 'name'") self.assertEqual(invce2.sold, invce.sold, msg="Invoice get 2, has no same attribute 'sold'") self.assertEqual(invce2.project, invce.project, msg="Invoice get 2, has no same attribute 'project'") self.assertEqual(invce2.day_rate, invce.day_rate, msg="Invoice get 2, has no same attribute 'day_rate'") self.assertEqual(invce2.total, invce.total, msg="Invoice get 2, has no same attribute 'total'") self.assertEqual(invce2.tax, invce.tax, msg="Invoice get 2, has no same attribute 'tax'") self.assertTrue(self.fdao.delete(invce), msg="Impossible to delete invoice from db") self.assertFalse(self.fdao.drop(True, False), msg="Drop table invoice not wanted") self.assertFalse(self.fdao.drop(False, True), msg="Drop table invoice not wanted") self.assertTrue(self.fdao.drop(True, True), msg="Cannot drop table invoice") self.assertFalse(self.fdao.drop(True, True), msg="The table has not deleted before")
class Test: def __init__(self): self.client = None self.insurance = None self.profile = None self.pdao = None def __init_db(self): print(bcolors.BOLD + '---------- CREATE DB ----------' + bcolors.ENDC) self.pdao = ProfileDAO(DB_PATH) pdaoret = self.pdao.create_table() print(bcolors.HEADER + 'ProfileDAO ?> ' + bcolors.ENDC, bcolors.OKGREEN + 'OK' if pdaoret else bcolors.FAIL + "KO", bcolors.ENDC) self.idao = InsuranceDAO(DB_PATH) idaoret = self.idao.create_table() print(bcolors.HEADER + 'InsuranceDAO ?> ' + bcolors.ENDC, bcolors.OKGREEN + 'OK' if idaoret else bcolors.FAIL + "KO", bcolors.ENDC) self.fdao = InvoiceDAO(DB_PATH) fdaoret = self.fdao.create_table() print(bcolors.HEADER + 'InvoiceDAO ?> ' + bcolors.ENDC, bcolors.OKGREEN + 'OK' if fdaoret else bcolors.FAIL + "KO", bcolors.ENDC) self.cdao = ClientDAO(DB_PATH) cdaoret = self.cdao.create_table() print(bcolors.HEADER + 'ClientDAO ?> ' + bcolors.ENDC, bcolors.OKGREEN + 'OK' if cdaoret else bcolors.FAIL + "KO", bcolors.ENDC) self.qdao = QuotationDAO(DB_PATH) qdaoret = self.qdao.create_table() print(bcolors.HEADER + 'QuotationDAO ?> ' + bcolors.ENDC, bcolors.OKGREEN + 'OK' if qdaoret else bcolors.FAIL + "KO", bcolors.ENDC) self.qidao = QuotationItemDAO(DB_PATH) qidaoret = self.qidao.create_table() print(bcolors.HEADER + 'QuotationItemDAO ?> ' + bcolors.ENDC, bcolors.OKGREEN + 'OK' if qidaoret else bcolors.FAIL + "KO", bcolors.ENDC) return pdaoret and idaoret and fdaoret and cdaoret and qdaoret and qidaoret def drop_db(self): print(bcolors.BOLD + '---------- DROP DB ----------' + bcolors.ENDC) pdaoret = self.pdao.drop(True, True) print(bcolors.HEADER + 'ProfileDAO ?> ' + bcolors.ENDC, bcolors.OKGREEN + 'OK' if pdaoret else bcolors.FAIL + "KO", bcolors.ENDC) idaoret = self.idao.drop(True, True) print(bcolors.HEADER + 'InsuranceDAO ?> ' + bcolors.ENDC, bcolors.OKGREEN + 'OK' if idaoret else bcolors.FAIL + "KO", bcolors.ENDC) fdaoret = self.fdao.drop(True, True) print(bcolors.HEADER + 'InvoiceDAO ?> ' + bcolors.ENDC, bcolors.OKGREEN + 'OK' if fdaoret else bcolors.FAIL + "KO", bcolors.ENDC) cdaoret = self.cdao.drop(True, True) print(bcolors.HEADER + 'ClientDAO ?> ' + bcolors.ENDC, bcolors.OKGREEN + 'OK' if cdaoret else bcolors.FAIL + "KO", bcolors.ENDC) qdaoret = self.qdao.drop(True, True) print(bcolors.HEADER + 'QuotationDAO ?> ' + bcolors.ENDC, bcolors.OKGREEN + 'OK' if qdaoret else bcolors.FAIL + "KO", bcolors.ENDC) qidaoret = self.qidao.drop(True, True) print(bcolors.HEADER + 'QuotationItemDAO ?> ' + bcolors.ENDC, bcolors.OKGREEN + 'OK' if qidaoret else bcolors.FAIL + "KO", bcolors.ENDC) return pdaoret and idaoret and fdaoret and cdaoret and qdaoret and qidaoret def __profile_db(self): print(bcolors.BOLD + '---------- Profile DB ----------' + bcolors.ENDC) self.profile = Profile() self.profile.address = 'TEST 1' self.profile.comp_address = 'TEST 2' self.profile.zipcode = '13132' self.profile.email = '[email protected]' self.profile.name = 'TEST 3' self.profile.firstname = 'TEST 4' self.profile.password = '******' self.profile.country = 'FRANCE' self.profile.siret = '0292029102' self.profile.phone = '0439403920' self.profile.city = 'MARSEILLE' inspdao = self.pdao.insert(self.profile) print(bcolors.HEADER + 'insert profile ?> ' + bcolors.ENDC, bcolors.OKGREEN + 'OK' if inspdao else bcolors.FAIL + "KO", bcolors.ENDC) self.profile.password = "******" chpasswdpdao = self.pdao.update(self.profile) print(bcolors.HEADER + 'update profile ?> ' + bcolors.ENDC, bcolors.OKGREEN + 'OK' if chpasswdpdao else bcolors.FAIL + "KO", bcolors.ENDC) get_profile_list = self.pdao.get( self.pdao.where('name', self.profile.name)) print( bcolors.HEADER + 'get profile ?> ' + bcolors.ENDC, bcolors.OKGREEN + 'OK' if get_profile_list else bcolors.FAIL + "KO", bcolors.ENDC) if not get_profile_list: return False get_profile = get_profile_list[0] chkauthpdao = self.pdao.check_auth(get_profile, "NEW PASSWORD") self.profile = get_profile print(bcolors.HEADER + 'auth profile ?> ' + bcolors.ENDC, bcolors.OKGREEN + 'OK' if chkauthpdao else bcolors.FAIL + "KO", bcolors.ENDC) return (inspdao and chpasswdpdao and chkauthpdao) def __insurance_db(self): print(bcolors.BOLD + '---------- Insurance DB ----------' + bcolors.ENDC) if not self.profile: print( bcolors.HEADER + 'no profile ?> ' + bcolors.ENDC, bcolors.OKGREEN + 'OK' if False else bcolors.FAIL + "Please call __profile_db()", bcolors.ENDC) return self.insurance = Insurance() self.insurance.id_profile = self.profile.id self.insurance.n_contract = "1234567891011121314" self.insurance.name = "TEST 1" self.insurance.region = "FRANCE" self.insurance.sel = False self.insurance.type = "ALL INCLUDE" insidao = self.idao.insert(self.insurance) print(bcolors.HEADER + 'insert insurance ?> ' + bcolors.ENDC, bcolors.OKGREEN + 'OK' if insidao else bcolors.FAIL + "KO", bcolors.ENDC) self.insurance.sel = True chselidao = self.idao.update(self.insurance) print(bcolors.HEADER + 'update insurance ?> ' + bcolors.ENDC, bcolors.OKGREEN + 'OK' if chselidao else bcolors.FAIL + "KO", bcolors.ENDC) get_insurance_list = self.idao.get( self.idao.where('n_contract', self.insurance.n_contract)) print( bcolors.HEADER + 'get insurance ?> ' + bcolors.ENDC, bcolors.OKGREEN + 'OK' if get_insurance_list else bcolors.FAIL + "KO", bcolors.ENDC) if not get_insurance_list: return False is_sel_insurance = get_insurance_list[0].sel print( bcolors.HEADER + 'change sel insurance ?> ' + bcolors.ENDC, bcolors.OKGREEN + 'OK' if is_sel_insurance else bcolors.FAIL + "KO", bcolors.ENDC) return (insidao and chselidao and is_sel_insurance) def __client_db(self): print(bcolors.BOLD + '---------- Client DB ----------' + bcolors.ENDC) if not self.profile: print( bcolors.HEADER + 'no profile ?> ' + bcolors.ENDC, bcolors.OKGREEN + 'OK' if False else bcolors.FAIL + "Please call __profile_db()", bcolors.ENDC) return self.client = Client() self.client.address = "TEST 1" self.client.comp_address = "TEST 2" self.client.zipcode = "TEST 3" self.client.id_profile = self.profile.id self.client.name = "TEST 4" self.client.country = "TEST 5" self.client.city = "TEST 6" inscdao = self.cdao.insert(self.client) print(bcolors.HEADER + 'insert client ?> ' + bcolors.ENDC, bcolors.OKGREEN + 'OK' if inscdao else bcolors.FAIL + "KO", bcolors.ENDC) self.client.zipcode = "13013" chzipcodecdao = self.cdao.update(self.client) print(bcolors.HEADER + 'update client ?> ' + bcolors.ENDC, bcolors.OKGREEN + 'OK' if chzipcodecdao else bcolors.FAIL + "KO", bcolors.ENDC) get_client_list = self.cdao.get( self.cdao.where('name', self.client.name)) print( bcolors.HEADER + 'get client ?> ' + bcolors.ENDC, bcolors.OKGREEN + 'OK' if get_client_list else bcolors.FAIL + "KO", bcolors.ENDC) if not get_client_list: return False self.client = get_client_list[0] is_change_zipcode = (self.client.zipcode == "13013") print( bcolors.HEADER + 'change zip code client ?> ' + bcolors.ENDC, bcolors.OKGREEN + 'OK' if is_change_zipcode else bcolors.FAIL + "KO", bcolors.ENDC) return (inscdao and chzipcodecdao and is_change_zipcode) def __invoice_db(self): print(bcolors.BOLD + '---------- Invoice DB ----------' + bcolors.ENDC) if not self.profile: print( bcolors.HEADER + 'no profile ?> ' + bcolors.ENDC, bcolors.OKGREEN + 'OK' if False else bcolors.FAIL + "Please call __profile_db()", bcolors.ENDC) return if not self.client: print( bcolors.HEADER + 'no client ?> ' + bcolors.ENDC, bcolors.OKGREEN + 'OK' if False else bcolors.FAIL + "Please call __client_db()", bcolors.ENDC) return self.invoice = Invoice() self.invoice.date_expiry = '01/01/2020' self.invoice.date_sent = '01/01/2020' self.invoice.days = 10 self.invoice.max_delay = '01/01/2020' self.invoice.id_client = self.client.id self.invoice.id_profile = self.profile.id self.invoice.name = 'Invoice TEST 1' self.invoice.sold = False self.invoice.project = 'TEST 1 2 3' self.invoice.day_rate = 5000 self.invoice.total = '50000' self.invoice.tax = False insindao = self.fdao.insert(self.invoice) print(bcolors.HEADER + 'insert invoice ?> ' + bcolors.ENDC, bcolors.OKGREEN + 'OK' if insindao else bcolors.FAIL + "KO", bcolors.ENDC) self.invoice.sold = True chpyfdao = self.fdao.update(self.invoice) print(bcolors.HEADER + 'update invoice ?> ' + bcolors.ENDC, bcolors.OKGREEN + 'OK' if chpyfdao else bcolors.FAIL + "KO", bcolors.ENDC) get_invoice_list = self.fdao.get( self.fdao.where('id_client', self.invoice.id_client)) print( bcolors.HEADER + 'get invoice ?> ' + bcolors.ENDC, bcolors.OKGREEN + 'OK' if get_invoice_list else bcolors.FAIL + "KO", bcolors.ENDC) if not get_invoice_list: return False self.invoice = get_invoice_list[0] is_change_sold = self.invoice.sold print( bcolors.HEADER + 'change to bill invoice ?> ' + bcolors.ENDC, bcolors.OKGREEN + 'OK' if is_change_sold else bcolors.FAIL + "KO", bcolors.ENDC) return (insindao and chpyfdao and is_change_sold) def __quotation_db(self): print(bcolors.BOLD + '---------- Quotation DB ----------' + bcolors.ENDC) if not self.profile: print( bcolors.HEADER + 'no profile ?> ' + bcolors.ENDC, bcolors.OKGREEN + 'OK' if False else bcolors.FAIL + "Please call __profile_db()", bcolors.ENDC) return self.quotation = Quotation() self.quotation.client = "TEST 1" self.quotation.date_sent = "01/01/2020" self.quotation.date_validity = "01/01/2021" self.quotation.end_text = "TEST 2\nTEST 3\nTEST 4" self.quotation.id_profile = self.profile.id self.quotation.number = 203 self.quotation.total = 2030.20 self.quotation.tax_price = (2030.20 * 0.2) insqdao = self.qdao.insert(self.quotation) print(bcolors.HEADER + 'insert quotation ?> ' + bcolors.ENDC, bcolors.OKGREEN + 'OK' if insqdao else bcolors.FAIL + "KO", bcolors.ENDC) self.quotation.end_text = "TEST 1\nTEST 2" chpyqdao = self.qdao.update(self.quotation) print(bcolors.HEADER + 'update quotation ?> ' + bcolors.ENDC, bcolors.OKGREEN + 'OK' if chpyqdao else bcolors.FAIL + "KO", bcolors.ENDC) get_quotation_list = self.qdao.get( self.qdao.where('number', self.quotation.number)) print( bcolors.HEADER + 'get quotation ?> ' + bcolors.ENDC, bcolors.OKGREEN + 'OK' if get_quotation_list else bcolors.FAIL + "KO", bcolors.ENDC) if not get_quotation_list: return False self.quotation = get_quotation_list[0] is_change_end_text = (self.quotation.end_text == "TEST 1\nTEST 2") print( bcolors.HEADER + 'change end text quotation ?> ' + bcolors.ENDC, bcolors.OKGREEN + 'OK' if is_change_end_text else bcolors.FAIL + "KO", bcolors.ENDC) return (insqdao and chpyqdao and is_change_end_text) def __quotation_item_db(self): print(bcolors.BOLD + '---------- QuotationItem DB ----------' + bcolors.ENDC) if not self.quotation: print( bcolors.HEADER + 'no quotation ?> ' + bcolors.ENDC, bcolors.OKGREEN + 'OK' if False else bcolors.FAIL + "Please call __quotation_db()", bcolors.ENDC) return self.quotation_item = QuotationItem() self.quotation_item.description = "" self.quotation_item.id_quotation = self.quotation.id self.quotation_item.quantity = 12 self.quotation_item.quantity_text = "12m2" self.quotation_item.reduction = False self.quotation_item.unit_price = 1.34 insqidao = self.qidao.insert(self.quotation_item) print(bcolors.HEADER + 'insert quotationitem ?> ' + bcolors.ENDC, bcolors.OKGREEN + 'OK' if insqidao else bcolors.FAIL + "KO", bcolors.ENDC) self.quotation_item.description = "Fondation en pierre" chdscqidao = self.qidao.update(self.quotation_item) print(bcolors.HEADER + 'update quotationitem ?> ' + bcolors.ENDC, bcolors.OKGREEN + 'OK' if chdscqidao else bcolors.FAIL + "KO", bcolors.ENDC) get_quotation_item_list = self.qidao.get( self.qidao.where('description', self.quotation_item.description)) print( bcolors.HEADER + 'get quotationitem ?> ' + bcolors.ENDC, bcolors.OKGREEN + 'OK' if get_quotation_item_list else bcolors.FAIL + "KO", bcolors.ENDC) if not get_quotation_item_list: return False self.quotation_item = get_quotation_item_list[0] is_change_description = ( self.quotation_item.description == "Fondation en pierre") print( bcolors.HEADER + 'change end text quotationitem ?> ' + bcolors.ENDC, bcolors.OKGREEN + 'OK' if is_change_description else bcolors.FAIL + "KO", bcolors.ENDC) return (insqidao and chdscqidao and is_change_description) def run_invoice(self): ret = False if self.__init_db(): ret = (self.__profile_db() and self.__client_db() and self.__invoice_db()) self.drop_db() return ret def run_quotation(self): ret = False if self.__init_db(): ret = (self.__profile_db() and self.__client_db() and self.__quotation_db() and self.__quotation_item_db()) self.drop_db() return ret