From 1bc16071b9a0c7b450f5914ce56a6f0cec8c9489 Mon Sep 17 00:00:00 2001 From: Marcel Savegnago Date: Tue, 16 Jul 2024 12:03:14 -0300 Subject: [PATCH] [IMP] l10n_br_cte: add document corretion feature --- l10n_br_cte/__manifest__.py | 3 +- l10n_br_cte/models/document.py | 61 +++++++++++++++++++ .../wizards/document_correction_wizard.xml | 27 ++++++++ 3 files changed, 90 insertions(+), 1 deletion(-) create mode 100644 l10n_br_cte/wizards/document_correction_wizard.xml diff --git a/l10n_br_cte/__manifest__.py b/l10n_br_cte/__manifest__.py index c88d09f126d5..cda962b9e935 100644 --- a/l10n_br_cte/__manifest__.py +++ b/l10n_br_cte/__manifest__.py @@ -27,13 +27,14 @@ "modal/modal_aereo.xml", "views/res_company.xml", "views/cte_document.xml", + "wizards/document_correction_wizard.xml", ], "post_init_hook": "post_init_hook", "installable": True, "auto_install": False, "external_dependencies": { "python": [ - "nfelib>=2.0.0", + "nfelib<=2.0.7", "erpbrasil.assinatura>=1.7.0", "erpbrasil.transmissao>=1.1.0", "erpbrasil.edoc>=2.5.2", diff --git a/l10n_br_cte/models/document.py b/l10n_br_cte/models/document.py index fd9b329b882e..07c2dc0fe6d4 100644 --- a/l10n_br_cte/models/document.py +++ b/l10n_br_cte/models/document.py @@ -31,6 +31,7 @@ DOCUMENT_ISSUER_COMPANY, EVENT_ENV_HML, EVENT_ENV_PROD, + EVENTO_RECEBIDO, LOTE_PROCESSADO, PROCESSADOR_OCA, SITUACAO_EDOC_A_ENVIAR, @@ -917,6 +918,11 @@ def _export_field(self, xsd_field, class_obj, member_spec, export_value=None): if xsd_field == "cte40_tpAmb": self.env.context = dict(self.env.context) self.env.context.update({"tpAmb": self[xsd_field]}) + + # TODO: Força a remoção da tag infGlobalizado já que o + # campo xObs está no l10n_br_fiscal.document + if xsd_field == "cte40_infGlobalizado": + return False return super()._export_field(xsd_field, class_obj, member_spec, export_value) ################################ @@ -1104,6 +1110,61 @@ def _cte_cancel(self): file_response_xml=processo.retorno.content.decode("utf-8"), ) + def _document_correction(self, justificative): + result = super(CTe, self)._document_correction(justificative) + online_event = self.filtered(filter_processador_edoc_cte) + if online_event: + online_event._cte_correction(justificative) + return result + + def _cte_correction(self, justificative): + self.ensure_one() + processador = self._processador() + + numeros = self.event_ids.filtered( + lambda e: e.type == "14" and e.state == "done" + ).mapped("sequence") + + sequence = str(int(max(numeros)) + 1) if numeros else "1" + + evento = processador.carta_correcao( + chave=self.document_key, + protocolo_autorizacao=self.authorization_protocol, + justificativa=justificative.replace("\n", "\\n"), + sequencia=sequence, + ) + processo = processador.enviar_lote_evento(lista_eventos=[evento]) + # Gravamos o arquivo no disco e no filestore ASAP. + event_id = self.event_ids.create_event_save_xml( + company_id=self.company_id, + environment=(EVENT_ENV_PROD if self.cte40_tpAmb == "1" else EVENT_ENV_HML), + event_type="14", + xml_file=processo.envio_xml, + document_id=self, + sequence=sequence, + justification=justificative, + ) + + resposta = processo.resposta.infEvento + + if resposta.cStat not in EVENTO_RECEBIDO and not ( + resposta.chCTe == self.document_key + ): + mensagem = "Erro na carta de correção" + mensagem += "\nCódigo: " + resposta.cStat + mensagem += "\nMotivo: " + resposta.xMotivo + raise UserError(mensagem) + + event_id.set_done( + status_code=resposta.cStat, + response=resposta.xMotivo, + protocol_date=fields.Datetime.to_string( + datetime.fromisoformat(resposta.dhRegEvento) + ), + protocol_number=resposta.nProt, + file_response_xml=processo.retorno.content.decode("utf-8"), + ) + def _document_qrcode(self): super()._document_qrcode() diff --git a/l10n_br_cte/wizards/document_correction_wizard.xml b/l10n_br_cte/wizards/document_correction_wizard.xml new file mode 100644 index 000000000000..5cd223b368da --- /dev/null +++ b/l10n_br_cte/wizards/document_correction_wizard.xml @@ -0,0 +1,27 @@ + + + + + + l10n_br_fiscal.document.correction.wizard + + + + + + + +
+ Por favor, para carta de correção de CT-e, no campo Justificativa informe por linha (Grupo Alterado;Campo Alterado;Valor Alterado).
+ Exemplo:
+ compl;xObs;Nova Observação
+ ide;cfop;6353 +
+
+
+
+
+ +