[IMP] *_import_online: always update next_run to cover current period

This commit is contained in:
Ronald Portier (Therp BV)
2023-01-06 22:26:37 +01:00
parent 7a398d195e
commit 2a4e793e29
5 changed files with 48 additions and 4 deletions

View File

@@ -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).
{

View File

@@ -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

View File

@@ -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

View File

@@ -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)

View File

@@ -75,6 +75,7 @@
<field name="interval_type" />
</div>
<field name="next_run" />
<field name="last_successful_run" />
</group>
<group name="configuration" string="Configuration">
<field name="statement_creation_mode" />