def test_can_generate_danfe(self): path = os.path.join(os.path.dirname(__file__), 'XMLs') xml_string = open(os.path.join(path, 'NFe00000857.xml'), "r").read() xml_element = etree.fromstring(xml_string) oDanfe = danfe(list_xml=[xml_element]) with tempfile.TemporaryFile(mode='w') as oFile: oDanfe.writeto_pdf(oFile)
def render_qweb_pdf(self, res_ids, data=None): if self.report_name != 'l10n_br_eletronic_document.main_template_br_nfe_danfe': return super(IrActionsReport, self).render_qweb_pdf(res_ids, data=data) nfe = self.env['eletronic.document'].search([('id', 'in', res_ids)]) nfe_xml = base64.decodestring(nfe.nfe_processada or nfe.xml_to_send) cce_xml_element = [] cce_list = self.env['ir.attachment'].search([('res_model', '=', 'eletronic.document'), ('res_id', '=', nfe.id), ('name', 'like', 'cce-')]) if cce_list: for cce in cce_list: cce_xml = base64.decodestring(cce.datas) cce_xml_element.append(etree.fromstring(cce_xml)) logo = False if nfe.state != 'imported' and nfe.company_id.logo: logo = base64.decodestring(nfe.company_id.logo) elif nfe.state != 'imported' and nfe.company_id.logo_web: logo = base64.decodestring(nfe.company_id.logo_web) if logo: tmpLogo = BytesIO() tmpLogo.write(logo) tmpLogo.seek(0) else: tmpLogo = False timezone = pytz.timezone(self.env.context.get('tz') or 'UTC') xml_element = etree.fromstring(nfe_xml) if nfe.model == 'nfce': oDanfe = danfce(list_xml=[xml_element], logo=tmpLogo, timezone=timezone) else: oDanfe = danfe(list_xml=[xml_element], logo=tmpLogo, cce_xml=cce_xml_element, timezone=timezone) tmpDanfe = BytesIO() oDanfe.writeto_pdf(tmpDanfe) danfe_file = tmpDanfe.getvalue() tmpDanfe.close() return danfe_file, 'pdf'
def test_can_generate_danfe(self): path = os.path.join(os.path.dirname(__file__), 'XMLs') xml_string = open(os.path.join(path, 'NFe00000857.xml'), "r").read() # xml_string = open('/home/danimar/Downloads/NFe (5).xml', "r").read() xml_element = etree.fromstring(xml_string) oDanfe = danfe(list_xml=[xml_element]) # Para testar localmente o Danfe # with open('/home/danimar/danfe.pdf', 'w') as oFile: with tempfile.TemporaryFile(mode='w') as oFile: oDanfe.writeto_pdf(oFile)
def _find_attachment_ids_email(self): atts = super(InvoiceEletronic, self)._find_attachment_ids_email() if self.model not in ('55'): return atts attachment_obj = self.env['ir.attachment'] nfe_xml = base64.decodestring(self.nfe_processada) logo = base64.decodestring(self.invoice_id.company_id.logo) tmpLogo = io.BytesIO() tmpLogo.write(logo) tmpLogo.seek(0) xml_element = etree.fromstring(nfe_xml) oDanfe = danfe(list_xml=[xml_element], logo=tmpLogo) tmpDanfe = io.BytesIO() oDanfe.writeto_pdf(tmpDanfe) if danfe: danfe_id = attachment_obj.create( dict( name="Danfe-%08d.pdf" % self.numero, datas_fname="Danfe-%08d.pdf" % self.numero, datas=base64.b64encode(tmpDanfe.getvalue()), mimetype='application/pdf', res_model='account.invoice', res_id=self.invoice_id.id, )) atts.append(danfe_id.id) if nfe_xml: xml_id = attachment_obj.create( dict( name=self.nfe_processada_name, datas_fname=self.nfe_processada_name, datas=base64.encodestring(nfe_xml), mimetype='application/xml', res_model='account.invoice', res_id=self.invoice_id.id, )) atts.append(xml_id.id) return atts