diff --git a/res_partner_operating_unit/README.rst b/res_partner_operating_unit/README.rst new file mode 100644 index 0000000000..0a2ff4e51b --- /dev/null +++ b/res_partner_operating_unit/README.rst @@ -0,0 +1,102 @@ +=========================== +Partner with Operating Unit +=========================== + +.. + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! This file is generated by oca-gen-addon-readme !! + !! changes will be overwritten. !! + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! source digest: sha256:65f5a695789262630b40a31a5f1c4684a91813cde3f2caa75d9a51530054073c + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + +.. |badge1| image:: https://img.shields.io/badge/maturity-Beta-yellow.png + :target: https://odoo-community.org/page/development-status + :alt: Beta +.. |badge2| image:: https://img.shields.io/badge/licence-LGPL--3-blue.png + :target: http://www.gnu.org/licenses/lgpl-3.0-standalone.html + :alt: License: LGPL-3 +.. |badge3| image:: https://img.shields.io/badge/github-OCA%2Foperating--unit-lightgray.png?logo=github + :target: https://github.com/OCA/operating-unit/tree/18.0/res_partner_operating_unit + :alt: OCA/operating-unit +.. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png + :target: https://translation.odoo-community.org/projects/operating-unit-18-0/operating-unit-18-0-res_partner_operating_unit + :alt: Translate me on Weblate +.. |badge5| image:: https://img.shields.io/badge/runboat-Try%20me-875A7B.png + :target: https://runboat.odoo-community.org/builds?repo=OCA/operating-unit&target_branch=18.0 + :alt: Try me on Runboat + +|badge1| |badge2| |badge3| |badge4| |badge5| + +This module introduces the following features: + +- Adds the Operating Unit (OU) to res partner. +- The user’s default Operating Unit (OU) is proposed at the time of + creating Partner. +- Security rules are defined to ensure that users can only see Partner + of that Operating Units in which they are allowed access to. + +**Table of contents** + +.. contents:: + :local: + +Usage +===== + +- Go to customer +- You only see the customer of your operating units +- Create an customer. It is assigned to your default operating unit. + +Bug Tracker +=========== + +Bugs are tracked on `GitHub Issues `_. +In case of trouble, please check there if your issue has already been reported. +If you spotted it first, help us to smash it by providing a detailed and welcomed +`feedback `_. + +Do not contact contributors directly about support or help with technical issues. + +Credits +======= + +Authors +------- + +* Edi Santoso +* Niaga Solution + +Contributors +------------ + +- Edi Santoso +- Maxime Chambreuil +- Hiren Dangar +- `Komit `__: + + - Cai Hoang Huynh + +Other credits +------------- + +- Niaga Solution +- Open Source Integrators +- Serpent Consulting Services Pvt. Ltd. + +Maintainers +----------- + +This module is maintained by the OCA. + +.. image:: https://odoo-community.org/logo.png + :alt: Odoo Community Association + :target: https://odoo-community.org + +OCA, or the Odoo Community Association, is a nonprofit organization whose +mission is to support the collaborative development of Odoo features and +promote its widespread use. + +This module is part of the `OCA/operating-unit `_ project on GitHub. + +You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute. diff --git a/res_partner_operating_unit/__init__.py b/res_partner_operating_unit/__init__.py new file mode 100644 index 0000000000..133f68732d --- /dev/null +++ b/res_partner_operating_unit/__init__.py @@ -0,0 +1,2 @@ +# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html). +from . import models diff --git a/res_partner_operating_unit/__manifest__.py b/res_partner_operating_unit/__manifest__.py new file mode 100644 index 0000000000..bb216faa2e --- /dev/null +++ b/res_partner_operating_unit/__manifest__.py @@ -0,0 +1,16 @@ +# © 2017 Niaga Solution - Edi Santoso +# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html). +{ + "name": "Partner with Operating Unit", + "summary": "Introduces Operating Unit fields in Partner", + "version": "18.0.1.1.1", + "author": "Edi Santoso, " "Niaga Solution, " "Odoo Community Association (OCA)", + "website": "https://github.com/OCA/operating-unit", + "category": "Generic", + "depends": ["operating_unit"], + "license": "LGPL-3", + "data": [ + "views/res_partner_view.xml", + ], + "installable": True, +} diff --git a/res_partner_operating_unit/i18n/it.po b/res_partner_operating_unit/i18n/it.po new file mode 100644 index 0000000000..dd1157c40a --- /dev/null +++ b/res_partner_operating_unit/i18n/it.po @@ -0,0 +1,43 @@ +# Translation of Odoo Server. +# This file contains the translation of the following modules: +# * res_partner_operating_unit +# +msgid "" +msgstr "" +"Project-Id-Version: Odoo Server 12.0\n" +"Report-Msgid-Bugs-To: \n" +"PO-Revision-Date: 2023-01-18 11:48+0000\n" +"Last-Translator: Francesco Foresti \n" +"Language-Team: none\n" +"Language: it\n" +"MIME-Version: 1.0\n" +"Content-Type: text/plain; charset=UTF-8\n" +"Content-Transfer-Encoding: \n" +"Plural-Forms: nplurals=2; plural=n != 1;\n" +"X-Generator: Weblate 4.14.1\n" + +#. module: res_partner_operating_unit +#: model:ir.model,name:res_partner_operating_unit.model_res_partner +msgid "Contact" +msgstr "Contatto" + +#. module: res_partner_operating_unit +#: model:ir.model.fields,field_description:res_partner_operating_unit.field_fsm_location__operating_unit_ids +#: model:ir.model.fields,field_description:res_partner_operating_unit.field_fsm_person__operating_unit_ids +#: model:ir.model.fields,field_description:res_partner_operating_unit.field_res_partner__operating_unit_ids +msgid "Operating Units" +msgstr "Unità operative" + +#. module: res_partner_operating_unit +#: code:addons/res_partner_operating_unit/models/res_users.py:34 +#, python-format +msgid "" +"The operating units of the partner must include the default one of the user." +msgstr "" +"Le unità operative del contatto devono includere quella predefinita " +"dell'utente." + +#. module: res_partner_operating_unit +#: model:ir.model,name:res_partner_operating_unit.model_res_users +msgid "Users" +msgstr "Utenti" diff --git a/res_partner_operating_unit/i18n/pt.po b/res_partner_operating_unit/i18n/pt.po new file mode 100644 index 0000000000..6ae999cbf3 --- /dev/null +++ b/res_partner_operating_unit/i18n/pt.po @@ -0,0 +1,43 @@ +# Translation of Odoo Server. +# This file contains the translation of the following modules: +# * res_partner_operating_unit +# +msgid "" +msgstr "" +"Project-Id-Version: Odoo Server 12.0\n" +"Report-Msgid-Bugs-To: \n" +"PO-Revision-Date: 2020-02-29 15:13+0000\n" +"Last-Translator: Pedro Castro Silva \n" +"Language-Team: none\n" +"Language: pt\n" +"MIME-Version: 1.0\n" +"Content-Type: text/plain; charset=UTF-8\n" +"Content-Transfer-Encoding: \n" +"Plural-Forms: nplurals=2; plural=n > 1;\n" +"X-Generator: Weblate 3.10\n" + +#. module: res_partner_operating_unit +#: model:ir.model,name:res_partner_operating_unit.model_res_partner +msgid "Contact" +msgstr "Contacto" + +#. module: res_partner_operating_unit +#: model:ir.model.fields,field_description:res_partner_operating_unit.field_fsm_location__operating_unit_ids +#: model:ir.model.fields,field_description:res_partner_operating_unit.field_fsm_person__operating_unit_ids +#: model:ir.model.fields,field_description:res_partner_operating_unit.field_res_partner__operating_unit_ids +msgid "Operating Units" +msgstr "Unidades Operacionais" + +#. module: res_partner_operating_unit +#: code:addons/res_partner_operating_unit/models/res_users.py:34 +#, python-format +msgid "" +"The operating units of the partner must include the default one of the user." +msgstr "" +"As unidades operacionais deste parceiro devem incluir a predefinida do " +"utilizador." + +#. module: res_partner_operating_unit +#: model:ir.model,name:res_partner_operating_unit.model_res_users +msgid "Users" +msgstr "Utilizadores" diff --git a/res_partner_operating_unit/i18n/res_partner_operating_unit.pot b/res_partner_operating_unit/i18n/res_partner_operating_unit.pot new file mode 100644 index 0000000000..4aeb36016a --- /dev/null +++ b/res_partner_operating_unit/i18n/res_partner_operating_unit.pot @@ -0,0 +1,38 @@ +# Translation of Odoo Server. +# This file contains the translation of the following modules: +# * res_partner_operating_unit +# +msgid "" +msgstr "" +"Project-Id-Version: Odoo Server 12.0\n" +"Report-Msgid-Bugs-To: \n" +"Last-Translator: <>\n" +"Language-Team: \n" +"MIME-Version: 1.0\n" +"Content-Type: text/plain; charset=UTF-8\n" +"Content-Transfer-Encoding: \n" +"Plural-Forms: \n" + +#. module: res_partner_operating_unit +#: model:ir.model,name:res_partner_operating_unit.model_res_partner +msgid "Contact" +msgstr "" + +#. module: res_partner_operating_unit +#: model:ir.model.fields,field_description:res_partner_operating_unit.field_fsm_location__operating_unit_ids +#: model:ir.model.fields,field_description:res_partner_operating_unit.field_fsm_person__operating_unit_ids +#: model:ir.model.fields,field_description:res_partner_operating_unit.field_res_partner__operating_unit_ids +msgid "Operating Units" +msgstr "" + +#. module: res_partner_operating_unit +#: code:addons/res_partner_operating_unit/models/res_users.py:34 +#, python-format +msgid "The operating units of the partner must include the default one of the user." +msgstr "" + +#. module: res_partner_operating_unit +#: model:ir.model,name:res_partner_operating_unit.model_res_users +msgid "Users" +msgstr "" + diff --git a/res_partner_operating_unit/models/__init__.py b/res_partner_operating_unit/models/__init__.py new file mode 100644 index 0000000000..e9e1cee2b0 --- /dev/null +++ b/res_partner_operating_unit/models/__init__.py @@ -0,0 +1,3 @@ +# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html). +from . import res_partner +from . import res_users diff --git a/res_partner_operating_unit/models/res_partner.py b/res_partner_operating_unit/models/res_partner.py new file mode 100644 index 0000000000..eff2794177 --- /dev/null +++ b/res_partner_operating_unit/models/res_partner.py @@ -0,0 +1,56 @@ +# © 2017 Niaga Solution - Edi Santoso +# Copyright (C) 2019 Serpent Consulting Services +# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html). +from odoo import api, fields, models + + +class ResPartner(models.Model): + _inherit = "res.partner" + + @api.model + def operating_unit_default_get(self, uid2=False): + if not uid2: + uid2 = self._uid + user = self.env["res.users"].browse(uid2) + return user.default_operating_unit_id + + @api.model + def _default_operating_unit(self): + return self.operating_unit_default_get() + + @api.model + def _default_operating_units(self): + return self._default_operating_unit() + + operating_unit_ids = fields.Many2many( + "operating.unit", + "operating_unit_partner_rel", + "partner_id", + "operating_unit_id", + "Operating Units", + default=lambda self: self._default_operating_units(), + ) + + # Extending methods to replace a record rule. + # Ref: https://github.com/OCA/operating-unit/issues/258 + @api.model + def search(self, args, offset=0, limit=None, order=None): + # Get the OUs of the user + ou_ids = self.env.user.operating_unit_ids.ids + domain = [ + "|", + ("operating_unit_ids", "in", ou_ids), + ("operating_unit_ids", "=", False), + ] + return super().search(domain + args, offset=offset, limit=limit, order=order) + + @api.model + def search_count(self, args, limit=None): + # Get the OUs of the user + ou_ids = self.env.user.operating_unit_ids.ids + domain = [ + "|", + ("operating_unit_ids", "in", ou_ids), + ("operating_unit_ids", "=", False), + ] + return super().search_count(domain + args, limit=limit) diff --git a/res_partner_operating_unit/models/res_users.py b/res_partner_operating_unit/models/res_users.py new file mode 100644 index 0000000000..c80b327982 --- /dev/null +++ b/res_partner_operating_unit/models/res_users.py @@ -0,0 +1,45 @@ +# © 2017 Niaga Solution - Edi Santoso +# Copyright (C) 2019 Serpent Consulting Services +# Copyright (C) 2019 Open Source Integrators +# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html). +from odoo import api, models +from odoo.exceptions import UserError + + +class ResUsers(models.Model): + _inherit = "res.users" + + @api.model_create_multi + def create(self, vals_list): + res = super().create(vals_list) + for rec in res: + if rec.default_operating_unit_id: + rec.partner_id.operating_unit_ids = [ + (4, rec.default_operating_unit_id.id) + ] + rec.check_partner_operating_unit() + return res + + def write(self, vals): + for user in self: + res = super().write(vals) + if vals.get("default_operating_unit_id"): + # Add the new OU + user.partner_id.operating_unit_ids = [ + (4, user.default_operating_unit_id.id) + ] + user.check_partner_operating_unit() + return res + + def check_partner_operating_unit(self): + if ( + self.partner_id.operating_unit_ids + and self.default_operating_unit_id.id + not in self.partner_id.operating_unit_ids.ids + ): + raise UserError( + self.env._( + "The operating units of the partner must include the default " + "one of the user." + ) + ) diff --git a/res_partner_operating_unit/pyproject.toml b/res_partner_operating_unit/pyproject.toml new file mode 100644 index 0000000000..4231d0cccb --- /dev/null +++ b/res_partner_operating_unit/pyproject.toml @@ -0,0 +1,3 @@ +[build-system] +requires = ["whool"] +build-backend = "whool.buildapi" diff --git a/res_partner_operating_unit/readme/CONTRIBUTORS.md b/res_partner_operating_unit/readme/CONTRIBUTORS.md new file mode 100644 index 0000000000..84f244954f --- /dev/null +++ b/res_partner_operating_unit/readme/CONTRIBUTORS.md @@ -0,0 +1,5 @@ +- Edi Santoso \ +- Maxime Chambreuil \ +- Hiren Dangar \ +- [Komit](https://komit-consulting.com): + - Cai Hoang Huynh diff --git a/res_partner_operating_unit/readme/CREDITS.md b/res_partner_operating_unit/readme/CREDITS.md new file mode 100644 index 0000000000..f4ebf7ffa2 --- /dev/null +++ b/res_partner_operating_unit/readme/CREDITS.md @@ -0,0 +1,3 @@ +- Niaga Solution +- Open Source Integrators +- Serpent Consulting Services Pvt. Ltd. \ diff --git a/res_partner_operating_unit/readme/DESCRIPTION.md b/res_partner_operating_unit/readme/DESCRIPTION.md new file mode 100644 index 0000000000..2a04768c25 --- /dev/null +++ b/res_partner_operating_unit/readme/DESCRIPTION.md @@ -0,0 +1,7 @@ +This module introduces the following features: + +- Adds the Operating Unit (OU) to res partner. +- The user’s default Operating Unit (OU) is proposed at the time of + creating Partner. +- Security rules are defined to ensure that users can only see Partner + of that Operating Units in which they are allowed access to. diff --git a/res_partner_operating_unit/readme/USAGE.md b/res_partner_operating_unit/readme/USAGE.md new file mode 100644 index 0000000000..612c30ec03 --- /dev/null +++ b/res_partner_operating_unit/readme/USAGE.md @@ -0,0 +1,3 @@ +- Go to customer +- You only see the customer of your operating units +- Create an customer. It is assigned to your default operating unit. diff --git a/res_partner_operating_unit/static/description/icon.png b/res_partner_operating_unit/static/description/icon.png new file mode 100644 index 0000000000..3a0328b516 Binary files /dev/null and b/res_partner_operating_unit/static/description/icon.png differ diff --git a/res_partner_operating_unit/static/description/index.html b/res_partner_operating_unit/static/description/index.html new file mode 100644 index 0000000000..4867794577 --- /dev/null +++ b/res_partner_operating_unit/static/description/index.html @@ -0,0 +1,455 @@ + + + + + +Partner with Operating Unit + + + +
+

