diff --git a/sale_loyalty_order_suggestion_multi_gift/README.rst b/sale_loyalty_order_suggestion_multi_gift/README.rst new file mode 100644 index 000000000..9ac34a10c --- /dev/null +++ b/sale_loyalty_order_suggestion_multi_gift/README.rst @@ -0,0 +1,97 @@ +======================================== +Sale Loyalty Order Suggestion Multi Gift +======================================== + +.. + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! This file is generated by oca-gen-addon-readme !! + !! changes will be overwritten. !! + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! source digest: sha256:3d28a9d1cda5d30a6d82161cb677692ee77bd9708deadb6b8b1cdbe2a3b4aa02 + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + +.. |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_gift + :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_gift + :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 reward is multi-gift and whose rules require a minimum quantity of products. + +**Table of contents** + +.. contents:: + :local: + +Usage +===== + +To use this module: + +* Configure or create a promotion and set in its rules a minimum quantity of products required. +* Create a sales order and add one of the products that were part of the rewards to the order lines. This line will be marked with an icon (🎁) that 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. + +Known issues / Roadmap +====================== + +For the moment only suggestions for promotions whose multi-gift rewards include products +added to the sales order and whose rules have product-based criteria will be taken into +account. This module could be extended to take into account other types of rules in the +loyalty programs to be able to apply the promotion from the wizard. + +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_gift/__init__.py b/sale_loyalty_order_suggestion_multi_gift/__init__.py new file mode 100644 index 000000000..9b4296142 --- /dev/null +++ b/sale_loyalty_order_suggestion_multi_gift/__init__.py @@ -0,0 +1,2 @@ +from . import models +from . import wizard diff --git a/sale_loyalty_order_suggestion_multi_gift/__manifest__.py b/sale_loyalty_order_suggestion_multi_gift/__manifest__.py new file mode 100644 index 000000000..f45c2b821 --- /dev/null +++ b/sale_loyalty_order_suggestion_multi_gift/__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 Gift", + "summary": "Suggest promotions with rewards multi gift 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_multi_gift"], + "installable": True, +} diff --git a/sale_loyalty_order_suggestion_multi_gift/models/__init__.py b/sale_loyalty_order_suggestion_multi_gift/models/__init__.py new file mode 100644 index 000000000..6aacb7531 --- /dev/null +++ b/sale_loyalty_order_suggestion_multi_gift/models/__init__.py @@ -0,0 +1 @@ +from . import sale_order diff --git a/sale_loyalty_order_suggestion_multi_gift/models/sale_order.py b/sale_loyalty_order_suggestion_multi_gift/models/sale_order.py new file mode 100644 index 000000000..6fa30967a --- /dev/null +++ b/sale_loyalty_order_suggestion_multi_gift/models/sale_order.py @@ -0,0 +1,24 @@ +# Copyright 2024 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 _available_programs(self): + self.ensure_one() + filtered_programs = self._filter_programs_by_rules_with_products() + programs_with_reward_multi_gift = filtered_programs.filtered( + lambda x: any(reward.reward_type == "multi_gift" for reward in x.reward_ids) + ) + programs = self.env["loyalty.program"] + if programs_with_reward_multi_gift: + product_id = self.env.context.get("product_id") + programs += programs_with_reward_multi_gift.filtered( + lambda x: any( + product_id in reward.loyalty_multi_gift_ids.reward_product_ids.ids + for reward in x.reward_ids + ) + ) + return super()._available_programs() + programs diff --git a/sale_loyalty_order_suggestion_multi_gift/readme/CONTRIBUTORS.rst b/sale_loyalty_order_suggestion_multi_gift/readme/CONTRIBUTORS.rst new file mode 100644 index 000000000..44075b9f2 --- /dev/null +++ b/sale_loyalty_order_suggestion_multi_gift/readme/CONTRIBUTORS.rst @@ -0,0 +1,3 @@ +* `Tecnativa `_: + + * Pilar Vargas diff --git a/sale_loyalty_order_suggestion_multi_gift/readme/DESCRIPTION.rst b/sale_loyalty_order_suggestion_multi_gift/readme/DESCRIPTION.rst new file mode 100644 index 000000000..363c12fdb --- /dev/null +++ b/sale_loyalty_order_suggestion_multi_gift/readme/DESCRIPTION.rst @@ -0,0 +1,2 @@ +This module extends the functionality of `sale_loyalty_order_suggestion` by giving hints +of available promotions whose reward is multi-gift and whose rules require a minimum quantity of products. diff --git a/sale_loyalty_order_suggestion_multi_gift/readme/ROADMAP.rst b/sale_loyalty_order_suggestion_multi_gift/readme/ROADMAP.rst new file mode 100644 index 000000000..3f80d60b3 --- /dev/null +++ b/sale_loyalty_order_suggestion_multi_gift/readme/ROADMAP.rst @@ -0,0 +1,4 @@ +For the moment only suggestions for promotions whose multi-gift rewards include products +added to the sales order and whose rules have product-based criteria will be taken into +account. This module could be extended to take into account other types of rules in the +loyalty programs to be able to apply the promotion from the wizard. diff --git a/sale_loyalty_order_suggestion_multi_gift/readme/USAGE.rst b/sale_loyalty_order_suggestion_multi_gift/readme/USAGE.rst new file mode 100644 index 000000000..1f64ad0f2 --- /dev/null +++ b/sale_loyalty_order_suggestion_multi_gift/readme/USAGE.rst @@ -0,0 +1,6 @@ +To use this module: + +* Configure or create a promotion and set in its rules a minimum quantity of products required. +* Create a sales order and add one of the products that were part of the rewards to the order lines. This line will be marked with an icon (🎁) that 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_gift/static/description/icon.png b/sale_loyalty_order_suggestion_multi_gift/static/description/icon.png new file mode 100644 index 000000000..f8d442615 Binary files /dev/null and b/sale_loyalty_order_suggestion_multi_gift/static/description/icon.png differ diff --git a/sale_loyalty_order_suggestion_multi_gift/static/description/index.html b/sale_loyalty_order_suggestion_multi_gift/static/description/index.html new file mode 100644 index 000000000..a5e39aa3e --- /dev/null +++ b/sale_loyalty_order_suggestion_multi_gift/static/description/index.html @@ -0,0 +1,444 @@ + + + + + + +Sale Loyalty Order Suggestion Multi Gift + + + +
+

