Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
110 changes: 61 additions & 49 deletions account_statement_import_online/models/account_journal.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Copyright 2019-2020 Brainbean Apps (https://brainbeanapps.com)
# Copyright 2019-2020 Dataplug (https://dataplug.io)
# Copyright 2023 Therp BV (https://therp.nl)
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).

import logging

from odoo import _, api, fields, models
Expand All @@ -12,73 +12,85 @@
class AccountJournal(models.Model):
_inherit = "account.journal"

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

# Keep provider fields for compatibility with other modules.
online_bank_statement_provider = fields.Selection(
selection=lambda self: self.env[
"account.journal"
]._selection_online_bank_statement_provider(),
help="Select the type of service provider (a model)",
selection=lambda self: self._selection_service(),
)
online_bank_statement_provider_id = fields.Many2one(
string="Statement Provider",
comodel_name="online.bank.statement.provider",
ondelete="restrict",
copy=False,
help="Select the actual instance of a configured provider (a record).\n"
"Selecting a type of provider will automatically create a provider"
" record linked to this journal.",
)

def __get_bank_statements_available_sources(self):
result = super().__get_bank_statements_available_sources()
result.append(("online", _("Online (OCA)")))
return result

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

@api.model
def values_online_bank_statement_provider(self):
"""Return values for provider type selection in the form view."""
res = self.env["online.bank.statement.provider"]._get_available_services()
if self.user_has_groups("base.group_no_one"):
res += [("dummy", "Dummy")]
return res
def _update_providers(self):
"""Automatically create service.

def _update_online_bank_statement_provider_id(self):
"""Keep provider synchronized with journal."""
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.filtered("id"):
provider_id = journal.online_bank_statement_provider_id
if journal.bank_statements_source != "online":
journal.online_bank_statement_provider_id = False
if provider_id:
provider_id.unlink()
continue
if provider_id.service == journal.online_bank_statement_provider:
for journal in self.filtered("online_bank_statement_provider"):
service = journal.online_bank_statement_provider
if (
journal.online_bank_statement_provider_id
and service == journal.online_bank_statement_provider_id.service
):
_logger.info(
"Journal %s already linked to service %s", journal.name, service
)
# Provider already exists.
continue
journal.online_bank_statement_provider_id = False
if provider_id:
provider_id.unlink()
# fmt: off
journal.online_bank_statement_provider_id = \
OnlineBankStatementProvider.create({
# Use existing or create new provider for service.
provider = OnlineBankStatementProvider.search(
[
("journal_id", "=", journal.id),
("service", "=", service),
],
limit=1,
) or OnlineBankStatementProvider.create(
{
"journal_id": journal.id,
"service": journal.online_bank_statement_provider,
})
# fmt: on
"service": service,
}
)
journal.online_bank_statement_provider_id = provider
_logger.info("Journal %s now linked to service %s", journal.name, service)

@api.model
def create(self, vals):
rec = super().create(vals)
if "bank_statements_source" in vals or "online_bank_statement_provider" in vals:
rec._update_online_bank_statement_provider_id()
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)
res = super().write(vals)
if "bank_statements_source" in vals or "online_bank_statement_provider" in vals:
self._update_online_bank_statement_provider_id()
self._update_providers()
return res

def _update_vals(self, vals):
"""Ensure consistent values."""
if (
"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 action_online_bank_statements_pull_wizard(self):
"""This method is also kept for compatibility reasons."""
self.ensure_one()
provider = self.online_bank_statement_provider_id
return provider.action_online_bank_statements_pull_wizard()
1 change: 0 additions & 1 deletion account_statement_import_online/tests/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).

from . import test_account_statement_import_online
from . import test_account_journal
22 changes: 0 additions & 22 deletions account_statement_import_online/tests/test_account_journal.py

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,9 @@

from dateutil.relativedelta import relativedelta
from odoo_test_helper import FakeModelLoader
from psycopg2 import IntegrityError

from odoo import fields
from odoo.tests import common
from odoo.tools import mute_logger

mock_obtain_statement_data = (
"odoo.addons.account_statement_import_online.tests."
Expand Down Expand Up @@ -43,18 +41,6 @@ def setUpClass(cls):
cls.AccountBankStatement = cls.env["account.bank.statement"]
cls.AccountBankStatementLine = cls.env["account.bank.statement.line"]

def test_provider_unlink_restricted(self):
journal = self.AccountJournal.create(
{"name": "Bank", "type": "bank", "code": "BANK"}
)
with common.Form(journal) as journal_form:
journal_form.bank_statements_source = "online"
journal_form.online_bank_statement_provider = "dummy"
journal_form.save()

with self.assertRaises(IntegrityError), mute_logger("odoo.sql_db"):
journal.online_bank_statement_provider_id.unlink()

def test_cascade_unlink(self):
journal = self.AccountJournal.create(
{"name": "Bank", "type": "bank", "code": "BANK"}
Expand All @@ -75,36 +61,6 @@ def test_cascade_unlink(self):
)
)

def test_source_change_cleanup(self):
journal = self.AccountJournal.create(
{"name": "Bank", "type": "bank", "code": "BANK"}
)
with common.Form(journal) as journal_form:
journal_form.bank_statements_source = "online"
journal_form.online_bank_statement_provider = "dummy"
journal_form.save()

self.assertTrue(journal.online_bank_statement_provider_id)
save_provider_id = journal.online_bank_statement_provider_id.id

# Stuff should not change when doing unrelated write.
journal.write({"code": "BIGBANK"})
self.assertTrue(journal.online_bank_statement_provider_id)
self.assertEqual(journal.online_bank_statement_provider_id.id, save_provider_id)

with common.Form(journal) as journal_form:
journal_form.bank_statements_source = "undefined"
journal_form.save()

self.assertFalse(journal.online_bank_statement_provider_id)
self.assertFalse(
self.OnlineBankStatementProvider.search(
[
("id", "=", save_provider_id),
]
)
)

def test_pull_mode_daily(self):
journal = self.AccountJournal.create(
{
Expand Down
8 changes: 5 additions & 3 deletions account_statement_import_online/views/account_journal.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@
<field name="model">account.journal</field>
<field name="inherit_id" ref="account.view_account_journal_form" />
<field name="arch" type="xml">
<xpath expr="//field[@name='bank_statements_source']/.." position="after">
<xpath
expr="//notebook//field[@name='bank_statements_source']/.."
position="after"
>
<group
name="online_bank_statements"
string="Online Bank Statements (OCA)"
Expand All @@ -31,8 +34,6 @@
attrs="{'required': [('bank_statements_source', '=', 'online')]}"
class="oe_edit_only"
groups="account.group_account_user"
widget="dynamic_dropdown"
values="values_online_bank_statement_provider"
/>
<label
for="online_bank_statement_provider_id"
Expand All @@ -55,6 +56,7 @@
name="%(action_online_bank_statements_pull_wizard)d"
attrs="{'invisible': [('online_bank_statement_provider', '=', False)]}"
string="Pull Online Bank Statement"
groups="account.group_account_user"
/>
</header>
</xpath>
Expand Down
Loading
Loading