Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[15.0][IMP] partner_multi_company: prevent inconsistencies between user and partner #667

Merged
merged 2 commits into from
Jul 23, 2024
Merged
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
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": "15.0.1.0.0",
"version": "15.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 @@ -16,6 +16,19 @@
"base.res_partner_rule",
"res.partner",
)
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)

Check warning on line 28 in partner_multi_company/hooks.py

View check run for this annotation

Codecov / codecov/patch

partner_multi_company/hooks.py#L28

Added line #L28 was not covered by tests
user.partner_id.write(
{"company_ids": [(4, company_id) for company_id in missing_company_ids]}
)


def uninstall_hook(cr, registry):
Expand Down
10 changes: 10 additions & 0 deletions partner_multi_company/migrations/15.0.1.1.0/post-migration.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Copyright 2024 ForgeFlow S.L. (http://www.forgeflow.com)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Double copyright

# Copyright 2024 ForgeFlow S.L. (http://www.forgeflow.com)
# License AGPL-3 - See http://www.gnu.org/licenses/agpl-3.0.html

# pylint: disable=W7950
from odoo.addons.partner_multi_company import hooks
Comment on lines +5 to +6
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
# pylint: disable=W7950
from odoo.addons.partner_multi_company import hooks
from ...hooks import fix_user_partner_companies

and change the call

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not critical, so I launched the merge command.



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 @@ -58,3 +59,33 @@
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 74 in partner_multi_company/models/res_partner.py

View check run for this annotation

Codecov / codecov/patch

partner_multi_company/models/res_partner.py#L74

Added line #L74 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 @@
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:
pedrobaeza marked this conversation as resolved.
Show resolved Hide resolved
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 = []
pedrobaeza marked this conversation as resolved.
Show resolved Hide resolved
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])
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)
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
12 changes: 7 additions & 5 deletions partner_multi_company/static/description/index.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
Expand All @@ -9,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 @@ -275,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 @@ -301,7 +301,7 @@
span.pre {
white-space: pre }

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

span.section-subtitle {
Expand Down Expand Up @@ -438,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
3 changes: 3 additions & 0 deletions res_company_code/demo/res_company.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,7 @@ License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
<record id="base.main_company" model="res.company">
<field name="code">C1</field>
</record>
<record model="res.partner" id="base.partner_demo">
<field name="company_id" eval="False" />
</record>
</odoo>
Loading