diff --git a/res_partner_operating_unit/README.rst b/res_partner_operating_unit/README.rst new file mode 100644 index 0000000000..7df77ae834 --- /dev/null +++ b/res_partner_operating_unit/README.rst @@ -0,0 +1,110 @@ +.. image:: https://odoo-community.org/readme-banner-image + :target: https://odoo-community.org/get-involved?utm_source=readme + :alt: Odoo Community Association + +=========================== +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/license-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/19.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-19-0/operating-unit-19-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=19.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 + +- `Camptocamp `__: + + - Maksym Yankin + +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..0e914b3948 --- /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": "19.0.1.0.0", + "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..bbf11515b0 --- /dev/null +++ b/res_partner_operating_unit/models/res_partner.py @@ -0,0 +1,58 @@ +# © 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 +from odoo.fields import Domain + + +class ResPartner(models.Model): + _inherit = "res.partner" + + @api.model + def operating_unit_default_get(self, uid2=False): + if not uid2: + uid2 = self.env.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( + comodel_name="operating.unit", + relation="operating_unit_partner_rel", + column1="partner_id", + column2="operating_unit_id", + string="Operating Units", + default=lambda self: self._default_operating_units(), + ) + + @api.model + def _user_ous_domain(self): + ou_ids = self.env.user.operating_unit_ids.ids + domain = Domain.OR( + [ + Domain("operating_unit_ids", "in", ou_ids), + Domain("operating_unit_ids", "=", False), + ] + ) + return domain + + # 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 + domain = self._user_ous_domain() + 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 + domain = self._user_ous_domain() + 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..bf3239c509 --- /dev/null +++ b/res_partner_operating_unit/models/res_users.py @@ -0,0 +1,46 @@ +# © 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 +from odoo.fields import Command + + +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 = [ + Command.link(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 = [ + Command.link(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..a508157f7d --- /dev/null +++ b/res_partner_operating_unit/readme/CONTRIBUTORS.md @@ -0,0 +1,7 @@ +- Edi Santoso \ +- Maxime Chambreuil \ +- Hiren Dangar \ +- [Komit](https://komit-consulting.com): + - Cai Hoang Huynh +- [Camptocamp](https://www.camptocamp.com): + - Maksym Yankin \ 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..f9212937ff --- /dev/null +++ b/res_partner_operating_unit/static/description/index.html @@ -0,0 +1,465 @@ + + + + + +README.rst + + + +
+ + + +Odoo Community Association + +
+

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..65727f24fa --- /dev/null +++ b/res_partner_operating_unit/tests/test_res_partner_operating_unit.py @@ -0,0 +1,101 @@ +# © 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.fields import Command, Domain + +from odoo.addons.operating_unit.tests.common import OperatingUnitCommon + + +class TestResPartnerOperatingUnit(OperatingUnitCommon): + @classmethod + def setUpClass(cls): + super().setUpClass() + cls.res_partner_model = cls.env["res.partner"] + cls.res_users_model = cls.env["res.users"] + cls.grp_user = cls.env.ref("base.group_user") + # Define User groups + cls.user1.group_ids = [Command.set([cls.grp_user.id])] + cls.user2.group_ids = [Command.set([cls.grp_user.id])] + # Create Partner 1 with Main OU + cls.partner1 = cls._create_partner("Test Partner 1", cls.ou1) + # Create Partner 2 with B2C OU + cls.partner2 = cls._create_partner("Test Partner 2", cls.b2c) + + @classmethod + def _create_partner(cls, name, operating_unit, context=None): + """Create a partner.""" + partner = cls.res_partner_model.create( + { + "name": name, + "operating_unit_ids": [Command.link(operating_unit.id)], + } + ) + return partner + + def test_create_user_adds_ou_to_partner(self): + new_user = self._create_user( + "new_user_ou", self.grp_ou_mngr, 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.grp_ou_mngr, 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": [Command.link(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( + Domain("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( + Domain("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": [Command.link(self.company.id)], + "operating_unit_ids": [Command.link(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.grp_ou_mngr, 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": [ + Command.link(self.ou1.id), + Command.link(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 + + + + + + + +