Skip to content

Commit

Permalink
[IMP] l10n_br_cte: add document corretion feature
Browse files Browse the repository at this point in the history
  • Loading branch information
marcelsavegnago committed Jul 16, 2024
1 parent bc30b46 commit 1bc1607
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 1 deletion.
3 changes: 2 additions & 1 deletion l10n_br_cte/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
61 changes: 61 additions & 0 deletions l10n_br_cte/models/document.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
DOCUMENT_ISSUER_COMPANY,
EVENT_ENV_HML,
EVENT_ENV_PROD,
EVENTO_RECEBIDO,
LOTE_PROCESSADO,
PROCESSADOR_OCA,
SITUACAO_EDOC_A_ENVIAR,
Expand Down Expand Up @@ -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)

################################
Expand Down Expand Up @@ -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()

Expand Down
27 changes: 27 additions & 0 deletions l10n_br_cte/wizards/document_correction_wizard.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?xml version="1.0" encoding="utf-8" ?>
<!-- Copyright 2024 - TODAY, Marcel Savegnago <[email protected]>
License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). -->
<odoo>

<record id="document_correction_wizard_form" model="ir.ui.view">
<field name="model">l10n_br_fiscal.document.correction.wizard</field>
<field name="inherit_id" ref="l10n_br_fiscal.document_correction_wizard_form" />
<field name="arch" type="xml">
<xpath expr="//footer" position="before">
<field name="document_type" invisible="1" />
<field name="document_type_id" invisible="1" readonly="1" />
<group attrs="{'invisible': [('document_type', '!=', '57')]}">
<!-- TODO: Melhorar -->
<div>
Por favor, para carta de correção de CT-e, no campo Justificativa informe por linha (Grupo Alterado;Campo Alterado;Valor Alterado).<br
/>
<strong>Exemplo:</strong><br />
compl;xObs;Nova Observação<br />
ide;cfop;6353
</div>
</group>
</xpath>
</field>
</record>

</odoo>

0 comments on commit 1bc1607

Please sign in to comment.