From 96c27c7a0de0eaa1ea5e3e85c3f324a5e712dc70 Mon Sep 17 00:00:00 2001 From: pilarvargas-tecnativa Date: Mon, 29 Jan 2024 18:59:11 +0100 Subject: [PATCH] [ADD] sale_loyalty_order_suggestion_multi_product: New module TT47449 --- .../README.rst | 88 ++++ .../__init__.py | 2 + .../__manifest__.py | 14 + .../models/__init__.py | 1 + .../models/sale_order.py | 32 ++ .../readme/CONTRIBUTORS.rst | 3 + .../readme/DESCRIPTION.rst | 1 + .../readme/USAGE.rst | 6 + .../static/description/icon.png | Bin 0 -> 6248 bytes .../static/description/index.html | 435 ++++++++++++++++++ .../tests/__init__.py | 2 + ..._loyalty_order_suggestion_multi_product.py | 176 +++++++ .../wizard/__init__.py | 1 + .../wizard/sale_loyalty_reward_wizard.py | 90 ++++ ...ale_loyalty_order_suggestion_multi_product | 1 + .../setup.py | 6 + 16 files changed, 858 insertions(+) create mode 100644 sale_loyalty_order_suggestion_multi_product/README.rst create mode 100644 sale_loyalty_order_suggestion_multi_product/__init__.py create mode 100644 sale_loyalty_order_suggestion_multi_product/__manifest__.py create mode 100644 sale_loyalty_order_suggestion_multi_product/models/__init__.py create mode 100644 sale_loyalty_order_suggestion_multi_product/models/sale_order.py create mode 100644 sale_loyalty_order_suggestion_multi_product/readme/CONTRIBUTORS.rst create mode 100644 sale_loyalty_order_suggestion_multi_product/readme/DESCRIPTION.rst create mode 100644 sale_loyalty_order_suggestion_multi_product/readme/USAGE.rst create mode 100644 sale_loyalty_order_suggestion_multi_product/static/description/icon.png create mode 100644 sale_loyalty_order_suggestion_multi_product/static/description/index.html create mode 100644 sale_loyalty_order_suggestion_multi_product/tests/__init__.py create mode 100644 sale_loyalty_order_suggestion_multi_product/tests/test_sale_loyalty_order_suggestion_multi_product.py create mode 100644 sale_loyalty_order_suggestion_multi_product/wizard/__init__.py create mode 100644 sale_loyalty_order_suggestion_multi_product/wizard/sale_loyalty_reward_wizard.py create mode 120000 setup/sale_loyalty_order_suggestion_multi_product/odoo/addons/sale_loyalty_order_suggestion_multi_product create mode 100644 setup/sale_loyalty_order_suggestion_multi_product/setup.py 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 000000000..32801db10 --- /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 000000000..9b4296142 --- /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 000000000..4cfdfe94a --- /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 000000000..6aacb7531 --- /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 000000000..a065a2f67 --- /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 000000000..44075b9f2 --- /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 000000000..2fa9a54ec --- /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 000000000..bb6a8b09d --- /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 0000000000000000000000000000000000000000..f8d4426158c3926825016c22ad906af27ed778c7 GIT binary patch literal 6248 zcmXY02RK|$v|qhMkGfU_t45SqC5W9bJothFz2><}7H8o&{_%{AuAt%MxA3Bv4 z@C}8##&b^qfFtT(AxK&~w8azIyj0D+jNI(JeBmCp0AF8U5eHW%PXyfER>aN2K6hUp z2mr90Xu_1A`Q`29zs_S^%kB$4G4VDwJ<}Pb;HC=4s6CbB66qqRS0QEzqkIsQBDv2` zn#zg*@7p(g_-XfqyO*_??*jR5fKORYUMY!kau_XFg2Tom= zbMj*z<^5CX7IPWo+gb6Z7> zP7T7WE?jy)S%Xz}#p6xyk(ETy1gL1)`H9X0oHP5I_R+2x(P8xVU5e$-zw#C$grOT; zLkxpMzYZ?s??;3f`geN|Z77V^SmJyv^VD*I?B;Lw3otQvn|R28J{N`&k+y{!6yJg7 zp{UhLe}^v}i2@Lh5gb_#o(lk=1Yjom(-c&yoXo`1-JH$c!fTwIl_r}LyXF<<)Zl+S z-B0g$7M>OqzXQ8heq!eg^s5hJI0XLi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U;Deg59@n$YlY zik|J4q|InxiW*3*Lr+BKM`bW2sDg6x;-P7VUUR$e}NP$#FmVOEJleVnc5`GB(ovjF<)dYF zP6NHo^!}keHRe(s2VcJbxVjkH;16N%LNz?LVNesl?-CA pB(}Dh0Rx}TPSxxXY5uw;W3FcoeE#)h2LG1;psD&4_5o@W_CKN9**5?H literal 0 HcmV?d00001 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 000000000..9b37c30d0 --- /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 000000000..1cb3842ee --- /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 000000000..12e8d1c4d --- /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 000000000..1ca11a9d4 --- /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 000000000..b9468d5c7 --- /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 000000000..85e860b67 --- /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 000000000..28c57bb64 --- /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, +)