Partner with Operating Unit

+ + +

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

+

This module introduces the following features:

+
    +
  • Adds the Operating Unit (OU) to res partner.
  • +
  • The user’s default Operating Unit (OU) is proposed at the time of +creating Partner.
  • +
  • Security rules are defined to ensure that users can only see Partner +of that Operating Units in which they are allowed access to.
  • +
+

Table of contents

+ +
+

Usage

+
    +
  • Go to customer
  • +
  • You only see the customer of your operating units
  • +
  • Create an customer. It is assigned to your default operating unit.
  • +
+
+
+

Bug Tracker

+

Bugs are tracked on GitHub Issues. +In case of trouble, please check there if your issue has already been reported. +If you spotted it first, help us to smash it by providing a detailed and welcomed +feedback.

+

Do not contact contributors directly about support or help with technical issues.

+
+
+

Credits

+
+

Authors

+
    +
  • Edi Santoso
  • +
  • Niaga Solution
  • +
+
+
+

Contributors

+ +
+
+

Other credits

+
    +
  • Niaga Solution
  • +
  • Open Source Integrators
  • +
  • Serpent Consulting Services Pvt. Ltd. <support@serpentcs.com>
  • +
+
+
+

Maintainers

+

This module is maintained by the OCA.

+ +Odoo Community Association + +

