diff --git a/l10n_br_cnab_structure/readme/README.rst b/l10n_br_cnab_structure/README.rst similarity index 100% rename from l10n_br_cnab_structure/readme/README.rst rename to l10n_br_cnab_structure/README.rst diff --git a/l10n_br_cnab_structure/tests/test_cnab_structure.py b/l10n_br_cnab_structure/tests/test_cnab_structure.py index 2e323d4cd3b9..7e130d54728b 100644 --- a/l10n_br_cnab_structure/tests/test_cnab_structure.py +++ b/l10n_br_cnab_structure/tests/test_cnab_structure.py @@ -2,12 +2,20 @@ # @author Antônio S. Pereira Neto # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + +import base64 + from odoo import fields from odoo.tests.common import tagged from odoo.addons.account.tests.common import AccountTestInvoicingCommon +# Help function +def replace_chars(string, index, replacement): + return string[:index] + replacement + string[index + len(replacement) :] + + @tagged("post_install", "-at_install") class TestCNABStructure(AccountTestInvoicingCommon): @classmethod @@ -15,6 +23,7 @@ def setUpClass(cls, chart_template_ref=None): super().setUpClass(chart_template_ref=chart_template_ref) cls.env = cls.env(context=dict(cls.env.context, tracking_disable=True)) cls.company = cls.company_data["company"] + cls.company.update({"cnpj_cpf": "82688625000152"}) cls.env.user.company_id = cls.company.id cls.res_partner_bank_model = cls.env["res.partner.bank"] cls.payment_mode_model = cls.env["account.payment.mode"] @@ -23,6 +32,9 @@ def setUpClass(cls, chart_template_ref=None): cls.attachment_model = cls.env["ir.attachment"] cls.res_partner_pix_model = cls.env["res.partner.pix"] cls.bank_341 = cls.env.ref("l10n_br_base.res_bank_341") + cls.import_wizard_obj = cls.env["cnab.import.wizard"] + cls.cnab_log_obj = cls.env["l10n_br_cnab.return.log"] + cls.partner_a.update({"cnpj_cpf": "45823449000198"}) cls.res_partner_pix_model.create( { "partner_id": cls.partner_a.id, @@ -107,7 +119,7 @@ def setUpClass(cls, chart_template_ref=None): ] cls.payment_order_model.search(cls.domain).unlink() - def test_file_generete(self): + def test_file_generete_and_return(self): # Open invoice self.invoice.action_post() # Add to payment order using the wizard @@ -119,5 +131,26 @@ def test_file_generete(self): # Open payment order payment_order.draft2open() action = payment_order.open2generated() - attachment = self.attachment_model.browse(action["res_id"]) - self.assertIsNotNone(attachment) + delivery_cnab_file = self.attachment_model.browse(action["res_id"]) + self.assertIsNotNone(delivery_cnab_file) + + cnab_data = base64.b64decode(delivery_cnab_file.datas).decode() + lines = cnab_data.splitlines() + lines[2] = replace_chars(lines[2], 154, "10112022") + lines[2] = replace_chars(lines[2], 172, "300") + return_data = "\r\n".join(lines).encode() + + self.wizard = self.import_wizard_obj.create( + { + "journal_id": self.bank_journal_itau.id, + "return_file": base64.b64encode(return_data), + "filename": "TEST.RET", + "type": "outbound", + "cnab_structure_id": self.env.ref( + "l10n_br_cnab_structure.cnab_itau_240" + ).id, + } + ) + action = self.wizard.import_cnab() + cnab_log = self.cnab_log_obj.browse(action["res_id"]) + self.assertIsNotNone(cnab_log) diff --git a/l10n_br_cnab_structure/wizard/cnab_import_wizard.py b/l10n_br_cnab_structure/wizard/cnab_import_wizard.py index 5de15af1c699..97d6cdb77e03 100644 --- a/l10n_br_cnab_structure/wizard/cnab_import_wizard.py +++ b/l10n_br_cnab_structure/wizard/cnab_import_wizard.py @@ -129,7 +129,8 @@ def _get_lines_from_file(self, file): file = base64.b64decode(self.return_file) string = StringIO(file.decode("utf-8")) lines = string.readlines() - lines = [line.replace("\r\n", "") for line in lines] + # Remove special character at the end of the line. + lines = [line.replace("\r", "").replace("\n", "") for line in lines] return lines def _check_bank(self, line):