forked from OCA/l10n-brazil
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[ADD] POS cfe xml export at fiscal closing
- Loading branch information
Showing
4 changed files
with
105 additions
and
0 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
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 |
---|---|---|
|
@@ -4,3 +4,4 @@ | |
from . import pos_payment_method | ||
from . import pos_order | ||
from . import pos_config | ||
from . import closing |
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 @@ | ||
# © 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 |
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,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> |