def profile_edit(): logging.warning('URL /profile') if not session.get('logged_in'): logging.warning('redirect /, no session enable') return redirect('/') if request.method == 'GET': profile = get_profile_from_session() return render_template('v3-profile.html', profile=profile) else: profile_old = get_profile_from_session() form = request.form if form['profile-password'] != form['profile-repassword']: return render_template('v3-profile.html', profile=profile_old, error=_('Your confirmation password does not match the password you entered')) profile = profile_old if form['profile-name']: profile.name = form['profile-name'] if form['profile-firstname']: profile.firstname = form['profile-firstname'] if form['profile-address']: profile.address = form['profile-address'] if form['profile-comp_address']: profile.comp_address = form['profile-comp_address'] if form['profile-city']: profile.city = form['profile-city'] if form['profile-zipcode']: profile.zipcode = form['profile-zipcode'] if form['profile-country']: profile.country = form['profile-country'] if form['profile-phone']: profile.phone = form['profile-phone'] if form['profile-email']: profile.email = form['profile-email'] if form['profile-siret']: profile.siret = form['profile-siret'] if form['profile-password']: profile.password = form['profile-password'] pdao = ProfileDAO() id_profile = profile.id if pdao.update(profile, pdao.where('id', id_profile)): logging.info('edit profile %s OK', str(id_profile)) else: logging.info('add profile %s FAILED', profile.name) return render_template('v3-profile.html',profile=profile_old, error=_('Impossible to edit user, please contact an admin !')) return redirect('/profile')
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 select_insurance(insurancename, select): adao = InsuranceDAO() profile = get_profile_from_session() assu = adao.get([ adao.where('name', insurancename), adao.where('id_profile', profile.id) ])[0] assu.sel = select ret = adao.update(assu) if ret: logging.info('insurance %s %s OK', str(select), insurancename) return 1 else: logging.info('insurance %s %s OK', str(select), insurancename) return 2
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 add_insurance(form): profile = get_profile_from_session() insurance = Insurance() insurance.name = form['insurance_name'] insurance.type = form['insurance_type'] insurance.region = form['insurance_region'] insurance.n_contract = form['insurance_contract'] insurance.sel = False insurance.id_profile = profile.id adao = InsuranceDAO() if adao.insert(insurance): logging.info('add insurance %s OK', insurance.name) return 1, insurance else: logging.warning('add insurance %s OK', insurance.name) return 2, None
def add_client(form): profileSession = get_profile_from_session() client = Client() client.name = form['client_name'] client.address = form['client_address'] client.comp_address = form['client_comp'] client.zipcode = form['client_zipcode'] client.city = form['client_city'] client.country = form['client_country'] client.id_profile = profileSession.id cdao = ClientDAO() if cdao.insert(client): logging.info('add client %s OK', client.name) return 1, client else: logging.warning('add client %s Failed', client.name) return 2, None
def quotation_id(number=None): if not session.get('logged_in'): return redirect('/') logging.info('go url /quotation/%s', number) profile = get_profile_from_session() ddao = QuotationDAO() if not ddao.exist(ddao.where('number', number)): logging.info('redirect if quotation doesnt exist number: %s', number) return redirect('/quotation') quotation = ddao.get(ddao.where('number', number))[0] logging.info( 'display pdf_template_quotation.html with quotation number: %s', number) return render_template('template/pdf_template_quotation.html', profile=profile, convert_date=convert_date, quotation=quotation, url="quotation")
def quotation(): if not session.get('logged_in'): return redirect('/') profile = get_profile_from_session() ddao = QuotationDAO() l_quotation = ddao.get(ddao.where('id_profile', profile.id)) logging.info('go url /quotation') last_quotation = l_quotation[-1].number if l_quotation else '' return render_template('quotation.html', convert_date=convert_date, Page_title=_('Quotation'), quotation=reversed(l_quotation), last_quotation=last_quotation, profile=profile, len=len, color=Color, url="quotation")
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 add_invoice(form): cdao = ClientDAO() client = cdao.get( cdao.where('name', form['invoice_client'].split(' -- ')[0])) if not client: logging.warning('(Invoice) This client doesnt exist: ' + form['invoice_client']) return 1, None client = client[0] profileSession = get_profile_from_session() if profileSession.id: id_profile = profileSession.id else: logging.warning('(Invoice) Session closed: ' + profileSession.id) return 2, None invoice = Invoice() invoice.name = form['invoice_name'] invoice.project = form['invoice_project'] invoice.day_rate = float(form['invoice_day_rate']) invoice.days = int(form['invoice_days']) invoice.date_sent = '/'.join(reversed(form['invoice_datesent'].split('-'))) invoice.date_expiry = '/'.join( reversed(form['invoice_dateexpiry'].split('-'))) invoice.max_delay = '/'.join(reversed(form['invoice_delay'].split('-'))) invoice.tax = form['invoice_tax'] == 'True' invoice.total = (invoice.day_rate * invoice.days) invoice.id_client = client.id invoice.id_profile = id_profile fdao = InvoiceDAO() if fdao.insert(invoice): logging.info('add invoice %s OK', invoice.name) if id_profile in CACHE_INVOICE.keys(): del CACHE_INVOICE[id_profile] return 3, invoice else: logging.info('add invoice %s FAILED', invoice.name) return 4, None
def add_quotation(form): profileSession = get_profile_from_session() if profileSession.id: id_profile = profileSession.id else: logging.warning('(Quotation) Session closed: %s', profileSession.id) flash(_("Impossible to add Quotation, Your session has been expired"), 'danger') return ddao = QuotationDAO() n_quotation = form['quotation'] if ddao.exist(ddao.where('number', n_quotation)): logging.info('quotation exist with number: %s', n_quotation) flash( _("Impossible to add Quotation, the number of quotation is ever used" ), 'danger') return client = form['client'] date_sent = form['date_sent'] date_validity = form['date_validity'] tax = (form['tax'] == "true") lines = [(x.replace('lines[', '').replace('][', '-').replace(']', ''), dict(form)[x]) for x in dict(form) if x.startswith('lines[')] list_text = [ dict(form)[x] for x in dict(form) if x.startswith('text_end[') ] quotation_obj = Quotation() quotation_obj.client = client quotation_obj.date_sent = date_sent quotation_obj.date_validity = date_validity quotation_obj.number = n_quotation quotation_obj.tax_price = 0 quotation_obj.id_profile = id_profile quotation_obj.end_text = '\n'.join(list_text) didao = QuotationItemDAO() success = True nb_items = int(len(lines) / 3) list_quotation_item = list() for i in range(0, nb_items): quotationItem = QuotationItem() quotationItem.description = lines[(i * 3) + 0][1] quotationItem.quantity_text = lines[(i * 3) + 1][1] result = re.findall(r'[-+]?\d*\.\d+|^\d+', quotationItem.quantity_text) if len(result) == 0: result = [0] quotationItem.quantity = float(result[0]) uprice = lines[(i * 3) + 2][1] if not uprice: uprice = 0 quotationItem.unit_price = float(uprice) quotationItem.reduction = False list_quotation_item.append(quotationItem) quotation_obj.total += (quotationItem.quantity * quotationItem.unit_price) if tax: quotation_obj.tax_price += ( (quotationItem.quantity * quotationItem.unit_price) * 20 / 100) if not ddao.insert(quotation_obj): logging.info('add quotation %s FAILED', n_quotation) flash( _("Impossible to add quotation n°%1").replace('%1', n_quotation), 'danger') return else: logging.info('add quotation %s OK', n_quotation) for quotationItem in list_quotation_item: quotationItem.id_quotation = quotation_obj.id success &= didao.insert(quotationItem) if not success: logging.warning('add quotation item %s FAILED', n_quotation) didao.delete(didao.where('id_quotation', n_quotation)) ddao.delete(ddao.where('id', n_quotation)) flash( _("Impossible to add quotation n°%1").replace('%1', n_quotation), 'danger') else: logging.info('add quotation item %s OK', n_quotation) flash( _("The quotation n°%1 has been added successfull").replace( '%1', n_quotation), 'success')
def accueil(): logging.warning('URL /home') if not session.get('logged_in'): logging.warning('redirect /, no session enable') return redirect('/') profile = get_profile_from_session() if not profile: logging.warning('redirect /, no session enable') del session['logged_in'] return redirect('/') dic_profile = get_element_profile_invoice(profile.id) l_invoice = dic_profile['l_invoice'] sold_collected = dic_profile['sold_collected'] last_i = dic_profile['last_i'] waiting_i = dic_profile['waiting_i'] now = datetime.datetime.now() year = now.year tax_total = 0 tax_collected = 0 invo_avail = 72000 tax_collected_last_year = 0 for invo in l_invoice: if invo.date_expiry.split('/')[2] == str(year): if invo.tax: invo_avail -= (float(invo.total)*(1+(TAX/100))) if invo.sold: tax_collected += (float(invo.total)*(1+(TAX/100))) else: invo_avail -= float(invo.total) if invo.sold: tax_collected += float(invo.total) elif invo.date_expiry.split('/')[2] == str(year-1): if invo.sold: if invo.tax: tax_collected_last_year += (float(invo.total)*(1+(TAX/100))) else: tax_collected_last_year += float(invo.total) if invo.sold: if invo.tax: tax_total += (float(invo.total)*0.20) date_now = now.date() cp_date_now = date_now cp_date_now=date_now.replace(day=31, month=12) days_left = nb_day_between_date(date_now, cp_date_now) logging.warning('display v3-home.html') list_client = get_list_client(profile.id) list_client.reverse() list_insurance = get_list_insurance(profile.id) list_insurance.reverse() l_invoice.reverse() return render_template( 'v3-home.html', convert_date=convert_date, days_left=days_left, Page_title=_('Home'), invoices=l_invoice, new_invoice=get_new_invoice(), sold_collected=sold_collected, last_invoice=last_i, insurances=list_insurance, solde_no_sold=waiting_i, year=year, clients=list_client, get_client_name=get_client_name, profile=profile, tax_total=tax_total, tax_collected=tax_collected, invoices_available=invo_avail, year_1=(year-1), inv_collect_last_year=tax_collected_last_year, url="home" )