OCA, or the Odoo Community Association, is a nonprofit organization whose +mission is to support the collaborative development of Odoo features and +promote its widespread use.

+

This module is part of the OCA/operating-unit project on GitHub.

+

You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute.

+
+
+
+ + diff --git a/res_partner_operating_unit/tests/__init__.py b/res_partner_operating_unit/tests/__init__.py new file mode 100644 index 0000000000..b335a6145a --- /dev/null +++ b/res_partner_operating_unit/tests/__init__.py @@ -0,0 +1,2 @@ +# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html). +from . import test_res_partner_operating_unit diff --git a/res_partner_operating_unit/tests/test_res_partner_operating_unit.py b/res_partner_operating_unit/tests/test_res_partner_operating_unit.py new file mode 100644 index 0000000000..17702f89c9 --- /dev/null +++ b/res_partner_operating_unit/tests/test_res_partner_operating_unit.py @@ -0,0 +1,112 @@ +# © 2017 Niaga Solution - Edi Santoso +# Copyright (C) 2019 Serpent Consulting Services +# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html). +from odoo.tests import common + + +class TestResPartnerOperatingUnit(common.TransactionCase): + def setUp(self): + super().setUp() + self.res_partner_model = self.env["res.partner"] + self.res_users_model = self.env["res.users"] + # Company + self.company = self.env.ref("base.main_company") + # Main Operating Unit + self.ou1 = self.env.ref("operating_unit.main_operating_unit") + # B2C Operating Unit + self.b2c = self.env.ref("operating_unit.b2c_operating_unit") + + # Create User 1 with Main OU + self.user1 = self._create_user("user_1", self.company, [self.ou1]) + # Create User 2 with B2C OU + self.user2 = self._create_user("user_2", self.company, [self.b2c]) + + # Create Partner 1 with Main OU + self.partner1 = self._create_partner("Test Partner 1", self.ou1) + + # Create Partner 2 with B2C OU + self.partner2 = self._create_partner("Test Partner 2", self.b2c) + + def _create_partner(self, name, operating_unit, context=None): + """Create a partner.""" + partner = self.res_partner_model.create( + { + "name": name, + "operating_unit_ids": [(4, operating_unit.id)], + } + ) + return partner + + def _create_user(self, login, company, operating_units, context=None): + """Create a user.""" + user = self.res_users_model.create( + { + "name": "Test User", + "login": login, + "password": "demo", + "email": "test@yourcompany.com", + "company_id": company.id, + "company_ids": [(4, company.id)], + "operating_unit_ids": [(4, ou.id) for ou in operating_units], + } + ) + return user + + def test_create_user_adds_ou_to_partner(self): + new_user = self._create_user("new_user_ou", self.company, [self.ou1]) + self.assertIn(self.ou1, new_user.partner_id.operating_unit_ids) + + def test_write_user_adds_new_ou_to_partner(self): + new_user = self._create_user("edit_user_ou", self.company, [self.ou1]) + self.assertIn(self.ou1, new_user.partner_id.operating_unit_ids) + + new_user.write( + { + "default_operating_unit_id": self.b2c.id, + "operating_unit_ids": [(4, self.b2c.id)], + } + ) + + self.assertIn(self.b2c, new_user.partner_id.operating_unit_ids) + + def test_search_partner_with_operating_unit(self): + partners = self.res_partner_model.with_user(self.user1).search( + [("name", "!=", "")] + ) + self.assertIn(self.partner1, partners) + self.assertNotIn(self.partner2, partners) + + def test_search_count_partner_with_operating_unit(self): + count = self.res_partner_model.with_user(self.user2).search_count( + [("name", "!=", "")] + ) + self.assertGreaterEqual(count, 1) + + def test_create_user_with_default_operating_unit(self): + new_user = self.res_users_model.create( + { + "name": "Create With Default OU", + "login": "user_with_default_ou", + "password": "demo", + "email": "user@default.com", + "company_id": self.company.id, + "company_ids": [(4, self.company.id)], + "operating_unit_ids": [(4, self.ou1.id)], + "default_operating_unit_id": self.ou1.id, + } + ) + + self.assertIn(self.ou1, new_user.partner_id.operating_unit_ids) + + def test_write_user_sets_default_operating_unit(self): + user = self._create_user("write_user_default_ou", self.company, [self.ou1]) + self.assertIn(self.ou1, user.partner_id.operating_unit_ids) + + user.write( + { + "default_operating_unit_id": self.b2c.id, + "operating_unit_ids": [(4, self.ou1.id), (4, self.b2c.id)], + } + ) + + self.assertIn(self.b2c, user.partner_id.operating_unit_ids) diff --git a/res_partner_operating_unit/views/res_partner_view.xml b/res_partner_operating_unit/views/res_partner_view.xml new file mode 100644 index 0000000000..ec9ed92d20 --- /dev/null +++ b/res_partner_operating_unit/views/res_partner_view.xml @@ -0,0 +1,54 @@ + + + + + + res.partner.form + res.partner + + + + + + + + + + + res.partner.tree + res.partner + + + + + + + + + + + res.partner.select + res.partner + + + + + + + +