Skip to content

Commit

Permalink
[NEW] DAMDFe implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
felipezago committed Sep 29, 2023
1 parent 7f4a19e commit 705e082
Show file tree
Hide file tree
Showing 12 changed files with 557 additions and 0 deletions.
3 changes: 3 additions & 0 deletions l10n_br_mdfe/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@
"views/modal/modal_aquaviario.xml",
"views/modal/modal_rodoviario.xml",
"views/modal/modal_ferroviario.xml",
"views/template.xml",
"wizards/document_closure_wizard.xml",
"report/reports.xml",
"report/damdfe.xml",
],
"post_init_hook": "post_init_hook",
"installable": True,
Expand Down
78 changes: 78 additions & 0 deletions l10n_br_mdfe/models/document.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

from erpbrasil.assinatura import certificado as cert
from erpbrasil.base.fiscal.edoc import ChaveEdoc
from erpbrasil.edoc.mdfe import QR_CODE_URL
from erpbrasil.transmissao import TransmissaoSOAP
from nfelib.mdfe.bindings.v3_0.mdfe_v3_00 import Mdfe
from nfelib.nfe.ws.edoc_legacy import MDFeAdapter as edoc_mdfe
Expand Down Expand Up @@ -832,6 +833,12 @@ def _valida_xml(self, xml_file):
erros = "\n".join(erros)
self.write({"xml_error_message": erros or False})

def view_pdf(self):
if not self.filtered(filtered_processador_edoc_mdfe):
return super().view_pdf()

return self.action_damdfe_report()

def atualiza_status_mdfe(self, processo):
self.ensure_one()

Expand Down Expand Up @@ -1012,3 +1019,74 @@ def get_mdfe_qrcode(self):
serialized_doc = self.serialize()[0]
xml = processador.assina_raiz(serialized_doc, serialized_doc.infMDFe.Id)
return processador.monta_qrcode_contingencia(serialized_doc, xml)

def action_damdfe_report(self):
return (
self.env["ir.actions.report"]
.search(
[("report_name", "=", "l10n_br_mdfe.report_damdfe")],
limit=1,
)
.report_action(self.id, data=self._prepare_damdfe_values())
)

def _prepare_damdfe_values(self):
return {
"company_id": self.company_id.id,
"company_has_logo": bool(self.company_id.logo),
"company_ie": self.company_id.inscr_est,
"company_logo": self.company_id.inscr_est,
"company_cnpj": self.company_id.cnpj_cpf,
"company_legal_name": self.company_id.legal_name,
"company_street": self.company_id.street,
"company_number": self.company_id.street_number,
"company_district": self.company_id.district,
"company_city": self.company_id.city_id.display_name,
"company_state": self.company_id.state_id.code,
"company_zip": self.company_id.zip,
"uf_carreg": self.mdfe_initial_state_id.code,
"uf_descarreg": self.mdfe_final_state_id.code,
"qt_cte": self.mdfe30_qCTe,
"qt_nfe": self.mdfe30_qNFe,
"total_weight": self.mdfe30_qCarga,
"weight_measure": "KG" if self.mdfe30_cUnid == "01" else "TON",
"qr_code_url": QR_CODE_URL,
"document_key": self._get_document_key_formatted(),
"document_number": self.document_number,
"document_model": self.document_type,
"document_serie": self.document_serie_id.code,
"document_date": self.document_date.astimezone().strftime(
"%d/%m/%y %H:%M:%S"
),
"authorization_protocol": self.authorization_protocol,
"contingency": self.mdfe_transmission != "1",
"environment": self.mdfe_environment,
"qr_code": self.get_mdfe_qrcode(),
"document_info": self.unloading_city_ids._prepare_damdfe_values(),
"modal": self.mdfe_modal,
"modal_str": self._get_modal_str(),
"modal_aereo_data": self.modal_aereo_id._prepare_damdfe_values(),
"modal_rodoviario_data": self.modal_rodoviario_id._prepare_damdfe_values(),
"modal_aquaviario_data": self.modal_aquaviario_id._prepare_damdfe_values(),
"modal_ferroviario_data": self.modal_ferroviario_id._prepare_damdfe_values(),
}

def _get_modal_str(self):
MODAL_TO_STR = {
"1": "Rodoviário",
"2": "Aéreo",
"3": "Aquaviário",
"4": "Ferroviário",
}
return MODAL_TO_STR[self.mdfe_modal]

