diff --git a/product_pricelist_dependency_warning/README.rst b/product_pricelist_dependency_warning/README.rst new file mode 100644 index 000000000000..9dc5f418cec6 --- /dev/null +++ b/product_pricelist_dependency_warning/README.rst @@ -0,0 +1,107 @@ +============================ +Pricelist Dependency Warning +============================ + +.. + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! This file is generated by oca-gen-addon-readme !! + !! changes will be overwritten. !! + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! source digest: sha256:5a5575d94be502f5e6b5476f4b9fa96602f8d239f5e9cfd7b801ee7f07abe1ea + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + +.. |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-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%2Fproduct--attribute-lightgray.png?logo=github + :target: https://github.com/OCA/product-attribute/tree/16.0/product_pricelist_dependency_warning + :alt: OCA/product-attribute +.. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png + :target: https://translation.odoo-community.org/projects/product-attribute-16-0/product-attribute-16-0-product_pricelist_dependency_warning + :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/product-attribute&target_branch=16.0 + :alt: Try me on Runboat + +|badge1| |badge2| |badge3| |badge4| |badge5| + +Show a warning in the pricelist when a dependency pricelist is updated. + +**Table of contents** + +.. contents:: + :local: + +Usage +===== + +This module is useful in this simplified use-case: + +#. Create a **base** pricelist with a global price rule +#. Create a **discount** pricelist with a global price rule, using a formula based on **base** and a discount. +#. Change the global price rule in **base** + +Now the pricelist **discount** will show a warning telling you that the base price of some rules has changed. +This pricelist can also be found with the filter **Base price changed** in the pricelists list. + +Uncheck **Base price changed** on the affected price rules to get rid of the warning. + +Known issues / Roadmap +====================== + +The warning is only shown if depending price rules are applied on the same level (global/category/product/variant). + +Quantity is not considered when evaluating price rules applicability. + +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 +~~~~~~~ + +* Aion Tech + +Contributors +~~~~~~~~~~~~ + +* `Aion Tech `_: + + * Simone Rubino + +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-SirAionTech| image:: https://github.com/SirAionTech.png?size=40px + :target: https://github.com/SirAionTech + :alt: SirAionTech + +Current `maintainer `__: + +|maintainer-SirAionTech| + +This module is part of the `OCA/product-attribute `_ project on GitHub. + +You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute. diff --git a/product_pricelist_dependency_warning/__init__.py b/product_pricelist_dependency_warning/__init__.py new file mode 100644 index 000000000000..b18eea379608 --- /dev/null +++ b/product_pricelist_dependency_warning/__init__.py @@ -0,0 +1,3 @@ +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + +from . import models diff --git a/product_pricelist_dependency_warning/__manifest__.py b/product_pricelist_dependency_warning/__manifest__.py new file mode 100644 index 000000000000..2fc98375124c --- /dev/null +++ b/product_pricelist_dependency_warning/__manifest__.py @@ -0,0 +1,23 @@ +# Copyright 2023 Simone Rubino - Aion Tech +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + +{ + "name": "Pricelist Dependency Warning", + "summary": "Warn when a dependency pricelist is updated.", + "version": "16.0.1.0.0", + "author": "Aion Tech, Odoo Community Association (OCA)", + "maintainers": [ + "SirAionTech", + ], + "license": "AGPL-3", + "website": "https://github.com/OCA/product-attribute" + "/tree/16.0/product_pricelist_dependency_warning", + "category": "Product", + "depends": [ + "product", + ], + "data": [ + "views/product_pricelist_item_views.xml", + "views/product_pricelist_views.xml", + ], +} diff --git a/product_pricelist_dependency_warning/i18n/it.po b/product_pricelist_dependency_warning/i18n/it.po new file mode 100644 index 000000000000..9834bfcc9937 --- /dev/null +++ b/product_pricelist_dependency_warning/i18n/it.po @@ -0,0 +1,49 @@ +# Translation of Odoo Server. +# This file contains the translation of the following modules: +# * product_pricelist_dependency_warning +# +msgid "" +msgstr "" +"Project-Id-Version: Odoo Server 16.0\n" +"Report-Msgid-Bugs-To: \n" +"POT-Creation-Date: 2023-11-22 09:28+0000\n" +"PO-Revision-Date: 2023-11-22 09:28+0000\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_pricelist_dependency_warning +#: model:ir.model.fields,field_description:product_pricelist_dependency_warning.field_product_pricelist_item__is_base_price_changed +#: model:ir.model.fields,field_description:product_pricelist_dependency_warning.field_product_pricelist__is_base_price_changed +#: model_terms:ir.ui.view,arch_db:product_pricelist_dependency_warning.product_pricelist_view_search +msgid "Base price changed" +msgstr "Prezzo di base cambiato" + +#. module: product_pricelist_dependency_warning +#: model:ir.model,name:product_pricelist_dependency_warning.model_product_pricelist +msgid "Pricelist" +msgstr "Listino prezzi" + +#. module: product_pricelist_dependency_warning +#: model:ir.model,name:product_pricelist_dependency_warning.model_product_pricelist_item +msgid "Pricelist Rule" +msgstr "Regola listino prezzi" + +#. module: product_pricelist_dependency_warning +#: model_terms:ir.ui.view,arch_db:product_pricelist_dependency_warning.product_pricelist_view +msgid "" +"The base price of some Price Rules is changed,\n" +" please review them and uncheck the \"Base price changed\" flag\n" +" to remove this warning" +msgstr "" +"Il prezzo di base è cambiato per alcune Regole tariffarie,\n" +" revisionarle e deselezionare \"Prezzo di base cambiato\"\n" +" per rimuovere questo messaggio" + +#. module: product_pricelist_dependency_warning +#: model:ir.model.fields,help:product_pricelist_dependency_warning.field_product_pricelist__is_base_price_changed +msgid "The base price of some price rules is changed." +msgstr "Il prezzo di base è cambiato per alcune Regole tariffarie." diff --git a/product_pricelist_dependency_warning/models/__init__.py b/product_pricelist_dependency_warning/models/__init__.py new file mode 100644 index 000000000000..0dd377ea6e0f --- /dev/null +++ b/product_pricelist_dependency_warning/models/__init__.py @@ -0,0 +1,4 @@ +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + +from . import product_pricelist +from . import product_pricelist_item diff --git a/product_pricelist_dependency_warning/models/product_pricelist.py b/product_pricelist_dependency_warning/models/product_pricelist.py new file mode 100644 index 000000000000..2edc899ce0ff --- /dev/null +++ b/product_pricelist_dependency_warning/models/product_pricelist.py @@ -0,0 +1,25 @@ +# Copyright 2023 Simone Rubino - Aion Tech +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + +from odoo import api, fields, models + + +class ProductPricelist(models.Model): + _inherit = "product.pricelist" + + is_base_price_changed = fields.Boolean( + compute="_compute_is_base_price_changed", + store=True, + string="Base price changed", + help="The base price of some price rules is changed.", + ) + + @api.depends( + "item_ids.is_base_price_changed", + ) + def _compute_is_base_price_changed(self): + for pricelist in self: + lines_base_price_changed = pricelist.item_ids.mapped( + "is_base_price_changed" + ) + pricelist.is_base_price_changed = any(lines_base_price_changed) diff --git a/product_pricelist_dependency_warning/models/product_pricelist_item.py b/product_pricelist_dependency_warning/models/product_pricelist_item.py new file mode 100644 index 000000000000..f9052133ec63 --- /dev/null +++ b/product_pricelist_dependency_warning/models/product_pricelist_item.py @@ -0,0 +1,92 @@ +# Copyright 2023 Simone Rubino - Aion Tech +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + +from odoo import fields, models + + +class ProductPricelistItem(models.Model): + _inherit = "product.pricelist.item" + + is_base_price_changed = fields.Boolean( + string="Base price changed", + ) + + def _has_same_product_applicability(self, item): + """Return True iff `self` and `item` have the same applicability. + + This is not considering quantity applicability but only global/category/product/variant. + This is not considering included categories and such, but only exact matches. + """ + if not self and not item: + same_applicability = True + elif not self or not item: + same_applicability = False + else: + self.ensure_one() + item.ensure_one() + applied_on = self.applied_on + if applied_on != item.applied_on: + same_applicability = False + else: + same_applicability = ( + # Same global + applied_on == "3_global" + or ( + # Same category + applied_on == "2_product_category" + and self.categ_id == item.categ_id + ) + or ( + # Same product + applied_on == "1_product" + and self.product_tmpl_id == item.product_tmpl_id + ) + or ( + # Same variant + applied_on == "0_product_variant" + and self.product_id == item.product_id + ) + ) + return same_applicability + + def _get_same_applicability_depending_items(self): + """Get rules depending on the pricelist of `self` that have the same applicability.""" + # Group base price rules by their base pricelist + base_pricelist_items = self.search( + [ + ("base", "=", "pricelist"), + ] + ) + base_pricelist_to_items = {} + for base_pricelist_item in base_pricelist_items: + base_pricelist = base_pricelist_item.base_pricelist_id + existing_items = base_pricelist_to_items.get(base_pricelist, self.browse()) + if not existing_items: + base_pricelist_to_items[base_pricelist] = base_pricelist_item + else: + base_pricelist_to_items[base_pricelist] |= base_pricelist_item + + # Find rules having same applicability + same_applicability_depending_items = self.browse() + for changed_item in self: + changed_pricelist = changed_item.pricelist_id + depending_items = base_pricelist_to_items.get( + changed_pricelist, + self.browse(), + ) + for depending_item in depending_items: + if changed_item._has_same_product_applicability(depending_item): + same_applicability_depending_items |= depending_item + return same_applicability_depending_items + + def write(self, vals): + result = super().write(vals) + same_applicability_depending_items = ( + self._get_same_applicability_depending_items() + ) + same_applicability_depending_items.update( + { + "is_base_price_changed": True, + } + ) + return result diff --git a/product_pricelist_dependency_warning/readme/CONTRIBUTORS.rst b/product_pricelist_dependency_warning/readme/CONTRIBUTORS.rst new file mode 100644 index 000000000000..6afa1541b48d --- /dev/null +++ b/product_pricelist_dependency_warning/readme/CONTRIBUTORS.rst @@ -0,0 +1,3 @@ +* `Aion Tech `_: + + * Simone Rubino diff --git a/product_pricelist_dependency_warning/readme/DESCRIPTION.rst b/product_pricelist_dependency_warning/readme/DESCRIPTION.rst new file mode 100644 index 000000000000..fbc2ad111ca2 --- /dev/null +++ b/product_pricelist_dependency_warning/readme/DESCRIPTION.rst @@ -0,0 +1 @@ +Show a warning in the pricelist when a dependency pricelist is updated. diff --git a/product_pricelist_dependency_warning/readme/ROADMAP.rst b/product_pricelist_dependency_warning/readme/ROADMAP.rst new file mode 100644 index 000000000000..6e104fe28209 --- /dev/null +++ b/product_pricelist_dependency_warning/readme/ROADMAP.rst @@ -0,0 +1,3 @@ +The warning is only shown if depending price rules are applied on the same level (global/category/product/variant). + +Quantity is not considered when evaluating price rules applicability. diff --git a/product_pricelist_dependency_warning/readme/USAGE.rst b/product_pricelist_dependency_warning/readme/USAGE.rst new file mode 100644 index 000000000000..1347d58d6cc4 --- /dev/null +++ b/product_pricelist_dependency_warning/readme/USAGE.rst @@ -0,0 +1,10 @@ +This module is useful in this simplified use-case: + +#. Create a **base** pricelist with a global price rule +#. Create a **discount** pricelist with a global price rule, using a formula based on **base** and a discount. +#. Change the global price rule in **base** + +Now the pricelist **discount** will show a warning telling you that the base price of some rules has changed. +This pricelist can also be found with the filter **Base price changed** in the pricelists list. + +Uncheck **Base price changed** on the affected price rules to get rid of the warning. diff --git a/product_pricelist_dependency_warning/static/description/index.html b/product_pricelist_dependency_warning/static/description/index.html new file mode 100644 index 000000000000..ec3f7be74d87 --- /dev/null +++ b/product_pricelist_dependency_warning/static/description/index.html @@ -0,0 +1,445 @@ + + + + + + +Pricelist Dependency Warning + + + +
+

