diff --git a/README.md b/README.md index 76d4ab983c..2c7ac9447b 100644 --- a/README.md +++ b/README.md @@ -29,6 +29,7 @@ addon | version | maintainers | summary [operating_unit_access_all](operating_unit_access_all/) | 18.0.1.0.0 | kittiu | Access all Operating Units [purchase_operating_unit](purchase_operating_unit/) | 18.0.1.0.0 | | Adds the concecpt of operating unit (OU) in purchase order management [report_qweb_operating_unit](report_qweb_operating_unit/) | 18.0.1.0.0 | | Qweb Report With Operating Unit +[sale_operating_unit](sale_operating_unit/) | 18.0.1.0.0 | | An operating unit (OU) is an organizational entity part of a company [sales_team_operating_unit](sales_team_operating_unit/) | 18.0.1.0.0 | | Sales Team Operating Unit [stock_operating_unit](stock_operating_unit/) | 18.0.1.0.0 | | Adds the concept of operating unit (OU) in stock management [stock_operating_unit_access_all](stock_operating_unit_access_all/) | 18.0.1.0.0 | kittiu | Access all OUs' Stock diff --git a/sale_operating_unit/README.rst b/sale_operating_unit/README.rst new file mode 100644 index 0000000000..76330e3055 --- /dev/null +++ b/sale_operating_unit/README.rst @@ -0,0 +1,112 @@ +.. image:: https://odoo-community.org/readme-banner-image + :target: https://odoo-community.org/get-involved?utm_source=readme + :alt: Odoo Community Association + +======================= +Operating Unit in Sales +======================= + +.. + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! This file is generated by oca-gen-addon-readme !! + !! changes will be overwritten. !! + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! source digest: sha256:9c353c6d772418671c257be8a0adf330722edb7ed7ab93cfdb2a4b72284116a0 + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + +.. |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/18.0/sale_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-sale_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 extends the Sales capabilities of Odoo and introduces the +operating unit to the Sales Order. Security rules are defined to ensure +that users can only display the Sales Orders in which they are allowed +access to. + +**Table of contents** + +.. contents:: + :local: + +Usage +===== + +Follow these steps: + +1. Create a Sale Order. +2. The Operating Unit of the Sale Team is assigned to the Sale Order. + +Known issues / Roadmap +====================== + +The onchange_team_id method is not working. If you try to change the +team in a sales quotation and the operating unit is not taken from the +sales team, and moreover, I cannot select the operating unit from the +team, only Main OU shows, must have to have your salesperson in the +sales team of the 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 +------- + +* ForgeFlow +* Serpent Consulting Services Pvt. Ltd. + +Contributors +------------ + +- Jordi Ballester Alomar +- Aaron Henriquez +- Lois Rilo +- Miquel Raich +- Sudhir Arya +- Darshan Patel +- Alan Ramos +- Jorge Alberto Olvera Cuenca +- Alejandro Padrón +- `Heliconia Solutions Pvt. Ltd. `__ + + - Bhavesh Heliconia + +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/sale_operating_unit/__init__.py b/sale_operating_unit/__init__.py new file mode 100644 index 0000000000..1fc45468f5 --- /dev/null +++ b/sale_operating_unit/__init__.py @@ -0,0 +1,4 @@ +# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html). + +from . import models +from . import report diff --git a/sale_operating_unit/__manifest__.py b/sale_operating_unit/__manifest__.py new file mode 100644 index 0000000000..d1da0d7871 --- /dev/null +++ b/sale_operating_unit/__manifest__.py @@ -0,0 +1,23 @@ +# © 2019 ForgeFlow S.L. +# - Jordi Ballester Alomar +# © 2019 Serpent Consulting Services Pvt. Ltd. - Sudhir Arya +# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html). +{ + "name": "Operating Unit in Sales", + "version": "18.0.1.0.0", + "summary": "An operating unit (OU) is an organizational entity part of a " + "company", + "author": "ForgeFlow, " + "Serpent Consulting Services Pvt. Ltd.," + "Odoo Community Association (OCA)", + "license": "LGPL-3", + "website": "https://github.com/OCA/operating-unit", + "category": "Sales Management", + "depends": ["sale", "account_operating_unit", "sales_team_operating_unit"], + "data": [ + "security/sale_security.xml", + "views/sale_view.xml", + "views/sale_report_view.xml", + ], + "installable": True, +} diff --git a/sale_operating_unit/i18n/it.po b/sale_operating_unit/i18n/it.po new file mode 100644 index 0000000000..4631fdb47e --- /dev/null +++ b/sale_operating_unit/i18n/it.po @@ -0,0 +1,68 @@ +# Translation of Odoo Server. +# This file contains the translation of the following modules: +# * sale_operating_unit +# +msgid "" +msgstr "" +"Project-Id-Version: Odoo Server 17.0\n" +"Report-Msgid-Bugs-To: \n" +"PO-Revision-Date: 2024-08-07 08:58+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 5.6.2\n" + +#. module: sale_operating_unit +#. odoo-python +#: code:addons/sale_operating_unit/models/crm_team.py:0 +#, python-format +msgid "" +"Configuration error. It is not possible to change this team. There are sale " +"orders referencing it in other operating units" +msgstr "" +"Errore di configurazione. Non è possibile modificare questo team. Esistono " +"ordini di vendita relazionati ad esso in altre unità operative" + +#. module: sale_operating_unit +#. odoo-python +#: code:addons/sale_operating_unit/models/sale_order.py:0 +#, python-format +msgid "" +"Configuration error. The Operating Unit of the sales team must match with " +"that of the quote/sales order." +msgstr "" +"Errore di configurazione. L'unità operativa del team di vendita deve essere " +"la stessa del preventivo/ordine di vendita." + +#. module: sale_operating_unit +#: model:ir.model.fields,field_description:sale_operating_unit.field_sale_order__operating_unit_id +#: model:ir.model.fields,field_description:sale_operating_unit.field_sale_order_line__operating_unit_id +#: model:ir.model.fields,field_description:sale_operating_unit.field_sale_report__operating_unit_id +#: model_terms:ir.ui.view,arch_db:sale_operating_unit.view_sale_report_search +#: model_terms:ir.ui.view,arch_db:sale_operating_unit.view_sales_order_filter +msgid "Operating Unit" +msgstr "Unità operativa" + +#. module: sale_operating_unit +#: model:ir.model,name:sale_operating_unit.model_sale_report +msgid "Sales Analysis Report" +msgstr "Resoconto analisi vendite" + +#. module: sale_operating_unit +#: model:ir.model,name:sale_operating_unit.model_sale_order +msgid "Sales Order" +msgstr "Ordine di vendita" + +#. module: sale_operating_unit +#: model:ir.model,name:sale_operating_unit.model_sale_order_line +msgid "Sales Order Line" +msgstr "Riga ordine di vendita" + +#. module: sale_operating_unit +#: model:ir.model,name:sale_operating_unit.model_crm_team +msgid "Sales Team" +msgstr "Team di vendita" diff --git a/sale_operating_unit/i18n/sale_operating_unit.pot b/sale_operating_unit/i18n/sale_operating_unit.pot new file mode 100644 index 0000000000..33eb044fdd --- /dev/null +++ b/sale_operating_unit/i18n/sale_operating_unit.pot @@ -0,0 +1,59 @@ +# Translation of Odoo Server. +# This file contains the translation of the following modules: +# * sale_operating_unit +# +msgid "" +msgstr "" +"Project-Id-Version: Odoo Server 18.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: sale_operating_unit +#. odoo-python +#: code:addons/sale_operating_unit/models/crm_team.py:0 +msgid "" +"Configuration error. It is not possible to change this team. There are sale " +"orders referencing it in other operating units" +msgstr "" + +#. module: sale_operating_unit +#. odoo-python +#: code:addons/sale_operating_unit/models/sale_order.py:0 +msgid "" +"Configuration error. The Operating Unit of the sales team must match with " +"that of the quote/sales order." +msgstr "" + +#. module: sale_operating_unit +#: model:ir.model.fields,field_description:sale_operating_unit.field_sale_order__operating_unit_id +#: model:ir.model.fields,field_description:sale_operating_unit.field_sale_order_line__operating_unit_id +#: model:ir.model.fields,field_description:sale_operating_unit.field_sale_report__operating_unit_id +#: model_terms:ir.ui.view,arch_db:sale_operating_unit.view_sale_report_search +#: model_terms:ir.ui.view,arch_db:sale_operating_unit.view_sales_order_filter +msgid "Operating Unit" +msgstr "" + +#. module: sale_operating_unit +#: model:ir.model,name:sale_operating_unit.model_sale_report +msgid "Sales Analysis Report" +msgstr "" + +#. module: sale_operating_unit +#: model:ir.model,name:sale_operating_unit.model_sale_order +msgid "Sales Order" +msgstr "" + +#. module: sale_operating_unit +#: model:ir.model,name:sale_operating_unit.model_sale_order_line +msgid "Sales Order Line" +msgstr "" + +#. module: sale_operating_unit +#: model:ir.model,name:sale_operating_unit.model_crm_team +msgid "Sales Team" +msgstr "" diff --git a/sale_operating_unit/models/__init__.py b/sale_operating_unit/models/__init__.py new file mode 100644 index 0000000000..afea3f6575 --- /dev/null +++ b/sale_operating_unit/models/__init__.py @@ -0,0 +1,4 @@ +# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html). + +from . import crm_team +from . import sale_order diff --git a/sale_operating_unit/models/crm_team.py b/sale_operating_unit/models/crm_team.py new file mode 100644 index 0000000000..6e9736af07 --- /dev/null +++ b/sale_operating_unit/models/crm_team.py @@ -0,0 +1,34 @@ +# © 2019 ForgeFlow S.L. +# - Jordi Ballester Alomar +# © 2019 Serpent Consulting Services Pvt. Ltd. - Sudhir Arya +# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html). +from odoo import SUPERUSER_ID, api, models +from odoo.exceptions import ValidationError + + +class CrmTeam(models.Model): + _inherit = "crm.team" + + @api.constrains("operating_unit_id") + def _check_sales_order_operating_unit(self): + for rec in self: + orders = ( + self.with_user(SUPERUSER_ID) + .env["sale.order"] + .search( + [ + ("team_id", "=", rec.id), + ("operating_unit_id", "!=", rec.operating_unit_id.id), + ] + ) + ) + if orders: + raise ValidationError( + self.env._( + "Configuration error. It is not " + "possible to change this " + "team. There are sale orders " + "referencing it in other operating " + "units" + ) + ) diff --git a/sale_operating_unit/models/sale_order.py b/sale_operating_unit/models/sale_order.py new file mode 100644 index 0000000000..3dfe01cd96 --- /dev/null +++ b/sale_operating_unit/models/sale_order.py @@ -0,0 +1,92 @@ +# © 2019 ForgeFlow S.L. +# - Jordi Ballester Alomar +# © 2019 Serpent Consulting Services Pvt. Ltd. - Sudhir Arya +# 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 SaleOrder(models.Model): + _inherit = "sale.order" + + operating_unit_id = fields.Many2one( + comodel_name="operating.unit", + string="Operating Unit", + readonly=False, + store=True, + precompute=True, + check_company=True, + compute="_compute_operating_unit_id", + ) + + @api.depends("team_id") + def _compute_operating_unit_id(self): + for sale in self: + if sale.team_id: + sale.operating_unit_id = sale.team_id.operating_unit_id + + @api.depends("partner_id", "user_id", "operating_unit_id") + def _compute_team_id(self): + res = super()._compute_team_id() + for order in self: + if ( + order.team_id + and order.team_id.operating_unit_id != order.operating_unit_id + ): + order.team_id = False + return res + + @api.depends("operating_unit_id") + def _compute_journal_id(self): + res = super()._compute_journal_id() + for sale in self: + if not sale.journal_id or ( + sale.journal_id + and sale.operating_unit_id + and sale.journal_id.operating_unit_id != sale.operating_unit_id + ): + sale.journal_id = ( + self.env["account.journal"] + .search( + [ + "|", + ("operating_unit_id", "=", sale.operating_unit_id.id), + ("operating_unit_id", "=", False), + "|", + ("company_id", "=", sale.company_id.id), + ("company_id", "=", False), + ("type", "=", "sale"), + ], + limit=1, + ) + .id + ) + return res + + @api.constrains("team_id", "operating_unit_id") + def _check_team_operating_unit(self): + for rec in self: + if rec.team_id and rec.team_id.operating_unit_id != rec.operating_unit_id: + raise ValidationError( + self.env._( + "Configuration error. The Operating " + "Unit of the sales team must match " + "with that of the quote/sales order." + ) + ) + + def _prepare_invoice(self): + self.ensure_one() + invoice_vals = super()._prepare_invoice() + invoice_vals["operating_unit_id"] = self.operating_unit_id.id + return invoice_vals + + +class SaleOrderLine(models.Model): + _inherit = "sale.order.line" + + operating_unit_id = fields.Many2one( + related="order_id.operating_unit_id", + string="Operating Unit", + store=True, + ) diff --git a/sale_operating_unit/pyproject.toml b/sale_operating_unit/pyproject.toml new file mode 100644 index 0000000000..4231d0cccb --- /dev/null +++ b/sale_operating_unit/pyproject.toml @@ -0,0 +1,3 @@ +[build-system] +requires = ["whool"] +build-backend = "whool.buildapi" diff --git a/sale_operating_unit/readme/CONTRIBUTORS.md b/sale_operating_unit/readme/CONTRIBUTORS.md new file mode 100644 index 0000000000..c90c4e1418 --- /dev/null +++ b/sale_operating_unit/readme/CONTRIBUTORS.md @@ -0,0 +1,11 @@ +- Jordi Ballester Alomar \<\> +- Aaron Henriquez \<\> +- Lois Rilo \<\> +- Miquel Raich \<\> +- Sudhir Arya \<\> +- Darshan Patel \<\> +- Alan Ramos \<\> +- Jorge Alberto Olvera Cuenca \<\> +- Alejandro Padrón \<\> +- [Heliconia Solutions Pvt. Ltd.](https://www.heliconia.io) + - Bhavesh Heliconia diff --git a/sale_operating_unit/readme/DESCRIPTION.md b/sale_operating_unit/readme/DESCRIPTION.md new file mode 100644 index 0000000000..0038650727 --- /dev/null +++ b/sale_operating_unit/readme/DESCRIPTION.md @@ -0,0 +1,4 @@ +This module extends the Sales capabilities of Odoo and introduces the +operating unit to the Sales Order. Security rules are defined to ensure +that users can only display the Sales Orders in which they are allowed +access to. diff --git a/sale_operating_unit/readme/ROADMAP.md b/sale_operating_unit/readme/ROADMAP.md new file mode 100644 index 0000000000..5173b57f48 --- /dev/null +++ b/sale_operating_unit/readme/ROADMAP.md @@ -0,0 +1,5 @@ +The onchange_team_id method is not working. If you try to change the +team in a sales quotation and the operating unit is not taken from the +sales team, and moreover, I cannot select the operating unit from the +team, only Main OU shows, must have to have your salesperson in the +sales team of the operating unit. diff --git a/sale_operating_unit/readme/USAGE.md b/sale_operating_unit/readme/USAGE.md new file mode 100644 index 0000000000..5c3c9e30fb --- /dev/null +++ b/sale_operating_unit/readme/USAGE.md @@ -0,0 +1,4 @@ +Follow these steps: + +1. Create a Sale Order. +2. The Operating Unit of the Sale Team is assigned to the Sale Order. diff --git a/sale_operating_unit/report/__init__.py b/sale_operating_unit/report/__init__.py new file mode 100644 index 0000000000..fca264f877 --- /dev/null +++ b/sale_operating_unit/report/__init__.py @@ -0,0 +1,3 @@ +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html). + +from . import sale_report diff --git a/sale_operating_unit/report/sale_report.py b/sale_operating_unit/report/sale_report.py new file mode 100644 index 0000000000..49f8fda803 --- /dev/null +++ b/sale_operating_unit/report/sale_report.py @@ -0,0 +1,19 @@ +# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl-3.0). + +from odoo import fields, models + + +class SaleReport(models.Model): + _inherit = "sale.report" + + operating_unit_id = fields.Many2one("operating.unit", "Operating Unit") + + def _group_by_sale(self): + res = super()._group_by_sale() + res += """, s.operating_unit_id""" + return res + + def _select_additional_fields(self): + res = super()._select_additional_fields() + res["operating_unit_id"] = "s.operating_unit_id" + return res diff --git a/sale_operating_unit/security/sale_security.xml b/sale_operating_unit/security/sale_security.xml new file mode 100644 index 0000000000..fa19ea17e5 --- /dev/null +++ b/sale_operating_unit/security/sale_security.xml @@ -0,0 +1,42 @@ + + + + + + ['|',('operating_unit_id','=',False),('operating_unit_id','in', operating_unit_ids)] + Sales Orders from allowed operating units + + + + + + + + + ['|',('operating_unit_id','=',False),('operating_unit_id','in', operating_unit_ids)] + Sales Order lines from allowed operating units + + + + + + + + + ['|',('operating_unit_id','=',False),('operating_unit_id','in', operating_unit_ids)] + Sales Report from allowed operating units + + + + + + + diff --git a/sale_operating_unit/static/description/icon.png b/sale_operating_unit/static/description/icon.png new file mode 100644 index 0000000000..3a0328b516 Binary files /dev/null and b/sale_operating_unit/static/description/icon.png differ diff --git a/sale_operating_unit/static/description/index.html b/sale_operating_unit/static/description/index.html new file mode 100644 index 0000000000..602f07f01b --- /dev/null +++ b/sale_operating_unit/static/description/index.html @@ -0,0 +1,463 @@ + + + + + +README.rst + + + +
+ + + +Odoo Community Association + +
+

