-
-
Notifications
You must be signed in to change notification settings - Fork 247
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by rvalyi
- Loading branch information
Showing
14 changed files
with
284 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -115,6 +115,10 @@ Contributors | |
|
||
* Raphaël Valyi <[email protected]> | ||
|
||
* `Engenere <https://engenere.one>`_: | ||
|
||
* Antônio S. Pereira Neto <[email protected]> | ||
|
||
Maintainers | ||
~~~~~~~~~~~ | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,3 +10,7 @@ | |
* `AKRETION <https://akretion.com/pt-BR/>`_: | ||
|
||
* Raphaël Valyi <[email protected]> | ||
|
||
* `Engenere <https://engenere.one>`_: | ||
|
||
* Antônio S. Pereira Neto <[email protected]> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
from . import ir_actions_report |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
||
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) | ||
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.")) | ||
|
||
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 | ||
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
Oops, something went wrong.