From 2e24fd383e42a95d8615d33f053c10ffd24d1048 Mon Sep 17 00:00:00 2001 From: nie Date: Fri, 27 Nov 2020 10:39:09 +0000 Subject: [PATCH] [FIX] base: company not deleted on linked partners Steps: - Install Contacts - Have Multi-Companies enabled - Go to a Contact of type "Company" - Click "Sales & Purchases" - Assign it the company you're on - Save and Edit again - Remove the assigned company - Go to a contact of type "Individual" linked to this company - Click "Sales & Purchases" Bug: The company has not been deleted on the linked Individual Explanation: When we delete the company of a contact, the value of `vals.get('company_id')` is `False`. This is why it didn't enter the propagation process. This fix makes sure we can still remove a company from a contact linked to a user. Related: https://github.com/odoo/odoo/pull/62418 opw:2391464 closes odoo/odoo#62591 X-original-commit: 59413ad88a97b3221526c2b560178a0f94ddee05 Signed-off-by: Nicolas Lempereur (nle) Signed-off-by: backspac --- odoo/addons/base/models/res_partner.py | 9 +++++---- odoo/addons/base/tests/test_res_partner.py | 23 ++++++++++++++++++++++ 2 files changed, 28 insertions(+), 4 deletions(-) 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})