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

[15.0][FW][FIX] operating_unit: default operating unit more multi-company friendly #643

Merged
merged 1 commit into from
Jan 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion operating_unit/models/res_users.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,19 @@
if not uid2:
uid2 = self.env.user.id
user = self.env["res.users"].browse(uid2)
return user.default_operating_unit_id
# check if the company of the default OU is active
if user.default_operating_unit_id.sudo().company_id in self.env.companies:
return user.default_operating_unit_id
else:
# find an OU of the main active company
for ou in user.assigned_operating_unit_ids:
if ou.sudo().company_id in self.env.company:
return ou
# find an OU of any active company
for ou in user.assigned_operating_unit_ids:
if ou.sudo().company_id in self.env.companies:
return ou

Check warning on line 28 in operating_unit/models/res_users.py

View check run for this annotation

Codecov / codecov/patch

operating_unit/models/res_users.py#L28

Added line #L28 was not covered by tests
return False

@api.model
def _default_operating_unit(self):
Expand Down
42 changes: 40 additions & 2 deletions operating_unit/tests/test_operating_unit.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ def setUp(self):
self.grp_ou_multi = self.env.ref("operating_unit.group_multi_operating_unit")
# Company
self.company = self.env.ref("base.main_company")
self.company_2 = self.env["res.company"].create({"name": "Second company"})
# Main Operating Unit
self.ou1 = self.env.ref("operating_unit.main_operating_unit")
# B2C Operating Unit
Expand Down Expand Up @@ -50,12 +51,21 @@ def _create_user(self, login, group, company, operating_units, context=None):
)
return user

def _create_operating_unit(self, uid, name, code):
def _create_operating_unit(self, uid, name, code, company_id=None):
"""Create Operating Unit"""
if company_id is None:
company_id = self.company
ou = (
self.env["operating.unit"]
.with_user(uid)
.create({"name": name, "code": code, "partner_id": self.company.id})
.create(
{
"name": name,
"code": code,
"partner_id": company_id.partner_id.id,
"company_id": company_id.id,
}
)
)
return ou

Expand Down Expand Up @@ -138,3 +148,31 @@ def test_02_operating_unit(self):
line.code = "007"
user_form.name = "Test Customer"
user_form.login = "test2"

def test_03_operating_unit(self):
"""
The method operating_unit_default_get should not return
operating units belonging to a company that is not active
"""
self.assertEqual(
self.res_users_model.operating_unit_default_get(uid2=self.user1.id),
self.ou1,
)
self.assertEqual(
self.res_users_model.with_company(
self.company_2
).operating_unit_default_get(uid2=self.user1.id),
False,
)

self.user1.company_ids += self.company_2
ou_company_2 = self._create_operating_unit(
self.user1.id, "Test Company", "TESTC", self.company_2
)
self.user1.assigned_operating_unit_ids += ou_company_2
self.assertEqual(
self.res_users_model.with_company(
self.company_2
).operating_unit_default_get(uid2=self.user1.id),
ou_company_2,
)
Loading