Skip to content

Commit

Permalink
[ADD] POS cfe xml export at fiscal closing
Browse files Browse the repository at this point in the history
  • Loading branch information
lfdivino committed Feb 6, 2023
1 parent 3c3ebe6 commit d2b8863
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 0 deletions.
2 changes: 2 additions & 0 deletions l10n_br_pos_cfe/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"depends": [
"point_of_sale",
"l10n_br_pos",
"l10n_br_fiscal_closing",
],
"external_dependencies": {
"python": ["satcomum"],
Expand All @@ -22,6 +23,7 @@
"data/pos_payment_method_data.xml",
# Views
"views/pos_payment_method_view.xml",
"views/closing_view.xml",
# Templates
"views/pos_template.xml",
],
Expand Down
1 change: 1 addition & 0 deletions l10n_br_pos_cfe/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@
from . import pos_payment_method
from . import pos_order
from . import pos_config
from . import closing
81 changes: 81 additions & 0 deletions l10n_br_pos_cfe/models/closing.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
# © 2023 KMEE INFORMATICA LTDA (https://kmee.com.br)
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
import base64
import logging
import os

from odoo import _, fields, models
from odoo.exceptions import RedirectWarning

_logger = logging.getLogger(__name__)

try:
from erpbrasil.base import misc
except ImportError:
_logger.error("Biblioteca erpbrasil.base não instalada")


class FiscalClosing(models.Model):
_inherit = "l10n_br_fiscal.closing"

document_cfe_pos_ids = fields.One2many(
comodel_name="pos.order",
string="CFe POS Documents",
inverse_name="close_id",
)

def _prepare_files(self, temp_dir):
temp_dir = super(FiscalClosing, self)._prepare_files(temp_dir)
date_min, date_max = self._date_range()
orders = self.env["pos.order"].search(
[
("company_id", "=", self.company_id.id),
("date_order", ">=", date_min),
("date_order", "<=", date_max),
("state", "not in", ["draft"]),
("amount_paid", ">=", 0),
("authorization_file", "!=", False),
]
)

document_path = "/".join(
["cfe_pos", misc.punctuation_rm(self.company_id.cnpj_cpf)]
)

for order in orders:
try:
filename = os.path.join(
temp_dir, document_path, f"{order.document_key}.xml"
)
if not os.path.exists(os.path.dirname(filename)):
os.makedirs(os.path.dirname(filename))

with open(filename, "wb") as file:
file.write(base64.b64decode(order.authorization_file))

if order.cancel_document_key:
filename = os.path.join(
temp_dir, document_path, f"{order.cancel_document_key}.xml"
)
if not os.path.exists(os.path.dirname(filename)):
os.makedirs(os.path.dirname(filename))

with open(filename, "wb") as file:
file.write(base64.b64decode(order.cancel_file))
except OSError as e:
raise RedirectWarning(_("Error!"), _("I/O Error")) from e
except PermissionError as e:
raise RedirectWarning(
_("Error!"), _("Check write permissions in your system temp folder")
) from e
except Exception:
_logger.error(
_(
"Replication failed: document attachments "
"[id = {}] is not present in the database."
).format(order.name)
)

self.write({"document_cfe_pos_ids": [(6, 0, orders.ids)]})

return temp_dir
21 changes: 21 additions & 0 deletions l10n_br_pos_cfe/views/closing_view.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?xml version="1.0" encoding="utf-8" ?>
<!--
Copyright 2023 KMEE INFORMATICA LTDA
License AGPL-3 or later (http://www.gnu.org/licenses/agpl)
-->
<odoo>

<record id="fiscal_closing_form" model="ir.ui.view">
<field name="name">l10n_br_fiscal.closing.form (in l10n_br_pos_cfe)</field>
<field name="model">l10n_br_fiscal.closing</field>
<field name="inherit_id" ref="l10n_br_fiscal_closing.fiscal_closing_form" />
<field name="arch" type="xml">
<xpath expr="//form/sheet/group[2]//notebook/page[4]" position="after">
<page string="CFe POS">
<field name="document_cfe_pos_ids" readonly="1" />
</page>
</xpath>
</field>
</record>

</odoo>

0 comments on commit d2b8863

Please sign in to comment.