From 0248e490f62507b562aa9b21bdae2dec911c869d Mon Sep 17 00:00:00 2001 From: Goffin Simon Date: Wed, 4 Jan 2023 13:55:38 +0000 Subject: [PATCH] [FIX] base: Commercial fields not syncing after changing parent_id Steps to reproduce the bug: Here are aliases for the used contacts: - A: Azure Interior - B: Gemini Furniture - C: Gemini Furniture, Edwin Hansen - D: (delivery address for Edwin Hansen) 1. Login on a runbot. 2. Set VAT of A and B to different values. 3. Open C, and create a delivery address: D 4. Check: VAT of D = VAT of B 5. Change the related company of C from B to A. Bug: The VAT of C = VAT of A, however VAT of D did not change. When syncing the commercial fields of a partner, we also sync his childs. PS: Due to function _compute_commercial_partner, the commercial partner is always updated for all the childs. So the condition child.commercial_partner_id != self.commercial_partner_id is never satisfied in function _children_sync and the commercial fields were never updated. opw:3111493 closes odoo/odoo#115932 X-original-commit: df87448f2a39eead079cffea566cd760a8918542 Signed-off-by: Nicolas Lempereur (nle) Signed-off-by: Raphael Collet Co-authored-by: kmagusiak <> --- odoo/addons/base/models/res_partner.py | 1 + odoo/addons/base/tests/test_res_partner.py | 23 ++++++++++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/odoo/addons/base/models/res_partner.py b/odoo/addons/base/models/res_partner.py index 4e62de90ee3..f1ccf26f554 100644 --- a/odoo/addons/base/models/res_partner.py +++ b/odoo/addons/base/models/res_partner.py @@ -561,6 +561,7 @@ class Partner(models.Model): if commercial_partner != self: sync_vals = commercial_partner._update_fields_values(self._commercial_fields()) self.write(sync_vals) + self._commercial_sync_to_children() def _commercial_sync_to_children(self): """ Handle sync of commercial fields to descendants """ diff --git a/odoo/addons/base/tests/test_res_partner.py b/odoo/addons/base/tests/test_res_partner.py index ea5f0e57f19..6ad7c7989f3 100644 --- a/odoo/addons/base/tests/test_res_partner.py +++ b/odoo/addons/base/tests/test_res_partner.py @@ -65,6 +65,29 @@ class TestPartner(TransactionCase): 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}) + def test_commercial_field_sync(self): + """Check if commercial fields are synced properly: testing with VAT field""" + Partner = self.env['res.partner'] + company_1 = Partner.create({'name': 'company 1', 'is_company': True, 'vat': 'BE0123456789'}) + company_2 = Partner.create({'name': 'company 2', 'is_company': True, 'vat': 'BE9876543210'}) + + partner = Partner.create({'name': 'someone', 'is_company': False, 'parent_id': company_1.id}) + Partner.flush_recordset() + self.assertEqual(partner.vat, company_1.vat, "VAT should be inherited from the company 1") + + # create a delivery address for the partner + delivery = Partner.create({'name': 'somewhere', 'type': 'delivery', 'parent_id': partner.id}) + self.assertEqual(delivery.commercial_partner_id.id, company_1.id, "Commercial partner should be recomputed") + self.assertEqual(delivery.vat, company_1.vat, "VAT should be inherited from the company 1") + + # move the partner to another company + partner.write({'parent_id': company_2.id}) + partner.flush_recordset() + self.assertEqual(partner.commercial_partner_id.id, company_2.id, "Commercial partner should be recomputed") + self.assertEqual(partner.vat, company_2.vat, "VAT should be inherited from the company 2") + self.assertEqual(delivery.commercial_partner_id.id, company_2.id, "Commercial partner should be recomputed on delivery") + self.assertEqual(delivery.vat, company_2.vat, "VAT should be inherited from the company 2 to delivery") + def test_lang_computation_code(self): """ Check computation of lang: coming from installed languages, forced default value and propagation from parent."""