diff --git a/loyalty_partner_applicability/README.rst b/loyalty_partner_applicability/README.rst new file mode 100644 index 000000000..af0fb6b0a --- /dev/null +++ b/loyalty_partner_applicability/README.rst @@ -0,0 +1,76 @@ +============================= +Loyalty Partner Applicability +============================= + +.. + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! This file is generated by oca-gen-addon-readme !! + !! changes will be overwritten. !! + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! source digest: sha256:792331e9b353cd01f0b311e86cfc8fd640addeb737f01510d03a2c4d93ed9ee9 + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + +.. |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/17.0/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-17-0/sale-promotion-17-0-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=17.0 + :alt: Try me on Runboat + +|badge1| |badge2| |badge3| |badge4| |badge5| + + + +**Table of contents** + +.. contents:: + :local: + +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. + +.. 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/loyalty_partner_applicability/__init__.py b/loyalty_partner_applicability/__init__.py new file mode 100644 index 000000000..0650744f6 --- /dev/null +++ b/loyalty_partner_applicability/__init__.py @@ -0,0 +1 @@ +from . import models diff --git a/loyalty_partner_applicability/__manifest__.py b/loyalty_partner_applicability/__manifest__.py new file mode 100644 index 000000000..78047e012 --- /dev/null +++ b/loyalty_partner_applicability/__manifest__.py @@ -0,0 +1,14 @@ +# Copyright 2023 Tecnativa - Pilar Vargas +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). +{ + "name": "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": "17.0.1.0.0", + "category": "Sale", + "website": "https://github.com/OCA/sale-promotion", + "author": "Tecnativa, Odoo Community Association (OCA)", + "license": "AGPL-3", + "depends": ["loyalty"], + "data": ["views/loyalty_rule_view_form.xml"], +} diff --git a/loyalty_partner_applicability/i18n/it.po b/loyalty_partner_applicability/i18n/it.po new file mode 100644 index 000000000..7457afe94 --- /dev/null +++ b/loyalty_partner_applicability/i18n/it.po @@ -0,0 +1,42 @@ +# Translation of Odoo Server. +# This file contains the translation of the following modules: +# * loyalty_partner_applicability +# +msgid "" +msgstr "" +"Project-Id-Version: Odoo Server 16.0\n" +"Report-Msgid-Bugs-To: \n" +"PO-Revision-Date: 2023-12-12 11:33+0000\n" +"Last-Translator: mymage \n" +"Language-Team: none\n" +"Language: it\n" +"MIME-Version: 1.0\n" +"Content-Type: text/plain; charset=UTF-8\n" +"Content-Transfer-Encoding: \n" +"Plural-Forms: nplurals=2; plural=n != 1;\n" +"X-Generator: Weblate 4.17\n" + +#. module: loyalty_partner_applicability +#: model:ir.model.fields,field_description:loyalty_partner_applicability.field_res_config_settings__allow_coupon_sharing +msgid "Allow coupon sharing" +msgstr "Consenti condivisine coupon" + +#. module: loyalty_partner_applicability +#: model:ir.model.fields,field_description:loyalty_partner_applicability.field_loyalty_rule__rule_partners_domain +msgid "Based on Customers" +msgstr "Basato sui clienti" + +#. module: loyalty_partner_applicability +#: model:ir.model,name:loyalty_partner_applicability.model_res_config_settings +msgid "Config Settings" +msgstr "Impostazioni configurazione" + +#. module: loyalty_partner_applicability +#: model:ir.model,name:loyalty_partner_applicability.model_loyalty_rule +msgid "Loyalty Rule" +msgstr "Regola programma fedeltà" + +#. module: loyalty_partner_applicability +#: model:ir.model.fields,help:loyalty_partner_applicability.field_loyalty_rule__rule_partners_domain +msgid "Loyalty program will work for selected customers only" +msgstr "Il rogramma fedeltà funzionerà solo per clienti selezionati" diff --git a/loyalty_partner_applicability/i18n/loyalty_partner_applicability.pot b/loyalty_partner_applicability/i18n/loyalty_partner_applicability.pot new file mode 100644 index 000000000..159cb0a7c --- /dev/null +++ b/loyalty_partner_applicability/i18n/loyalty_partner_applicability.pot @@ -0,0 +1,39 @@ +# Translation of Odoo Server. +# This file contains the translation of the following modules: +# * loyalty_partner_applicability +# +msgid "" +msgstr "" +"Project-Id-Version: Odoo Server 16.0\n" +"Report-Msgid-Bugs-To: \n" +"Last-Translator: \n" +"Language-Team: \n" +"MIME-Version: 1.0\n" +"Content-Type: text/plain; charset=UTF-8\n" +"Content-Transfer-Encoding: \n" +"Plural-Forms: \n" + +#. module: loyalty_partner_applicability +#: model:ir.model.fields,field_description:loyalty_partner_applicability.field_res_config_settings__allow_coupon_sharing +msgid "Allow coupon sharing" +msgstr "" + +#. module: loyalty_partner_applicability +#: model:ir.model.fields,field_description:loyalty_partner_applicability.field_loyalty_rule__rule_partners_domain +msgid "Based on Customers" +msgstr "" + +#. module: loyalty_partner_applicability +#: model:ir.model,name:loyalty_partner_applicability.model_res_config_settings +msgid "Config Settings" +msgstr "" + +#. module: loyalty_partner_applicability +#: model:ir.model,name:loyalty_partner_applicability.model_loyalty_rule +msgid "Loyalty Rule" +msgstr "" + +#. module: loyalty_partner_applicability +#: model:ir.model.fields,help:loyalty_partner_applicability.field_loyalty_rule__rule_partners_domain +msgid "Loyalty program will work for selected customers only" +msgstr "" diff --git a/loyalty_partner_applicability/models/__init__.py b/loyalty_partner_applicability/models/__init__.py new file mode 100644 index 000000000..75eb0c087 --- /dev/null +++ b/loyalty_partner_applicability/models/__init__.py @@ -0,0 +1,2 @@ +from . import loyalty_rule +from . import res_config_settings diff --git a/loyalty_partner_applicability/models/loyalty_rule.py b/loyalty_partner_applicability/models/loyalty_rule.py new file mode 100644 index 000000000..bae80778e --- /dev/null +++ b/loyalty_partner_applicability/models/loyalty_rule.py @@ -0,0 +1,14 @@ +# Copyright 2023 Tecnativa - Pilar Vargas +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). +from odoo import fields, models + + +class LoyaltyRule(models.Model): + _inherit = "loyalty.rule" + _description = "Loyalty Rule" + + rule_partners_domain = fields.Char( + string="Based on Customers", + help="Loyalty program will work for selected customers only", + default="[]", + ) diff --git a/loyalty_partner_applicability/models/res_config_settings.py b/loyalty_partner_applicability/models/res_config_settings.py new file mode 100644 index 000000000..2aa063af7 --- /dev/null +++ b/loyalty_partner_applicability/models/res_config_settings.py @@ -0,0 +1,17 @@ +# 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" + + # Allows a coupon generated for one partner to be used by any other member + # of your commercial entity. There is a view for this option in the module + # sale_loyalty_partner_applicability + allow_coupon_sharing = fields.Boolean( + string="Allow coupon sharing", + config_parameter="allow_coupon_sharing", + ) diff --git a/loyalty_partner_applicability/pyproject.toml b/loyalty_partner_applicability/pyproject.toml new file mode 100644 index 000000000..4231d0ccc --- /dev/null +++ b/loyalty_partner_applicability/pyproject.toml @@ -0,0 +1,3 @@ +[build-system] +requires = ["whool"] +build-backend = "whool.buildapi" diff --git a/loyalty_partner_applicability/readme/CONTRIBUTORS.md b/loyalty_partner_applicability/readme/CONTRIBUTORS.md new file mode 100644 index 000000000..8b1378917 --- /dev/null +++ b/loyalty_partner_applicability/readme/CONTRIBUTORS.md @@ -0,0 +1 @@ + diff --git a/loyalty_partner_applicability/readme/DESCRIPTION.md b/loyalty_partner_applicability/readme/DESCRIPTION.md new file mode 100644 index 000000000..8b1378917 --- /dev/null +++ b/loyalty_partner_applicability/readme/DESCRIPTION.md @@ -0,0 +1 @@ + diff --git a/loyalty_partner_applicability/static/description/icon.png b/loyalty_partner_applicability/static/description/icon.png new file mode 100644 index 000000000..e69de29bb diff --git a/loyalty_partner_applicability/static/description/index.html b/loyalty_partner_applicability/static/description/index.html new file mode 100644 index 000000000..9a40f8833 --- /dev/null +++ b/loyalty_partner_applicability/static/description/index.html @@ -0,0 +1,417 @@ + + + + + + +Loyalty Partner Applicability + + + +
+

