diff --git a/product_operating_unit/README.rst b/product_operating_unit/README.rst new file mode 100644 index 0000000000..612c15f5ba --- /dev/null +++ b/product_operating_unit/README.rst @@ -0,0 +1,106 @@ +========================== +Operating Unit in Products +========================== + +.. + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! This file is generated by oca-gen-addon-readme !! + !! changes will be overwritten. !! + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! source digest: sha256:f3b0543d0a608529f25ca22274bb47444a153c7c3565bf4ac96e6f0a75c1d585 + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + +.. |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/product_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-product_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: + +- It introduces the operating unit to the product template. +- The operating unit from the user is assigned by default when creating + a new product template. +- In case of multi-company, no operating unit from another company to + that assigned to the product can be set for it +- It implements user's security rules. + +**Table of contents** + +.. contents:: + :local: + +Usage +===== + +1. Create a Product: the Default Operating Unit of the user is assigned + to it. If you want, you can change to another Operating Unit. +2. Assignment of an operating unit of another company to the one set in + the product raises an error for a multi-company setting. +3. Access rules allow to just show those products having the same + operating units as the user + +Known issues / Roadmap +====================== + + + +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 +------- + +* brain-tec AG +* Open Source Integrators +* Serpent Consulting Services Pvt. Ltd. + +Contributors +------------ + +- Raul Martin Felez +- Nikul Chaudhary +- Maxime Chambreuil +- Jesus Alcala +- `Komit `__: + + - Cai Hoang Huynh + +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/product_operating_unit/__init__.py b/product_operating_unit/__init__.py new file mode 100644 index 0000000000..133f68732d --- /dev/null +++ b/product_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/product_operating_unit/__manifest__.py b/product_operating_unit/__manifest__.py new file mode 100644 index 0000000000..cb02784d23 --- /dev/null +++ b/product_operating_unit/__manifest__.py @@ -0,0 +1,22 @@ +# Copyright (c) 2019 brain-tec AG (http://www.braintec-group.com) +# Copyright (C) 2019 Open Source Integrators +# Copyright (C) 2019 Serpent Consulting Services +# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html). +{ + "name": "Operating Unit in Products", + "summary": "Adds the concept of operating unit (OU) in products", + "version": "18.0.1.0.0", + "author": "brain-tec AG, " + "Open Source Integrators, " + "Serpent Consulting Services Pvt. Ltd.," + "Odoo Community Association (OCA)", + "website": "https://github.com/OCA/operating-unit", + "category": "Product", + "depends": ["product", "operating_unit"], + "license": "LGPL-3", + "data": [ + "security/product_template_security.xml", + "views/product_template_view.xml", + "views/product_category_view.xml", + ], +} diff --git a/product_operating_unit/i18n/es.po b/product_operating_unit/i18n/es.po new file mode 100644 index 0000000000..ca9ab23d4c --- /dev/null +++ b/product_operating_unit/i18n/es.po @@ -0,0 +1,64 @@ +# Translation of Odoo Server. +# This file contains the translation of the following modules: +# * product_operating_unit +# +msgid "" +msgstr "" +"Project-Id-Version: Odoo Server 16.0\n" +"Report-Msgid-Bugs-To: \n" +"PO-Revision-Date: 2023-09-02 20:36+0000\n" +"Last-Translator: Ivorra78 \n" +"Language-Team: none\n" +"Language: es\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.17\n" + +#. module: product_operating_unit +#: model:ir.model.fields,field_description:product_operating_unit.field_product_category__operating_unit_ids +#: model:ir.model.fields,field_description:product_operating_unit.field_product_product__operating_unit_ids +#: model:ir.model.fields,field_description:product_operating_unit.field_product_template__operating_unit_ids +msgid "Operating Units" +msgstr "Unidades operativas" + +#. module: product_operating_unit +#: model:ir.model,name:product_operating_unit.model_product_template +msgid "Product" +msgstr "Producto" + +#. module: product_operating_unit +#: model:ir.model,name:product_operating_unit.model_product_category +#: model:ir.model.fields,field_description:product_operating_unit.field_product_product__categ_id +#: model:ir.model.fields,field_description:product_operating_unit.field_product_template__categ_id +msgid "Product Category" +msgstr "Categoria de producto" + +#. module: product_operating_unit +#. odoo-python +#: code:addons/product_operating_unit/models/product_template.py:0 +#, python-format +msgid "" +"The operating units of the product must include the ones from the category." +msgstr "" +"Las unidades operativas del producto deben incluir las de la categoría." + +#, python-format +#~ msgid "Go to Product Categories" +#~ msgstr "Ir a las categorías del producto" + +#~ msgid "Select category for the current product" +#~ msgstr "Seleccione la categoría para el producto actual" + +#, python-format +#~ msgid "" +#~ "You must define at least one product category " +#~ "within your Operating Unit in order to be able to " +#~ "create products." +#~ msgstr "" +#~ "Debe definir al menos una categoría de " +#~ "producto dentro " +#~ "de su Unidad Operativa para " +#~ "poder crear " +#~ "productos." diff --git a/product_operating_unit/i18n/it.po b/product_operating_unit/i18n/it.po new file mode 100644 index 0000000000..1962ae9b84 --- /dev/null +++ b/product_operating_unit/i18n/it.po @@ -0,0 +1,62 @@ +# Translation of Odoo Server. +# This file contains the translation of the following modules: +# * product_operating_unit +# +msgid "" +msgstr "" +"Project-Id-Version: Odoo Server 16.0\n" +"Report-Msgid-Bugs-To: \n" +"PO-Revision-Date: 2024-01-19 07:35+0000\n" +"Last-Translator: mymage \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.17\n" + +#. module: product_operating_unit +#: model:ir.model.fields,field_description:product_operating_unit.field_product_category__operating_unit_ids +#: model:ir.model.fields,field_description:product_operating_unit.field_product_product__operating_unit_ids +#: model:ir.model.fields,field_description:product_operating_unit.field_product_template__operating_unit_ids +msgid "Operating Units" +msgstr "Unità operative" + +#. module: product_operating_unit +#: model:ir.model,name:product_operating_unit.model_product_template +msgid "Product" +msgstr "Prodotto" + +#. module: product_operating_unit +#: model:ir.model,name:product_operating_unit.model_product_category +#: model:ir.model.fields,field_description:product_operating_unit.field_product_product__categ_id +#: model:ir.model.fields,field_description:product_operating_unit.field_product_template__categ_id +msgid "Product Category" +msgstr "Categoria prodotto" + +#. module: product_operating_unit +#. odoo-python +#: code:addons/product_operating_unit/models/product_template.py:0 +#, python-format +msgid "" +"The operating units of the product must include the ones from the category." +msgstr "" +"Le unità operative del prodotto devono includere quelle della categoria." + +#, python-format +#~ msgid "Go to Product Categories" +#~ msgstr "Vai alle categorie prodotto" + +#~ msgid "Select category for the current product" +#~ msgstr "Selezionare una categoria per il prodotto corrente" + +#, python-format +#~ msgid "" +#~ "You must define at least one product category " +#~ "within your Operating Unit in order to be able to " +#~ "create products." +#~ msgstr "" +#~ "Bisogna indicare almeno una categoria prodotto " +#~ "all'interno della tua unità operativa per poter " +#~ "creare nuovi prodotti." diff --git a/product_operating_unit/i18n/ja.po b/product_operating_unit/i18n/ja.po new file mode 100644 index 0000000000..01fcaccc34 --- /dev/null +++ b/product_operating_unit/i18n/ja.po @@ -0,0 +1,48 @@ +# Translation of Odoo Server. +# This file contains the translation of the following modules: +# * product_operating_unit +# +msgid "" +msgstr "" +"Project-Id-Version: Odoo Server 16.0\n" +"Report-Msgid-Bugs-To: \n" +"PO-Revision-Date: 2023-10-28 12:57+0000\n" +"Last-Translator: Yoshi Tashiro \n" +"Language-Team: none\n" +"Language: ja\n" +"MIME-Version: 1.0\n" +"Content-Type: text/plain; charset=UTF-8\n" +"Content-Transfer-Encoding: \n" +"Plural-Forms: nplurals=1; plural=0;\n" +"X-Generator: Weblate 4.17\n" + +#. module: product_operating_unit +#: model:ir.model.fields,field_description:product_operating_unit.field_product_category__operating_unit_ids +#: model:ir.model.fields,field_description:product_operating_unit.field_product_product__operating_unit_ids +#: model:ir.model.fields,field_description:product_operating_unit.field_product_template__operating_unit_ids +msgid "Operating Units" +msgstr "" + +#. module: product_operating_unit +#: model:ir.model,name:product_operating_unit.model_product_template +msgid "Product" +msgstr "" + +#. module: product_operating_unit +#: model:ir.model,name:product_operating_unit.model_product_category +#: model:ir.model.fields,field_description:product_operating_unit.field_product_product__categ_id +#: model:ir.model.fields,field_description:product_operating_unit.field_product_template__categ_id +msgid "Product Category" +msgstr "" + +#. module: product_operating_unit +#. odoo-python +#: code:addons/product_operating_unit/models/product_template.py:0 +#, python-format +msgid "" +"The operating units of the product must include the ones from the category." +msgstr "" + +#, python-format +#~ msgid "Go to Product Categories" +#~ msgstr "プロダクトカテゴリに移動" diff --git a/product_operating_unit/i18n/product_operating_unit.pot b/product_operating_unit/i18n/product_operating_unit.pot new file mode 100644 index 0000000000..b296e0934c --- /dev/null +++ b/product_operating_unit/i18n/product_operating_unit.pot @@ -0,0 +1,41 @@ +# Translation of Odoo Server. +# This file contains the translation of the following modules: +# * product_operating_unit +# +msgid "" +msgstr "" +"Project-Id-Version: Odoo Server 17.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: product_operating_unit +#: model:ir.model.fields,field_description:product_operating_unit.field_product_category__operating_unit_ids +#: model:ir.model.fields,field_description:product_operating_unit.field_product_product__operating_unit_ids +#: model:ir.model.fields,field_description:product_operating_unit.field_product_template__operating_unit_ids +msgid "Operating Units" +msgstr "" + +#. module: product_operating_unit +#: model:ir.model,name:product_operating_unit.model_product_template +msgid "Product" +msgstr "" + +#. module: product_operating_unit +#: model:ir.model,name:product_operating_unit.model_product_category +#: model:ir.model.fields,field_description:product_operating_unit.field_product_product__categ_id +#: model:ir.model.fields,field_description:product_operating_unit.field_product_template__categ_id +msgid "Product Category" +msgstr "" + +#. module: product_operating_unit +#. odoo-python +#: code:addons/product_operating_unit/models/product_template.py:0 +#, python-format +msgid "" +"The operating units of the product must include the ones from the category." +msgstr "" diff --git a/product_operating_unit/i18n/pt.po b/product_operating_unit/i18n/pt.po new file mode 100644 index 0000000000..3df3044dd5 --- /dev/null +++ b/product_operating_unit/i18n/pt.po @@ -0,0 +1,42 @@ +# Translation of Odoo Server. +# This file contains the translation of the following modules: +# * product_operating_unit +# +msgid "" +msgstr "" +"Project-Id-Version: Odoo Server 12.0\n" +"Report-Msgid-Bugs-To: \n" +"Last-Translator: Automatically generated\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" + +#. module: product_operating_unit +#: model:ir.model.fields,field_description:product_operating_unit.field_product_category__operating_unit_ids +#: model:ir.model.fields,field_description:product_operating_unit.field_product_product__operating_unit_ids +#: model:ir.model.fields,field_description:product_operating_unit.field_product_template__operating_unit_ids +msgid "Operating Units" +msgstr "" + +#. module: product_operating_unit +#: model:ir.model,name:product_operating_unit.model_product_template +msgid "Product" +msgstr "" + +#. module: product_operating_unit +#: model:ir.model,name:product_operating_unit.model_product_category +#: model:ir.model.fields,field_description:product_operating_unit.field_product_product__categ_id +#: model:ir.model.fields,field_description:product_operating_unit.field_product_template__categ_id +msgid "Product Category" +msgstr "" + +#. module: product_operating_unit +#. odoo-python +#: code:addons/product_operating_unit/models/product_template.py:0 +#, python-format +msgid "" +"The operating units of the product must include the ones from the category." +msgstr "" diff --git a/product_operating_unit/models/__init__.py b/product_operating_unit/models/__init__.py new file mode 100644 index 0000000000..044da9a2ad --- /dev/null +++ b/product_operating_unit/models/__init__.py @@ -0,0 +1,3 @@ +# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html). +from . import product_template +from . import product_category diff --git a/product_operating_unit/models/product_category.py b/product_operating_unit/models/product_category.py new file mode 100644 index 0000000000..23716f0e93 --- /dev/null +++ b/product_operating_unit/models/product_category.py @@ -0,0 +1,29 @@ +# Copyright (C) 2019 Open Source Integrators +# Copyright (C) 2019 Serpent Consulting Services +# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html). +from odoo import fields, models +from odoo.models import Command + + +class ProductCategory(models.Model): + _inherit = "product.category" + + operating_unit_ids = fields.Many2many( + "operating.unit", + "product_category_operating_unit_rel", + string="Operating Units", + ) + + def write(self, vals): + res = super().write(vals) + product_template_obj = self.env["product.template"] + if vals.get("operating_unit_ids"): + for rec in self: + products = product_template_obj.search( + [("categ_id", "child_of", rec.id)] + ) + category_ou_ids = rec.operating_unit_ids + for product in products: + ou_ids = product.operating_unit_ids | category_ou_ids + product.operating_unit_ids = [Command.set(ou_ids.ids)] + return res diff --git a/product_operating_unit/models/product_template.py b/product_operating_unit/models/product_template.py new file mode 100644 index 0000000000..8bdf1ca477 --- /dev/null +++ b/product_operating_unit/models/product_template.py @@ -0,0 +1,51 @@ +# Copyright (c) 2019 brain-tec AG (http://www.braintec-group.com) +# Copyright (C) 2019 Open Source Integrators +# 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.exceptions import ValidationError + + +class ProductTemplate(models.Model): + _inherit = "product.template" + + operating_unit_ids = fields.Many2many( + "operating.unit", + "product_operating_unit_rel", + string="Operating Units", + compute="_compute_operating_unit_ids", + store=True, + readonly=False, + ) + categ_id = fields.Many2one( + default=lambda self: self._get_default_category_id(), + ) + + def _get_default_category_id(self): + category = self.env["product.category"].search([], limit=1) + if category: + return category.id + else: + return super()._get_default_category_id() + + @api.constrains("operating_unit_ids", "categ_id") + def _check_operating_unit(self): + for record in self: + if ( + record.operating_unit_ids and record.categ_id.operating_unit_ids + ) and not all( + ou in record.operating_unit_ids.ids + for ou in record.categ_id.operating_unit_ids.ids + ): + raise ValidationError( + _( + "The operating units of the product must include the " + "ones from the category." + ) + ) + + @api.depends("categ_id") + def _compute_operating_unit_ids(self): + for record in self: + record.operating_unit_ids = [(6, 0, record.categ_id.operating_unit_ids.ids)] diff --git a/product_operating_unit/pyproject.toml b/product_operating_unit/pyproject.toml new file mode 100644 index 0000000000..4231d0cccb --- /dev/null +++ b/product_operating_unit/pyproject.toml @@ -0,0 +1,3 @@ +[build-system] +requires = ["whool"] +build-backend = "whool.buildapi" diff --git a/product_operating_unit/readme/CONTRIBUTORS.md b/product_operating_unit/readme/CONTRIBUTORS.md new file mode 100644 index 0000000000..9d2358261d --- /dev/null +++ b/product_operating_unit/readme/CONTRIBUTORS.md @@ -0,0 +1,6 @@ +- Raul Martin Felez \<\> +- Nikul Chaudhary \<\> +- Maxime Chambreuil \<\> +- Jesus Alcala \<\> +- [Komit](https://komit-consulting.com): + - Cai Hoang Huynh diff --git a/product_operating_unit/readme/DESCRIPTION.md b/product_operating_unit/readme/DESCRIPTION.md new file mode 100644 index 0000000000..9fa7cad3b9 --- /dev/null +++ b/product_operating_unit/readme/DESCRIPTION.md @@ -0,0 +1,8 @@ +This module introduces the following features: + +- It introduces the operating unit to the product template. +- The operating unit from the user is assigned by default when creating + a new product template. +- In case of multi-company, no operating unit from another company to + that assigned to the product can be set for it +- It implements user's security rules. diff --git a/product_operating_unit/readme/ROADMAP.md b/product_operating_unit/readme/ROADMAP.md new file mode 100644 index 0000000000..8b13789179 --- /dev/null +++ b/product_operating_unit/readme/ROADMAP.md @@ -0,0 +1 @@ + diff --git a/product_operating_unit/readme/USAGE.md b/product_operating_unit/readme/USAGE.md new file mode 100644 index 0000000000..a04f228753 --- /dev/null +++ b/product_operating_unit/readme/USAGE.md @@ -0,0 +1,6 @@ +1. Create a Product: the Default Operating Unit of the user is assigned + to it. If you want, you can change to another Operating Unit. +2. Assignment of an operating unit of another company to the one set in + the product raises an error for a multi-company setting. +3. Access rules allow to just show those products having the same + operating units as the user diff --git a/product_operating_unit/security/product_template_security.xml b/product_operating_unit/security/product_template_security.xml new file mode 100644 index 0000000000..5e1a224830 --- /dev/null +++ b/product_operating_unit/security/product_template_security.xml @@ -0,0 +1,31 @@ + + + + + ['|', ('operating_unit_ids', '=', False), + ('operating_unit_ids', 'in', operating_unit_ids)] + Product Templates from allowed operating units + + + + + + + + + + ['|', ('operating_unit_ids', '=', False), + ('operating_unit_ids', 'in', operating_unit_ids)] + Product Category from allowed operating units + + + + + + + diff --git a/product_operating_unit/static/description/icon.png b/product_operating_unit/static/description/icon.png new file mode 100644 index 0000000000..3a0328b516 Binary files /dev/null and b/product_operating_unit/static/description/icon.png differ diff --git a/product_operating_unit/static/description/index.html b/product_operating_unit/static/description/index.html new file mode 100644 index 0000000000..9a5c051acb --- /dev/null +++ b/product_operating_unit/static/description/index.html @@ -0,0 +1,456 @@ + + + + + +Operating Unit in Products + + + +
+

