diff --git a/odoo/addons/base/models/res_partner.py b/odoo/addons/base/models/res_partner.py index c1cc1d04570..d6c13fc57d1 100644 --- a/odoo/addons/base/models/res_partner.py +++ b/odoo/addons/base/models/res_partner.py @@ -527,16 +527,17 @@ class Partner(models.Model): vals['website'] = self._clean_website(vals['website']) if vals.get('parent_id'): vals['company_name'] = False - if vals.get('company_id'): - company = self.env['res.company'].browse(vals['company_id']) + if 'company_id' in vals: + company_id = vals['company_id'] for partner in self: - if partner.user_ids: + if company_id and partner.user_ids: + company = self.env['res.company'].browse(company_id) companies = set(user.company_id for user in partner.user_ids) if len(companies) > 1 or company not in companies: raise UserError( ("The selected company is not compatible with the companies of the related user(s)")) if partner.child_ids: - partner.child_ids.write({'company_id': company.id}) + partner.child_ids.write({'company_id': company_id}) result = True # To write in SUPERUSER on field is_company and avoid access rights problems. if 'is_company' in vals and self.user_has_groups('base.group_partner_manager') and not self.env.su: diff --git a/odoo/addons/base/tests/test_res_partner.py b/odoo/addons/base/tests/test_res_partner.py index 2a6722553af..743303ac100 100644 --- a/odoo/addons/base/tests/test_res_partner.py +++ b/odoo/addons/base/tests/test_res_partner.py @@ -2,6 +2,7 @@ # Part of Odoo. See LICENSE file for full copyright and licensing details. from odoo.tests.common import TransactionCase +from odoo.exceptions import UserError class TestPartner(TransactionCase): @@ -17,3 +18,25 @@ class TestPartner(TransactionCase): ns_res = self.env['res.partner'].name_search('Vlad', args=[('user_ids.email', 'ilike', 'vlad')]) self.assertEqual(set(i[0] for i in ns_res), set(test_user.partner_id.ids)) + + def test_company_change_propagation(self): + """ Check propagation of company_id across children """ + User = self.env['res.users'] + Partner = self.env['res.partner'] + Company = self.env['res.company'] + + company_1 = Company.create({'name': 'company_1'}) + company_2 = Company.create({'name': 'company_2'}) + + test_partner_company = Partner.create({'name': 'This company'}) + test_user = User.create({'name': 'This user', 'login': 'thisu', 'email': 'this.user@example.com', 'company_id': company_1.id, 'company_ids': [company_1.id]}) + test_user.partner_id.write({'parent_id': test_partner_company.id}) + + test_partner_company.write({'company_id': company_1.id}) + self.assertEqual(test_user.partner_id.company_id.id, company_1.id, "The new company_id of the partner company should be propagated to its children") + + test_partner_company.write({'company_id': False}) + self.assertFalse(test_user.partner_id.company_id.id, "If the company_id is deleted from the partner company, it should be propagated to its children") + + with self.assertRaises(UserError, msg="You should not be able to update the company_id of the partner company if the linked user of a child partner is not an allowed to be assigned to that company"), self.cr.savepoint(): + test_partner_company.write({'company_id': company_2.id})