From 8a2b1cb06328523dfb64ea74345508c10d6c62e3 Mon Sep 17 00:00:00 2001 From: Marcel Savegnago Date: Tue, 26 Nov 2024 16:34:10 -0300 Subject: [PATCH] [IMP] l10n_br_cte: transmissao --- l10n_br_cte/models/document.py | 72 ++++++++++++++++------------------ 1 file changed, 33 insertions(+), 39 deletions(-) diff --git a/l10n_br_cte/models/document.py b/l10n_br_cte/models/document.py index 36a27a90e6da..db850fad2bab 100644 --- a/l10n_br_cte/models/document.py +++ b/l10n_br_cte/models/document.py @@ -12,14 +12,12 @@ from erpbrasil.base.fiscal import cnpj_cpf from erpbrasil.base.fiscal.edoc import ChaveEdoc - -# TODO: precisa tratar +from erpbrasil.edoc.cte import TransmissaoCTE from lxml import etree from nfelib.cte.bindings.v4_0.cte_v4_00 import Cte from nfelib.cte.bindings.v4_0.proc_cte_v4_00 import CteProc - -# TODO: precisa tratar nfelib -# from nfelib.nfe.ws.edoc_legacy import CTeAdapter as edoc_cte +from nfelib.nfe.ws.edoc_legacy import CTeAdapter as edoc_cte +from requests import Session from xsdata.formats.dataclass.parsers import XmlParser from odoo import _, api, fields @@ -1384,33 +1382,29 @@ def _serialize(self, edocs): edocs.append(cte) return edocs - # def _edoc_processor(self): - # if self.document_type != MODELO_FISCAL_CTE: - # return super()._edoc_processor() - - # if not self.company_id.certificate_nfe_id: - # raise UserError(_("Certificado não encontrado")) - - # certificado = self.env.company._get_br_ecertificate() - # session = Session() - # session.verify = False - - # params = { - # "transmissao": TransmissaoCTE(certificado, session), - # "uf": self.company_id.state_id.ibge_code, - # "versao": self.cte_version, - # "ambiente": self.cte_environment, - # } - # return edoc_cte(**params) - def _edoc_processor(self): - pass + if not self.filtered(filter_processador_edoc_cte): + return super()._edoc_processor() + + if not self.company_id.certificate_nfe_id: + raise UserError(_("Certificado não encontrado")) + + certificado = self.env.company._get_br_ecertificate() + session = Session() + session.verify = False + transmissao = TransmissaoCTE(certificado, session) + return edoc_cte( + transmissao, + self.company_id.state_id.ibge_code, + self.cte40_versao, + self.cte40_tpAmb, + ) def _document_export(self, pretty_print=True): result = super()._document_export() for record in self.filtered(filter_processador_edoc_cte): edoc = record.serialize()[0] - # processador = record._edoc_processor() + processador = record._edoc_processor() xml_file = edoc.to_xml() event_id = self.event_ids.create_event_save_xml( company_id=self.company_id, @@ -1423,8 +1417,8 @@ def _document_export(self, pretty_print=True): ) record.authorization_event_id = event_id - # xml_assinado = processador.assina_raiz(edoc, edoc.infCte.Id) - # self._validate_xml(xml_assinado) + xml_assinado = processador.assina_raiz(edoc, edoc.infCte.Id) + self._validate_xml(xml_assinado) return result def _validate_xml(self, xml_file): @@ -1620,17 +1614,17 @@ def _cte_correction(self, justificative): file_response_xml=process.retorno.content.decode("utf-8"), ) - # def _document_qrcode(self): - # super()._document_qrcode() - - # for record in self.filtered(filter_processador_edoc_cte): - # record.cte40_infCTeSupl = self.env[ - # "l10n_br_fiscal.document.supplement" - # ].create( - # { - # "qrcode": record.get_cte_qrcode(), - # } - # ) + def _document_qrcode(self): + super()._document_qrcode() + + for record in self.filtered(filter_processador_edoc_cte): + record.cte40_infCTeSupl = self.env[ + "l10n_br_fiscal.document.supplement" + ].create( + { + "qrcode": record.get_cte_qrcode(), + } + ) def get_cte_qrcode(self): if self.document_type != MODELO_FISCAL_CTE: