diff --git a/sale_product_approval/README.rst b/sale_product_approval/README.rst new file mode 100644 index 00000000000..39b0fe9d381 --- /dev/null +++ b/sale_product_approval/README.rst @@ -0,0 +1,117 @@ +===================== +Sale Product Approval +===================== + +.. + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! This file is generated by oca-gen-addon-readme !! + !! changes will be overwritten. !! + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! source digest: sha256:5290a42368d5045c781cfd4477d2cb3a5310ed2dc8a0d5168a0f6109c3ca9ba9 + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + +.. |badge1| image:: https://img.shields.io/badge/maturity-Alpha-red.png + :target: https://odoo-community.org/page/development-status + :alt: Alpha +.. |badge2| image:: https://img.shields.io/badge/licence-AGPL--3-blue.png + :target: http://www.gnu.org/licenses/agpl-3.0-standalone.html + :alt: License: AGPL-3 +.. |badge3| image:: https://img.shields.io/badge/github-OCA%2Fsale--workflow-lightgray.png?logo=github + :target: https://github.com/OCA/sale-workflow/tree/17.0/sale_product_approval + :alt: OCA/sale-workflow +.. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png + :target: https://translation.odoo-community.org/projects/sale-workflow-17-0/sale-workflow-17-0-sale_product_approval + :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/sale-workflow&target_branch=17.0 + :alt: Try me on Runboat + +|badge1| |badge2| |badge3| |badge4| |badge5| + +This module links the 'Can be sold' field to the states. The states then +control whether or not the product can be sold or not in the particular +state. + +.. IMPORTANT:: + This is an alpha version, the data model and design can change at any time without warning. + Only for development or testing purpose, do not use in production. + `More details on development status `_ + +**Table of contents** + +.. contents:: + :local: + +Configuration +============= + +- Create product states. +- Check the 'Approved to be added to SO' box on the states where the + product is allowed to be added to an SO when it's in that state. +- Check the 'Approved to be sold' box on the states where the product + is allowed to be sold when it's in that state. + +Usage +===== + +- When a product is moved to a state where it's approved, the 'Can be + sold' box should be checked and the product is selectable on sales + quotes. +- If the product is not in a state where it's approved to be sold, then + the 'Can be sold' check box is not checked and the product can not be + added to sales quotes. +- If it's not approved, any sales quote that already contained the + product will show a warning and have an exception activity. + +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 +------- + +* Open Source Integrators + +Contributors +------------ + +- `Open Source Integrators `__. + + - Chandresh Thakkar + - Daniel Reis + - Patrick Wilson + - Hardik Suthar + +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. + +.. |maintainer-dreispt| image:: https://github.com/dreispt.png?size=40px + :target: https://github.com/dreispt + :alt: dreispt + +Current `maintainer `__: + +|maintainer-dreispt| + +This module is part of the `OCA/sale-workflow `_ project on GitHub. + +You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute. diff --git a/sale_product_approval/__init__.py b/sale_product_approval/__init__.py new file mode 100644 index 00000000000..0a5e205d4e6 --- /dev/null +++ b/sale_product_approval/__init__.py @@ -0,0 +1,5 @@ +# Copyright 2021 Open Source Integrators +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + +from . import models +from .hooks import _set_candidate_sale diff --git a/sale_product_approval/__manifest__.py b/sale_product_approval/__manifest__.py new file mode 100644 index 00000000000..013047bde28 --- /dev/null +++ b/sale_product_approval/__manifest__.py @@ -0,0 +1,26 @@ +# Copyright 2021 Open Source Integrators +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + +{ + "name": "Sale Product Approval", + "summary": """Control whether or not the product can be sold or not + in the particular state""", + "version": "17.0.1.0.0", + "website": "https://github.com/OCA/sale-workflow", + "category": "Products", + "author": "Open Source Integrators, Odoo Community Association (OCA)", + "license": "AGPL-3", + "installable": True, + "maintainers": ["dreispt"], + "development_status": "Alpha", + "depends": ["product_state", "sale_management", "stock"], + "data": [ + "security/res_groups.xml", + "views/menu_items.xml", + "views/product_exception.xml", + "views/product_state.xml", + "views/product_template.xml", + "views/sale_order.xml", + ], + "post_init_hook": "_set_candidate_sale", +} diff --git a/sale_product_approval/hooks.py b/sale_product_approval/hooks.py new file mode 100644 index 00000000000..df8e1220e07 --- /dev/null +++ b/sale_product_approval/hooks.py @@ -0,0 +1,13 @@ +# Copyright 2019 ForgeFlow S.L. +# (http://www.forgeflow.com) +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + + +def _set_candidate_sale(env): + """ + This post-init-hook will update last price information for all products + """ + product_obj = env["product.template"] + products = product_obj.search([("sale_ok", "=", True)]) + products.write({"candidate_sale": True, "sale_ok": True}) + products.write({"candidate_sale_confirm": True, "sale_ok_confirm": True}) diff --git a/sale_product_approval/models/__init__.py b/sale_product_approval/models/__init__.py new file mode 100644 index 00000000000..da58949756f --- /dev/null +++ b/sale_product_approval/models/__init__.py @@ -0,0 +1,7 @@ +# Copyright 2021 Open Source Integrators +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + +from . import product_product +from . import product_state +from . import sale_order +from . import sale_order_line diff --git a/sale_product_approval/models/product_product.py b/sale_product_approval/models/product_product.py new file mode 100644 index 00000000000..19bd3cc83d7 --- /dev/null +++ b/sale_product_approval/models/product_product.py @@ -0,0 +1,94 @@ +# Copyright 2021 Open Source Integrators +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + +from odoo import api, fields, models + + +class ProductTemplate(models.Model): + _inherit = "product.template" + + sale_ok = fields.Boolean( + string="Can be added to Quote", + copy=False, + readonly=True, + ) + sale_ok_confirm = fields.Boolean( + string="Can be Sold", + copy=False, + readonly=True, + ) + candidate_sale = fields.Boolean( + string="Candidate to add to Quote", + compute="_compute_candidate_sale_confirm", + store=True, + readonly=False, + ) + candidate_sale_confirm = fields.Boolean( + string="Candidate to be Sold", + compute="_compute_candidate_sale_confirm", + store=True, + readonly=False, + ) + can_edit_candidate = fields.Boolean(compute="_compute_can_edit_candidate") + + def _compute_can_edit_candidate(self): + self.update( + { + "can_edit_candidate": self.env.user.has_group( + "sale_product_approval.group_product_administrator" + ) + } + ) + + @api.depends("candidate_sale_confirm") + def _compute_candidate_sale_confirm(self): + for product in self: + if product.candidate_sale_confirm: + product.candidate_sale = True + + @api.model_create_multi + def create(self, vals_list): + templates = super().create(vals_list) + templates._set_sale_ok_product() + return templates + + def write(self, vals): + res = super().write(vals) + if ( + "product_state_id" in vals + or "candidate_sale" in vals + or "candidate_sale_confirm" in vals + ): + self._set_sale_ok_product() + return res + + def _set_sale_ok_product(self): + order_ids = ( + self.env["sale.order.line"] + .search( + [ + ("product_id", "in", self.product_variant_ids.ids), + ("state", "in", ["draft", "sent"]), + ("approved_sale_confirm", "=", True), + ] + ) + .mapped("order_id") + ) + for product in self: + if product.product_state_id: + product.sale_ok = ( + product.candidate_sale and product.product_state_id.approved_sale + ) + product.sale_ok_confirm = ( + product.candidate_sale_confirm + and product.product_state_id.approved_sale_confirm + ) + + if not product.sale_ok_confirm: + order_ids._log_exception_activity_sale(product) + + @api.model + def _get_default_product_state_id(self): + return self.env.ref( + "product_state.product_state_draft", raise_if_not_found=False + ) diff --git a/sale_product_approval/models/product_state.py b/sale_product_approval/models/product_state.py new file mode 100644 index 00000000000..172bfd0dbe9 --- /dev/null +++ b/sale_product_approval/models/product_state.py @@ -0,0 +1,11 @@ +# Copyright 2021 Open Source Integrators +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + +from odoo import fields, models + + +class ProductState(models.Model): + _inherit = "product.state" + + approved_sale = fields.Boolean(string="Approved to be added to SO", default=True) + approved_sale_confirm = fields.Boolean(string="Approved to be Sold", default=True) diff --git a/sale_product_approval/models/sale_order.py b/sale_product_approval/models/sale_order.py new file mode 100644 index 00000000000..792c69dc731 --- /dev/null +++ b/sale_product_approval/models/sale_order.py @@ -0,0 +1,59 @@ +# Copyright 2021 Open Source Integrators +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). +from odoo import SUPERUSER_ID, _, api, fields, models +from odoo.exceptions import UserError + + +class SaleOrder(models.Model): + _inherit = "sale.order" + + exceptions_sale_approval_confirm = fields.Boolean( + compute="_compute_exceptions_sale_approval_confirm", + string="Exception", + ) + override_saleable_exception = fields.Boolean() + + @api.depends("order_line.approved_sale_confirm") + def _compute_exceptions_sale_approval_confirm(self): + for so in self: + so.exceptions_sale_approval_confirm = any( + not line.approved_sale_confirm + for line in so.order_line.filtered(lambda x: not x.display_type) + ) + + def _log_exception_activity_sale(self, product_id): + for order in self: + # Convert current date to the timezone of the user set on the order + local_date = fields.Datetime.context_timestamp( + order.user_id, fields.Datetime.now() + ).date() + + note = self._render_product_approval_exception(order, product_id) + order.activity_schedule( + "mail.mail_activity_data_warning", + local_date, + note=note, + user_id=order.user_id.id or SUPERUSER_ID, + ) + + def _render_product_approval_exception(self, order, product_id): + values = {"sale_order_ref": order, "product_ref": product_id} + return self.env["ir.ui.view"]._render_template( + template=self.env.ref("sale_product_approval.exception_on_product").id, + values=values, + ) + + def action_confirm(self): + res = super().action_confirm() + for sale in self: + if ( + sale.exceptions_sale_approval_confirm + and not sale.override_saleable_exception + ): + raise UserError( + _( + "You can not confirm this sale order " + "because some products are not sellable in this order." + ) + ) + return res diff --git a/sale_product_approval/models/sale_order_line.py b/sale_product_approval/models/sale_order_line.py new file mode 100644 index 00000000000..bde19d9647d --- /dev/null +++ b/sale_product_approval/models/sale_order_line.py @@ -0,0 +1,15 @@ +# Copyright 2021 Open Source Integrators +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + +from odoo import fields, models + + +class SaleOrderLine(models.Model): + _inherit = "sale.order.line" + + approved_sale_confirm = fields.Boolean( + related="product_id.sale_ok_confirm", + string="Approved for Sale", + store=True, + default=True, + ) diff --git a/sale_product_approval/pyproject.toml b/sale_product_approval/pyproject.toml new file mode 100644 index 00000000000..4231d0cccb3 --- /dev/null +++ b/sale_product_approval/pyproject.toml @@ -0,0 +1,3 @@ +[build-system] +requires = ["whool"] +build-backend = "whool.buildapi" diff --git a/sale_product_approval/readme/CONFIGURE.md b/sale_product_approval/readme/CONFIGURE.md new file mode 100644 index 00000000000..036436b1f5a --- /dev/null +++ b/sale_product_approval/readme/CONFIGURE.md @@ -0,0 +1,5 @@ +- Create product states. +- Check the 'Approved to be added to SO' box on the states where the + product is allowed to be added to an SO when it's in that state. +- Check the 'Approved to be sold' box on the states where the product is + allowed to be sold when it's in that state. diff --git a/sale_product_approval/readme/CONTRIBUTORS.md b/sale_product_approval/readme/CONTRIBUTORS.md new file mode 100644 index 00000000000..4b30741f4c0 --- /dev/null +++ b/sale_product_approval/readme/CONTRIBUTORS.md @@ -0,0 +1,5 @@ +- [Open Source Integrators](https://opensourceintegrators.com). + - Chandresh Thakkar \<\> + - Daniel Reis \<\> + - Patrick Wilson \<\> + - Hardik Suthar \<\> diff --git a/sale_product_approval/readme/DESCRIPTION.md b/sale_product_approval/readme/DESCRIPTION.md new file mode 100644 index 00000000000..a9cc459e4df --- /dev/null +++ b/sale_product_approval/readme/DESCRIPTION.md @@ -0,0 +1,3 @@ +This module links the 'Can be sold' field to the states. The states then +control whether or not the product can be sold or not in the particular +state. diff --git a/sale_product_approval/readme/USAGE.md b/sale_product_approval/readme/USAGE.md new file mode 100644 index 00000000000..2924f9046b3 --- /dev/null +++ b/sale_product_approval/readme/USAGE.md @@ -0,0 +1,8 @@ +- When a product is moved to a state where it's approved, the 'Can be + sold' box should be checked and the product is selectable on sales + quotes. +- If the product is not in a state where it's approved to be sold, then + the 'Can be sold' check box is not checked and the product can not be + added to sales quotes. +- If it's not approved, any sales quote that already contained the + product will show a warning and have an exception activity. diff --git a/sale_product_approval/security/res_groups.xml b/sale_product_approval/security/res_groups.xml new file mode 100644 index 00000000000..11b24863fb9 --- /dev/null +++ b/sale_product_approval/security/res_groups.xml @@ -0,0 +1,8 @@ + + + + Product Administrator + + + + diff --git a/sale_product_approval/static/description/icon.png b/sale_product_approval/static/description/icon.png new file mode 100644 index 00000000000..3a0328b516c Binary files /dev/null and b/sale_product_approval/static/description/icon.png differ diff --git a/sale_product_approval/static/description/index.html b/sale_product_approval/static/description/index.html new file mode 100644 index 00000000000..ab15789d4d0 --- /dev/null +++ b/sale_product_approval/static/description/index.html @@ -0,0 +1,461 @@ + + + + + +Sale Product Approval + + + +
+