Loyalty Partner Applicability

+ + +

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

+

Table of contents

+ +
+

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
  • +
+
+ +
+

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/loyalty_partner_applicability/views/loyalty_rule_view_form.xml b/loyalty_partner_applicability/views/loyalty_rule_view_form.xml new file mode 100644 index 000000000..f8b86b896 --- /dev/null +++ b/loyalty_partner_applicability/views/loyalty_rule_view_form.xml @@ -0,0 +1,17 @@ + + + + + loyalty.rule + + + + + + + + diff --git a/pandoc-3.1.12.3-1-amd64.deb b/pandoc-3.1.12.3-1-amd64.deb new file mode 100644 index 000000000..b350dd23d Binary files /dev/null and b/pandoc-3.1.12.3-1-amd64.deb differ diff --git a/sale_loyalty_partner_applicability/README.rst b/sale_loyalty_partner_applicability/README.rst new file mode 100644 index 000000000..2f07004a8 --- /dev/null +++ b/sale_loyalty_partner_applicability/README.rst @@ -0,0 +1,102 @@ +================================== +Sale Loyalty Partner Applicability +================================== + +.. + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! This file is generated by oca-gen-addon-readme !! + !! changes will be overwritten. !! + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! source digest: sha256:edcda3cdc7b646ac8f1803c8b3165a505e0b4c03eabee3fa41d988f031044c7a + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + +.. |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/17.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-17-0/sale-promotion-17-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=17.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..3da455560 --- /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": "17.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/i18n/it.po b/sale_loyalty_partner_applicability/i18n/it.po new file mode 100644 index 000000000..e61466816 --- /dev/null +++ b/sale_loyalty_partner_applicability/i18n/it.po @@ -0,0 +1,45 @@ +# Translation of Odoo Server. +# This file contains the translation of the following modules: +# * sale_loyalty_partner_applicability +# +msgid "" +msgstr "" +"Project-Id-Version: Odoo Server 16.0\n" +"Report-Msgid-Bugs-To: \n" +"PO-Revision-Date: 2023-12-06 17:33+0000\n" +"Last-Translator: mymage \n" +"Language-Team: none\n" +"Language: it\n" +"MIME-Version: 1.0\n" +"Content-Type: text/plain; charset=UTF-8\n" +"Content-Transfer-Encoding: \n" +"Plural-Forms: nplurals=2; plural=n != 1;\n" +"X-Generator: Weblate 4.17\n" + +#. module: sale_loyalty_partner_applicability +#: model_terms:ir.ui.view,arch_db:sale_loyalty_partner_applicability.res_config_settings_view_form +msgid "" +"Allows a coupon generated for one partner to be used by any other member of " +"your commercial entity." +msgstr "" +"Consente di utilizzare un coupon creato per un partner da parte di un altro " +"componente della vostra entità commerciale." + +#. module: sale_loyalty_partner_applicability +#: model_terms:ir.ui.view,arch_db:sale_loyalty_partner_applicability.res_config_settings_view_form +msgid "Coupon sharing between partners within the commercial entity." +msgstr "" +"Condivisione coupon tra partner all'interno della stessa entità commerciale." + +#. module: sale_loyalty_partner_applicability +#: model:ir.model,name:sale_loyalty_partner_applicability.model_sale_order +msgid "Sales Order" +msgstr "Ordine di vendita" + +#. module: sale_loyalty_partner_applicability +#. odoo-python +#: code:addons/sale_loyalty_partner_applicability/models/sale_order.py:0 +#: code:addons/sale_loyalty_partner_applicability/models/sale_order.py:0 +#, python-format +msgid "The customer doesn't have access to this reward." +msgstr "Il cliente non a accesso a questo premio." diff --git a/sale_loyalty_partner_applicability/i18n/sale_loyalty_partner_applicability.pot b/sale_loyalty_partner_applicability/i18n/sale_loyalty_partner_applicability.pot new file mode 100644 index 000000000..dae46c26b --- /dev/null +++ b/sale_loyalty_partner_applicability/i18n/sale_loyalty_partner_applicability.pot @@ -0,0 +1,39 @@ +# Translation of Odoo Server. +# This file contains the translation of the following modules: +# * sale_loyalty_partner_applicability +# +msgid "" +msgstr "" +"Project-Id-Version: Odoo Server 16.0\n" +"Report-Msgid-Bugs-To: \n" +"Last-Translator: \n" +"Language-Team: \n" +"MIME-Version: 1.0\n" +"Content-Type: text/plain; charset=UTF-8\n" +"Content-Transfer-Encoding: \n" +"Plural-Forms: \n" + +#. module: sale_loyalty_partner_applicability +#: model_terms:ir.ui.view,arch_db:sale_loyalty_partner_applicability.res_config_settings_view_form +msgid "" +"Allows a coupon generated for one partner to be used by any other member of " +"your commercial entity." +msgstr "" + +#. module: sale_loyalty_partner_applicability +#: model_terms:ir.ui.view,arch_db:sale_loyalty_partner_applicability.res_config_settings_view_form +msgid "Coupon sharing between partners within the commercial entity." +msgstr "" + +#. module: sale_loyalty_partner_applicability +#: model:ir.model,name:sale_loyalty_partner_applicability.model_sale_order +msgid "Sales Order" +msgstr "" + +#. module: sale_loyalty_partner_applicability +#. odoo-python +#: code:addons/sale_loyalty_partner_applicability/models/sale_order.py:0 +#: code:addons/sale_loyalty_partner_applicability/models/sale_order.py:0 +#, python-format +msgid "The customer doesn't have access to this reward." +msgstr "" diff --git a/sale_loyalty_partner_applicability/models/__init__.py b/sale_loyalty_partner_applicability/models/__init__.py new file mode 100644 index 000000000..6aacb7531 --- /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 000000000..d839bb3bb --- /dev/null +++ b/sale_loyalty_partner_applicability/models/sale_order.py @@ -0,0 +1,77 @@ +# 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 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/pyproject.toml b/sale_loyalty_partner_applicability/pyproject.toml new file mode 100644 index 000000000..4231d0ccc --- /dev/null +++ b/sale_loyalty_partner_applicability/pyproject.toml @@ -0,0 +1,3 @@ +[build-system] +requires = ["whool"] +build-backend = "whool.buildapi" diff --git a/sale_loyalty_partner_applicability/readme/CONFIGURE.md b/sale_loyalty_partner_applicability/readme/CONFIGURE.md new file mode 100644 index 000000000..3bd1a381a --- /dev/null +++ b/sale_loyalty_partner_applicability/readme/CONFIGURE.md @@ -0,0 +1,9 @@ +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.md b/sale_loyalty_partner_applicability/readme/CONTRIBUTORS.md new file mode 100644 index 000000000..df940da7c --- /dev/null +++ b/sale_loyalty_partner_applicability/readme/CONTRIBUTORS.md @@ -0,0 +1,2 @@ +- [Tecnativa](https://www.tecnativa.com): + - Pilar Vargas diff --git a/sale_loyalty_partner_applicability/readme/DESCRIPTION.md b/sale_loyalty_partner_applicability/readme/DESCRIPTION.md new file mode 100644 index 000000000..e5144120d --- /dev/null +++ b/sale_loyalty_partner_applicability/readme/DESCRIPTION.md @@ -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.md b/sale_loyalty_partner_applicability/readme/USAGE.md new file mode 100644 index 000000000..84f072385 --- /dev/null +++ b/sale_loyalty_partner_applicability/readme/USAGE.md @@ -0,0 +1,3 @@ +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/icon.png b/sale_loyalty_partner_applicability/static/description/icon.png new file mode 100644 index 000000000..3a0328b51 Binary files /dev/null and b/sale_loyalty_partner_applicability/static/description/icon.png differ 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..d377823fc --- /dev/null +++ b/sale_loyalty_partner_applicability/static/description/index.html @@ -0,0 +1,446 @@ + + + + + + +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..f59f56de4 --- /dev/null +++ b/sale_loyalty_partner_applicability/tests/test_sale_loyalty_partner_applicability.py @@ -0,0 +1,228 @@ +# Copyright 2023 Tecnativa - Pilar Vargas +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). +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): + status = order._try_apply_code(code) + # Check for error when applying the code and throw exception + if "error" in status: + raise ValidationError(status["error"]) + 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) + + 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..eefae8026 --- /dev/null +++ b/sale_loyalty_partner_applicability/views/res_config_settings.xml @@ -0,0 +1,22 @@ + + + + + + partner.applicability.res.config.settings.form + res.config.settings + + + + + + + + + +