From 076e4328f8b34e9758dd28bd9921df79be5fc840 Mon Sep 17 00:00:00 2001 From: Luc De Meyer Date: Sun, 11 Dec 2022 20:01:35 +0100 Subject: [PATCH] account_ebics - mulit-company fix company_ids field --- account_ebics/__manifest__.py | 2 +- .../migrations/15.0.1.1/pre-migration.py | 54 +++++++++++++++++++ account_ebics/models/ebics_config.py | 27 ++++++++-- account_ebics/models/ebics_file.py | 2 + account_ebics/views/ebics_config_views.xml | 2 +- 5 files changed, 81 insertions(+), 6 deletions(-) create mode 100644 account_ebics/migrations/15.0.1.1/pre-migration.py diff --git a/account_ebics/__manifest__.py b/account_ebics/__manifest__.py index 121a454..00eaaae 100644 --- a/account_ebics/__manifest__.py +++ b/account_ebics/__manifest__.py @@ -3,7 +3,7 @@ { "name": "EBICS banking protocol", - "version": "15.0.1.0.1", + "version": "15.0.1.1.0", "license": "LGPL-3", "author": "Noviat", "website": "https://www.noviat.com", diff --git a/account_ebics/migrations/15.0.1.1/pre-migration.py b/account_ebics/migrations/15.0.1.1/pre-migration.py new file mode 100644 index 0000000..4082ce7 --- /dev/null +++ b/account_ebics/migrations/15.0.1.1/pre-migration.py @@ -0,0 +1,54 @@ +# Copyright 2009-2022 Noviat. +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + + +def migrate(cr, version): + if not version: + return + + cr.execute("select id from ebics_config") + cfg_ids = [x[0] for x in cr.fetchall()] + for cfg_id in cfg_ids: + cr.execute( + """ + SELECT aj.company_id + FROM account_journal_ebics_config_rel rel + JOIN account_journal aj ON rel.account_journal_id = aj.id + WHERE ebics_config_id = %s + """, + (cfg_id,), + ) + new_cpy_ids = [x[0] for x in cr.fetchall()] + cr.execute( + """ + SELECT res_company_id + FROM ebics_config_res_company_rel + WHERE ebics_config_id = %s + """, + (cfg_id,), + ) + old_cpy_ids = [x[0] for x in cr.fetchall()] + + to_add = [] + for cid in new_cpy_ids: + if cid in old_cpy_ids: + old_cpy_ids.remove(cid) + else: + to_add.append(cid) + if old_cpy_ids: + cr.execute( + """ + DELETE FROM ebics_config_res_company_rel + WHERE res_company_id IN %s + """, + (tuple(old_cpy_ids),), + ) + if to_add: + for cid in to_add: + cr.execute( + """ + INSERT INTO ebics_config_res_company_rel(ebics_config_id, res_company_id) + VALUES (%s, %s); + """, + (cfg_id, cid), + ) diff --git a/account_ebics/models/ebics_config.py b/account_ebics/models/ebics_config.py index b25b668..4cde65c 100644 --- a/account_ebics/models/ebics_config.py +++ b/account_ebics/models/ebics_config.py @@ -28,6 +28,7 @@ class EbicsConfig(models.Model): ) journal_ids = fields.Many2many( comodel_name="account.journal", + relation="account_journal_ebics_config_rel", readonly=True, states={"draft": [("readonly", False)]}, string="Bank Accounts", @@ -148,8 +149,9 @@ class EbicsConfig(models.Model): active = fields.Boolean(default=True) company_ids = fields.Many2many( comodel_name="res.company", + relation="ebics_config_res_company_rel", string="Companies", - required=True, + readonly=True, help="Companies sharing this EBICS contract.", ) @@ -181,9 +183,26 @@ class EbicsConfig(models.Model): ) ) - @api.onchange("journal_ids") - def _onchange_journal_ids(self): - self.company_ids = self.journal_ids.mapped("company_id") + def write(self, vals): + """ + Due to the multi-company nature of the EBICS config we + need to adapt the company_ids in the write method. + """ + if "journal_ids" not in vals: + return super().write(vals) + for rec in self: + old_company_ids = rec.journal_ids.mapped("company_id").ids + super(EbicsConfig, rec).write(vals) + new_company_ids = rec.journal_ids.mapped("company_id").ids + updates = [] + for cid in new_company_ids: + if cid in old_company_ids: + old_company_ids.remove(cid) + else: + updates += [(4, cid)] + updates += [(3, x) for x in old_company_ids] + super(EbicsConfig, rec).write({"company_ids": updates}) + return True def unlink(self): for ebics_config in self: diff --git a/account_ebics/models/ebics_file.py b/account_ebics/models/ebics_file.py index 4da76fd..76cc41c 100644 --- a/account_ebics/models/ebics_file.py +++ b/account_ebics/models/ebics_file.py @@ -245,6 +245,8 @@ class EbicsFile(models.Model): statement_ids = [x["statement_id"] for x in sts_data] if statement_ids: self.sudo().bank_statement_ids = [(4, x) for x in statement_ids] + company_ids = self.sudo().bank_statement_ids.mapped("company_id").ids + self.company_ids = [(6, 0, company_ids)] ctx = dict(self.env.context, statement_ids=statement_ids) module = __name__.split("addons.")[1].split(".")[0] result_view = self.env.ref("%s.ebics_file_view_form_result" % module) diff --git a/account_ebics/views/ebics_config_views.xml b/account_ebics/views/ebics_config_views.xml index b15b21e..f7220d9 100644 --- a/account_ebics/views/ebics_config_views.xml +++ b/account_ebics/views/ebics_config_views.xml @@ -68,8 +68,8 @@ name="order_number" attrs="{'invisible': [('ebics_version', '!=', 'H003')]}" /> + -