# from winmentor import count_down, tarif, headers import os import re import mimetypes from time import sleep from datetime import datetime, time from email.header import Header from email.mime.base import MIMEBase from email.mime.multipart import MIMEMultipart from email.mime.application import MIMEApplication from email.mime.audio import MIMEAudio from email.mime.image import MIMEImage from email.mime.text import MIMEText from fpdf import FPDF from envelopes import Envelope from winmentor import WinMentor # imports for Envelope subclass # imports for Envelope subclass # import logging # logging.basicConfig(filename='winmentor/generated_files/inv_number', # format='%(asctime)s %(message)s', datefmt='%m/%d/%Y %I:%M:%S %p', level=logging.INFO) def _printer(func): def wrapper(*args, **kwargs): print(datetime.now().microsecond, 'now processing', func.__name__) return func(*args) return wrapper class HtmlEnvelope(Envelope): def __init__(self, msg_type='alternative'): self.msg_type = msg_type Envelope.__init__(self) def to_mime_message(self): """Returns the envelope as :py:class:`email.mime.multipart.MIMEMultipart`.""" msg = MIMEMultipart(self.msg_type) # html for attachments msg['Subject'] = self._header(self._subject or '') msg['From'] = self._encoded(self._addrs_to_header([self._from])) msg['To'] = self._encoded(self._addrs_to_header(self._to)) if self._cc: msg['CC'] = self._addrs_to_header(self._cc) if self._headers: for key, value in self._headers.items(): msg[key] = self._header(value) for part in self._parts: type_maj, type_min = part[0].split('/') if type_maj == 'text' and type_min in ('html', 'plain'): msg.attach(MIMEText(part[1], type_min, self._charset)) else: msg.attach(part[1]) return msg def add_to_addr(self, to_addr): """Adds a ``To`` address.""" if isinstance(to_addr, str): self._to.append(to_addr) else: self._to.extend(to_addr) class InvoiceDetails(object): def __init__(self): self.invoiceref = '' self.supplref = '' self.clientref = '' class Invoice(object): PATH_PDF = 'winmentor/generated_files' TARIF = {} def __init__(self, fiscal_code, _companies, winment_path=os.getenv('WINMENT', 'c:/winment/data/').replace('\\', '/')): self._companies = _companies self.bank_account = set() self.net = set() self.winment_path = winment_path self.fiscal_code = fiscal_code self._name = self.__supplyer__().name self._fiscal_code = self.__supplyer__().vat_code self._reg = self.__supplyer__().reg_number self._address = self.__supplyer__().address + self.__supplyer__().location @classmethod def set_tarifs(cls, tarif): cls.TARIF = tarif return cls.TARIF @classmethod def set_pdf_path(cls, new_path): cls.PATH_PDF = new_path return cls.PATH_PDF def add_net(self, value): self.net.add(value) return self.net def net_printer(self): for i in self.net: return i def add_bank_account(self, account_num): self.bank_account.add(account_num) return self.bank_account @_printer def account_printer(self): for i in self.bank_account: return i @_printer def __supplyer__(self): for i in self._companies: if i.vat_code == self.fiscal_code: return i @property def get_code(self): return self.fiscal_code @get_code.setter def set_code(self, _value): self.fiscal_code = _value return self.fiscal_code def pdf_body(self, list_, tarifs, numbers, service_name): for i in tarifs.keys(): if i == list_.name: # deprecated: d_set = [tarifs[i]['mail'], tarifs[i]['price']]# list_.extend(d_set) list_.mail = tarifs[i]['mail'] list_.price = tarifs[i]['price'] self.pdf.cell(110, 10, 'Cota tva: 19.00, tva la incasare', border=0, ln=1, align='L') self.pdf.cell(130, 10, 'Denumire', border=1, ln=0, align='C') self.pdf.cell(20, 10, 'PU', border=1, ln=0, align='C') self.pdf.cell(20, 10, 'Valoare', border=1, ln=0, align='C') self.pdf.cell(10, 10, 'Tva', border=1, ln=1, align='C') self.pdf.cell(130, 10, service_name, border=1, ln=0) self.pdf.cell(20, 10, f'{list_.price}', border=1, ln=0, align='C') # unit price self.pdf.cell(20, 10, f'{list_.price}', border=1, ln=0, align='C') # value self.pdf.cell(10, 10, f'{list_.price * 0.19:.2f}', border=1, ln=1, align='C') # Vat self.pdf.cell(40, 10, 'TOTAL:', border=0, ln=0) self.pdf.cell(140, 10, f'{round(list_.price * 1.19, 2)}', border=0, ln=1, align='R') # total # deprecated: if isinstance(list_[-2], int): above values else: above values self.pdf.cell(170, 10, 'Semnarea si stampilarea facturilor nu constituie elemente', border=0, ln=1, align='C') self.pdf.cell(170, 10, 'obligatorii pe care trebuie sa le contina factura, ART. 319 alin. 29 leg.227/2015, oug. 17/2015.', border=0, ln=1, align='C') self.pdf.cell(170, 10, 'Operator: Darvai Claudia, Termen: 10 zile', border=0, ln=1, align='L') # due date # self.pdf.cell(170, 10, 'Semnatura furnizorului', border=0, ln=1, align='L') # picture link # self.pdf.cell(170, 10, 'Semnatura de primire', border=0, ln=1, align='L') @_printer def gen_file(self, list_, image, link, tarifs, numbers, service_name): self.pdf = FPDF() self.pdf.add_page() self.pdf.set_font('Helvetica', style='B', size=12) self.pdf.cell(65, 10, ' ', align='C', border=0) self.pdf.multi_cell(60, 10, txt=f'Factura seria/numarul AB-{numbers} din {datetime.now().date()}', border=1, align='C') self.pdf.set_font('Helvetica', style='', size=9) self.pdf.multi_cell(100, 10, txt=f'Furnizor: {self._name}\nCod fiscal: {self._fiscal_code}\n\ Nr. reg.: {self._reg}\nAdresa: {self._address}\nnr.cont: {self.account_printer()}\n\ web: {self.net_printer()}', border=0, align='L') self.pdf.cell(90, 10, ' ', align='R', border=0) self.pdf.multi_cell(110, 10, txt=f'Client: {list_.name}\nCod fiscal: {list_.vat_code}\n\ Nr. reg.: {list_.reg_number}\nAdresa {list_.address} {list_.location}', border=0, align='R') self.pdf.ln(25) self.pdf_body(list_, tarifs, numbers, service_name) self.pdf.image(image, x=80, w=50, h=25, link=link) self.pdf.output(f'{self.PATH_PDF}/Factura {list_.name.title()} {datetime.now().date()}.pdf')