Skip to content

Commit

Permalink
[IMP] partner_multi_company: prevent inconsistencies between user and…
Browse files Browse the repository at this point in the history
… partner companies
  • Loading branch information
ArnauCForgeFlow committed Jul 23, 2024
1 parent 12caaf0 commit 6c9ea0c
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 11 deletions.
2 changes: 1 addition & 1 deletion partner_multi_company/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
{
"name": "Partner multi-company",
"summary": "Select individually the partner visibility on each company",
"version": "16.0.1.0.1",
"version": "16.0.1.1.0",
"license": "AGPL-3",
"depends": ["base_multi_company"],
"author": "Tecnativa, " "Odoo Community Association (OCA)",
Expand Down
13 changes: 13 additions & 0 deletions partner_multi_company/hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,19 @@ def post_init_hook(cr, registry):
WHERE company_id IS NOT NULL
"""
)
fix_user_partner_companies(cr)


def fix_user_partner_companies(cr):
env = api.Environment(cr, SUPERUSER_ID, {})
for user in env["res.users"].search([]):
user_company_ids = set(user.company_ids.ids)
partner_company_ids = set(user.partner_id.company_ids.ids)
if not user_company_ids.issubset(partner_company_ids) and partner_company_ids:
missing_company_ids = list(user_company_ids - partner_company_ids)
user.partner_id.write(
{"company_ids": [(4, company_id) for company_id in missing_company_ids]}
)


def uninstall_hook(cr, registry):
Expand Down
9 changes: 9 additions & 0 deletions partner_multi_company/migrations/16.0.1.1.0/post-migration.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Copyright 2024 ForgeFlow S.L. (http://www.forgeflow.com)
# License AGPL-3 - See http://www.gnu.org/licenses/agpl-3.0.html

# pylint: disable=W8150
from odoo.addons.partner_multi_company import hooks


def migrate(cr, version):
hooks.fix_user_partner_companies(cr)
33 changes: 32 additions & 1 deletion partner_multi_company/models/res_partner.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
# Copyright 2015-2019 Pedro M. Baeza <[email protected]>
# License AGPL-3 - See http://www.gnu.org/licenses/agpl-3.0.html.html

from odoo import api, fields, models
from odoo import _, api, fields, models
from odoo.exceptions import ValidationError


class ResPartner(models.Model):
Expand Down Expand Up @@ -59,3 +60,33 @@ def _amend_company_id(self, vals):
elif "company_id" not in vals:
vals["company_ids"] = False
return vals

@api.constrains("company_ids")
def _check_company_id(self):
for rec in self:
if rec.user_ids:
user_company_ids = set(rec.user_ids.mapped("company_ids").ids)
partner_company_ids = set(rec.company_ids.ids)

if (
not user_company_ids.issubset(partner_company_ids)
and partner_company_ids
):
raise ValidationError(

Check warning on line 75 in partner_multi_company/models/res_partner.py

View check run for this annotation

Codecov / codecov/patch

partner_multi_company/models/res_partner.py#L75

Added line #L75 was not covered by tests
_(
"The partner must have at least all the companies "
"associated with the user."
)
)

def _inverse_company_id(self):
if self.env.context.get("from_res_users"):
# don't delete all partner company_ids when
# the user's related company_id is modified.
for record in self:
company = record.company_id
if company:
record.company_ids = [(4, company.id)]
return
else:
return super()._inverse_company_id()
22 changes: 17 additions & 5 deletions partner_multi_company/models/res_users.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,28 @@ def create(self, vals):
res = super(ResUsers, self).create(vals)
if "company_ids" in vals:
res.partner_id.company_ids = vals["company_ids"]
if "company_id" in vals and res.partner_id.company_ids:
res.partner_id.company_id = vals["company_id"]
return res

def write(self, vals):
res = super(ResUsers, self).write(vals)
res = super(ResUsers, self.with_context(from_res_users=True)).write(vals)
if "company_ids" in vals:
for user in self.sudo():
if user.partner_id.company_ids:
user.partner_id.company_ids = vals["company_ids"]
new_company_ids = []
company_ids_data = vals["company_ids"]
if isinstance(company_ids_data, list) and user.partner_id.company_ids:
for item in company_ids_data:
if isinstance(item, tuple):
if item[0] == 4:
new_company_ids.append(item[1])
elif item[0] == 6:
new_company_ids.extend(item[2])

Check warning on line 29 in partner_multi_company/models/res_users.py

View check run for this annotation

Codecov / codecov/patch

partner_multi_company/models/res_users.py#L29

Added line #L29 was not covered by tests
elif isinstance(item, list):
new_company_ids = item[2]

Check warning on line 31 in partner_multi_company/models/res_users.py

View check run for this annotation

Codecov / codecov/patch

partner_multi_company/models/res_users.py#L31

Added line #L31 was not covered by tests
else:
new_company_ids.append(item)

Check warning on line 33 in partner_multi_company/models/res_users.py

View check run for this annotation

Codecov / codecov/patch

partner_multi_company/models/res_users.py#L33

Added line #L33 was not covered by tests
user.partner_id.company_ids = [
(4, company_id) for company_id in new_company_ids
]
if "company_id" in vals:
for user in self.sudo():
if user.partner_id.company_ids:
Expand Down
11 changes: 7 additions & 4 deletions partner_multi_company/static/description/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@

/*
:Author: David Goodger ([email protected])
:Id: $Id: html4css1.css 8954 2022-01-20 10:10:25Z milde $
:Id: $Id: html4css1.css 9511 2024-01-13 09:50:07Z milde $
:Copyright: This stylesheet has been placed in the public domain.

Default cascading style sheet for the HTML output of Docutils.
Despite the name, some widely supported CSS2 features are used.

See https://docutils.sourceforge.io/docs/howto/html-stylesheets.html for how to
customize this style sheet.
Expand Down Expand Up @@ -274,7 +275,7 @@
margin-left: 2em ;
margin-right: 2em }

pre.code .ln { color: grey; } /* line numbers */
pre.code .ln { color: gray; } /* line numbers */
pre.code, code { background-color: #eeeeee }
pre.code .comment, code .comment { color: #5C6576 }
pre.code .keyword, code .keyword { color: #3B0D06; font-weight: bold }
Expand All @@ -300,7 +301,7 @@
span.pre {
white-space: pre }

span.problematic {
span.problematic, pre.problematic {
color: red }

span.section-subtitle {
Expand Down Expand Up @@ -437,7 +438,9 @@ <h2><a class="toc-backref" href="#toc-entry-7">Contributors</a></h2>
<div class="section" id="maintainers">
<h2><a class="toc-backref" href="#toc-entry-8">Maintainers</a></h2>
<p>This module is maintained by the OCA.</p>
<a class="reference external image-reference" href="https://odoo-community.org"><img alt="Odoo Community Association" src="https://odoo-community.org/logo.png" /></a>
<a class="reference external image-reference" href="https://odoo-community.org">
<img alt="Odoo Community Association" src="https://odoo-community.org/logo.png" />
</a>
<p>OCA, or the Odoo Community Association, is a nonprofit organization whose
mission is to support the collaborative development of Odoo features and
promote its widespread use.</p>
Expand Down

0 comments on commit 6c9ea0c

Please sign in to comment.