Skip to content

Commit d1b52d5

Browse files
trabalho em desenvolvimento
1 parent 73980dd commit d1b52d5

13 files changed

+164
-118
lines changed

l10n_br_fiscal_dfe/models/dfe.py

+1-34
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,8 @@
11
# Copyright (C) 2025-Today - Engenere (<https://engenere.one>).
22
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
33
import base64
4-
from io import BytesIO
54

6-
from brazilfiscalreport.danfe import Danfe
7-
8-
from odoo import _, fields, models
9-
from odoo.exceptions import UserError
5+
from odoo import fields, models
106

117
from ..constants.dfe import (
128
OPERATION_TYPE,
@@ -153,32 +149,3 @@ def download_attachment(self, attachment_id):
153149
),
154150
"target": "self",
155151
}
156-
157-
def make_pdf(self):
158-
if self.dfe_nfe_document_type != "dfe_nfe_complete":
159-
raise UserError(_("Can only generate DANFE when DF-e is complete."))
160-
nfe_xml = base64.b64decode(self.attachment_id.datas)
161-
162-
danfe = Danfe(xml=nfe_xml)
163-
164-
tmpDanfe = BytesIO()
165-
danfe.output(tmpDanfe)
166-
danfe_file = tmpDanfe.getvalue()
167-
tmpDanfe.close()
168-
169-
pdf_attachment = self.env["ir.attachment"].create(
170-
{
171-
"name": "DANFE.pdf",
172-
"type": "binary",
173-
"datas": base64.b64encode(danfe_file),
174-
"res_model": self._name,
175-
"res_id": self.id,
176-
"mimetype": "application/pdf",
177-
}
178-
)
179-
180-
return {
181-
"type": "ir.actions.act_url",
182-
"url": f"/web/content/{pdf_attachment.id}?download=true",
183-
"target": "self",
184-
}

l10n_br_fiscal_dfe/models/dfe_access_key.py

+75-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
# Copyright (C) 2025-Today - Engenere (<https://engenere.one>).
22
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
3-
from odoo import api, fields, models
3+
import base64
4+
from io import BytesIO
5+
6+
from brazilfiscalreport.danfe import Danfe
7+
8+
from odoo import _, api, fields, models
9+
from odoo.exceptions import UserError
410

511

612
class AccessKey(models.Model):
@@ -55,3 +61,71 @@ def _compute_color_status(self):
5561
record.color_status = "blue"
5662
else:
5763
record.color_status = "normal"
64+
65+
def name_get(self):
66+
return [(record.id, record.key) for record in self]
67+
68+
def action_download_xml(self):
69+
complete_dfe_ids = self.dfe_ids.filtered(
70+
lambda dfe: dfe.dfe_nfe_document_type == "dfe_nfe_complete"
71+
)
72+
if complete_dfe_ids:
73+
return complete_dfe_ids.action_download_xml()
74+
raise UserError(
75+
_("It is only possible to download XML when DF-e is completed.")
76+
)
77+
78+
def make_pdf(self):
79+
complete_dfe_ids = self.dfe_ids.filtered(
80+
lambda dfe: dfe.dfe_nfe_document_type == "dfe_nfe_complete"
81+
)
82+
83+
if not complete_dfe_ids:
84+
raise UserError(_("No DF-e with 'DF-e complete' type found."))
85+
86+
complete_dfe = complete_dfe_ids[0]
87+
attachment = complete_dfe.attachment_id
88+
nfe_xml = base64.b64decode(attachment.datas)
89+
danfe = Danfe(xml=nfe_xml)
90+
91+
tmpDanfe = BytesIO()
92+
danfe.output(tmpDanfe)
93+
danfe_file = tmpDanfe.getvalue()
94+
tmpDanfe.close()
95+
96+
pdf_attachment = self.env["ir.attachment"].create(
97+
{
98+
"name": f"DANFE{complete_dfe.key}.pdf",
99+
"type": "binary",
100+
"datas": base64.b64encode(danfe_file),
101+
"res_model": self._name,
102+
"res_id": complete_dfe.id,
103+
"mimetype": "application/pdf",
104+
}
105+
)
106+
107+
return {
108+
"type": "ir.actions.act_url",
109+
"url": f"/web/content/{pdf_attachment.id}?download=true",
110+
"target": "self",
111+
}
112+
113+
def create_mde_action(self):
114+
return {
115+
"name": _("Manifestação do Destinatário"),
116+
"type": "ir.actions.act_window",
117+
"res_model": "nfe_recipient_manifestation_event.wizard",
118+
"view_mode": "form",
119+
"target": "new",
120+
"context": {
121+
"default_dfe_access_key_id": self.id,
122+
},
123+
}
124+
125+
def import_document(self):
126+
complete_dfe_ids = self.dfe_ids.filtered(
127+
lambda dfe: dfe.dfe_nfe_document_type == "dfe_nfe_complete"
128+
)
129+
if complete_dfe_ids:
130+
return complete_dfe_ids.import_document()
131+
raise UserError(_("You can only import the NF-e when the DF-e is completed."))

