diff --git a/l10n_br_cte/models/document.py b/l10n_br_cte/models/document.py index 7e42b291cb1f..7a357483dcc4 100644 --- a/l10n_br_cte/models/document.py +++ b/l10n_br_cte/models/document.py @@ -11,16 +11,17 @@ from enum import Enum from erpbrasil.base.fiscal import cnpj_cpf +from erpbrasil.base.fiscal.edoc import ChaveEdoc # TODO: precisa tratar -# from erpbrasil.edoc.cte import TransmissaoCTE -from erpbrasil.base.fiscal.edoc import ChaveEdoc +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 @@ -1385,32 +1386,29 @@ def _serialize(self, edocs): return edocs # TODO: precisa tratar a lib nfelib - # 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 - # transmissao = TransmissaoCTE(certificado, session) - # return edoc_cte( - # transmissao, - # self.company_id.state_id.ibge_code, - # self.cte40_versao, - # self.cte40_tpAmb, - # ) - def _edoc_processor(self): - super()._edoc_processor() + 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, @@ -1424,8 +1422,8 @@ def _document_export(self, pretty_print=True): record.authorization_event_id = event_id # TODO: precisa tratar - # 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):