diff --git a/account_statement_import_online/__manifest__.py b/account_statement_import_online/__manifest__.py index 8d5f5ad9..70e97e03 100644 --- a/account_statement_import_online/__manifest__.py +++ b/account_statement_import_online/__manifest__.py @@ -1,5 +1,6 @@ # Copyright 2019-2020 Brainbean Apps (https://brainbeanapps.com) # Copyright 2020 CorporateHub (https://corporatehub.eu) +# Copyright 2022 Therp BV (https://therp.nl) # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). { 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 11021f47..3f79f47c 100644 --- a/account_statement_import_online/models/online_bank_statement_provider.py +++ b/account_statement_import_online/models/online_bank_statement_provider.py @@ -69,7 +69,10 @@ class OnlineBankStatementProvider(models.Model): string="Update Schedule", compute="_compute_update_schedule", ) - last_successful_run = fields.Datetime(string="Last successful pull") + last_successful_run = fields.Datetime( + string="Last successful pull", + readonly=True, + ) next_run = fields.Datetime( string="Next scheduled pull", default=fields.Datetime.now, @@ -368,7 +371,8 @@ class OnlineBankStatementProvider(models.Model): "Pulling online bank statements of: %s" % ", ".join(providers.mapped("journal_id.name")) ) - for provider in providers.with_context({"scheduled": True}): + for provider in providers.with_context(scheduled=True): + provider._adjust_schedule() date_since = ( (provider.last_successful_run) if provider.last_successful_run @@ -379,6 +383,22 @@ class OnlineBankStatementProvider(models.Model): _logger.info("Scheduled pull of online bank statements complete.") + def _adjust_schedule(self): + """Make sure next_run is current. + + Current means adding one more period would put if after the + current moment. This will be done at the end of the run. + The net effect of this method and the adjustment after the run + will be for the next_run to be in the future. + """ + self.ensure_one() + delta = self._get_next_run_period() + now = datetime.now() + next_run = self.next_run + delta + while next_run < now: + self.next_run = next_run + next_run = self.next_run + delta + def _obtain_statement_data(self, date_since, date_until): """Hook for extension""" # Check tests/online_bank_statement_provider_dummy.py for reference diff --git a/account_statement_import_online/tests/__init__.py b/account_statement_import_online/tests/__init__.py index 9f86f27b..0f30959b 100644 --- a/account_statement_import_online/tests/__init__.py +++ b/account_statement_import_online/tests/__init__.py @@ -1,4 +1,4 @@ # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). -from . import test_account_bank_statement_import_online +from . import test_account_statement_import_online from . import test_account_journal diff --git a/account_statement_import_online/tests/test_account_bank_statement_import_online.py b/account_statement_import_online/tests/test_account_statement_import_online.py similarity index 96% rename from account_statement_import_online/tests/test_account_bank_statement_import_online.py rename to account_statement_import_online/tests/test_account_statement_import_online.py index 7d0a5c17..d0f93a69 100644 --- a/account_statement_import_online/tests/test_account_bank_statement_import_online.py +++ b/account_statement_import_online/tests/test_account_statement_import_online.py @@ -21,7 +21,7 @@ mock_obtain_statement_data = ( ) -class TestAccountBankAccountStatementImportOnline(common.SavepointCase): +class TestAccountAccountStatementImportOnline(common.SavepointCase): @classmethod def setUpClass(cls): super().setUpClass() @@ -765,3 +765,25 @@ class TestAccountBankAccountStatementImportOnline(common.SavepointCase): 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_schedule_adjustment(self): + 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.interval_type = "hours" + five_minutes_ago = self.now - relativedelta(minutes=5) + three_hours_earlier = five_minutes_ago - relativedelta(hours=3) + fiftyfive_minutes_later = five_minutes_ago + relativedelta(hours=1) + provider.next_run = three_hours_earlier + provider.last_successful_run = three_hours_earlier - relativedelta(hours=1) + provider._scheduled_pull() + self.assertEqual(provider.last_successful_run, five_minutes_ago) + self.assertEqual(provider.next_run, fiftyfive_minutes_later) 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 49662d5a..f31476e8 100644 --- a/account_statement_import_online/views/online_bank_statement_provider.xml +++ b/account_statement_import_online/views/online_bank_statement_provider.xml @@ -75,6 +75,7 @@ +