Sale Product Approval

+ + +

Alpha License: AGPL-3 OCA/sale-workflow Translate me on Weblate Try me on Runboat

+

This module links the ‘Can be sold’ field to the states. The states then +control whether or not the product can be sold or not in the particular +state.

+
+

Important

+

This is an alpha version, the data model and design can change at any time without warning. +Only for development or testing purpose, do not use in production. +More details on development status

+
+

Table of contents

+ +
+

Configuration

+
    +
  • Create product states.
  • +
  • Check the ‘Approved to be added to SO’ box on the states where the +product is allowed to be added to an SO when it’s in that state.
  • +
  • Check the ‘Approved to be sold’ box on the states where the product +is allowed to be sold when it’s in that state.
  • +
+
+
+

Usage

+
    +
  • When a product is moved to a state where it’s approved, the ‘Can be +sold’ box should be checked and the product is selectable on sales +quotes.
  • +
  • If the product is not in a state where it’s approved to be sold, then +the ‘Can be sold’ check box is not checked and the product can not be +added to sales quotes.
  • +
  • If it’s not approved, any sales quote that already contained the +product will show a warning and have an exception activity.
  • +
+
+
+

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

+
    +
  • Open Source Integrators
  • +
+
+ +
+

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.

+

Current maintainer:

+

