[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) <nle@odoo.com>
Signed-off-by: backspac <backspac@users.noreply.github.com>
This commit is contained in:
nie
2020-11-30 10:28:35 +00:00
parent b46153f6cf
commit 2e24fd383e
2 changed files with 28 additions and 4 deletions
+5 -4
View File
@@ -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:
@@ -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})