diff --git a/account_bank_statement_import_online/models/online_bank_statement_provider.py b/account_bank_statement_import_online/models/online_bank_statement_provider.py index 8316a6c..caa2fa6 100644 --- a/account_bank_statement_import_online/models/online_bank_statement_provider.py +++ b/account_bank_statement_import_online/models/online_bank_statement_provider.py @@ -106,6 +106,7 @@ class OnlineBankStatementProvider(models.Model): certificate_public_key = fields.Text() certificate_private_key = fields.Text() certificate_chain = fields.Text() + allow_empty_statements = fields.Boolean() _sql_constraints = [ ( @@ -219,6 +220,14 @@ class OnlineBankStatementProvider(models.Model): lines_data = [] if not statement_values: statement_values = {} + if ( + not lines_data + and not statement_values + and not self.allow_empty_statements + ): + # Continue with next possible statement. + statement_date_since = statement_date_until + continue statement = AccountBankStatement.search([ ('journal_id', '=', provider.journal_id.id), ('state', '=', 'open'), diff --git a/account_bank_statement_import_online/readme/CONFIGURE.rst b/account_bank_statement_import_online/readme/CONFIGURE.rst index d2ae6f2..e7831ca 100644 --- a/account_bank_statement_import_online/readme/CONFIGURE.rst +++ b/account_bank_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_bank_statement_import_online/tests/test_account_bank_statement_import_online.py b/account_bank_statement_import_online/tests/test_account_bank_statement_import_online.py index b42b8ad..26fe96f 100644 --- a/account_bank_statement_import_online/tests/test_account_bank_statement_import_online.py +++ b/account_bank_statement_import_online/tests/test_account_bank_statement_import_online.py @@ -2,6 +2,7 @@ # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). from datetime import date, datetime +from unittest import mock from dateutil.relativedelta import relativedelta from psycopg2 import IntegrityError from urllib.error import HTTPError @@ -10,6 +11,12 @@ from odoo.tests import common from odoo.tools import mute_logger from odoo import fields +mock_obtain_statement_data = ( + "odoo.addons.account_bank_statement_import_online.tests." + + "online_bank_statement_provider_dummy.OnlineBankStatementProviderDummy." + + "_obtain_statement_data" +) + class TestAccountBankAccountStatementImportOnline(common.TransactionCase): @@ -642,3 +649,82 @@ 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 [ + { + "name": "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(len(statements[1].line_ids), 1) + self.assertEqual(statements[0].balance_start, 100) + 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(len(statements[3].line_ids), 1) + # Empty + self.assertEqual(statements[2].balance_start, 100) + self.assertEqual(len(statements[2].line_ids), 0) diff --git a/account_bank_statement_import_online/views/online_bank_statement_provider.xml b/account_bank_statement_import_online/views/online_bank_statement_provider.xml index 8879d4b..7ea8fd4 100644 --- a/account_bank_statement_import_online/views/online_bank_statement_provider.xml +++ b/account_bank_statement_import_online/views/online_bank_statement_provider.xml @@ -80,6 +80,7 @@ +