diff --git a/sale_loyalty_order_suggestion_multi_product/README.rst b/sale_loyalty_order_suggestion_multi_product/README.rst new file mode 100644 index 00000000..32801db1 --- /dev/null +++ b/sale_loyalty_order_suggestion_multi_product/README.rst @@ -0,0 +1,88 @@ +=========================================== +Sale Loyalty Order Suggestion Multi Product +=========================================== + +.. + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! This file is generated by oca-gen-addon-readme !! + !! changes will be overwritten. !! + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! source digest: sha256:ba5cd450b0bfbd38818701f89fb899819decaf2886209492d130773c447b41c1 + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + +.. |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%2Fsale--promotion-lightgray.png?logo=github + :target: https://github.com/OCA/sale-promotion/tree/16.0/sale_loyalty_order_suggestion_multi_product + :alt: OCA/sale-promotion +.. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png + :target: https://translation.odoo-community.org/projects/sale-promotion-16-0/sale-promotion-16-0-sale_loyalty_order_suggestion_multi_product + :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-promotion&target_branch=16.0 + :alt: Try me on Runboat + +|badge1| |badge2| |badge3| |badge4| |badge5| + +This module extends the functionality of `sale_loyalty_order_suggestion` by giving hints of available promotions whose criteria is multi-product. + +**Table of contents** + +.. contents:: + :local: + +Usage +===== + +To use this module: + +* Configure or create a promotion and set in its rules multi-product criteria and the respective products required. +* Create a sales order and add to the order lines one of the products that were part of the rules. This line will then be marked with an icon (🎁) which will appear at the bottom right. +* Click on the icon and the wizard will open with the available promotions. +* Select the promotion and the products needed to apply it. + +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 +~~~~~~~ + +* Tecnativa + +Contributors +~~~~~~~~~~~~ + +* `Tecnativa `_: + + * Pilar Vargas + +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/sale-promotion `_ project on GitHub. + +You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute. diff --git a/sale_loyalty_order_suggestion_multi_product/__init__.py b/sale_loyalty_order_suggestion_multi_product/__init__.py new file mode 100644 index 00000000..9b429614 --- /dev/null +++ b/sale_loyalty_order_suggestion_multi_product/__init__.py @@ -0,0 +1,2 @@ +from . import models +from . import wizard diff --git a/sale_loyalty_order_suggestion_multi_product/__manifest__.py b/sale_loyalty_order_suggestion_multi_product/__manifest__.py new file mode 100644 index 00000000..4cfdfe94 --- /dev/null +++ b/sale_loyalty_order_suggestion_multi_product/__manifest__.py @@ -0,0 +1,14 @@ +# Copyright 2024 Tecnativa - Pilar Vargas +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). +{ + "name": "Sale Loyalty Order Suggestion Multi Product", + "summary": "Suggest promotions with criteria multi product in the sale order line", + "version": "16.0.1.0.0", + "development_status": "Beta", + "category": "Sale", + "website": "https://github.com/OCA/sale-promotion", + "author": "Tecnativa, Odoo Community Association (OCA)", + "license": "AGPL-3", + "depends": ["sale_loyalty_order_suggestion", "sale_loyalty_criteria_multi_product"], + "installable": True, +} diff --git a/sale_loyalty_order_suggestion_multi_product/models/__init__.py b/sale_loyalty_order_suggestion_multi_product/models/__init__.py new file mode 100644 index 00000000..6aacb753 --- /dev/null +++ b/sale_loyalty_order_suggestion_multi_product/models/__init__.py @@ -0,0 +1 @@ +from . import sale_order diff --git a/sale_loyalty_order_suggestion_multi_product/models/sale_order.py b/sale_loyalty_order_suggestion_multi_product/models/sale_order.py new file mode 100644 index 00000000..a065a2f6 --- /dev/null +++ b/sale_loyalty_order_suggestion_multi_product/models/sale_order.py @@ -0,0 +1,32 @@ +# Copyright 2023 Tecnativa - Pilar Vargas +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). +from odoo import models + + +class SaleOrder(models.Model): + _inherit = "sale.order" + + def _filter_programs_by_rules_with_products(self): + res = super()._filter_programs_by_rules_with_products() + valid_programs = self._get_available_programs() + # Filters programs that have rules multi_product + programs_with_criteria_multi_product = valid_programs.filtered( + lambda x: any( + rule.loyalty_criteria == "multi_product" for rule in x.rule_ids + ) + ) + return res + (programs_with_criteria_multi_product - res) + + def _available_programs(self): + self.ensure_one() + filtered_programs = self._filter_programs_by_rules_with_products() + programs = self.env["loyalty.program"] + if filtered_programs: + product_id = self.env.context.get("product_id") + programs += filtered_programs.filtered( + lambda x: any( + product_id in rule.loyalty_criteria_ids.product_ids.ids + for rule in x.rule_ids + ) + ) + return super()._available_programs() + programs diff --git a/sale_loyalty_order_suggestion_multi_product/readme/CONTRIBUTORS.rst b/sale_loyalty_order_suggestion_multi_product/readme/CONTRIBUTORS.rst new file mode 100644 index 00000000..44075b9f --- /dev/null +++ b/sale_loyalty_order_suggestion_multi_product/readme/CONTRIBUTORS.rst @@ -0,0 +1,3 @@ +* `Tecnativa `_: + + * Pilar Vargas diff --git a/sale_loyalty_order_suggestion_multi_product/readme/DESCRIPTION.rst b/sale_loyalty_order_suggestion_multi_product/readme/DESCRIPTION.rst new file mode 100644 index 00000000..2fa9a54e --- /dev/null +++ b/sale_loyalty_order_suggestion_multi_product/readme/DESCRIPTION.rst @@ -0,0 +1 @@ +This module extends the functionality of `sale_loyalty_order_suggestion` by giving hints of available promotions whose criteria is multi-product. diff --git a/sale_loyalty_order_suggestion_multi_product/readme/USAGE.rst b/sale_loyalty_order_suggestion_multi_product/readme/USAGE.rst new file mode 100644 index 00000000..bb6a8b09 --- /dev/null +++ b/sale_loyalty_order_suggestion_multi_product/readme/USAGE.rst @@ -0,0 +1,6 @@ +To use this module: + +* Configure or create a promotion and set in its rules multi-product criteria and the respective products required. +* Create a sales order and add to the order lines one of the products that were part of the rules. This line will then be marked with an icon (🎁) which will appear at the bottom right. +* Click on the icon and the wizard will open with the available promotions. +* Select the promotion and the products needed to apply it. diff --git a/sale_loyalty_order_suggestion_multi_product/static/description/icon.png b/sale_loyalty_order_suggestion_multi_product/static/description/icon.png new file mode 100644 index 00000000..f8d44261 Binary files /dev/null and b/sale_loyalty_order_suggestion_multi_product/static/description/icon.png differ diff --git a/sale_loyalty_order_suggestion_multi_product/static/description/index.html b/sale_loyalty_order_suggestion_multi_product/static/description/index.html new file mode 100644 index 00000000..9b37c30d --- /dev/null +++ b/sale_loyalty_order_suggestion_multi_product/static/description/index.html @@ -0,0 +1,435 @@ + + + + + + +Sale Loyalty Order Suggestion Multi Product + + + +
+