Operating Unit in Sales

+ +

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

+

This module extends the Sales capabilities of Odoo and introduces the +operating unit to the Sales Order. Security rules are defined to ensure +that users can only display the Sales Orders in which they are allowed +access to.

+

Table of contents

+ +
+

Usage

+

Follow these steps:

+
    +
  1. Create a Sale Order.
  2. +
  3. The Operating Unit of the Sale Team is assigned to the Sale Order.
  4. +
+
+
+

Known issues / Roadmap

+

The onchange_team_id method is not working. If you try to change the +team in a sales quotation and the operating unit is not taken from the +sales team, and moreover, I cannot select the operating unit from the +team, only Main OU shows, must have to have your salesperson in the +sales team of the 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

+
    +
  • ForgeFlow
  • +
  • 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/sale_operating_unit/tests/__init__.py b/sale_operating_unit/tests/__init__.py new file mode 100644 index 0000000000..fb298f99cf --- /dev/null +++ b/sale_operating_unit/tests/__init__.py @@ -0,0 +1,3 @@ +# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html). + +from . import test_sale_operating_unit diff --git a/sale_operating_unit/tests/test_sale_operating_unit.py b/sale_operating_unit/tests/test_sale_operating_unit.py new file mode 100644 index 0000000000..5c563c8b13 --- /dev/null +++ b/sale_operating_unit/tests/test_sale_operating_unit.py @@ -0,0 +1,219 @@ +# © 2019 ForgeFlow S.L. - +# Jordi Ballester Alomar +# © 2019 Serpent Consulting Services Pvt. Ltd. - Sudhir Arya +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html). + +from odoo.exceptions import ValidationError +from odoo.models import Command + +from odoo.addons.operating_unit.tests.common import OperatingUnitCommon + + +class TestSaleOperatingUnit(OperatingUnitCommon): + @classmethod + def setUpClass(cls): + super().setUpClass() + cls.sale_model = cls.env["sale.order"] + cls.sale_line_model = cls.env["sale.order.line"] + cls.sale_team_model = cls.env["crm.team"] + cls.acc_move_model = cls.env["account.move"] + cls.product_model = cls.env["product.product"] + cls.payment_model = cls.env["sale.advance.payment.inv"] + # Company + cls.grp_sale_user = cls.env.ref("sales_team.group_sale_manager") + cls.grp_acc_user = cls.env.ref("account.group_account_invoice") + # Payment Term + cls.pay = cls.env.ref("account.account_payment_term_immediate") + # Customer + cls.customer = cls.env.ref("base.res_partner_2") + # Price list + cls.pricelist = cls.env["product.pricelist"].search([], limit=1) + # Products + cls.product1 = cls.env.ref("product.product_product_2") + cls.product1.write({"invoice_policy": "order"}) + # Update users + cls.user1.write( + { + "groups_id": [ + Command.link(cls.grp_sale_user.id), + Command.link(cls.grp_acc_user.id), + ], + "operating_unit_ids": [ + Command.link(cls.ou1.id), + Command.link(cls.b2c.id), + ], + } + ) + cls.user2.write( + { + "groups_id": [ + Command.link(cls.grp_sale_user.id), + Command.link(cls.grp_acc_user.id), + ], + "default_operating_unit_id": [], + } + ) + + # Create sales team OU1 + cls.sale_team_ou1 = cls._create_sale_team(cls.user1.id, cls.ou1) + + # Create sales team OU2 + cls.sale_team_b2c = cls._create_sale_team(cls.user2.id, cls.b2c) + + # Create Sale Order1 + cls.sale1 = cls._create_sale_order( + cls.user1.id, + cls.customer, + cls.product1, + cls.pricelist, + cls.sale_team_ou1, + ) + # Create Sale Order2 + cls.sale2 = cls._create_sale_order( + cls.user2.id, + cls.customer, + cls.product1, + cls.pricelist, + cls.sale_team_b2c, + ) + + @classmethod + def _create_sale_team(cls, uid, operating_unit): + """Create a sale team.""" + team = ( + cls.sale_team_model.with_user(uid) + .with_context(mail_create_nosubscribe=True) + .create( + { + "name": operating_unit.name, + "operating_unit_id": operating_unit.id, + "company_id": operating_unit.company_id.id, + } + ) + ) + return team + + @classmethod + def _create_sale_order(cls, uid, customer, product, pricelist, team): + """Create a sale order.""" + sale = cls.sale_model.with_user(uid).create( + { + "partner_id": customer.id, + "partner_invoice_id": customer.id, + "partner_shipping_id": customer.id, + "pricelist_id": pricelist.id, + "team_id": team.id, + "operating_unit_id": team.operating_unit_id.id, + } + ) + cls.sale_line_model.with_user(uid).create( + { + "order_id": sale.id, + "product_id": product.id, + "name": "Sale Order Line", + "product_uom_qty": 1, + } + ) + return sale + + def _confirm_sale(self, sale): + sale.action_confirm() + sale_context = { + "active_model": "sale.order", + "active_ids": [sale.id], + "active_id": sale.id, + } + # Let's do an invoice with invoiceable lines + payment = ( + self.env["sale.advance.payment.inv"] + .with_context(**sale_context) + .create({"advance_payment_method": "delivered"}) + ) + res = payment.create_invoices() + invoice_id = res["res_id"] + return invoice_id + + def test_security(self): + """Test Sale Operating Unit""" + # User 2 is only assigned to Operating Unit B2C, and cannot + # Access Sales order from Main Operating Unit. + sale = self.sale_model.with_user(self.user2.id).search( + [("id", "=", self.sale1.id), ("operating_unit_id", "=", self.ou1.id)] + ) + self.assertEqual( + sale.ids, [], f"User 2 should not have access to OU {self.ou1.name}" + ) + # Confirm Sale1 + self._confirm_sale(self.sale1) + # Confirm Sale2 + b2c_invoice_id = self._confirm_sale(self.sale2) + # Checks that invoice has OU b2c + b2c = self.acc_move_model.with_user(self.user2.id).search( + [("id", "=", b2c_invoice_id), ("operating_unit_id", "=", self.b2c.id)] + ) + self.assertNotEqual(b2c.ids, [], "Invoice should have b2c OU") + + def test_security_2(self): + """Test Sale Operating Unit""" + # User 2 is only assigned to Operating Unit B2C, and cannot + # Access Sales order from Main Operating Unit. + sale = self.sale_model.with_user(self.user2.id).search( + [("id", "=", self.sale1.id), ("operating_unit_id", "=", self.ou1.id)] + ) + self.assertEqual( + sale.ids, [], f"User 2 should not have access to OU {self.ou1.name}" + ) + + sale = self.sale_model.with_user(self.user2.id).search( + [("id", "=", self.sale2.id), ("operating_unit_id", "=", self.b2c.id)] + ) + + self.assertEqual( + len(sale.ids), 1, f"User 1 should have access to OU {self.b2c.name}" + ) + + def test_check_sales_order_operating_unit(self): + """Test that changing the operating unit of a sales team + with existing sale orders raises an error.""" + with self.assertRaises(ValidationError): + self.sale_team_ou1.operating_unit_id = self.b2c.id + + def test_compute_operating_unit_id(self): + """Test that changing the sales team updates the + operating unit in sale order.""" + sale_order = self.sale_model.create( + { + "partner_id": self.customer.id, + "team_id": self.sale_team_ou1.id, + } + ) + self.assertEqual(sale_order.operating_unit_id, self.ou1) + sale_order.team_id = self.sale_team_b2c.id + self.assertEqual(sale_order.operating_unit_id, self.b2c) + + def test_compute_team_id(self): + """Test that sales team resets if its operating unit does not match.""" + sale_order = self.sale_model.create( + { + "partner_id": self.customer.id, + "operating_unit_id": self.ou1.id, + "team_id": self.sale_team_ou1.id, + } + ) + self.assertEqual(sale_order.team_id, self.sale_team_ou1) + sale_order.operating_unit_id = self.b2c.id + self.assertFalse( + sale_order.team_id, "Team should reset when operating unit mismatches" + ) + + def test_check_team_operating_unit_violation(self): + """Test that a ValidationError is raised when the team + and operating unit do not match.""" + with self.assertRaises(ValidationError): + self.sale_model.create( + { + "partner_id": self.customer.id, + "operating_unit_id": self.ou1.id, + "team_id": self.sale_team_b2c.id, + } + ) diff --git a/sale_operating_unit/views/sale_report_view.xml b/sale_operating_unit/views/sale_report_view.xml new file mode 100644 index 0000000000..43adcf6c9e --- /dev/null +++ b/sale_operating_unit/views/sale_report_view.xml @@ -0,0 +1,27 @@ + + + + + sale.report.search + sale.report + + + + + + + + + + + diff --git a/sale_operating_unit/views/sale_view.xml b/sale_operating_unit/views/sale_view.xml new file mode 100644 index 0000000000..6efe3efaa6 --- /dev/null +++ b/sale_operating_unit/views/sale_view.xml @@ -0,0 +1,78 @@ + + + + + sale.order.tree + sale.order + + + + + + + + + sale.order.tree + sale.order + + + + + + + + + sale.order.form + sale.order + + + + + + + + [('type', '=', 'sale'),"|",("operating_unit_id","=", operating_unit_id),("operating_unit_id","=", False)] + + + + + sale.order.list.select + sale.order + + + + + + + + + + + diff --git a/setup/_metapackage/pyproject.toml b/setup/_metapackage/pyproject.toml index 1242b6c1ab..82667f2df6 100644 --- a/setup/_metapackage/pyproject.toml +++ b/setup/_metapackage/pyproject.toml @@ -1,6 +1,6 @@ [project] name = "odoo-addons-oca-operating-unit" -version = "18.0.20251111.0" +version = "18.0.20251209.0" dependencies = [ "odoo-addon-account_operating_unit==18.0.*", "odoo-addon-analytic_operating_unit==18.0.*", @@ -10,6 +10,7 @@ dependencies = [ "odoo-addon-operating_unit_access_all==18.0.*", "odoo-addon-purchase_operating_unit==18.0.*", "odoo-addon-report_qweb_operating_unit==18.0.*", + "odoo-addon-sale_operating_unit==18.0.*", "odoo-addon-sales_team_operating_unit==18.0.*", "odoo-addon-stock_operating_unit==18.0.*", "odoo-addon-stock_operating_unit_access_all==18.0.*",