dreispt

+

This module is part of the OCA/sale-workflow project on GitHub.

+

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

+
+
+
+ + diff --git a/sale_product_approval/tests/__init__.py b/sale_product_approval/tests/__init__.py new file mode 100644 index 00000000000..246dfad5f28 --- /dev/null +++ b/sale_product_approval/tests/__init__.py @@ -0,0 +1,4 @@ +# Copyright 2021 Open Source Integrators +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + +from . import test_product_approval diff --git a/sale_product_approval/tests/test_product_approval.py b/sale_product_approval/tests/test_product_approval.py new file mode 100644 index 00000000000..8159d7dd95c --- /dev/null +++ b/sale_product_approval/tests/test_product_approval.py @@ -0,0 +1,42 @@ +# Copyright 2021 Open Source Integrators +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). +from odoo.exceptions import UserError +from odoo.tests import tagged +from odoo.tests.common import TransactionCase + + +@tagged("post_install", "-at_install") +class TestSaleProductApproval(TransactionCase): + @classmethod + def setUpClass(cls): + super().setUpClass() + cls.customer = cls.env.ref("base.res_partner_12") + cls.product_state_sale = cls.env.ref("product_state.product_state_sellable") + cls.product_state_end = cls.env.ref("product_state.product_state_end") + cls.product_id = cls.env["product.product"].create( + {"name": "Test Product", "type": "product"} + ) + cls.sale_id = cls.env["sale.order"].create( + { + "partner_id": cls.customer.id, + "order_line": [ + ( + 0, + 0, + { + "product_id": cls.product_id.id, + "product_uom_qty": 2.0, + "price_unit": 10.0, + }, + ) + ], + } + ) + + def test_write_product_state(self): + self.product_id.write({"product_state_id": self.product_state_sale.id}) + self.product_state_end.write({"approved_sale": False}) + self.product_id.write({"product_state_id": self.product_state_end.id}) + with self.assertRaises(UserError): + self.sale_id.action_confirm() + self.product_id.write({"product_state_id": self.product_state_sale.id}) diff --git a/sale_product_approval/views/menu_items.xml b/sale_product_approval/views/menu_items.xml new file mode 100644 index 00000000000..5b8ff048d11 --- /dev/null +++ b/sale_product_approval/views/menu_items.xml @@ -0,0 +1,9 @@ + + + + Product States + + + + + diff --git a/sale_product_approval/views/product_exception.xml b/sale_product_approval/views/product_exception.xml new file mode 100644 index 00000000000..a708f5f0fa9 --- /dev/null +++ b/sale_product_approval/views/product_exception.xml @@ -0,0 +1,28 @@ + + + + + diff --git a/sale_product_approval/views/product_state.xml b/sale_product_approval/views/product_state.xml new file mode 100644 index 00000000000..c8bfa7f3e48 --- /dev/null +++ b/sale_product_approval/views/product_state.xml @@ -0,0 +1,30 @@ + + + + product_state_form_view_inherit + product.state + + + + +
+
+ +
+
+ +
+
+
+
+
+ +
diff --git a/sale_product_approval/views/product_template.xml b/sale_product_approval/views/product_template.xml new file mode 100644 index 00000000000..e4466ac52be --- /dev/null +++ b/sale_product_approval/views/product_template.xml @@ -0,0 +1,61 @@ + + + + product_template_form_view_inherit + product.template + 100 + + + + + + + + +
+ + + +
+
+ + + + + + + + + + + + + not candidate_sale + +
+
+ + + product_template_search_view_inherit + product.template + + + + + + + + +
diff --git a/sale_product_approval/views/sale_order.xml b/sale_product_approval/views/sale_order.xml new file mode 100644 index 00000000000..82e17d7a589 --- /dev/null +++ b/sale_product_approval/views/sale_order.xml @@ -0,0 +1,52 @@ + + + + sale_order_form_view_inherit + sale.order + 99 + + + + + + + + +