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_mdfe: add damdfe report #3506

Merged
merged 4 commits into from
Nov 27, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
3 changes: 2 additions & 1 deletion l10n_br_mdfe/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
from . import models
from .hooks import post_init_hook
from . import models
from . import report
7 changes: 6 additions & 1 deletion l10n_br_mdfe/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,11 @@
"spec_driven_model",
],
"data": [
"security/ir.model.access.csv",
# Data
"data/ir_config_parameter.xml",
# Security
"security/ir.model.access.csv",
# Views
"views/document.xml",
"views/mdfe_action.xml",
"views/mdfe_menu.xml",
Expand All @@ -30,6 +33,8 @@
"views/modal/modal_aquaviario.xml",
"views/modal/modal_rodoviario.xml",
"views/modal/modal_ferroviario.xml",
# Report
"report/damdfe_report.xml",
],
"demo": [
"demo/fiscal_document_demo.xml",
Expand Down
83 changes: 79 additions & 4 deletions l10n_br_mdfe/demo/fiscal_document_demo.xml
Original file line number Diff line number Diff line change
@@ -1,16 +1,25 @@
<?xml version="1.0" encoding="utf-8" ?>
<odoo noupdate="1">

<!-- MDFe Test - Fiscal Operation - Comment Dummy-->
<record id="l10n_br_fiscal.fo_manifesto" model="l10n_br_fiscal.operation">
<field
name="comment_ids"
eval="[(6,0,[ref('l10n_br_fiscal.fiscal_comment_dummy')])]"
/>
</record>

<!-- MDFe Test - NFe Related -->
<record id="demo_mdfe_related_nfe" model="l10n_br_fiscal.document.related">
<field name="document_key">41190806117473000150550010000586251016759484</field>
<field name="document_total_weight">10</field>
<field name="document_total_amount">33.19</field>
</record>


<!-- MDFe Test - Modal Ferroviário - LC -->
<record id="demo_mdfe_lc_modal_ferroviario" model="l10n_br_fiscal.document">
<field name="fiscal_operation_id" ref="l10n_br_fiscal.fo_venda" />
<field name="fiscal_operation_id" ref="l10n_br_fiscal.fo_manifesto" />
<field name="document_type_id" ref="l10n_br_fiscal.document_58" />
<field
name="document_serie_id"
Expand Down Expand Up @@ -48,9 +57,28 @@
/>
</record>

<!-- MDFe Test - Modal Ferroviário - LC - Document Lines -->
<record
id="demo_mdfe_lc_modal_ferroviario_1-1"
model="l10n_br_fiscal.document.line"
>
<field name="document_id" ref="demo_mdfe_lc_modal_ferroviario" />
<field name="name">Manifesto</field>
<field name="uom_id" ref="uom.product_uom_unit" />
<field name="price_unit">0</field>
<field name="quantity">1</field>
<field name="fiscal_operation_type">out</field>
<field name="fiscal_operation_id" ref="l10n_br_fiscal.fo_manifesto" />
<field
name="fiscal_operation_line_id"
ref="l10n_br_fiscal.fo_manifesto_manifesto"
/>
</record>


<!-- MDFe Test - Modal Rodoviário - LC -->
<record id="demo_mdfe_lc_modal_rodoviario" model="l10n_br_fiscal.document">
<field name="fiscal_operation_id" ref="l10n_br_fiscal.fo_venda" />
<field name="fiscal_operation_id" ref="l10n_br_fiscal.fo_manifesto" />
<field name="document_type_id" ref="l10n_br_fiscal.document_58" />
<field
name="document_serie_id"
Expand Down Expand Up @@ -88,9 +116,25 @@
/>
</record>

<!-- MDFe Test - Modal Rodoviário - LC - Document Lines -->
<record id="demo_mdfe_lc_modal_rodoviario_1-1" model="l10n_br_fiscal.document.line">
<field name="document_id" ref="demo_mdfe_lc_modal_rodoviario" />
<field name="name">Manifesto</field>
<field name="uom_id" ref="uom.product_uom_unit" />
<field name="price_unit">0</field>
<field name="quantity">1</field>
<field name="fiscal_operation_type">out</field>
<field name="fiscal_operation_id" ref="l10n_br_fiscal.fo_manifesto" />
<field
name="fiscal_operation_line_id"
ref="l10n_br_fiscal.fo_manifesto_manifesto"
/>
</record>


<!-- MDFe Test - Modal Aéreo - SN -->
<record id="demo_mdfe_sn_modal_aereo" model="l10n_br_fiscal.document">
<field name="fiscal_operation_id" ref="l10n_br_fiscal.fo_venda" />
<field name="fiscal_operation_id" ref="l10n_br_fiscal.fo_manifesto" />
<field name="document_type_id" ref="l10n_br_fiscal.document_58" />
<field
name="document_serie_id"
Expand Down Expand Up @@ -125,9 +169,25 @@
/>
</record>

<!-- MDFe Test - Modal Aéreo - SN - Document Lines -->
<record id="demo_mdfe_sn_modal_aereo_1-1" model="l10n_br_fiscal.document.line">
<field name="document_id" ref="demo_mdfe_sn_modal_aereo" />
<field name="name">Manifesto</field>
<field name="uom_id" ref="uom.product_uom_unit" />
<field name="price_unit">0</field>
<field name="quantity">1</field>
<field name="fiscal_operation_type">out</field>
<field name="fiscal_operation_id" ref="l10n_br_fiscal.fo_manifesto" />
<field
name="fiscal_operation_line_id"
ref="l10n_br_fiscal.fo_manifesto_manifesto"
/>
</record>


<!-- MDFe Test - Modal Aquaviário - SN -->
<record id="demo_mdfe_sn_modal_aquaviario" model="l10n_br_fiscal.document">
<field name="fiscal_operation_id" ref="l10n_br_fiscal.fo_venda" />
<field name="fiscal_operation_id" ref="l10n_br_fiscal.fo_manifesto" />
<field name="document_type_id" ref="l10n_br_fiscal.document_58" />
<field
name="document_serie_id"
Expand Down Expand Up @@ -165,4 +225,19 @@
/>
</record>

<!-- MDFe Test - Modal Aquaviário - SN - Document Lines -->
<record id="demo_mdfe_sn_modal_aquaviario_1-1" model="l10n_br_fiscal.document.line">
<field name="document_id" ref="demo_mdfe_sn_modal_aquaviario" />
<field name="name">Manifesto</field>
<field name="uom_id" ref="uom.product_uom_unit" />
<field name="price_unit">0</field>
<field name="quantity">1</field>
<field name="fiscal_operation_type">out</field>
<field name="fiscal_operation_id" ref="l10n_br_fiscal.fo_manifesto" />
<field
name="fiscal_operation_line_id"
ref="l10n_br_fiscal.fo_manifesto_manifesto"
/>
</record>

</odoo>
21 changes: 21 additions & 0 deletions l10n_br_mdfe/models/document.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Copyright 2023 KMEE
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).