Sale Loyalty Order Suggestion Multi Product

+ + +

Beta License: AGPL-3 OCA/sale-promotion Translate me on Weblate Try me on Runboat

+

This module extends the functionality of sale_loyalty_order_suggestion by giving hints of available promotions whose criteria is multi-product.

+

Table of contents

+ +
+

Usage

+

To use this module:

+
    +
  • Configure or create a promotion and set in its rules multi-product criteria and the respective products required.
  • +
  • Create a sales order and add to the order lines one of the products that were part of the rules. This line will then be marked with an icon (🎁) which will appear at the bottom right.
  • +
  • Click on the icon and the wizard will open with the available promotions.
  • +
  • Select the promotion and the products needed to apply it.
  • +
+
+
+

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

+
    +
  • Tecnativa
  • +
+
+
+

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/sale-promotion project on GitHub.

+

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

+
+
+
+ + diff --git a/sale_loyalty_order_suggestion_multi_product/tests/__init__.py b/sale_loyalty_order_suggestion_multi_product/tests/__init__.py new file mode 100644 index 00000000..1cb3842e --- /dev/null +++ b/sale_loyalty_order_suggestion_multi_product/tests/__init__.py @@ -0,0 +1,2 @@ +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). +from . import test_sale_loyalty_order_suggestion_multi_product diff --git a/sale_loyalty_order_suggestion_multi_product/tests/test_sale_loyalty_order_suggestion_multi_product.py b/sale_loyalty_order_suggestion_multi_product/tests/test_sale_loyalty_order_suggestion_multi_product.py new file mode 100644 index 00000000..12e8d1c4 --- /dev/null +++ b/sale_loyalty_order_suggestion_multi_product/tests/test_sale_loyalty_order_suggestion_multi_product.py @@ -0,0 +1,176 @@ +# Copyright 2024 Tecnativa - Pilar Vargas +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). +from odoo.tests.common import Form, TransactionCase + + +class TestSaleLoyaltyOrderSuggestion(TransactionCase): + @classmethod + def setUpClass(cls): + super().setUpClass() + product_obj = cls.env["product.product"] + cls.pricelist = cls.env["product.pricelist"].create( + { + "name": "Test pricelist", + "item_ids": [ + ( + 0, + 0, + { + "applied_on": "3_global", + "compute_price": "formula", + "base": "list_price", + }, + ) + ], + } + ) + cls.partner = cls.env["res.partner"].create( + {"name": "Mr. Odoo", "property_product_pricelist": cls.pricelist.id} + ) + cls.product_a = product_obj.create({"name": "Product A", "list_price": 50}) + cls.product_b = product_obj.create({"name": "Product B", "list_price": 10}) + cls.product_c = product_obj.create({"name": "Product C", "list_price": 70}) + cls.loyalty_program = cls.env["loyalty.program"].create( + { + "name": "Test Sale Loyalty Order Suggestion Multi Product", + "program_type": "promotion", + "trigger": "auto", + "applies_on": "current", + "rule_ids": [ + ( + 0, + 0, + { + "reward_point_mode": "order", + "loyalty_criteria": "multi_product", + "loyalty_criteria_ids": [ + ( + 0, + 0, + { + "product_ids": [(4, cls.product_a.id)], + }, + ), + ( + 0, + 0, + { + "product_ids": [ + (4, cls.product_b.id), + (4, cls.product_c.id), + ], + }, + ), + ], + }, + ), + ], + "reward_ids": [ + ( + 0, + 0, + { + "reward_type": "discount", + "required_points": 1, + "discount": 10, + "discount_mode": "percent", + "discount_applicability": "order", + }, + ) + ], + } + ) + sale_form = Form(cls.env["sale.order"]) + sale_form.partner_id = cls.partner + with sale_form.order_line.new() as line_form: + line_form.product_id = cls.product_a + line_form.product_uom_qty = 1 + cls.sale = sale_form.save() + + def _open_suggested_promotion_wizard(self, suggested_reward_ids): + self.sale._update_programs_and_rewards() + wizard = ( + self.env["sale.loyalty.reward.wizard"] + .with_context(active_id=self.sale) + .create({"order_id": self.sale.id}) + ) + wizard.reward_ids = suggested_reward_ids + return wizard + + def test_01_suggested_promotion_for_product_a_no_applicable(self): + # In this test a suggestion is made for a promotion that contains in its rules + # multi criteria the product added to the lines of the order but does not meet all the + # requirements to be applied so it will be necessary to configure the + # products in the wizard. + line_a = self.sale.order_line.filtered(lambda x: x.product_id == self.product_a) + wizard = self._open_suggested_promotion_wizard(line_a.suggested_reward_ids) + self.assertEqual(wizard.reward_ids, line_a.suggested_promotion_ids.reward_ids) + # Select promotion to apply + wizard.selected_reward_id = self.loyalty_program.reward_ids[0].id + # The promotion is not directly applicable as it does not comply with all the rules. + self.assertFalse(wizard.applicable_program) + # The wizard contains 3 lines, one for each product configured in the rules + self.assertEqual(len(wizard.loyalty_rule_line_ids), 3) + wiz_line_a = wizard.loyalty_rule_line_ids.filtered( + lambda x: x.product_id == self.product_a + ) + self.assertTrue(wiz_line_a) + self.assertEqual(wiz_line_a.units_included, 1) + self.assertEqual(wiz_line_a.units_required, 1) + self.assertEqual(wiz_line_a.units_to_include, 0) + wiz_line_b = wizard.loyalty_rule_line_ids.filtered( + lambda x: x.product_id == self.product_b + ) + self.assertTrue(wiz_line_b) + self.assertEqual(wiz_line_b.units_included, 0) + self.assertEqual(wiz_line_b.units_required, 1) + self.assertEqual(wiz_line_b.units_to_include, 0) + wiz_line_c = wizard.loyalty_rule_line_ids.filtered( + lambda x: x.product_id == self.product_c + ) + self.assertTrue(wiz_line_c) + self.assertEqual(wiz_line_c.units_included, 0) + self.assertEqual(wiz_line_c.units_required, 1) + self.assertEqual(wiz_line_c.units_to_include, 0) + # More units are added to make the promotion compliant and applicable. + wiz_line_b.units_to_include = 1 + wiz_line_c.units_to_include = 1 + wizard.action_apply() + line_b = self.sale.order_line.filtered(lambda x: x.product_id == self.product_b) + self.assertTrue(line_b) + self.assertEqual(line_b.product_uom_qty, 1) + self.assertTrue(self.sale.order_line.filtered(lambda x: x.is_reward_line)) + line_c = self.sale.order_line.filtered(lambda x: x.product_id == self.product_c) + self.assertTrue(line_c) + self.assertEqual(line_c.product_uom_qty, 1) + self.assertTrue(self.sale.order_line.filtered(lambda x: x.is_reward_line)) + + def test_02_suggested_promotion_for_product_a_auto_applicable(self): + # In this test a suggestion is made for a promotion that contains in its rules + # the product added to the order lines and that meets all the requirements to be + # applied. + order_lines_values = [ + { + "order_id": self.sale.id, + "product_id": self.product_b.id, + "product_uom_qty": 1.0, + }, + { + "order_id": self.sale.id, + "product_id": self.product_c.id, + "product_uom_qty": 1.0, + }, + ] + self.env["sale.order.line"].create(order_lines_values) + line_a = self.sale.order_line.filtered(lambda x: x.product_id == self.product_a) + wizard = self._open_suggested_promotion_wizard(line_a.suggested_reward_ids) + self.assertEqual(wizard.reward_ids, line_a.suggested_promotion_ids.reward_ids) + # Select promotion to apply + wizard.selected_reward_id = self.loyalty_program.reward_ids[0].id + # The promotion is directly applicable as it does not comply with all the rules. + self.assertTrue(wizard.applicable_program) + # The wizard contains 0 lines + self.assertEqual(len(wizard.loyalty_rule_line_ids), 0) + self.assertTrue(self.sale.order_line.filtered(lambda x: not x.is_reward_line)) + wizard.action_apply() + self.assertTrue(self.sale.order_line.filtered(lambda x: x.is_reward_line)) diff --git a/sale_loyalty_order_suggestion_multi_product/wizard/__init__.py b/sale_loyalty_order_suggestion_multi_product/wizard/__init__.py new file mode 100644 index 00000000..1ca11a9d --- /dev/null +++ b/sale_loyalty_order_suggestion_multi_product/wizard/__init__.py @@ -0,0 +1 @@ +from . import sale_loyalty_reward_wizard diff --git a/sale_loyalty_order_suggestion_multi_product/wizard/sale_loyalty_reward_wizard.py b/sale_loyalty_order_suggestion_multi_product/wizard/sale_loyalty_reward_wizard.py new file mode 100644 index 00000000..b9468d5c --- /dev/null +++ b/sale_loyalty_order_suggestion_multi_product/wizard/sale_loyalty_reward_wizard.py @@ -0,0 +1,90 @@ +from odoo import _, api, fields, models +from odoo.exceptions import ValidationError + + +class SaleLoyaltyRewardWizard(models.TransientModel): + _inherit = "sale.loyalty.reward.wizard" + + multi_criteria = fields.Boolean(compute="_compute_multi_criteria") + + @api.depends("reward_ids", "selected_reward_id") + def _compute_multi_criteria(self): + self.multi_criteria = ( + self.selected_reward_id.program_id.rule_ids.loyalty_criteria + == "multi_product" + ) + + def _compute_loyalty_rule_line_ids(self): + self.loyalty_rule_line_ids = None + # The products of the rule with criteria "multi_product" related to the product_id + # of the line are taken into consideration in case the line contains a product of + # the rules, otherwise the product_id will coincide with a reward product and in + # that case the products of the first rule "multi_product" are taken into consideration. + products = ( + self.selected_reward_id.program_id.rule_ids.filtered( + lambda x: x.loyalty_criteria == "multi_product" + and self.product_id in x.loyalty_criteria_ids.product_ids + )[:1].loyalty_criteria_ids.product_ids + or self.selected_reward_id.program_id.rule_ids.filtered( + lambda x: x.loyalty_criteria == "multi_product" + )[:1].loyalty_criteria_ids.product_ids + ) + if self.selected_reward_id and not self.applicable_program and products: + lines_vals = [] + for record in products: + units_included = self.order_id.order_line.filtered( + lambda x: x.product_id == record and not x.is_reward_line + ).product_uom_qty + lines_vals.append( + ( + 0, + 0, + { + "wizard_id": self.id, + "product_id": record.id, + "units_required": 1, + "units_included": units_included or 0, + }, + ) + ) + self.loyalty_rule_line_ids = lines_vals + else: + return super()._compute_loyalty_rule_line_ids() + + def _compute_loyalty_rule_line_description(self): + self.loyalty_rule_line_description = False + products = list( + set( + self.selected_reward_id.program_id.rule_ids.loyalty_criteria_ids.product_ids + ) + ) + if self.selected_reward_id and not self.applicable_program and products: + if len(products) > 1: + products_str = f"{', '.join(map(str, [product.name for product in products][:-1]))} {_('and')} {[product.name for product in products][-1]}" # noqa: B950 + self.loyalty_rule_line_description = ( + f"* {_('Required quantity')}: 1 {_('unit of')} {products_str}" + ) + else: + return super()._compute_loyalty_rule_line_description() + + def action_apply(self): + if self.selected_reward_id.program_id.rule_ids.filtered( + lambda x: x.loyalty_criteria == "multi_product" + ): + if all( + line.units_to_include > 0 or line.units_included > 0 + for line in self.loyalty_rule_line_ids + ): + return super().action_apply() + else: + raise ValidationError( + _("The quantities necessary to apply the promotion are not added.") + ) + else: + return super().action_apply() + + +class SaleLoyaltyRuleProductLineWizard(models.TransientModel): + _inherit = "sale.loyalty.rule.product_line.wizard" + + multi_criteria = fields.Boolean(related="wizard_id.multi_criteria") diff --git a/setup/sale_loyalty_order_suggestion_multi_product/odoo/addons/sale_loyalty_order_suggestion_multi_product b/setup/sale_loyalty_order_suggestion_multi_product/odoo/addons/sale_loyalty_order_suggestion_multi_product new file mode 120000 index 00000000..85e860b6 --- /dev/null +++ b/setup/sale_loyalty_order_suggestion_multi_product/odoo/addons/sale_loyalty_order_suggestion_multi_product @@ -0,0 +1 @@ +../../../../sale_loyalty_order_suggestion_multi_product \ No newline at end of file diff --git a/setup/sale_loyalty_order_suggestion_multi_product/setup.py b/setup/sale_loyalty_order_suggestion_multi_product/setup.py new file mode 100644 index 00000000..28c57bb6 --- /dev/null +++ b/setup/sale_loyalty_order_suggestion_multi_product/setup.py @@ -0,0 +1,6 @@ +import setuptools + +setuptools.setup( + setup_requires=['setuptools-odoo'], + odoo_addon=True, +)