Sale Loyalty Order Suggestion Multi Gift

+ + +

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 reward is multi-gift and whose rules require a minimum quantity of products.

+

Table of contents

+ +
+

Usage

+

To use this module:

+
    +
  • Configure or create a promotion and set in its rules a minimum quantity of products required.
  • +
  • Create a sales order and add one of the products that were part of the rewards to the order lines. This line will be marked with an icon (🎁) that 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.
  • +
+
+
+

Known issues / Roadmap

+

For the moment only suggestions for promotions whose multi-gift rewards include products +added to the sales order and whose rules have product-based criteria will be taken into +account. This module could be extended to take into account other types of rules in the +loyalty programs to be able to apply the promotion from the wizard.

+
+
+

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_gift/tests/__init__.py b/sale_loyalty_order_suggestion_multi_gift/tests/__init__.py new file mode 100644 index 000000000..6e3e00e4d --- /dev/null +++ b/sale_loyalty_order_suggestion_multi_gift/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_gift diff --git a/sale_loyalty_order_suggestion_multi_gift/tests/test_sale_loyalty_order_suggestion_multi_gift.py b/sale_loyalty_order_suggestion_multi_gift/tests/test_sale_loyalty_order_suggestion_multi_gift.py new file mode 100644 index 000000000..ef79996e1 --- /dev/null +++ b/sale_loyalty_order_suggestion_multi_gift/tests/test_sale_loyalty_order_suggestion_multi_gift.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() + cls.env = cls.env(context=dict(cls.env.context, tracking_disable=True)) + 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_1 = cls.env["product.product"].create( + {"name": "Test 1", "sale_ok": True, "list_price": 50} + ) + cls.product_2 = cls.env["product.product"].create( + {"name": "Test 2", "sale_ok": False, "list_price": 60} + ) + cls.product_3 = cls.env["product.product"].create( + {"name": "Test 3", "sale_ok": False, "list_price": 70} + ) + cls.product_4 = cls.env["product.product"].create( + {"name": "Test 4", "sale_ok": False, "list_price": 80} + ) + cls.loyalty_program_form = cls.env["loyalty.program"].create( + { + "name": "Test Multi Gift Program", + "program_type": "promotion", + "trigger": "auto", + "applies_on": "current", + "rule_ids": [ + ( + 0, + 0, + { + "reward_point_mode": "order", + "minimum_qty": 2, + "minimum_amount": 0, + }, + ), + ], + "reward_ids": [ + ( + 0, + 0, + { + "reward_type": "multi_gift", + "required_points": 1, + "loyalty_multi_gift_ids": [ + ( + 0, + 0, + { + "reward_product_ids": [(4, cls.product_2.id)], + "reward_product_quantity": 2, + }, + ), + ( + 0, + 0, + { + "reward_product_ids": [ + (4, cls.product_3.id), + (4, cls.product_4.id), + ], + "reward_product_quantity": 3, + }, + ), + ], + }, + ) + ], + } + ) + 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_1 + line_form.product_uom_qty = 1 + with sale_form.order_line.new() as line_form: + line_form.product_id = cls.product_2 + 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_no_applicable(self): + # In this test a suggestion is made for a promotion which contains in its rewards + # multi-gift product added to the order lines but does not meet all of the + # requirements to be applied so it will be necessary to configure the products in the + # products in the wizard. + self.sale.order_line.filtered(lambda x: x.product_id == self.product_1).unlink() + line_2 = self.sale.order_line.filtered(lambda x: x.product_id == self.product_2) + wizard = self._open_suggested_promotion_wizard(line_2.suggested_reward_ids) + self.assertEqual(wizard.reward_ids, line_2.suggested_promotion_ids.reward_ids) + self.assertTrue( + self.loyalty_program_form in line_2.suggested_reward_ids.program_id + ) + # Select promotion to apply + wizard.selected_reward_id = self.loyalty_program_form.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 all the products to add as no specific product has been + # set but among these products is the product added to the order lines and this + # is the one we will proceed with the test. + self.assertTrue(len(wizard.loyalty_rule_line_ids) > 0) + wiz_line_2 = wizard.loyalty_rule_line_ids.filtered( + lambda x: x.product_id == self.product_2 + ) + self.assertEqual(wiz_line_2.units_included, 1) + self.assertEqual(wiz_line_2.units_required, 2) + self.assertEqual(wiz_line_2.units_to_include, 0) + # More units are added to make the promotion compliant and applicable. + # If more quantity of the reward product is added from the wizard to apply the + # promotion, it is considered that more quantity of product is wanted in addition + # to the reward so the initial quantity added to the sales order will be respected. + wiz_line_2.units_to_include = 1 + wizard.action_apply() + self.assertEqual( + self.sale.order_line.filtered( + lambda x: x.product_id == self.product_2 and not x.is_reward_line + ).product_uom_qty, + 2, + ) + self.assertEqual( + self.sale.order_line.filtered( + lambda x: x.product_id == self.product_2 and x.is_reward_line + ).product_uom_qty, + 2, + ) + self.assertEqual( + self.sale.order_line.filtered( + lambda x: x.product_id == self.product_3 and x.is_reward_line + ).product_uom_qty, + 3, + ) + self.assertFalse( + self.sale.order_line.filtered( + lambda x: x.product_id == self.product_4 and x.is_reward_line + ) + ) + + def test_02_no_suggested_promotion_for_product_1(self): + # This test checks that the promotion containing reward products is not found + # in the suggested promotions as product_1 is not part of the rewards. + self.loyalty_program_form.rule_ids.minimum_amount = 200 + self.loyalty_program_form.rule_ids.minimum_qty = 0 + line_1 = self.sale.order_line.filtered(lambda x: x.product_id == self.product_1) + self.assertFalse( + self.loyalty_program_form in line_1.suggested_reward_ids.program_id + ) diff --git a/sale_loyalty_order_suggestion_multi_gift/wizard/__init__.py b/sale_loyalty_order_suggestion_multi_gift/wizard/__init__.py new file mode 100644 index 000000000..1ca11a9d4 --- /dev/null +++ b/sale_loyalty_order_suggestion_multi_gift/wizard/__init__.py @@ -0,0 +1 @@ +from . import sale_loyalty_reward_wizard diff --git a/sale_loyalty_order_suggestion_multi_gift/wizard/sale_loyalty_reward_wizard.py b/sale_loyalty_order_suggestion_multi_gift/wizard/sale_loyalty_reward_wizard.py new file mode 100644 index 000000000..e4ba14c2f --- /dev/null +++ b/sale_loyalty_order_suggestion_multi_gift/wizard/sale_loyalty_reward_wizard.py @@ -0,0 +1,47 @@ +from odoo import models + + +class SaleLoyaltyRewardWizard(models.TransientModel): + _inherit = "sale.loyalty.reward.wizard" + + def action_apply(self): + res = super().action_apply() + # It's necessary to adjust the order line when suggestions are made on order + # lines that contain products that are part of a multi-gift reward, i.e. if product A is + # added to the order and a promotion is suggested that has product A as a reward, + # that order line will become an order reward line or the quantity indicated in + # the promotion reward. + if self.multi_gift_reward: + for gift_line in self.loyalty_gift_line_ids: + selected_product = gift_line.selected_gift_id + product_qty = gift_line.line_id.reward_product_quantity + order_line = self.order_id.order_line.filtered( + lambda x: x.product_id == selected_product and not x.is_reward_line + ) + units_to_include = ( + self.loyalty_rule_line_ids.filtered( + lambda x: x.product_id == selected_product + ).units_to_include + or False + ) + if not units_to_include: + update_qty = order_line.product_uom_qty - product_qty + if update_qty < 1: + order_line.unlink() + else: + self._update_order_line_with_units(order_line, -abs(update_qty)) + return res + + +class SaleLoyaltyRewardProductLineWizard(models.TransientModel): + _inherit = "sale.loyalty.reward.product_line.wizard" + + def _compute_selected_gift_id(self): + res = super()._compute_selected_gift_id() + for wizard in self: + if ( + wizard.wizard_id.multi_gift_reward + and wizard.wizard_id.product_id in wizard.gift_ids._origin + ): + wizard.selected_gift_id = wizard.wizard_id.product_id + return res diff --git a/setup/sale_loyalty_order_suggestion_multi_gift/odoo/addons/sale_loyalty_order_suggestion_multi_gift b/setup/sale_loyalty_order_suggestion_multi_gift/odoo/addons/sale_loyalty_order_suggestion_multi_gift new file mode 120000 index 000000000..54e59c457 --- /dev/null +++ b/setup/sale_loyalty_order_suggestion_multi_gift/odoo/addons/sale_loyalty_order_suggestion_multi_gift @@ -0,0 +1 @@ +../../../../sale_loyalty_order_suggestion_multi_gift \ No newline at end of file diff --git a/setup/sale_loyalty_order_suggestion_multi_gift/setup.py b/setup/sale_loyalty_order_suggestion_multi_gift/setup.py new file mode 100644 index 000000000..28c57bb64 --- /dev/null +++ b/setup/sale_loyalty_order_suggestion_multi_gift/setup.py @@ -0,0 +1,6 @@ +import setuptools + +setuptools.setup( + setup_requires=['setuptools-odoo'], + odoo_addon=True, +)