Skip to content

Commit

Permalink
[FIX] *_import_online: refactor link between provider and journal
Browse files Browse the repository at this point in the history
  • Loading branch information
NL66278 committed Sep 25, 2023
1 parent 9223fcb commit 02fd776
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 24 deletions.
45 changes: 21 additions & 24 deletions account_statement_import_online/models/account_journal.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,16 @@
class AccountJournal(models.Model):
_inherit = "account.journal"

@api.model
def _selection_service(self):
OnlineBankStatementProvider = self.env["online.bank.statement.provider"]
return OnlineBankStatementProvider._get_available_services() + [
("dummy", "Dummy")
]

# Keep provider fields for compatibility with other modules.
online_bank_statement_provider = fields.Selection(
related="online_bank_statement_provider_id.service",
readonly=False,
selection=lambda self: self._selection_service(),
)
online_bank_statement_provider_id = fields.Many2one(
string="Statement Provider",
Expand All @@ -28,17 +34,18 @@ def __get_bank_statements_available_sources(self):
result.append(("online", _("Online (OCA)")))
return result

def _update_online_bank_statement_provider_id(self, service):
def _update_providers(self):
"""Automatically create service.
This method exists for compatibility reasons. The preferred method
to create an online provider is directly through the menu,
"""
OnlineBankStatementProvider = self.env["online.bank.statement.provider"]
for journal in self:
for journal in self.filtered("online_bank_statement_provider"):
service = journal.online_bank_statement_provider
if (
journal.online_bank_statement_provider_id
and journal.online_bank_statement_provider == service
and service == journal.online_bank_statement_provider_id.service
):
_logger.info(
"Journal %s already linked to service %s", journal.name, service
Expand All @@ -61,21 +68,18 @@ def _update_online_bank_statement_provider_id(self, service):
journal.online_bank_statement_provider_id = provider
_logger.info("Journal %s now linked to service %s", journal.name, service)

Check warning on line 69 in account_statement_import_online/models/account_journal.py

View check run for this annotation

Codecov / codecov/patch

account_statement_import_online/models/account_journal.py#L68-L69

Added lines #L68 - L69 were not covered by tests

@api.model
def create(self, vals):
self._update_vals(vals)
service = self._get_service(vals)
rec = super().create(vals)
if service:
rec._update_online_bank_statement_provider_id(service)
return rec
@api.model_create_multi
def create(self, vals_list):
for vals in vals_list:
self._update_vals(vals)
journals = super().create(vals_list)
journals._update_providers()
return journals

def write(self, vals):
self._update_vals(vals)
service = self._get_service(vals)
res = super().write(vals)
if service:
self._update_online_bank_statement_provider_id(service)
self._update_providers()
return res

def _update_vals(self, vals):
Expand All @@ -84,16 +88,9 @@ def _update_vals(self, vals):
"bank_statements_source" in vals
and vals.get("bank_statements_source") != "online"
):
vals["online_bank_statement_provider"] = False
vals["online_bank_statement_provider_id"] = False

def _get_service(self, vals):
"""Check wether user wants to create service."""
return (
vals.pop("online_bank_statement_provider")
if "online_bank_statement_provider" in vals
else False
)

def action_online_bank_statements_pull_wizard(self):
"""This method is also kept for compatibility reasons."""
self.ensure_one()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,10 +128,24 @@ def _update_journals(self):
this.journal_id.write(
{
"online_bank_statement_provider_id": this.id,
"online_bank_statement_provider": this.service,
"bank_statements_source": "online",
}
)

def unlink(self):
"""Reset journals."""
journals = self.mapped("journal_id")
if journals:
vals = {
"bank_statements_source": "undefined",
"online_bank_statement_provider": False,
"online_bank_statement_provider_id": False,
}
journals.write(vals)
result = super().unlink()
return result

@api.model
def _get_available_services(self):
"""Hook for extension"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,13 @@ def test_dont_create_empty_statements(self):
self.assertEqual(statements[1].balance_end, 200)
self.assertEqual(len(statements[1].line_ids), 1)

def test_unlink_provider(self):
"""Unlink provider should clear fields on journal."""
self.provider.unlink()
self.assertEqual(self.journal.bank_statements_source, "undefined")
self.assertEqual(self.journal.online_bank_statement_provider, False)
self.assertEqual(self.journal.online_bank_statement_provider_id.id, False)

def _getExpectedStatements(self, expected_length):
"""Check for length of statement recordset, with helpfull logging."""
statements = self.AccountBankStatement.search(
Expand Down

0 comments on commit 02fd776

Please sign in to comment.