Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[14.0][IMP] l10n_br_cte: add transmission, correction and cancellation #3525

Draft
wants to merge 1 commit into
base: 14.0
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 33 additions & 39 deletions l10n_br_cte/models/document.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand All @@ -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):
Expand Down Expand Up @@ -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:
Expand Down
Loading