import base64
import re
import string
from enum import Enum
Expand Down Expand Up @@ -914,3 +915,23 @@
erros = Mdfe.schema_validation(xml_file)
erros = "\n".join(erros)
self.write({"xml_error_message": erros or False})

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

Check warning on line 921 in l10n_br_mdfe/models/document.py

View check run for this annotation

Codecov / codecov/patch

l10n_br_mdfe/models/document.py#L921

Added line #L921 was not covered by tests

attachment_data = {
"name": self.document_key + ".pdf",
"res_model": self._name,
"res_id": self.id,
"mimetype": "application/pdf",
"type": "binary",
}
report = self.env.ref("l10n_br_mdfe.report_damdfe")
pdf_data = report._render_qweb_pdf(self.fiscal_line_ids.document_id.ids)
attachment_data["datas"] = base64.b64encode(pdf_data[0])
file_pdf = self.file_report_id
self.file_report_id = False
file_pdf.unlink()

self.file_report_id = self.env["ir.attachment"].create(attachment_data)
22 changes: 22 additions & 0 deletions l10n_br_mdfe/models/res_company.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,28 @@ class ResCompany(spec_models.SpecModel):
compute="_compute_mdfe_data",
)

mdfe_authorize_accountant_download_xml = fields.Boolean(
string="Include Accountant Partner data in persons authorized to "
"download MDFe XML",
default=False,
)

