From c3c5a07ec904c2c9892569e1285b47ed0f87d494 Mon Sep 17 00:00:00 2001 From: Alessandro Uffreduzzi Date: Thu, 4 Jan 2024 16:25:52 +0100 Subject: [PATCH 1/2] [FIX] operating_unit: default operating unit more multi-company friendly This PR fixes an issue with the method operating_unit_default_get where it would return an operating unit even for a company that was not active, which would cause all sorts of issues Steps to reproduce on runboat, taking as example purchase_operating_unit: - User has a default ou belonging to Company 1 - Switch to Company 2 (make sure Company 1 is not active at all) - Try to create a Purchase Order The issue arises because the operating_unit field is pre-compiled on the PO with an Operating Unit whose company is inactive, and the onchanges cannot find or access related data. --- operating_unit/models/res_users.py | 14 ++++++- operating_unit/tests/test_operating_unit.py | 42 ++++++++++++++++++++- 2 files changed, 53 insertions(+), 3 deletions(-) diff --git a/operating_unit/models/res_users.py b/operating_unit/models/res_users.py index 2b71506a86..34e1e57570 100644 --- a/operating_unit/models/res_users.py +++ b/operating_unit/models/res_users.py @@ -14,7 +14,19 @@ def operating_unit_default_get(self, uid2=False): 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 + return False @api.model def _default_operating_unit(self): diff --git a/operating_unit/tests/test_operating_unit.py b/operating_unit/tests/test_operating_unit.py index 2665b006ca..c7e1ca3ce2 100644 --- a/operating_unit/tests/test_operating_unit.py +++ b/operating_unit/tests/test_operating_unit.py @@ -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 @@ -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 @@ -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, + ) From 955b2060cf875fac4f59eae7511d39f22db80dbd Mon Sep 17 00:00:00 2001 From: OCA-git-bot Date: Wed, 10 Jan 2024 09:30:36 +0000 Subject: [PATCH 2/2] [BOT] post-merge updates --- README.md | 2 +- operating_unit/README.rst | 2 +- operating_unit/__manifest__.py | 2 +- operating_unit/static/description/index.html | 3 +-- 4 files changed, 4 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index bf392abe61..390f187297 100644 --- a/README.md +++ b/README.md @@ -39,7 +39,7 @@ addon | version | maintainers | summary [mis_builder_operating_unit_access_all](mis_builder_operating_unit_access_all/) | 15.0.1.0.0 | [![ps-tubtim](https://github.com/ps-tubtim.png?size=30px)](https://github.com/ps-tubtim) | Access all OUs' MIS Builder [mrp_operating_unit](mrp_operating_unit/) | 15.0.1.0.0 | | Operating Unit in MRP [mrp_operating_unit_access_all](mrp_operating_unit_access_all/) | 15.0.1.0.0 | [![ps-tubtim](https://github.com/ps-tubtim.png?size=30px)](https://github.com/ps-tubtim) | Access all OUs' MRP -[operating_unit](operating_unit/) | 15.0.1.0.6 | | An operating unit (OU) is an organizational entity part of a company +[operating_unit](operating_unit/) | 15.0.1.0.7 | | An operating unit (OU) is an organizational entity part of a company [operating_unit_access_all](operating_unit_access_all/) | 15.0.1.0.0 | [![kittiu](https://github.com/kittiu.png?size=30px)](https://github.com/kittiu) | Access all Operating Units [project_operating_unit](project_operating_unit/) | 15.0.1.0.0 | [![max3903](https://github.com/max3903.png?size=30px)](https://github.com/max3903) | This module adds operating unit information to projects and tasks. [purchase_operating_unit](purchase_operating_unit/) | 15.0.1.1.0 | | Adds the concecpt of operating unit (OU) in purchase order management diff --git a/operating_unit/README.rst b/operating_unit/README.rst index d3526e2934..64e68c6871 100644 --- a/operating_unit/README.rst +++ b/operating_unit/README.rst @@ -7,7 +7,7 @@ Operating Unit !! This file is generated by oca-gen-addon-readme !! !! changes will be overwritten. !! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! - !! source digest: sha256:d2d952bbb132fcefc967728ae9ecd49b95d9b0abf7fe5da895b396c25be11d81 + !! source digest: sha256:808772644168895deaa3fc498818a5fb56e6bc6307fa79df36e2b9bfa5274d3e !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! .. |badge1| image:: https://img.shields.io/badge/maturity-Beta-yellow.png diff --git a/operating_unit/__manifest__.py b/operating_unit/__manifest__.py index e92c04411b..b48b38ea9c 100644 --- a/operating_unit/__manifest__.py +++ b/operating_unit/__manifest__.py @@ -6,7 +6,7 @@ "name": "Operating Unit", "summary": "An operating unit (OU) is an organizational entity part of a " "company", - "version": "15.0.1.0.6", + "version": "15.0.1.0.7", "author": "ForgeFlow, " "Serpent Consulting Services Pvt. Ltd.," "Odoo Community Association (OCA)", diff --git a/operating_unit/static/description/index.html b/operating_unit/static/description/index.html index 2c73170927..f1f27f4a0c 100644 --- a/operating_unit/static/description/index.html +++ b/operating_unit/static/description/index.html @@ -1,4 +1,3 @@ - @@ -367,7 +366,7 @@

Operating Unit

!! This file is generated by oca-gen-addon-readme !! !! changes will be overwritten. !! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! -!! source digest: sha256:d2d952bbb132fcefc967728ae9ecd49b95d9b0abf7fe5da895b396c25be11d81 +!! source digest: sha256:808772644168895deaa3fc498818a5fb56e6bc6307fa79df36e2b9bfa5274d3e !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! -->

Beta License: LGPL-3 OCA/operating-unit Translate me on Weblate Try me on Runboat

An operating unit (OU) is an organizational entity part of a company, with