diff --git a/sale_loyalty_partner_applicability/README.rst b/sale_loyalty_partner_applicability/README.rst new file mode 100644 index 000000000..e53f47747 --- /dev/null +++ b/sale_loyalty_partner_applicability/README.rst @@ -0,0 +1,100 @@ +================================== +Sale Loyalty Partner Applicability +================================== + +.. + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! This file is generated by oca-gen-addon-readme !! + !! changes will be overwritten. !! + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! source digest: sha256:bd99f5149f7b31775a82ebf75fa2031b9afc04d0bfde9e4a566329bf4054cd0b + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + +.. |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_partner_applicability + :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_partner_applicability + :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 loyalty_partner_applicability functionality. When this filter +is defined, the promotion rule will only be applied to customers who meet the specified +conditions in the filter. +The coupons generated in a sales order for a specific member promotion can be shared +between members of the same trading entity. + +**Table of contents** + +.. contents:: + :local: + +Configuration +============= + +To configure the partner based promotion filter: + +Go to *Sales > Products > Discount & Loyalty* and select or create a new one. +In conditional rules set the condition based on customers. + +In promotions that generate coupons for the next sale, we can allow coupon sharing +between members of the same commercial entity. +To allow this, go to *Settings > Sales > Pricing* and check the option "Allow coupon sharing". + +Usage +===== + +Go to a sales order and apply the promotion accordingly. If the sales order meets the +requirements set in the partner based filter the promotion will be applied to that order. + +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_partner_applicability/__init__.py b/sale_loyalty_partner_applicability/__init__.py new file mode 100644 index 000000000..0650744f6 --- /dev/null +++ b/sale_loyalty_partner_applicability/__init__.py @@ -0,0 +1 @@ +from . import models diff --git a/sale_loyalty_partner_applicability/__manifest__.py b/sale_loyalty_partner_applicability/__manifest__.py new file mode 100644 index 000000000..7b1349b3a --- /dev/null +++ b/sale_loyalty_partner_applicability/__manifest__.py @@ -0,0 +1,16 @@ +# Copyright 2023 Tecnativa - Pilar Vargas +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). +{ + "name": "Sale Loyalty Partner Applicability", + "summary": "Enables the definition of a customer filter for promotion rules that will " + "only be applied to customers who meet the specified conditions in the filter.", + "version": "16.0.1.0.0", + "category": "Sale", + "website": "https://github.com/OCA/sale-promotion", + "author": "Tecnativa, Odoo Community Association (OCA)", + "license": "AGPL-3", + "depends": ["sale_loyalty", "loyalty_partner_applicability"], + "data": [ + "views/res_config_settings.xml", + ], +} diff --git a/sale_loyalty_partner_applicability/models/__init__.py b/sale_loyalty_partner_applicability/models/__init__.py new file mode 100644 index 000000000..a55660209 --- /dev/null +++ b/sale_loyalty_partner_applicability/models/__init__.py @@ -0,0 +1,2 @@ +from . import sale_order +from . import res_config_settings diff --git a/sale_loyalty_partner_applicability/models/res_config_settings.py b/sale_loyalty_partner_applicability/models/res_config_settings.py new file mode 100644 index 000000000..be3009b76 --- /dev/null +++ b/sale_loyalty_partner_applicability/models/res_config_settings.py @@ -0,0 +1,13 @@ +# Copyright 2023 Tecnativa - Pilar Vargas Moreno +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + +from odoo import fields, models + + +class ResConfigSettings(models.TransientModel): + + _inherit = "res.config.settings" + + allow_coupon_sharing = fields.Boolean( + string="Allow coupon sharing", config_parameter="allow_coupon_sharing" + ) diff --git a/sale_loyalty_partner_applicability/models/sale_order.py b/sale_loyalty_partner_applicability/models/sale_order.py new file mode 100644 index 000000000..fc7e5aebf --- /dev/null +++ b/sale_loyalty_partner_applicability/models/sale_order.py @@ -0,0 +1,78 @@ +# Copyright 2023 Tecnativa - Pilar Vargas +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). +import ast + +from odoo import _, models +from odoo.osv import expression + + +class SaleOrder(models.Model): + _inherit = "sale.order" + + def _get_partner_domain(self, rule, partner_id): + domain = [] + if rule.rule_partners_domain != "[]": + allow_sharing = ( + self.env["ir.config_parameter"].sudo().get_param("allow_coupon_sharing") + ) + if allow_sharing: + domain = [ + ("commercial_partner_id", "=", partner_id.commercial_partner_id.id) + ] + else: + domain = [("id", "=", partner_id.id)] + domain = expression.AND( + [ast.literal_eval(rule.rule_partners_domain), domain] + ) + return domain + + def _is_valid_partner(self, program): + """ + Check if the partner is eligible for a loyalty program based on partner domains. + This method iterates through the loyalty program's rules and their partner + domains. It verifies if the partner meets the eligibility criteria specified + in the partner domain of each rule. When the partner is found eligible for a + rule, the program is considered valid. + Args: + program (recordset): The loyalty program for which partner eligibility is checked. + Returns: + bool: True if the partner is eligible for the program, False otherwise. + """ + for rule in program.rule_ids: + partner_domain = self._get_partner_domain(rule, self.partner_id) + if partner_domain: + if self.env["res.partner"].search_count(partner_domain): + return True + return False + + def _program_check_compute_points(self, programs): + """ + Check and ensure partner eligibility for loyalty programs. + This method extends the behavior of checking and computing loyalty program + points. It checks if the customer meets the partner eligibility criteria + for each program. If a customer does not meet the criteria, an error is + added to the results for that program. + """ + res = super()._program_check_compute_points(programs) + for program, result in res.items(): + if result.get("error", False): + continue + if not self._is_valid_partner(program): + res[program] = { + "error": _("The customer doesn't have access to this reward.") + } + return res + + def _try_apply_code(self, code): + res = super()._try_apply_code(code) + base_domain = self._get_trigger_domain() + domain = expression.AND( + [base_domain, [("mode", "=", "with_code"), ("code", "=", code)]] + ) + program = self.env["loyalty.rule"].search(domain).program_id + if not program: + program = self.env["loyalty.card"].search([("code", "=", code)]).program_id + # Check that the partner is valid when applying the coupon code. + if not self._is_valid_partner(program): + return {"error": _("The customer doesn't have access to this reward.")} + return res diff --git a/sale_loyalty_partner_applicability/readme/CONFIGURE.rst b/sale_loyalty_partner_applicability/readme/CONFIGURE.rst new file mode 100644 index 000000000..7c544f752 --- /dev/null +++ b/sale_loyalty_partner_applicability/readme/CONFIGURE.rst @@ -0,0 +1,8 @@ +To configure the partner based promotion filter: + +Go to *Sales > Products > Discount & Loyalty* and select or create a new one. +In conditional rules set the condition based on customers. + +In promotions that generate coupons for the next sale, we can allow coupon sharing +between members of the same commercial entity. +To allow this, go to *Settings > Sales > Pricing* and check the option "Allow coupon sharing". diff --git a/sale_loyalty_partner_applicability/readme/CONTRIBUTORS.rst b/sale_loyalty_partner_applicability/readme/CONTRIBUTORS.rst new file mode 100644 index 000000000..44075b9f2 --- /dev/null +++ b/sale_loyalty_partner_applicability/readme/CONTRIBUTORS.rst @@ -0,0 +1,3 @@ +* `Tecnativa `_: + + * Pilar Vargas diff --git a/sale_loyalty_partner_applicability/readme/DESCRIPTION.rst b/sale_loyalty_partner_applicability/readme/DESCRIPTION.rst new file mode 100644 index 000000000..c0f101123 --- /dev/null +++ b/sale_loyalty_partner_applicability/readme/DESCRIPTION.rst @@ -0,0 +1,5 @@ +This module extends the loyalty_partner_applicability functionality. When this filter +is defined, the promotion rule will only be applied to customers who meet the specified +conditions in the filter. +The coupons generated in a sales order for a specific member promotion can be shared +between members of the same trading entity. diff --git a/sale_loyalty_partner_applicability/readme/USAGE.rst b/sale_loyalty_partner_applicability/readme/USAGE.rst new file mode 100644 index 000000000..76e4874c6 --- /dev/null +++ b/sale_loyalty_partner_applicability/readme/USAGE.rst @@ -0,0 +1,2 @@ +Go to a sales order and apply the promotion accordingly. If the sales order meets the +requirements set in the partner based filter the promotion will be applied to that order. diff --git a/sale_loyalty_partner_applicability/static/description/index.html b/sale_loyalty_partner_applicability/static/description/index.html new file mode 100644 index 000000000..048a07153 --- /dev/null +++ b/sale_loyalty_partner_applicability/static/description/index.html @@ -0,0 +1,444 @@ + + + + + + +Sale Loyalty Partner Applicability + + + +
+