damdfe_margin_top = fields.Integer(
default=5, help="Top margin in mm for the DAMDFE layout."
)

damdfe_margin_right = fields.Integer(
default=5, help="Right margin in mm for the DAMDFE layout."
)

damdfe_margin_bottom = fields.Integer(
default=5, help="Bottom margin in mm for the DAMDFE layout."
)

damdfe_margin_left = fields.Integer(
default=5, help="Left margin in mm for the DAMDFE layout."
)

def _compute_mdfe_data(self):
for rec in self:
if rec.partner_id.is_company:
Expand Down
1 change: 1 addition & 0 deletions l10n_br_mdfe/report/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import ir_actions_report
12 changes: 12 additions & 0 deletions l10n_br_mdfe/report/damdfe_report.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<odoo>
<record id="report_damdfe" model="ir.actions.report">
<field name="name">DAMDFE</field>
<field name="model">l10n_br_fiscal.document</field>
<field name="report_type">qweb-pdf</field>
<field name="report_name">main_template_damdfe</field>
<field name="report_file">main_template_damdfe</field>
<field name="print_report_name">'%s' % (object.document_key)</field>
<field name="binding_model_id" ref="model_l10n_br_fiscal_document" />
<field name="binding_type">report</field>
</record>
</odoo>
81 changes: 81 additions & 0 deletions l10n_br_mdfe/report/ir_actions_report.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
# Copyright 2024 Engenere.one
# Copyright 2024 - TODAY, Marcel Savegnago <[email protected]>
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
import base64
from io import BytesIO

from brazilfiscalreport.damdfe import Damdfe, DamdfeConfig, Margins

from odoo import _, api, models
from odoo.exceptions import UserError


class IrActionsReport(models.Model):
_inherit = "ir.actions.report"

def _render_qweb_html(self, res_ids, data=None):
if self.report_name == "main_template_damdfe":
return

Check warning on line 18 in l10n_br_mdfe/report/ir_actions_report.py

View check run for this annotation

Codecov / codecov/patch

l10n_br_mdfe/report/ir_actions_report.py#L18

Added line #L18 was not covered by tests

return super()._render_qweb_html(res_ids, data=data)

def _render_qweb_pdf(self, res_ids, data=None):
if self.report_name not in ["main_template_damdfe"]:
return super()._render_qweb_pdf(res_ids, data=data)

mdfe = self.env["l10n_br_fiscal.document"].search([("id", "in", res_ids)])

return self._render_damdfe(mdfe)

def _render_damdfe(self, mdfe):
if mdfe.document_type != "58":
raise UserError(_("You can only print a DAMDFE of a MDFe(58)."))

mdfe_xml = False
if mdfe.authorization_file_id:
mdfe_xml = base64.b64decode(mdfe.authorization_file_id.datas)

Check warning on line 36 in l10n_br_mdfe/report/ir_actions_report.py

View check run for this annotation

Codecov / codecov/patch

l10n_br_mdfe/report/ir_actions_report.py#L36

Added line #L36 was not covered by tests
elif mdfe.send_file_id:
mdfe_xml = base64.b64decode(mdfe.send_file_id.datas)

if not mdfe_xml:
raise UserError(_("No xml file was found."))

Check warning on line 41 in l10n_br_mdfe/report/ir_actions_report.py

View check run for this annotation

Codecov / codecov/patch

l10n_br_mdfe/report/ir_actions_report.py#L41

Added line #L41 was not covered by tests

