From 404387072e79327c21ad252e594711d1f75463d1 Mon Sep 17 00:00:00 2001 From: Carlos Lopez Date: Mon, 30 Dec 2024 10:26:13 -0500 Subject: [PATCH] [IMP] account_statement_import_online: add option to enable or disable statement creation. Starting from Odoo 16, bank statements are optional. This commit introduces the option to create statements automatically or skip their creation. --- .../models/online_bank_statement_provider.py | 47 ++++++++++++------- ...st_account_bank_statement_import_online.py | 9 ++++ .../views/online_bank_statement_provider.xml | 1 + 3 files changed, 39 insertions(+), 18 deletions(-) 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 e62e9dfe3..0a99599aa 100644 --- a/account_statement_import_online/models/online_bank_statement_provider.py +++ b/account_statement_import_online/models/online_bank_statement_provider.py @@ -26,6 +26,10 @@ class OnlineBankStatementProvider(models.Model): company_id = fields.Many2one(related="journal_id.company_id", store=True) active = fields.Boolean(default=True) + create_statement = fields.Boolean( + default=True, + help="Create statements automatically or not. Since V16, statements are optional.", + ) name = fields.Char(compute="_compute_name", store=True) journal_id = fields.Many2one( comodel_name="account.journal", @@ -81,6 +85,7 @@ class OnlineBankStatementProvider(models.Model): ("monthly", "Monthly statements"), ], default="daily", + string="Transaction Syncronization", required=True, ) api_base = fields.Char() @@ -297,27 +302,33 @@ def make_statement_name(self, statement_date_since): def _statement_create_or_write(self, statement_values): """Final creation of statement if new, else write.""" AccountBankStatement = self.env["account.bank.statement"] + AccountBankStatementLine = self.env["account.bank.statement.line"] is_scheduled = self.env.context.get("scheduled") - if is_scheduled: - AccountBankStatement = AccountBankStatement.with_context( - tracking_disable=True, + if self.create_statement: + if is_scheduled: + AccountBankStatement = AccountBankStatement.with_context( + tracking_disable=True, + ) + statement_name = statement_values["name"] + statement = AccountBankStatement.search( + [ + ("journal_id", "=", self.journal_id.id), + ("name", "=", statement_name), + ], + limit=1, ) - statement_name = statement_values["name"] - statement = AccountBankStatement.search( - [ - ("journal_id", "=", self.journal_id.id), - ("name", "=", statement_name), - ], - limit=1, - ) - if not statement: - statement_values["journal_id"] = self.journal_id.id - statement = AccountBankStatement.with_context( - journal_id=self.journal_id.id, - ).create(statement_values) + if not statement: + statement_values["journal_id"] = self.journal_id.id + statement = AccountBankStatement.with_context( + journal_id=self.journal_id.id, + ).create(statement_values) + else: + statement.write(statement_values) + return statement else: - statement.write(statement_values) - return statement + lines = [line[2] for line in statement_values.get("line_ids", [])] + AccountBankStatementLine.create(lines) + return AccountBankStatement # Return empty statement def _get_statement_filtered_lines( self, 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 26ac1c58d..526fdf568 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 @@ -385,6 +385,15 @@ def test_dont_create_empty_statements(self): self.assertEqual(statements[1].balance_end, 200) self.assertEqual(len(statements[1].line_ids), 1) + def test_dont_create_statement(self): + self.provider.statement_creation_mode = "monthly" + self.provider.create_statement = False + date_since = datetime(2024, 12, 1) + date_until = datetime(2024, 12, 31, 23, 59, 59) + self.provider.with_context(step={"days": 1})._pull(date_since, date_until) + self._getExpectedStatements(0) + self._getExpectedLines(31) + def test_unlink_provider(self): """Unlink provider should clear fields on journal.""" self.provider.unlink() 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 c21a9ce6e..d136a2918 100644 --- a/account_statement_import_online/views/online_bank_statement_provider.xml +++ b/account_statement_import_online/views/online_bank_statement_provider.xml @@ -83,6 +83,7 @@ +