diff --git a/sale_loyalty_partner_applicability/README.rst b/sale_loyalty_partner_applicability/README.rst new file mode 100644 index 00000000..a52d67d9 --- /dev/null +++ b/sale_loyalty_partner_applicability/README.rst @@ -0,0 +1,94 @@ +================================== +Sale Loyalty Partner Applicability +================================== + +.. + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! This file is generated by oca-gen-addon-readme !! + !! changes will be overwritten. !! + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! source digest: sha256:9332aae6140978b18c7f33e4daca2b50f8899956b118ccbc53061d20dd2b9564 + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + +.. |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. + +**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. + +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 00000000..0650744f --- /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 00000000..ebd5c09d --- /dev/null +++ b/sale_loyalty_partner_applicability/__manifest__.py @@ -0,0 +1,13 @@ +# 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"], +} diff --git a/sale_loyalty_partner_applicability/models/__init__.py b/sale_loyalty_partner_applicability/models/__init__.py new file mode 100644 index 00000000..6aacb753 --- /dev/null +++ b/sale_loyalty_partner_applicability/models/__init__.py @@ -0,0 +1 @@ +from . import sale_order 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 00000000..83abbc44 --- /dev/null +++ b/sale_loyalty_partner_applicability/models/sale_order.py @@ -0,0 +1,40 @@ +# Copyright 2023 Tecnativa - Pilar Vargas +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). +import ast + +from odoo import _, models + + +class SaleOrder(models.Model): + _inherit = "sale.order" + + def _is_valid_partner(self, program): + is_valid = False + for rule in program.rule_ids: + if not is_valid and rule.rule_partners_domain != "[]": + domain = ast.literal_eval(rule.rule_partners_domain) + [ + ("id", "=", self.partner_id.id) + ] + is_valid = bool(self.env["res.partner"].search_count(domain)) + else: + is_valid = True + break + return is_valid + + def _program_check_compute_points(self, programs): + res = super()._program_check_compute_points(programs) + # Iterate through the programs that initially have no errors + for program, result in res.items(): + if result == "error": + 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_program(self, program, coupon, status): + res = super().__try_apply_program(program, coupon, status) + 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 00000000..2c6d2e34 --- /dev/null +++ b/sale_loyalty_partner_applicability/readme/CONFIGURE.rst @@ -0,0 +1,4 @@ +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. diff --git a/sale_loyalty_partner_applicability/readme/CONTRIBUTORS.rst b/sale_loyalty_partner_applicability/readme/CONTRIBUTORS.rst new file mode 100644 index 00000000..44075b9f --- /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 00000000..daa05bc2 --- /dev/null +++ b/sale_loyalty_partner_applicability/readme/DESCRIPTION.rst @@ -0,0 +1,3 @@ +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. diff --git a/sale_loyalty_partner_applicability/readme/USAGE.rst b/sale_loyalty_partner_applicability/readme/USAGE.rst new file mode 100644 index 00000000..76e4874c --- /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 00000000..b6706576 --- /dev/null +++ b/sale_loyalty_partner_applicability/static/description/index.html @@ -0,0 +1,439 @@ + + + + + + +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.

+

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.

+
+
+

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 00000000..08aa82ef --- /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 00000000..9c78560b --- /dev/null +++ b/sale_loyalty_partner_applicability/tests/test_sale_loyalty_partner_applicability.py @@ -0,0 +1,161 @@ +# 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() + product_obj = cls.env["product.product"] + partner_obj = cls.env["res.partner"] + 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"}) + cls.partner2 = partner_obj.create({"name": "Mr. Partner Two"}) + 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", + }, + ) + ], + } + ) + + 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): + 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) + self._action_apply_program(sale_3, self.promotion_program) + self.assertFalse(bool(sale_3.order_line.filtered("is_reward_line"))) + + def test_02_sale_loyalty_partner_applicability_promo_code(self): + 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) + with self.assertRaises(ValidationError): + self._apply_promo_code(sale_3, "10DISCOUNT") 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 00000000..0fd8a05e --- /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 00000000..28c57bb6 --- /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, +)