Pricelist Dependency Warning

+ + +

Beta License: AGPL-3 OCA/product-attribute Translate me on Weblate Try me on Runboat

+

Show a warning in the pricelist when a dependency pricelist is updated.

+

Table of contents

+ +
+

Usage

+

This module is useful in this simplified use-case:

+
    +
  1. Create a base pricelist with a global price rule
  2. +
  3. Create a discount pricelist with a global price rule, using a formula based on base and a discount.
  4. +
  5. Change the global price rule in base
  6. +
+

Now the pricelist discount will show a warning telling you that the base price of some rules has changed. +This pricelist can also be found with the filter Base price changed in the pricelists list.

+

Uncheck Base price changed on the affected price rules to get rid of the warning.

+
+
+

Known issues / Roadmap

+

The warning is only shown if depending price rules are applied on the same level (global/category/product/variant).

+

Quantity is not considered when evaluating price rules applicability.

+
+
+

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

+
    +
  • Aion Tech
  • +
+
+
+

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.

+

Current maintainer:

+

SirAionTech

+

This module is part of the OCA/product-attribute project on GitHub.

+

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

+
+
+
+ + diff --git a/product_pricelist_dependency_warning/tests/__init__.py b/product_pricelist_dependency_warning/tests/__init__.py new file mode 100644 index 000000000000..a933b050d0b9 --- /dev/null +++ b/product_pricelist_dependency_warning/tests/__init__.py @@ -0,0 +1,3 @@ +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + +from . import test_pricelist diff --git a/product_pricelist_dependency_warning/tests/test_pricelist.py b/product_pricelist_dependency_warning/tests/test_pricelist.py new file mode 100644 index 000000000000..58fe101b11a6 --- /dev/null +++ b/product_pricelist_dependency_warning/tests/test_pricelist.py @@ -0,0 +1,128 @@ +# Copyright 2023 Simone Rubino - Aion Tech +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + +from odoo.fields import Command, first +from odoo.tests import TransactionCase + + +class TestPricelist(TransactionCase): + @classmethod + def setUpClass(cls): + super().setUpClass() + pricelist_model = cls.env["product.pricelist"] + cls.base_pricelist = pricelist_model.create( + { + "name": "Base", + "item_ids": [ + Command.create( + { + "compute_price": "percentage", + "percent_price": 10, + "applied_on": "3_global", + } + ), + ], + } + ) + cls.depending_pricelist = pricelist_model.create( + { + "name": "Depending", + "item_ids": [ + Command.create( + { + "compute_price": "formula", + "base": "pricelist", + "base_pricelist_id": cls.base_pricelist.id, + "price_discount": 10, + "applied_on": "3_global", + } + ), + Command.create( + { + "compute_price": "formula", + "base": "pricelist", + "base_pricelist_id": cls.base_pricelist.id, + "price_discount": 10, + "applied_on": "2_product_category", + "categ_id": cls.env.ref("product.product_category_all").id, + } + ), + Command.create( + { + "base": "list_price", + "compute_price": "formula", + "price_discount": 10, + "applied_on": "3_global", + } + ), + ], + } + ) + + def test_base_item_price_changed(self): + """When changing the price of the base rule, + only depending items/pricelists are updated.""" + # Arrange + base_pricelist = self.base_pricelist + base_item = base_pricelist.item_ids + depending_pricelist = self.depending_pricelist + depending_items = depending_pricelist.item_ids + depending_item = first( + depending_items.filtered( + lambda item: item.base_pricelist_id == base_pricelist + and item.applied_on == base_item.applied_on + ) + ) + other_applied_item = first( + depending_items.filtered( + lambda item: item.base == depending_item.base + and item.applied_on != depending_item.applied_on + ) + ) + other_base_item = first( + depending_items.filtered( + lambda item: item.base != depending_item.base + and item.applied_on == depending_item.applied_on + ) + ) + # pre-condition + self.assertFalse(depending_pricelist.is_base_price_changed) + self.assertFalse(depending_item.is_base_price_changed) + self.assertTrue(other_applied_item) + self.assertTrue(other_base_item) + self.assertFalse(other_applied_item.is_base_price_changed) + self.assertFalse(other_base_item.is_base_price_changed) + + # Act + base_item.percent_price = 20 + + # Assert + self.assertTrue(depending_pricelist.is_base_price_changed) + self.assertTrue(depending_item.is_base_price_changed) + self.assertFalse(other_applied_item.is_base_price_changed) + self.assertFalse(other_base_item.is_base_price_changed) + + def test_uncheck_item_pricelist_changed(self): + """When unchecking `is_base_price_changed` in the items, + its pricelist is updated.""" + # Arrange + base_pricelist = self.base_pricelist + base_item = base_pricelist.item_ids + base_item.percent_price = 20 + depending_pricelist = self.depending_pricelist + depending_item = first( + depending_pricelist.item_ids.filtered( + lambda item: item.base_pricelist_id == base_pricelist + and item.applied_on == base_item.applied_on + ) + ) + # pre-condition + self.assertTrue(depending_pricelist.is_base_price_changed) + self.assertTrue(depending_item.is_base_price_changed) + + # Act + depending_item.is_base_price_changed = False + + # Assert + self.assertFalse(depending_pricelist.is_base_price_changed) + self.assertFalse(depending_item.is_base_price_changed) diff --git a/product_pricelist_dependency_warning/views/product_pricelist_item_views.xml b/product_pricelist_dependency_warning/views/product_pricelist_item_views.xml new file mode 100644 index 000000000000..d7103980b4a1 --- /dev/null +++ b/product_pricelist_dependency_warning/views/product_pricelist_item_views.xml @@ -0,0 +1,28 @@ + + + + + Add Dependency warning fields to pricelist item form view + product.pricelist.item + + + + + + + + diff --git a/product_pricelist_dependency_warning/views/product_pricelist_views.xml b/product_pricelist_dependency_warning/views/product_pricelist_views.xml new file mode 100644 index 000000000000..702cae3f66ab --- /dev/null +++ b/product_pricelist_dependency_warning/views/product_pricelist_views.xml @@ -0,0 +1,54 @@ + + + + + Add Dependency warning fields to pricelist form view + product.pricelist + + + + + + + + + + + + + + Add Dependency warning fields to pricelist search view + product.pricelist + + + + + + + + diff --git a/setup/product_pricelist_dependency_warning/odoo/addons/product_pricelist_dependency_warning b/setup/product_pricelist_dependency_warning/odoo/addons/product_pricelist_dependency_warning new file mode 120000 index 000000000000..010137481320 --- /dev/null +++ b/setup/product_pricelist_dependency_warning/odoo/addons/product_pricelist_dependency_warning @@ -0,0 +1 @@ +../../../../product_pricelist_dependency_warning \ No newline at end of file diff --git a/setup/product_pricelist_dependency_warning/setup.py b/setup/product_pricelist_dependency_warning/setup.py new file mode 100644 index 000000000000..28c57bb64031 --- /dev/null +++ b/setup/product_pricelist_dependency_warning/setup.py @@ -0,0 +1,6 @@ +import setuptools + +setuptools.setup( + setup_requires=['setuptools-odoo'], + odoo_addon=True, +)