def _get_document_key_formatted(self):
if not self.document_key:
return False

pace = 4
formatted_key = ""
for i in range(0, len(self.document_key), pace):
formatted_key += self.document_key[i : i + pace] + " "

return formatted_key
13 changes: 13 additions & 0 deletions l10n_br_mdfe/models/document_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,3 +115,16 @@ def _compute_document_data(self):
(0, 0, {"mdfe30_chMDFe": mdfe.mdfe30_chMDFe})
for mdfe in record.mdfe_ids
]

def _prepare_damdfe_values(self):
if not self:
return {}

documents = []
for record in self:
if record.document_type == "nfe":
documents += [{"key": nfe.mdfe30_chNFe} for nfe in self.nfe_ids]
elif record.document_type == "cte":
documents += [{"key": cte.mdfe30_chCTe} for cte in self.cte_ids]

return {"documents": documents}
2 changes: 2 additions & 0 deletions l10n_br_mdfe/models/document_related.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,5 @@ class MDFeRelated(spec_models.StackedModel):
mdfe30_chMDFe = fields.Char(related="document_key")

mdfe30_peri = fields.One2many(comodel_name="l10n_br_mdfe.transporte.perigoso")

mdfe30_infUnidTransp = fields.One2many(comodel_name="l10n_br_mdfe.transporte.inf")
13 changes: 13 additions & 0 deletions l10n_br_mdfe/models/modal_aereo.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,16 @@ class MDFeModalAereo(spec_models.StackedModel):
mdfe30_cAerEmb = fields.Char(related="document_id.boarding_airfield")

mdfe30_cAerDes = fields.Char(related="document_id.landing_airfield")

def _prepare_damdfe_values(self):
if not self:
return {}

return {
"airplane_nationality": self.mdfe30_nac,
"airplane_registration": self.mdfe30_matr,
"flight_number": self.mdfe30_nVoo,
"flight_date": self.mdfe30_dVoo,
"boarding_airfield": self.mdfe30_nac,
"landing_airfield": self.mdfe30_cAerDes,
}
4 changes: 4 additions & 0 deletions l10n_br_mdfe/models/modal_aquaviario.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@ def _compute_boarding_landing_point(self):
record.mdfe30_cPrtEmb = record.document_id.ship_boarding_point
record.mdfe30_cPrtDest = record.document_id.ship_landing_point

def _prepare_damdfe_values(self):
if not self:
return {}


class MDFeModalAquaviarioCarregamento(spec_models.SpecModel):
_name = "l10n_br_mdfe.modal.aquaviario.carregamento"
Expand Down
4 changes: 4 additions & 0 deletions l10n_br_mdfe/models/modal_ferroviario.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ class MDFeModalFerroviario(spec_models.StackedModel):

mdfe30_vag = fields.One2many(related="document_id.train_wagon_ids")

def _prepare_damdfe_values(self):
if not self:
return {}


class MDFeModalFerroviarioVagao(spec_models.SpecModel):
_name = "l10n_br_mdfe.modal.ferroviario.vagao"
Expand Down
44 changes: 44 additions & 0 deletions l10n_br_mdfe/models/modal_rodoviario.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,50 @@ def _compute_contractor(self):
(6, 0, record.document_id.rodo_contractor_ids.ids)
]

def _prepare_damdfe_values(self):
if not self:
return {}

vehicles = [
{
"RNTRC": self.mdfe30_RNTRC,
"plate": self.mdfe30_placa,
}
]

for reboque in self.mdfe30_veicReboque:
vehicles.append(
{
"RNTRC": reboque.mdfe30_RNTRC,
"plate": reboque.mdfe30_placa,
}
)

return {
"vehicles": vehicles,
"conductors": [
{
"name": cond.xNome,
"cpf": cond.xCPF,
}
for cond in self.mdfe30_condutor
],
"toll": [
{
"resp_cnpj": disp.mdfe30_CNPJPg,
"forn_cnpj": disp.mdfe30_CNPJForn,
"purchase_number": disp.mdfe30_nCompra,
}
for disp in self.mdfe30_disp
],
"ciot": [
{
"code": disp.mdfe30_CIOT,
}
for disp in self.mdfe30_infCIOT
],
}


class MDFeModalRodoviarioCIOT(spec_models.SpecModel):
_name = "l10n_br_mdfe.modal.rodoviario.ciot"
Expand Down
Loading

0 comments on commit 705e082

Please sign in to comment.