l10n_br_fiscal_dfe/views/dfe/dfe_access_key.xml

+10
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@
77
<field name="priority">1</field>
88
<field name="arch" type="xml">
99
<form string="DF-e Access Key">
10+
<header>
11+
<button string="XML" name="action_download_xml" type="object" />
12+
<button string="DANFE" name="make_pdf" type="object" />
13+
</header>
1014
<separator string="Access Key" />
1115
<field name="key" select="1" required="1" readonly="1" />
1216
<separator string="Document Info" />
@@ -36,6 +40,12 @@
3640
<field name="emitter" />
3741
<field name="cnpj_cpf" />
3842
<field name="document_value" />
43+
<button string="XML" name="action_download_xml" type="object" />
44+
<button
45+
string="DANFE"
46+
name="make_pdf"
47+
type="object"
48+
/>
3949
</tree>
4050
</field>
4151
</record>

l10n_br_fiscal_dfe/views/dfe/dfe_monitor_views.xml

+5-5
Original file line numberDiff line numberDiff line change
@@ -38,19 +38,19 @@
3838
</group>
3939

4040
<notebook>
41+
<page string="Access Key">
42+
<field name="dfe_access_key_id" nolabel="1" readonly="1" />
43+
</page>
4144
<page string="DF-e">
42-
<field name="dfe_ids" nolabel="1">
45+
<field name="dfe_ids" nolabel="1" readonly="1">
4346
<tree>
44-
<field name="key" />
4547
<field name="nsu" />
48+
<field name="dfe_access_key_id" />
4649
<field name="dfe_nfe_document_type" />
4750
</tree>
4851
</field>
4952
</page>
5053

51-
<page string="Access Key">
52-
<field name="dfe_access_key_id" nolabel="1" readonly="1" />
53-
</page>
5454
</notebook>
5555
</sheet>
5656

l10n_br_fiscal_dfe/views/dfe/dfe_views.xml

+2-15
Original file line numberDiff line numberDiff line change
@@ -43,22 +43,9 @@
4343
<field name="priority">1</field>
4444
<field name="arch" type="xml">
4545
<tree create="false">
46-
<field name="key" />
47-
<field name="number" />
48-
<field name="cnpj_cpf" />
49-
<field name="document_value" />
50-
<field name="document_state" />
5146
<field name="dfe_nfe_document_type" />
52-
<button
53-
string="Download XML"
54-
name="action_download_xml"
55-
type="object"
56-
/>
57-
<button
58-
string="Gerar DANFE"
59-
name="make_pdf"
60-
type="object"
61-
/>
47+
<field name="nsu" />
48+
<button string="XML" name="action_download_xml" type="object" />
6249
</tree>
6350
</field>
6451
</record>

l10n_br_nfe/__manifest__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
"views/nfe_document_view.xml",
3131
"views/res_config_settings_view.xml",
3232
"views/nfe_recipient_manifestation_event/nfe_recipient_manifestation_event_view.xml",
33-
"views/dfe/dfe_views.xml",
33+
"views/dfe/dfe_access_key_views.xml",
3434
"views/supplier_info_view.xml",
3535
# Report
3636
"report/reports.xml",

l10n_br_nfe/models/dfe.py

+3-21
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,11 @@
1-
# Copyright (C) 2023 KMEE Informatica LTDA from odoo import _, fields, models
2-
# License AGPL-3 or later (http://www.gnu.org/licenses/agpl)
3-
from odoo import _, fields, models
1+
# Copyright (C) 2025-Today - Engenere (<https://engenere.one>).
2+
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
3+
from odoo import _, models
44

55

66
class DFe(models.Model):
77
_inherit = "l10n_br_fiscal.dfe"
88

9-
nfe_recipient_manifestation_event_ids = fields.One2many(
10-
comodel_name="l10n_br_nfe.recipient_manifestation_event",
11-
inverse_name="dfe_id",
12-
string="Manifestações do Destinatário Importadas",
13-
)
14-
15-
def create_mde_action(self):
16-
return {
17-
"name": _("Manifestação do Destinatário"),
18-
"type": "ir.actions.act_window",
19-
"res_model": "nfe_recipient_manifestation_event.wizard",
20-
"view_mode": "form",
21-
"target": "new",
22-
"context": {
23-
"default_dfe_id": self.id,
24-
},
25-
}
26-
279
def import_document(self):
2810
self.ensure_one()
2911
try:

l10n_br_nfe/models/dfe_access_key.py

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Copyright (C) 2025-Today - Engenere (<https://engenere.one>).
2+
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
3+
from odoo import fields, models
4+
5+
6+
class DFeAccessKey(models.Model):
7+
_inherit = "l10n_br_fiscal.dfe_access_key"
8+
9+
nfe_recipient_manifestation_event_ids = fields.One2many(
10+
comodel_name="l10n_br_nfe.recipient_manifestation_event",
11+
inverse_name="dfe_access_key_id",
12+
string="Manifestações do Destinatário Importadas",
13+
)

l10n_br_nfe/models/nfe_recipient_manifestation_event.py

+4-9
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,9 @@ class NfeRecipientManifestationEvent(models.Model):
3939

4040
environment = fields.Selection(related="company_id.dfe_environment")
4141

42-
dfe_id = fields.Many2one(string="DF-e", comodel_name="l10n_br_fiscal.dfe")
42+
dfe_access_key_id = fields.Many2one(
43+
string="DF-e", comodel_name="l10n_br_fiscal.dfe_access_key"
44+
)
4345

4446
mde_document_type = fields.Selection(
4547
selection=[
@@ -50,14 +52,7 @@ class NfeRecipientManifestationEvent(models.Model):
5052
)
5153

5254
def name_get(self):
53-
return [
54-
(
55-
rec.id,
56-
f"NFº: {rec.dfe_id.number} ({rec.dfe_id.cnpj_cpf}): "
57-
f"{rec.company_id.legal_name}",
58-
)
59-
for rec in self
60-
]
55+
return [(rec.id, f"NFº: {rec.key} {rec.company_id.legal_name}") for rec in self]
6156

6257
def _get_processor(self):
6358
certificado = self.env.company._get_br_ecertificate()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?xml version="1.0" encoding="UTF-8" ?>
2+
<!-- Copyright (C) 2025-Today - Engenere (<https://engenere.one>).
3+
License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). -->
4+
<odoo>
5+
6+
<record model="ir.ui.view" id="nfe_dfe_access_key">
7+
<field name="name">l10n_br_nfe.dfe_access_key.tree</field>
8+
<field name="model">l10n_br_fiscal.dfe_access_key</field>
9+
<field name="inherit_id" ref="l10n_br_fiscal_dfe.dfe_access_key_tree" />
10+
<field name="arch" type="xml">
11+
<xpath expr="//tree/button[@name='make_pdf']" position="after">
12+
<button
13+
name="import_document"
14+
string="Importar Documento"
15+
type="object"
16+
/>
17+
<button string="Manifestar" name="create_mde_action" type="object" />
18+
</xpath>
19+
</field>
20+
</record>
21+
22+
<record model="ir.ui.view" id="nfe_dfe_access_key_form">
23+
<field name="name">l10n_br_nfe.dfe_access_key.form</field>
24+
<field name="model">l10n_br_fiscal.dfe_access_key</field>
25+
<field name="inherit_id" ref="l10n_br_fiscal_dfe.dfe_access_key_form" />
26+
<field name="arch" type="xml">
27+
<xpath expr="//header/button[@name='make_pdf']" position="after">
28+
<button
29+
name="import_document"
30+
string="Importar Documento"
31+
type="object"
32+
/>
33+
<button string="Manifestar" name="create_mde_action" type="object" />
34+
</xpath>
35+
</field>
36+
</record>
37+
38+
</odoo>

l10n_br_nfe/views/dfe/dfe_views.xml

-22
This file was deleted.

l10n_br_nfe/views/nfe_recipient_manifestation_event/nfe_recipient_manifestation_event_view.xml

+2-2
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@
5454
<field name="number" readonly="1" />
5555
<field name="serie" readonly="1" />
5656
<field name="mde_document_type" readonly="1" />
57-
<field name="dfe_id" readonly="1" />
57+
<field name="dfe_access_key_id" readonly="1" />
5858
</group>
5959
</form>
6060
</field>
@@ -108,7 +108,7 @@
108108
<field name="company_id" />
109109
<field name="state" />
110110
<field name="mde_document_type" />
111-
<field name="dfe_id" string="DFe" />
111+
<field name="dfe_access_key_id" string="DFe" />
112112
</tree>
113113
</field>
114114
</record>

0 commit comments

Comments
 (0)