Skip to content

Commit b89f940

Browse files
trabalho em desenvolvimento
1 parent 73980dd commit b89f940

15 files changed

+211
-155
lines changed

l10n_br_fiscal_dfe/models/dfe.py

+3-36
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,
@@ -29,7 +25,7 @@ class DFe(models.Model):
2925

3026
serie = fields.Char(size=3, index=True)
3127

32-
number = fields.Float(string="Document Number", index=True, digits=(18, 0))
28+
document_number = fields.Float(index=True, digits=(18, 0))
3329

3430
emitter = fields.Char(size=60)
3531

@@ -41,7 +37,7 @@ class DFe(models.Model):
4137
selection=OPERATION_TYPE,
4238
)
4339

44-
document_value = fields.Float(
40+
document_amount = fields.Float(
4541
string="Document Total Value",
4642
readonly=True,
4743
digits=(18, 2),
@@ -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

+81-3
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):
@@ -19,10 +25,14 @@ class AccessKey(models.Model):
1925

2026
cnpj_cpf = fields.Char(related="dfe_ids.cnpj_cpf")
2127

22-
document_value = fields.Float(
23-
string="Document Total Value", digits=(18, 2), related="dfe_ids.document_value"
28+
document_amount = fields.Float(
29+
string="Document Total Value", digits=(18, 2), related="dfe_ids.document_amount"
2430
)
2531

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

l10n_br_fiscal_dfe/models/dfe_monitor.py

+15-3
Original file line numberDiff line numberDiff line change
@@ -236,12 +236,12 @@ def _create_dfe_from_procNFe(self, root, nsu):
236236

237237
dfe = self.env["l10n_br_fiscal.dfe"].create(
238238
{
239-
"number": root.NFe.infNFe.ide.nNF,
239+
"document_number": root.NFe.infNFe.ide.nNF,
240240
"emitter": root.NFe.infNFe.emit.xNome,
241241
"key": nfe_key,
242242
"serie": root.NFe.infNFe.ide.serie,
243243
"operation_type": str(root.NFe.infNFe.ide.tpNF),
244-
"document_value": root.NFe.infNFe.total.ICMSTot.vNF,
244+
"document_amount": root.NFe.infNFe.total.ICMSTot.vNF,
245245
"inclusion_datetime": datetime.now(),
246246
"cnpj_cpf": supplier_cnpj,
247247
"ie": root.NFe.infNFe.emit.IE,
@@ -276,7 +276,7 @@ def _create_dfe_from_resNFe(self, root, nsu):
276276
"key": nfe_key,
277277
"emitter": root.xNome,
278278
"operation_type": str(root.tpNF),
279-
"document_value": root.vNF,
279+
"document_amount": root.vNF,
280280
"document_state": str(root.cSitNFe),
281281
"inclusion_datetime": datetime.now(),
282282
"cnpj_cpf": supplier_cnpj,
@@ -293,6 +293,18 @@ def _create_dfe_from_resNFe(self, root, nsu):
293293
}
294294
)
295295

296+
if self.automatically_acknowledge_receipt:
297+
mde = self.env["l10n_br_nfe.recipient_manifestation_event"].create(
298+
{
299+
"key": nfe_key,
300+
"event_type": "pendente",
301+
"company_id": self.env.company.id,
302+
"dfe_access_key_id": access_key.id,
303+
"mde_document_type": "mde_nfe",
304+
}
305+
)
306+
mde.action_ciencia_emissao()
307+
296308
access_key.dfe_ids = [(4, dfe.id)]
297309
return dfe
298310

l10n_br_fiscal_dfe/views/dfe/dfe_access_key.xml

+14-2
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,19 @@
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" />
1317
<group>
18+
<field name="document_state" />
19+
<field name="document_number" />
1420
<field name="emitter" readonly="1" />
1521
<field name="cnpj_cpf" readonly="1" />
16-
<field name="document_value" widget="monetary" readonly="1" />
22+
<field name="document_amount" widget="monetary" readonly="1" />
1723
</group>
1824
<field name="dfe_ids" />
1925
</form>
@@ -35,7 +41,13 @@
3541
<field name="key" />
3642
<field name="emitter" />
3743
<field name="cnpj_cpf" />
38-
<field name="document_value" />
44+
<field name="document_amount" />
45+
<button string="XML" name="action_download_xml" type="object" />
46+
<button
47+
string="DANFE"
48+
name="make_pdf"
49+
type="object"
50+
/>
3951
</tree>
4052
</field>
4153
</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

+4-17
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@
1616
<separator string="Document Info" />
1717
<group>
1818
<group>
19-
<field name="number" readonly="1" />
19+
<field name="document_number" readonly="1" />
2020
<field name="nsu" readonly="1" />
2121
<field name="cnpj_cpf" readonly="1" />
2222
<field name="ie" readonly="1" />
23-
<field name="document_value" widget="monetary" readonly="1" />
23+
<field name="document_amount" widget="monetary" readonly="1" />
2424
<field name="document_state" readonly="1" />
2525
</group>
2626
<field name="cfop_ids" readonly="1" />
@@ -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+
)

0 commit comments

Comments
 (0)