Sale Loyalty Partner Applicability

+ + +

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

+

This module extends the loyalty_partner_applicability functionality. When this filter +is defined, the promotion rule will only be applied to customers who meet the specified +conditions in the filter. +The coupons generated in a sales order for a specific member promotion can be shared +between members of the same trading entity.

+

Table of contents

+ +
+

Configuration

+

To configure the partner based promotion filter:

+

Go to Sales > Products > Discount & Loyalty and select or create a new one. +In conditional rules set the condition based on customers.

+

In promotions that generate coupons for the next sale, we can allow coupon sharing +between members of the same commercial entity. +To allow this, go to Settings > Sales > Pricing and check the option “Allow coupon sharing”.

+
+
+

Usage

+

Go to a sales order and apply the promotion accordingly. If the sales order meets the +requirements set in the partner based filter the promotion will be applied to that order.

+
+
+

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_partner_applicability/tests/__init__.py b/sale_loyalty_partner_applicability/tests/__init__.py new file mode 100644 index 000000000..08aa82ef6 --- /dev/null +++ b/sale_loyalty_partner_applicability/tests/__init__.py @@ -0,0 +1 @@ +from . import test_sale_loyalty_partner_applicability diff --git a/sale_loyalty_partner_applicability/tests/test_sale_loyalty_partner_applicability.py b/sale_loyalty_partner_applicability/tests/test_sale_loyalty_partner_applicability.py new file mode 100644 index 000000000..d1384a420 --- /dev/null +++ b/sale_loyalty_partner_applicability/tests/test_sale_loyalty_partner_applicability.py @@ -0,0 +1,234 @@ +# Copyright 2023 Tecnativa - Pilar Vargas +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). +from odoo import _ +from odoo.exceptions import ValidationError +from odoo.tests import Form, TransactionCase + + +class TestSaleLoyaltyPartnerApplicability(TransactionCase): + @classmethod + def setUpClass(cls): + super().setUpClass() + # Disable sharing of coupons between members of the same trading entity + cls.env["ir.config_parameter"].set_param("allow_coupon_sharing", "False") + product_obj = cls.env["product.product"] + partner_obj = cls.env["res.partner"] + cls.commercial_entity = cls.env["res.partner"].create( + {"name": "Mr. Commercial Entity"} + ) + 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.partner1 = partner_obj.create( + {"name": "Mr. Partner One", "parent_id": cls.commercial_entity.id} + ) + cls.partner2 = partner_obj.create( + {"name": "Mr. Partner Two", "parent_id": cls.commercial_entity.id} + ) + cls.partner3 = partner_obj.create({"name": "Mr. Partner Three"}) + cls.promotion_program = cls.env["loyalty.program"].create( + { + "name": "Test Promotions Sale Loyalty Partner Applicability", + "program_type": "promotion", + "trigger": "auto", + "applies_on": "current", + "rule_ids": [ + ( + 0, + 0, + { + "rule_partners_domain": [("id", "=", cls.partner1.id)], + "reward_point_mode": "order", + "minimum_qty": 1, + }, + ), + ( + 0, + 0, + { + "rule_partners_domain": [("id", "=", cls.partner2.id)], + "reward_point_mode": "order", + "minimum_qty": 1, + }, + ), + ], + "reward_ids": [ + ( + 0, + 0, + { + "reward_type": "discount", + "required_points": 1, + "discount": 10, + "discount_mode": "percent", + "discount_applicability": "order", + }, + ) + ], + } + ) + cls.promo_code_program = cls.env["loyalty.program"].create( + { + "name": "Test Discount Code Sale Loyalty Partner Applicability", + "program_type": "promo_code", + "trigger": "with_code", + "applies_on": "current", + "rule_ids": [ + ( + 0, + 0, + { + "code": "10DISCOUNT", + "rule_partners_domain": [("id", "=", cls.partner1.id)], + "reward_point_mode": "order", + "minimum_qty": 1, + }, + ), + ( + 0, + 0, + { + "rule_partners_domain": [("id", "=", cls.partner2.id)], + "reward_point_mode": "order", + "minimum_qty": 1, + }, + ), + ], + "reward_ids": [ + ( + 0, + 0, + { + "reward_type": "discount", + "required_points": 1, + "discount": 10, + "discount_mode": "percent", + "discount_applicability": "order", + }, + ) + ], + } + ) + cls.next_order_coupon = cls.env["loyalty.program"].create( + { + "name": "Test Loyalty Limit Next Order Coupons", + "program_type": "next_order_coupons", + "trigger": "auto", + "applies_on": "future", + "rule_ids": [ + ( + 0, + 0, + { + "rule_partners_domain": [("id", "=", cls.partner1.id)], + "reward_point_mode": "order", + "minimum_qty": 1, + }, + ), + ], + "reward_ids": [ + ( + 0, + 0, + { + "reward_type": "discount", + "required_points": 1, + "discount": 10, + "discount_mode": "percent", + "discount_applicability": "order", + }, + ) + ], + } + ) + + def _create_sale(self, partner): + sale_form = Form(self.env["sale.order"]) + sale_form.partner_id = partner + with sale_form.order_line.new() as line_form: + line_form.product_id = self.product_a + line_form.product_uom_qty = 1 + with sale_form.order_line.new() as line_form: + line_form.product_id = self.product_b + line_form.product_uom_qty = 1 + return sale_form.save() + + def _action_apply_program(self, sale, program): + sale._update_programs_and_rewards() + wizard = ( + self.env["sale.loyalty.reward.wizard"] + .with_context(active_id=sale) + .create({"selected_reward_id": program.reward_ids.id}) + ) + wizard.action_apply() + + def _apply_promo_code(self, order, code, no_reward_fail=True): + status = order._try_apply_code(code) + if "error" in status: + raise ValidationError(status["error"]) + if not status and no_reward_fail: + # Can happen if global discount got filtered out in `_get_claimable_rewards` + raise ValidationError(_("No reward to claim with this coupon")) + coupons = self.env["loyalty.card"] + rewards = self.env["loyalty.reward"] + for coupon, coupon_rewards in status.items(): + coupons |= coupon + rewards |= coupon_rewards + if len(coupons) == 1 and len(rewards) == 1: + status = order._apply_program_reward(rewards, coupons) + if "error" in status: + raise ValidationError(status["error"]) + + def test_01_sale_loyalty_partner_applicability_promotion(self): + # The test is executed with a promotion which states in its rules that + # the promotion will be applicable on partner1 and partner2. + sale_1 = self._create_sale(self.partner1) + self._action_apply_program(sale_1, self.promotion_program) + self.assertTrue(bool(sale_1.order_line.filtered("is_reward_line"))) + sale_2 = self._create_sale(self.partner2) + self._action_apply_program(sale_2, self.promotion_program) + self.assertTrue(bool(sale_2.order_line.filtered("is_reward_line"))) + sale_3 = self._create_sale(self.partner3) + # There should be no promotion applicable, therefore the wizard should not + # appear to choose one, therefore the error. + with self.assertRaises(ValidationError): + self._action_apply_program(sale_3, self.promotion_program) + + def test_02_sale_loyalty_partner_applicability_promo_code(self): + # The test is executed with a promotion code which states in its rules that + # the promotion will be applicable on partner1 and partner2. + sale_1 = self._create_sale(self.partner1) + self._apply_promo_code(sale_1, "10DISCOUNT") + self.assertTrue(bool(sale_1.order_line.filtered("is_reward_line"))) + sale_2 = self._create_sale(self.partner2) + self._apply_promo_code(sale_2, "10DISCOUNT") + self.assertTrue(bool(sale_2.order_line.filtered("is_reward_line"))) + sale_3 = self._create_sale(self.partner3) + # Promotion does not apply to partner3. + with self.assertRaises(ValidationError): + self._apply_promo_code(sale_3, "10DISCOUNT") + + def test_03_coupon_code_next_order_customer_limit(self): + """ """ + # Enable sharing of coupons between members of the same trading entity + self.env["ir.config_parameter"].set_param("allow_coupon_sharing", "True") + # The first order generates the coupon for the next one + sale_1 = self._create_sale(self.partner1) + sale_1._update_programs_and_rewards() + sale_1.action_confirm() + coupon_1 = self.next_order_coupon.coupon_ids + # New order for another member of the same trading entity to apply the + # generated coupon + sale_2 = self._create_sale(self.partner2) + self._apply_promo_code(sale_2, coupon_1.code) + self.assertTrue(bool(sale_2.order_line.filtered("is_reward_line"))) + # New order to generate a coupon + sale_3 = self._create_sale(self.partner1) + sale_3._update_programs_and_rewards() + sale_3.action_confirm() + coupon_2 = self.next_order_coupon.coupon_ids[1] + # New order for another partner from another business entity. The coupon will + # not be applied. + sale_4 = self._create_sale(self.partner3) + with self.assertRaises(ValidationError): + self._apply_promo_code(sale_4, coupon_2.code) diff --git a/sale_loyalty_partner_applicability/views/res_config_settings.xml b/sale_loyalty_partner_applicability/views/res_config_settings.xml new file mode 100644 index 000000000..89da4d4fd --- /dev/null +++ b/sale_loyalty_partner_applicability/views/res_config_settings.xml @@ -0,0 +1,29 @@ + + + + + partner.applicability.res.config.settings.form + res.config.settings + + + +
+
+ +
+
+
+
+
+
+
+
diff --git a/setup/sale_loyalty_partner_applicability/odoo/addons/sale_loyalty_partner_applicability b/setup/sale_loyalty_partner_applicability/odoo/addons/sale_loyalty_partner_applicability new file mode 120000 index 000000000..0fd8a05e9 --- /dev/null +++ b/setup/sale_loyalty_partner_applicability/odoo/addons/sale_loyalty_partner_applicability @@ -0,0 +1 @@ +../../../../sale_loyalty_partner_applicability \ No newline at end of file diff --git a/setup/sale_loyalty_partner_applicability/setup.py b/setup/sale_loyalty_partner_applicability/setup.py new file mode 100644 index 000000000..28c57bb64 --- /dev/null +++ b/setup/sale_loyalty_partner_applicability/setup.py @@ -0,0 +1,6 @@ +import setuptools + +setuptools.setup( + setup_requires=['setuptools-odoo'], + odoo_addon=True, +)