account_ebics - mulit-company fix company_ids field

This commit is contained in:
Luc De Meyer 2022-12-11 20:01:35 +01:00
parent 215753d87f
commit 9dbc63a17f
5 changed files with 81 additions and 6 deletions

View File

@ -3,7 +3,7 @@
{ {
"name": "EBICS banking protocol", "name": "EBICS banking protocol",
"version": "15.0.1.0.1", "version": "15.0.1.1.0",
"license": "LGPL-3", "license": "LGPL-3",
"author": "Noviat", "author": "Noviat",
"website": "https://www.noviat.com", "website": "https://www.noviat.com",

View File

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

View File

@ -28,6 +28,7 @@ class EbicsConfig(models.Model):
) )
journal_ids = fields.Many2many( journal_ids = fields.Many2many(
comodel_name="account.journal", comodel_name="account.journal",
relation="account_journal_ebics_config_rel",
readonly=True, readonly=True,
states={"draft": [("readonly", False)]}, states={"draft": [("readonly", False)]},
string="Bank Accounts", string="Bank Accounts",
@ -148,8 +149,9 @@ class EbicsConfig(models.Model):
active = fields.Boolean(default=True) active = fields.Boolean(default=True)
company_ids = fields.Many2many( company_ids = fields.Many2many(
comodel_name="res.company", comodel_name="res.company",
relation="ebics_config_res_company_rel",
string="Companies", string="Companies",
required=True, readonly=True,
help="Companies sharing this EBICS contract.", help="Companies sharing this EBICS contract.",
) )
@ -181,9 +183,26 @@ class EbicsConfig(models.Model):
) )
) )
@api.onchange("journal_ids") def write(self, vals):
def _onchange_journal_ids(self): """
self.company_ids = self.journal_ids.mapped("company_id") 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): def unlink(self):
for ebics_config in self: for ebics_config in self:

View File

@ -245,6 +245,8 @@ class EbicsFile(models.Model):
statement_ids = [x["statement_id"] for x in sts_data] statement_ids = [x["statement_id"] for x in sts_data]
if statement_ids: if statement_ids:
self.sudo().bank_statement_ids = [(4, x) for x in 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) ctx = dict(self.env.context, statement_ids=statement_ids)
module = __name__.split("addons.")[1].split(".")[0] module = __name__.split("addons.")[1].split(".")[0]
result_view = self.env.ref("%s.ebics_file_view_form_result" % module) result_view = self.env.ref("%s.ebics_file_view_form_result" % module)

View File

@ -68,8 +68,8 @@
name="order_number" name="order_number"
attrs="{'invisible': [('ebics_version', '!=', 'H003')]}" attrs="{'invisible': [('ebics_version', '!=', 'H003')]}"
/> />
<field name="company_ids" widget="many2many_tags" invisible="1" />
</group> </group>
<field name="company_ids" invisible="1" />
</group> </group>
<notebook> <notebook>
<page string="EBICS Users" groups="account_ebics.group_ebics_manager"> <page string="EBICS Users" groups="account_ebics.group_ebics_manager">