Operating Unit in Products

+ + +

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

+

This module introduces the following features:

+
    +
  • It introduces the operating unit to the product template.
  • +
  • The operating unit from the user is assigned by default when creating +a new product template.
  • +
  • In case of multi-company, no operating unit from another company to +that assigned to the product can be set for it
  • +
  • It implements user’s security rules.
  • +
+

Table of contents

+ +
+

Usage

+
    +
  1. Create a Product: the Default Operating Unit of the user is assigned +to it. If you want, you can change to another Operating Unit.
  2. +
  3. Assignment of an operating unit of another company to the one set in +the product raises an error for a multi-company setting.
  4. +
  5. Access rules allow to just show those products having the same +operating units as the user
  6. +
+
+ +
+

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

+
    +
  • brain-tec AG
  • +
  • Open Source Integrators
  • +
  • Serpent Consulting Services Pvt. Ltd.
  • +
+
+
+

Contributors

+ +
+
+

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/product_operating_unit/tests/__init__.py b/product_operating_unit/tests/__init__.py new file mode 100644 index 0000000000..2fcbf95749 --- /dev/null +++ b/product_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_product_operating_unit diff --git a/product_operating_unit/tests/test_product_operating_unit.py b/product_operating_unit/tests/test_product_operating_unit.py new file mode 100644 index 0000000000..c9ee1fb2e4 --- /dev/null +++ b/product_operating_unit/tests/test_product_operating_unit.py @@ -0,0 +1,102 @@ +# Copyright (c) 2019 brain-tec AG (http://www.braintec-group.com) +# Copyright (C) 2019 Open Source Integrators +# Copyright (C) 2019 Serpent Consulting Services +# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html). +from odoo.exceptions import ValidationError +from odoo.tests import common + + +class TestProductOperatingUnit(common.TransactionCase): + def setUp(self): + super().setUp() + self.ResUsers = self.env["res.users"] + self.ProductTemplate = self.env["product.template"] + self.ProductCategory = self.env["product.category"] + # company + self.company = self.env.ref("base.main_company") + # group + self.group_user = self.env.ref("base.group_user") + # Main Operating Unit + self.ou1 = self.env.ref("operating_unit.main_operating_unit") + # B2B Operating Unit + self.b2b = self.env.ref("operating_unit.b2b_operating_unit") + # Products + self.product1 = self.env.ref("product.product_product_1_product_template") + self.product2 = self.env.ref("product.product_product_9_product_template") + self.product3 = self.env.ref("product.product_product_11_product_template") + # Create users + self.user1_id = self._create_user( + "user_1", [self.group_user], self.company, [self.ou1, self.b2b] + ) + self.user2_id = self._create_user( + "user_2", [self.group_user], self.company, [self.b2b] + ) + self.product1.categ_id.operating_unit_ids = [(6, 0, [self.ou1.id])] + self.product2.categ_id.operating_unit_ids = [(6, 0, [self.b2b.id])] + self.product3.categ_id.operating_unit_ids = [(6, 0, [self.ou1.id, self.b2b.id])] + + def _create_user(self, login, groups, company, operating_units): + """Create a user.""" + group_ids = [group.id for group in groups] + user = self.ResUsers.with_context(no_reset_password=True).create( + { + "name": "Chicago Purchase User", + "login": login, + "password": "demo", + "email": "chicago@yourcompany.com", + "company_id": company.id, + "company_ids": [(4, company.id)], + "operating_unit_ids": [(4, ou.id) for ou in operating_units], + "groups_id": [(6, 0, group_ids)], + } + ) + return user.id + + def test_po_ou_onchange(self): + with self.assertRaises(ValidationError): + self.product1.operating_unit_ids = [(6, 0, [self.b2b.id])] + + def test_po_ou_security(self): + """Test Security of Product Operating Unit""" + + # User 1 is only assigned to Operating Unit 1, and can see all + # products having Operating Unit 1. + ou_domain = [("operating_unit_ids", "in", self.ou1.id)] + product_ids = ( + self.ProductTemplate.with_user(self.user1_id).search(ou_domain).ids + ) + category_ids = ( + self.ProductCategory.with_user(self.user1_id).search(ou_domain).ids + ) + self.assertIn(category_ids[0], product_ids) + + # User 2 is only assigned to Operating Unit 2, so cannot see products + # having Operating Unit 1, expect those also having Operating Unit b2b + product_ids = ( + self.ProductTemplate.with_user(self.user2_id).search(ou_domain).ids + ) + category_ids = ( + self.ProductCategory.with_user(self.user2_id).search(ou_domain).ids + ) + self.assertIn(category_ids[0], product_ids) + + # User 2 is only assigned to Operating Unit 2, and can see all + # products having Operating Unit b2b. + b2b_domain = [("operating_unit_ids", "in", self.b2b.id)] + product_ids = ( + self.ProductTemplate.with_user(self.user2_id).search(b2b_domain).ids + ) + category_ids = ( + self.ProductCategory.with_user(self.user2_id).search(b2b_domain).ids + ) + self.assertIn(category_ids[0], product_ids) + + # User 1 is only assigned to Operating Unit 1, so cannot see products + # having Operating Unit b2b, expect those also having Operating Unit 1 + product_ids = ( + self.ProductTemplate.with_user(self.user1_id).search(b2b_domain).ids + ) + category_ids = ( + self.ProductCategory.with_user(self.user2_id).search(b2b_domain).ids + ) + self.assertIn(category_ids[0], product_ids) diff --git a/product_operating_unit/views/product_category_view.xml b/product_operating_unit/views/product_category_view.xml new file mode 100644 index 0000000000..86afaa7404 --- /dev/null +++ b/product_operating_unit/views/product_category_view.xml @@ -0,0 +1,21 @@ + + + + product.category.form + product.category + + + + + + + + diff --git a/product_operating_unit/views/product_template_view.xml b/product_operating_unit/views/product_template_view.xml new file mode 100644 index 0000000000..deaaeeac20 --- /dev/null +++ b/product_operating_unit/views/product_template_view.xml @@ -0,0 +1,59 @@ + + + + product.template_tree + product.template + + + + + + + + + + + product.template_form + product.template + + + + + + + + + + product.template_filter + product.template + + + + + + + +