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

[FIX] base: set parent company only once #147912

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion odoo/addons/base/models/res_company.py
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ def write(self, values):
# and thus needs to invalidate the assets cache when this is changed
self.env.registry.clear_cache('assets') # not 100% it is useful a test is missing if it is the case

if 'parent_id' in values:
if 'parent_id' in values and any(company.parent_id for company in self):
raise UserError(_("The company hierarchy cannot be changed."))

if values.get('currency_id'):
Expand Down
26 changes: 25 additions & 1 deletion odoo/addons/base/tests/test_res_company.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.

from odoo.exceptions import ValidationError
from odoo.exceptions import ValidationError, UserError
from odoo.tests.common import TransactionCase


Expand Down Expand Up @@ -50,3 +50,27 @@ def test_logo_check(self):
self.assertTrue(company.uses_default_logo)
company.partner_id.image_1920 = "R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7"
self.assertFalse(company.uses_default_logo)

def _create_companies(self):
return self.env['res.company'].create([{'name': f'company_{i}'} for i in range(4)])

def test_setting_parent_company_for_single_child(self):
child_1, _, parent_1, parent_2 = self._create_companies()

# Can set parent company only for the first time
child_1.write({'parent_id': parent_1.id})

# The company hierarchy cannot be changed.
with self.assertRaises(UserError):
child_1.write({'parent_id': parent_2.id})

def test_setting_parent_company_for_multiple_children_01(self):
companies = self._create_companies()
children, parent_1, parent_2 = companies[:2], companies[2], companies[3]

# Can set parent company only for the first time
children.write({'parent_id': parent_1.id})

# The company hierarchy cannot be changed.
with self.assertRaises(UserError):
children.write({'parent_id': parent_2.id})