return self.render_damdfe_brazilfiscalreport(mdfe, mdfe_xml)

def render_damdfe_brazilfiscalreport(self, mdfe, mdfe_xml):
logo = False
if mdfe.issuer == "company" and mdfe.company_id.logo:
logo = base64.b64decode(mdfe.company_id.logo)
elif mdfe.issuer != "company" and mdfe.company_id.logo_web:
logo = base64.b64decode(mdfe.company_id.logo_web)

if logo:
tmpLogo = BytesIO()
tmpLogo.write(logo)
tmpLogo.seek(0)
else:
tmpLogo = False

Check warning on line 57 in l10n_br_mdfe/report/ir_actions_report.py

View check run for this annotation

Codecov / codecov/patch

l10n_br_mdfe/report/ir_actions_report.py#L57

Added line #L57 was not covered by tests
config = self._get_damdfe_config(tmpLogo, mdfe.company_id)

damdfe = Damdfe(xml=mdfe_xml, config=config)

tmpDamdfe = BytesIO()
damdfe.output(tmpDamdfe)
damdfe_file = tmpDamdfe.getvalue()
tmpDamdfe.close()

return damdfe_file, "pdf"

@api.model
def _get_damdfe_config(self, tmpLogo, company):
margins = Margins(
top=company.damdfe_margin_top,
right=company.damdfe_margin_right,
bottom=company.damdfe_margin_bottom,
left=company.damdfe_margin_left,
)
damdfe_config = {
"logo": tmpLogo,
"margins": margins,
}
return DamdfeConfig(**damdfe_config)
1 change: 1 addition & 0 deletions l10n_br_mdfe/tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@
from . import test_mdfe_import
from . import test_mdfe_structure
from . import test_mdfe_res_partner
from . import test_mdfe_damdfe
39 changes: 39 additions & 0 deletions l10n_br_mdfe/tests/test_mdfe_damdfe.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# Copyright 2024 - TODAY, Marcel Savegnago <[email protected]>
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).

from odoo.exceptions import UserError
from odoo.tests.common import TransactionCase


class TestDamdfeGeneration(TransactionCase):
def setUp(self):
super().setUp()

def test_generate_damdfe_brazil_fiscal_report(self):
mdfe = self.env.ref("l10n_br_mdfe.demo_mdfe_lc_modal_rodoviario")
mdfe.action_document_confirm()
mdfe.view_pdf()

self.assertTrue(mdfe.file_report_id)

def test_generate_damdfe_document_type_error(self):
damdfe_report = self.env["ir.actions.report"].search(
[("report_name", "=", "main_template_damdfe")]
)
mdfe = self.env.ref("l10n_br_mdfe.demo_mdfe_lc_modal_rodoviario")
mdfe.document_type_id = self.env.ref("l10n_br_fiscal.document_01")
mdfe.action_document_confirm()
with self.assertRaises(UserError) as captured_exception:
damdfe_report._render_qweb_pdf([mdfe.id])
self.assertEqual(
captured_exception.exception.args[0],
"You can only print a DAMDFE of a MDFe(58).",
)

def test_generate_damdfe_brazil_fiscal_report_partner(self):
mdfe = self.env.ref("l10n_br_mdfe.demo_mdfe_lc_modal_rodoviario")
mdfe.action_document_confirm()
mdfe.issuer = "partner"
mdfe.view_pdf()

self.assertTrue(mdfe.file_report_id)
8 changes: 8 additions & 0 deletions l10n_br_mdfe/views/res_company.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,14 @@
<field name="mdfe_environment" required="1" />
</group>
</group>
<group name="damdfe_settings" string="DAMDFE Settings">
<group>
<field name="damdfe_margin_top" string="Margin Top" />
<field name="damdfe_margin_right" string="Margin Right" />
<field name="damdfe_margin_bottom" string="Margin Bottom" />
<field name="damdfe_margin_left" string="Margin Left" />
</group>
</group>
</page>
</xpath>
</field>
Expand Down
Loading