From 0c42f56ce07cfbb51ca55076d7a5ba4cfb6bf907 Mon Sep 17 00:00:00 2001 From: Ernesto Tejeda Date: Thu, 19 Aug 2021 18:47:53 -0400 Subject: [PATCH] [IMP] account_statement_import_online: don't import empty statements. --- account_statement_import_online/README.rst | 4 + .../models/online_bank_statement_provider.py | 3 + .../readme/CONFIGURE.rst | 4 + .../static/description/index.html | 3 + ...st_account_bank_statement_import_online.py | 98 +++++++++++++++++++ .../views/online_bank_statement_provider.xml | 1 + 6 files changed, 113 insertions(+) diff --git a/account_statement_import_online/README.rst b/account_statement_import_online/README.rst index 3e610e90..8eaeca5f 100644 --- a/account_statement_import_online/README.rst +++ b/account_statement_import_online/README.rst @@ -56,6 +56,10 @@ or, alternatively: #. Save the bank account #. Click on provider and configure provider-specific settings. +If you want to allow empty bank statements to be created every time the +information is pulled, you can check the option "Allow empty statements" +at the provider configuration level. + **NOTE**: To access these features, user needs to belong to *Show Full Accounting Features* group. diff --git a/account_statement_import_online/models/online_bank_statement_provider.py b/account_statement_import_online/models/online_bank_statement_provider.py index 58675e39..113b976f 100644 --- a/account_statement_import_online/models/online_bank_statement_provider.py +++ b/account_statement_import_online/models/online_bank_statement_provider.py @@ -95,6 +95,7 @@ class OnlineBankStatementProvider(models.Model): certificate_public_key = fields.Text() certificate_private_key = fields.Text() certificate_chain = fields.Text() + allow_empty_statements = fields.Boolean(string="Allow empty statements") _sql_constraints = [ ( @@ -204,6 +205,8 @@ class OnlineBankStatementProvider(models.Model): ) if not data: data = ([], {}) + if not data[0] and not data[1] and not self.allow_empty_statements: + return lines_data, statement_values = data if not lines_data: lines_data = [] diff --git a/account_statement_import_online/readme/CONFIGURE.rst b/account_statement_import_online/readme/CONFIGURE.rst index d2ae6f21..e7831caa 100644 --- a/account_statement_import_online/readme/CONFIGURE.rst +++ b/account_statement_import_online/readme/CONFIGURE.rst @@ -19,5 +19,9 @@ or, alternatively: #. Save the bank account #. Click on provider and configure provider-specific settings. +If you want to allow empty bank statements to be created every time the +information is pulled, you can check the option "Allow empty statements" +at the provider configuration level. + **NOTE**: To access these features, user needs to belong to *Show Full Accounting Features* group. diff --git a/account_statement_import_online/static/description/index.html b/account_statement_import_online/static/description/index.html index 5a9a78fe..6a274b09 100644 --- a/account_statement_import_online/static/description/index.html +++ b/account_statement_import_online/static/description/index.html @@ -406,6 +406,9 @@ section
  • Save the bank account
  • Click on provider and configure provider-specific settings.
  • +

    If you want to allow empty bank statements to be created every time the +information is pulled, you can check the option “Allow empty statements” +at the provider configuration level.

    NOTE: To access these features, user needs to belong to Show Full Accounting Features group.

    diff --git a/account_statement_import_online/tests/test_account_bank_statement_import_online.py b/account_statement_import_online/tests/test_account_bank_statement_import_online.py index 70f722e6..ad132adf 100644 --- a/account_statement_import_online/tests/test_account_bank_statement_import_online.py +++ b/account_statement_import_online/tests/test_account_bank_statement_import_online.py @@ -3,6 +3,7 @@ # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). from datetime import date, datetime +from unittest import mock from urllib.error import HTTPError from dateutil.relativedelta import relativedelta @@ -12,6 +13,12 @@ from odoo import fields from odoo.tests import common from odoo.tools import mute_logger +mock_obtain_statement_data = ( + "odoo.addons.account_statement_import_online.tests." + + "online_bank_statement_provider_dummy.OnlineBankStatementProviderDummy." + + "_obtain_statement_data" +) + class TestAccountBankAccountStatementImportOnline(common.TransactionCase): def setUp(self): @@ -657,3 +664,94 @@ class TestAccountBankAccountStatementImportOnline(common.TransactionCase): self.assertEqual(lines[1].date, date(2020, 4, 18)) self.assertEqual(lines[2].date, date(2020, 4, 18)) self.assertEqual(lines[3].date, date(2020, 4, 18)) + + def _get_statement_line_data(self, statement_date): + return [ + { + "payment_ref": "payment", + "amount": 100, + "date": statement_date, + "unique_import_id": str(statement_date), + "partner_name": "John Doe", + "account_number": "XX00 0000 0000 0000", + } + ], {} + + def test_dont_create_empty_statements(self): + """Test the default behavior of not creating empty bank + statements ('Allow empty statements' field is uncheck at the + provider level.). + """ + journal = self.AccountJournal.create( + { + "name": "Bank", + "type": "bank", + "code": "BANK", + "bank_statements_source": "online", + "online_bank_statement_provider": "dummy", + } + ) + provider = journal.online_bank_statement_provider_id + provider.active = True + provider.statement_creation_mode = "daily" + with mock.patch(mock_obtain_statement_data) as mock_data: + mock_data.side_effect = [ + self._get_statement_line_data(date(2021, 8, 10)), + ([], {}), # August 8th, doesn't have statement + ([], {}), # August 9th, doesn't have statement + self._get_statement_line_data(date(2021, 8, 13)), + ] + provider._pull(datetime(2021, 8, 10), datetime(2021, 8, 14)) + statements = self.AccountBankStatement.search([("journal_id", "=", journal.id)]) + self.assertEqual(len(statements), 2) + self.assertEqual(statements[1].balance_start, 0) + self.assertEqual(statements[1].balance_end_real, 100) + self.assertEqual(len(statements[1].line_ids), 1) + self.assertEqual(statements[0].balance_start, 100) + self.assertEqual(statements[0].balance_end_real, 200) + self.assertEqual(len(statements[0].line_ids), 1) + + def test_create_empty_statements(self): + """Test creating empty bank statements + ('Allow empty statements' field is check at the provider level). + """ + journal = self.AccountJournal.create( + { + "name": "Bank", + "type": "bank", + "code": "BANK", + "bank_statements_source": "online", + "online_bank_statement_provider": "dummy", + } + ) + provider = journal.online_bank_statement_provider_id + provider.active = True + provider.allow_empty_statements = True + provider.statement_creation_mode = "daily" + with mock.patch(mock_obtain_statement_data) as mock_data: + mock_data.side_effect = [ + self._get_statement_line_data(date(2021, 8, 10)), + ([], {}), # August 8th, doesn't have statement + ([], {}), # August 9th, doesn't have statement + self._get_statement_line_data(date(2021, 8, 13)), + ] + provider._pull(datetime(2021, 8, 10), datetime(2021, 8, 14)) + statements = self.AccountBankStatement.search([("journal_id", "=", journal.id)]) + # 4 Statements: 2 with movements and 2 empty + self.assertEqual(len(statements), 4) + # With movement + self.assertEqual(statements[3].balance_start, 0) + self.assertEqual(statements[3].balance_end_real, 100) + self.assertEqual(len(statements[3].line_ids), 1) + # Empty + self.assertEqual(statements[2].balance_start, 100) + self.assertEqual(statements[2].balance_end_real, 100) + self.assertEqual(len(statements[2].line_ids), 0) + # Empty + self.assertEqual(statements[1].balance_start, 100) + self.assertEqual(statements[1].balance_end_real, 100) + self.assertEqual(len(statements[1].line_ids), 0) + # With movement + self.assertEqual(statements[0].balance_start, 100) + self.assertEqual(statements[0].balance_end_real, 200) + self.assertEqual(len(statements[0].line_ids), 1) diff --git a/account_statement_import_online/views/online_bank_statement_provider.xml b/account_statement_import_online/views/online_bank_statement_provider.xml index b81c8a47..49662d5a 100644 --- a/account_statement_import_online/views/online_bank_statement_provider.xml +++ b/account_statement_import_online/views/online_bank_statement_provider.xml @@ -79,6 +79,7 @@ +