mirror of
https://github.com/OCA/bank-statement-import.git
synced 2025-01-20 12:37:43 +02:00
[IMP] *_import_online: always update next_run to cover current period
This commit is contained in:
@@ -1,5 +1,6 @@
|
|||||||
# Copyright 2019-2020 Brainbean Apps (https://brainbeanapps.com)
|
# Copyright 2019-2020 Brainbean Apps (https://brainbeanapps.com)
|
||||||
# Copyright 2020 CorporateHub (https://corporatehub.eu)
|
# 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).
|
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
|
||||||
|
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -69,7 +69,10 @@ class OnlineBankStatementProvider(models.Model):
|
|||||||
string="Update Schedule",
|
string="Update Schedule",
|
||||||
compute="_compute_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(
|
next_run = fields.Datetime(
|
||||||
string="Next scheduled pull",
|
string="Next scheduled pull",
|
||||||
default=fields.Datetime.now,
|
default=fields.Datetime.now,
|
||||||
@@ -368,7 +371,8 @@ class OnlineBankStatementProvider(models.Model):
|
|||||||
"Pulling online bank statements of: %s"
|
"Pulling online bank statements of: %s"
|
||||||
% ", ".join(providers.mapped("journal_id.name"))
|
% ", ".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 = (
|
date_since = (
|
||||||
(provider.last_successful_run)
|
(provider.last_successful_run)
|
||||||
if 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.")
|
_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):
|
def _obtain_statement_data(self, date_since, date_until):
|
||||||
"""Hook for extension"""
|
"""Hook for extension"""
|
||||||
# Check tests/online_bank_statement_provider_dummy.py for reference
|
# Check tests/online_bank_statement_provider_dummy.py for reference
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
|
# 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
|
from . import test_account_journal
|
||||||
|
|||||||
@@ -21,7 +21,7 @@ mock_obtain_statement_data = (
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
class TestAccountBankAccountStatementImportOnline(common.SavepointCase):
|
class TestAccountAccountStatementImportOnline(common.SavepointCase):
|
||||||
@classmethod
|
@classmethod
|
||||||
def setUpClass(cls):
|
def setUpClass(cls):
|
||||||
super().setUpClass()
|
super().setUpClass()
|
||||||
@@ -765,3 +765,25 @@ class TestAccountBankAccountStatementImportOnline(common.SavepointCase):
|
|||||||
self.assertEqual(statements[0].balance_start, 100)
|
self.assertEqual(statements[0].balance_start, 100)
|
||||||
self.assertEqual(statements[0].balance_end_real, 200)
|
self.assertEqual(statements[0].balance_end_real, 200)
|
||||||
self.assertEqual(len(statements[0].line_ids), 1)
|
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)
|
||||||
@@ -75,6 +75,7 @@
|
|||||||
<field name="interval_type" />
|
<field name="interval_type" />
|
||||||
</div>
|
</div>
|
||||||
<field name="next_run" />
|
<field name="next_run" />
|
||||||
|
<field name="last_successful_run" />
|
||||||
</group>
|
</group>
|
||||||
<group name="configuration" string="Configuration">
|
<group name="configuration" string="Configuration">
|
||||||
<field name="statement_creation_mode" />
|
<field name="statement_creation_mode" />
|
||||||
|
|||||||
Reference in New Issue
Block a user