diff --git a/sale_loyalty_margin_computation/README.rst b/sale_loyalty_margin_computation/README.rst new file mode 100644 index 00000000..98a630b3 --- /dev/null +++ b/sale_loyalty_margin_computation/README.rst @@ -0,0 +1,80 @@ +=============================== +Sale Loyalty Margin Computation +=============================== + +.. + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! This file is generated by oca-gen-addon-readme !! + !! changes will be overwritten. !! + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! source digest: sha256:9130c282b0515dc9f3fe5dfb48a3eb3f560b930e1067711ba89332ad2cc697c9 + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + +.. |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%2Fmargin--analysis-lightgray.png?logo=github + :target: https://github.com/OCA/margin-analysis/tree/18.0/sale_loyalty_margin_computation + :alt: OCA/margin-analysis +.. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png + :target: https://translation.odoo-community.org/projects/margin-analysis-18-0/margin-analysis-18-0-sale_loyalty_margin_computation + :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/margin-analysis&target_branch=18.0 + :alt: Try me on Runboat + +|badge1| |badge2| |badge3| |badge4| |badge5| + +This module allows configuring a formula to calculate the margin of a +sale order line with loyalty rewards + +**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 +------------ + +- `Tecnativa `__: + + - Pedro M. Baeza + - Andrii Kompaniiets + +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/margin-analysis `_ project on GitHub. + +You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute. diff --git a/sale_loyalty_margin_computation/__init__.py b/sale_loyalty_margin_computation/__init__.py new file mode 100644 index 00000000..0650744f --- /dev/null +++ b/sale_loyalty_margin_computation/__init__.py @@ -0,0 +1 @@ +from . import models diff --git a/sale_loyalty_margin_computation/__manifest__.py b/sale_loyalty_margin_computation/__manifest__.py new file mode 100644 index 00000000..a60acc55 --- /dev/null +++ b/sale_loyalty_margin_computation/__manifest__.py @@ -0,0 +1,19 @@ +# Copyright 2026 Tecnativa - Andrii Kompaniiets +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). +{ + "name": "Sale Loyalty Margin Computation", + "summary": "Allows to use a formula from reward for calculate sale margin", + "version": "18.0.1.0.0", + "category": "Sale", + "website": "https://github.com/OCA/margin-analysis", + "author": "Tecnativa, Odoo Community Association (OCA)", + "license": "AGPL-3", + "depends": [ + "loyalty_margin_computation", + "sale_loyalty_order_line_link", + "sale_margin_pricelist_computation", + ], + "data": [ + "views/loyalty_reward_views.xml", + ], +} diff --git a/sale_loyalty_margin_computation/i18n/es.po b/sale_loyalty_margin_computation/i18n/es.po new file mode 100644 index 00000000..e00e1a48 --- /dev/null +++ b/sale_loyalty_margin_computation/i18n/es.po @@ -0,0 +1,63 @@ +# Translation of Odoo Server. +# This file contains the translation of the following modules: +# * sale_loyalty_margin_computation +# +msgid "" +msgstr "" +"Project-Id-Version: Odoo Server 18.0\n" +"Report-Msgid-Bugs-To: \n" +"POT-Creation-Date: 2026-05-05 19:15+0000\n" +"PO-Revision-Date: 2026-05-05 21:18+0200\n" +"Last-Translator: Andrii Kompaniiets \n" +"Language-Team: \n" +"Language: es_ES\n" +"MIME-Version: 1.0\n" +"Content-Type: text/plain; charset=UTF-8\n" +"Content-Transfer-Encoding: 8bit\n" +"Plural-Forms: nplurals=2; plural=(n != 1);\n" +"X-Generator: Poedit 3.9\n" + +#. module: sale_loyalty_margin_computation +#: model_terms:ir.ui.view,arch_db:sale_loyalty_margin_computation.sale_margin_product_loyalty_reward_form_view +msgid "context: The context" +msgstr "context: El contexto" + +#. module: sale_loyalty_margin_computation +#: model_terms:ir.ui.view,arch_db:sale_loyalty_margin_computation.sale_margin_product_loyalty_reward_form_view +msgid "origin_line: The origin sale order line" +msgstr "origin_line: La línea del pedido de venta de origen" + +#. module: sale_loyalty_margin_computation +#: model_terms:ir.ui.view,arch_db:sale_loyalty_margin_computation.sale_margin_product_loyalty_reward_form_view +msgid "reward_line: The Reward line" +msgstr "reward_line: La línea de recompensa" + +#. module: sale_loyalty_margin_computation +#: model_terms:ir.ui.view,arch_db:sale_loyalty_margin_computation.sale_margin_product_loyalty_reward_form_view +msgid "reward_lines: The Reward lines" +msgstr "reward_lines: Las líneas de recompensas" + +#. module: sale_loyalty_margin_computation +#. odoo-python +#: code:addons/sale_loyalty_margin_computation/models/loyalty_reward.py:0 +msgid "" +"Invalid sale margin formula:\n" +"%(error)s" +msgstr "" +"Fórmula de margen de venta no válida:\n" +"%(error)s" + +#. module: sale_loyalty_margin_computation +#: model:ir.model,name:sale_loyalty_margin_computation.model_loyalty_reward +msgid "Loyalty Reward" +msgstr "" + +#. module: sale_loyalty_margin_computation +#: model:ir.model,name:sale_loyalty_margin_computation.model_sale_order_line +msgid "Sales Order Line" +msgstr "Linea de pedido de venta" + +#. module: sale_loyalty_margin_computation +#: model_terms:ir.ui.view,arch_db:sale_loyalty_margin_computation.sale_margin_product_loyalty_reward_form_view +msgid "result = origin_lines[0].price_unit * origin_lines[0].product_uom_qty - 11" +msgstr "" diff --git a/sale_loyalty_margin_computation/i18n/sale_loyalty_margin_computation.pot b/sale_loyalty_margin_computation/i18n/sale_loyalty_margin_computation.pot new file mode 100644 index 00000000..11b4be15 --- /dev/null +++ b/sale_loyalty_margin_computation/i18n/sale_loyalty_margin_computation.pot @@ -0,0 +1,60 @@ +# Translation of Odoo Server. +# This file contains the translation of the following modules: +# * sale_loyalty_margin_computation +# +msgid "" +msgstr "" +"Project-Id-Version: Odoo Server 18.0\n" +"Report-Msgid-Bugs-To: \n" +"POT-Creation-Date: 2026-05-05 19:15+0000\n" +"PO-Revision-Date: 2026-05-05 19:15+0000\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_margin_computation +#: model_terms:ir.ui.view,arch_db:sale_loyalty_margin_computation.sale_margin_product_loyalty_reward_form_view +msgid "context: The context" +msgstr "" + +#. module: sale_loyalty_margin_computation +#: model_terms:ir.ui.view,arch_db:sale_loyalty_margin_computation.sale_margin_product_loyalty_reward_form_view +msgid "origin_line: The origin sale order line" +msgstr "" + +#. module: sale_loyalty_margin_computation +#: model_terms:ir.ui.view,arch_db:sale_loyalty_margin_computation.sale_margin_product_loyalty_reward_form_view +msgid "reward_line: The Reward line" +msgstr "" + +#. module: sale_loyalty_margin_computation +#: model_terms:ir.ui.view,arch_db:sale_loyalty_margin_computation.sale_margin_product_loyalty_reward_form_view +msgid "reward_lines: The Reward lines" +msgstr "" + +#. module: sale_loyalty_margin_computation +#. odoo-python +#: code:addons/sale_loyalty_margin_computation/models/loyalty_reward.py:0 +msgid "" +"Invalid sale margin formula:\n" +"%(error)s" +msgstr "" + +#. module: sale_loyalty_margin_computation +#: model:ir.model,name:sale_loyalty_margin_computation.model_loyalty_reward +msgid "Loyalty Reward" +msgstr "" + +#. module: sale_loyalty_margin_computation +#: model:ir.model,name:sale_loyalty_margin_computation.model_sale_order_line +msgid "Sales Order Line" +msgstr "" + +#. module: sale_loyalty_margin_computation +#: model_terms:ir.ui.view,arch_db:sale_loyalty_margin_computation.sale_margin_product_loyalty_reward_form_view +msgid "" +"result = origin_lines[0].price_unit * origin_lines[0].product_uom_qty - 11" +msgstr "" diff --git a/sale_loyalty_margin_computation/models/__init__.py b/sale_loyalty_margin_computation/models/__init__.py new file mode 100644 index 00000000..d84f6c79 --- /dev/null +++ b/sale_loyalty_margin_computation/models/__init__.py @@ -0,0 +1,2 @@ +from . import loyalty_reward +from . import sale_order_line diff --git a/sale_loyalty_margin_computation/models/loyalty_reward.py b/sale_loyalty_margin_computation/models/loyalty_reward.py new file mode 100644 index 00000000..a5f635d4 --- /dev/null +++ b/sale_loyalty_margin_computation/models/loyalty_reward.py @@ -0,0 +1,60 @@ +# Copyright 2026 Tecnativa - Andrii Kompaniiets +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). +from odoo import Command, api, models +from odoo.exceptions import ValidationError +from odoo.tools.float_utils import float_compare +from odoo.tools.safe_eval import safe_eval + + +class LoyaltyReward(models.Model): + _inherit = "loyalty.reward" + + @api.constrains("sale_margin_formula") + def _check_sale_margin_formula(self): + res = super()._check_sale_margin_formula() + main_product = self.env["product.product"].new( + { + "name": "Main Product", + "list_price": 35.0, + "standard_price": 11.52, + } + ) + order = self.env["sale.order"].new( + { + "partner_id": self.env.ref("base.partner_admin").id, + "order_line": [ + Command.create( + { + "product_id": main_product.id, + "product_uom_qty": 2.0, + "price_unit": 35.0, + } + ) + ], + } + ) + fake_eval_context = { + "env": self.env, + "context": self.env.context, + "user": self.env.user, + "origin_line": order.order_line[0], + "reward_line": order.order_line[0], + "reward_lines": order.order_line, + "float_compare": float_compare, + } + for reward in self: + if not reward.sale_margin_formula: + continue + fake_eval_context.update({"reward": reward}) + try: + safe_eval( + str(reward.sale_margin_formula).strip(), + fake_eval_context, + mode="exec", + nocopy=True, + ) + except Exception as e: + raise ValidationError( + self.env._("Invalid sale margin formula:\n%(error)s", error=e) + ) from e + return res diff --git a/sale_loyalty_margin_computation/models/sale_order_line.py b/sale_loyalty_margin_computation/models/sale_order_line.py new file mode 100644 index 00000000..9d6b573d --- /dev/null +++ b/sale_loyalty_margin_computation/models/sale_order_line.py @@ -0,0 +1,75 @@ +from odoo import api, models +from odoo.tools.float_utils import float_compare +from odoo.tools.safe_eval import safe_eval + + +class SaleOrderLine(models.Model): + _inherit = "sale.order.line" + + @api.depends( + "reward_generated_line_ids", + "reward_origin_generated_line_ids", + "reward_generated_line_ids.purchase_price", + ) + def _compute_margin(self): + res = super()._compute_margin() + reward_lines_margin_without_formula = {} + for line in self.filtered(lambda x: x.reward_origin_generated_line_ids): + if not line.reward_id.sale_margin_formula: + if ( + line.reward_origin_generated_line_ids[0].id + in reward_lines_margin_without_formula + ): + reward_lines_margin_without_formula[ + line.reward_origin_generated_line_ids[0].id + ] += line.margin + else: + reward_lines_margin_without_formula[ + line.reward_origin_generated_line_ids[0].id + ] = line.margin + # It sets the margin of the reward line to 0 + line.margin = 0 + for origin_line in self.filtered(lambda x: x.reward_generated_line_ids): + calculated_margin = 0.0 + processed_reward = set() + for reward_line in origin_line.reward_generated_line_ids: + if ( + not reward_line.reward_id.sale_margin_formula + or reward_line.reward_id in processed_reward + ): + continue + processed_reward.add(reward_line.reward_id) + eval_context = origin_line._get_reward_eval_context(reward_line) + safe_eval( + str(reward_line.reward_id.sale_margin_formula).strip(), + eval_context, + mode="exec", + nocopy=True, + ) + calculated_margin += eval_context.get("result", 0) + if processed_reward: + origin_line.margin = ( + calculated_margin + + reward_lines_margin_without_formula.get(origin_line.id, 0.0) + ) + else: + origin_line.margin += reward_lines_margin_without_formula.get( + origin_line.id, 0.0 + ) + if origin_line.price_subtotal: + origin_line.margin_percent = ( + origin_line.margin / origin_line.price_subtotal + ) + return res + + def _get_reward_eval_context(self, reward_line): + return { + "env": self.env, + "context": self.env.context, + "user": self.env.user, + "origin_line": self, + "reward": reward_line.reward_id, + "reward_line": reward_line, + "reward_lines": self.reward_generated_line_ids, + "float_compare": float_compare, + } diff --git a/sale_loyalty_margin_computation/pyproject.toml b/sale_loyalty_margin_computation/pyproject.toml new file mode 100644 index 00000000..4231d0cc --- /dev/null +++ b/sale_loyalty_margin_computation/pyproject.toml @@ -0,0 +1,3 @@ +[build-system] +requires = ["whool"] +build-backend = "whool.buildapi" diff --git a/sale_loyalty_margin_computation/readme/CONTRIBUTORS.md b/sale_loyalty_margin_computation/readme/CONTRIBUTORS.md new file mode 100644 index 00000000..71701213 --- /dev/null +++ b/sale_loyalty_margin_computation/readme/CONTRIBUTORS.md @@ -0,0 +1,3 @@ +- [Tecnativa](https://www.tecnativa.com): + - Pedro M. Baeza + - Andrii Kompaniiets diff --git a/sale_loyalty_margin_computation/readme/DESCRIPTION.md b/sale_loyalty_margin_computation/readme/DESCRIPTION.md new file mode 100644 index 00000000..245ae0fb --- /dev/null +++ b/sale_loyalty_margin_computation/readme/DESCRIPTION.md @@ -0,0 +1,2 @@ +This module allows configuring a formula to calculate the margin of +a sale order line with loyalty rewards \ No newline at end of file diff --git a/sale_loyalty_margin_computation/static/description/icon.png b/sale_loyalty_margin_computation/static/description/icon.png new file mode 100644 index 00000000..3a0328b5 Binary files /dev/null and b/sale_loyalty_margin_computation/static/description/icon.png differ diff --git a/sale_loyalty_margin_computation/static/description/index.html b/sale_loyalty_margin_computation/static/description/index.html new file mode 100644 index 00000000..e8cc287a --- /dev/null +++ b/sale_loyalty_margin_computation/static/description/index.html @@ -0,0 +1,428 @@ + + + + + +Sale Loyalty Margin Computation + + + +
+

Sale Loyalty Margin Computation

+ + +

Beta License: AGPL-3 OCA/margin-analysis Translate me on Weblate Try me on Runboat

+

This module allows configuring a formula to calculate the margin of a +sale order line with loyalty rewards

+

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

Contributors

+
    +
  • Tecnativa:
      +
    • Pedro M. Baeza
    • +
    • Andrii Kompaniiets
    • +
    +
  • +
+
+
+

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/margin-analysis project on GitHub.

+

You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute.

+
+
+
+ + diff --git a/sale_loyalty_margin_computation/tests/__init__.py b/sale_loyalty_margin_computation/tests/__init__.py new file mode 100644 index 00000000..34312e41 --- /dev/null +++ b/sale_loyalty_margin_computation/tests/__init__.py @@ -0,0 +1 @@ +from . import test_sale_loyalty_margin_computation diff --git a/sale_loyalty_margin_computation/tests/test_sale_loyalty_margin_computation.py b/sale_loyalty_margin_computation/tests/test_sale_loyalty_margin_computation.py new file mode 100644 index 00000000..ef9d8033 --- /dev/null +++ b/sale_loyalty_margin_computation/tests/test_sale_loyalty_margin_computation.py @@ -0,0 +1,144 @@ +from odoo import Command +from odoo.exceptions import ValidationError +from odoo.tests import tagged + +from odoo.addons.sale_loyalty.tests.common import TestSaleCouponCommon + + +@tagged("post_install", "-at_install") +class TestRewardMarginComputation(TestSaleCouponCommon): + @classmethod + def setUpClass(cls): + super().setUpClass() + + cls.main_product = cls.env["product.product"].create( + { + "name": "Main Product", + "list_price": 35.0, + "standard_price": 11.52, + } + ) + + cls.reward_product_1 = cls.env["product.product"].create( + { + "name": "Reward Product 1", + "list_price": 11.0, + "standard_price": 11.0, + } + ) + + cls.reward_product_2 = cls.env["product.product"].create( + { + "name": "Reward Product 2", + "list_price": 14.0, + "standard_price": 14.0, + } + ) + + cls.program = cls.env["loyalty.program"].create( + { + "name": "Test Program", + "program_type": "promotion", + "trigger": "auto", + "applies_on": "current", + "rule_ids": [ + Command.create( + { + "minimum_qty": 2, + "product_ids": [Command.set(cls.main_product.ids)], + } + ) + ], + "reward_ids": [ + Command.create( + { + "reward_type": "multi_gift", + "reward_product_id": cls.reward_product_1.id, + "loyalty_multi_gift_ids": [ + Command.create( + { + "reward_default_product_id": ( + cls.reward_product_1.id + ), + "reward_product_quantity": 1, + "reward_product_ids": [ + Command.set(cls.reward_product_1.ids) + ], + } + ), + Command.create( + { + "reward_default_product_id": ( + cls.reward_product_2.id + ), + "reward_product_quantity": 2, + "reward_product_ids": [ + Command.set(cls.reward_product_2.ids) + ], + } + ), + Command.create( + { + "reward_default_product_id": ( + cls.main_product.id + ), + "reward_product_quantity": 1, + "reward_product_ids": [ + Command.set(cls.main_product.ids) + ], + } + ), + ], + "sale_margin_formula": ( + """ +reward_lines_cost = 0 +for reward_line in reward_lines: + reward_lines_cost += reward_line.purchase_price * reward_line.product_uom_qty +result=origin_line.margin - reward_lines_cost + 19 + """ + ), + } + ) + ], + } + ) + + def _create_order(self): + return self.env["sale.order"].create( + { + "partner_id": self.partner.id, + "order_line": [ + Command.create( + { + "product_id": self.main_product.id, + "product_uom_qty": 2.0, + "price_unit": 35.0, + } + ) + ], + } + ) + + def test_reward_applies_and_margin_is_computed(self): + order = self._create_order() + main_line = order.order_line.filtered( + lambda line: line.product_id == self.main_product and not line.reward_id + ) + order.action_open_reward_wizard() + reward_lines = order.order_line.filtered(lambda line: line.reward_id) + self.assertTrue(main_line, "Main line not found") + self.assertTrue(reward_lines, "Reward lines not found") + self.assertEqual(len(reward_lines), 3) + for reward_line in reward_lines: + self.assertIn(main_line, reward_line.reward_origin_generated_line_ids) + self.assertEqual(reward_line.margin, 0) + # 46.96 - 11.52 - 11.0 - 2*14 + 19 + self.assertAlmostEqual(main_line.margin, 15.44) + self.assertAlmostEqual( + main_line.margin_percent, 15.44 / main_line.price_subtotal + ) + self.assertAlmostEqual(order.margin, 15.44) + + def test_formula(self): + with self.assertRaises(ValidationError): + self.program.reward_ids[0].sale_margin_formula = "test=3+ '" diff --git a/sale_loyalty_margin_computation/views/loyalty_reward_views.xml b/sale_loyalty_margin_computation/views/loyalty_reward_views.xml new file mode 100644 index 00000000..01f05d27 --- /dev/null +++ b/sale_loyalty_margin_computation/views/loyalty_reward_views.xml @@ -0,0 +1,24 @@ + + + + loyalty.reward + + 99 + + +
  • context: The context
  • +
  • origin_line: The origin sale order line
  • +
  • reward_line: The Reward line
  • +
  • reward_lines: The Reward lines
  • +
    + +
    result = origin_lines[0].price_unit * origin_lines[0].product_uom_qty - 11
    +
    +
    +
    +
    diff --git a/test-requirements.txt b/test-requirements.txt new file mode 100644 index 00000000..6cbe14e1 --- /dev/null +++ b/test-requirements.txt @@ -0,0 +1,2 @@ +odoo-addon-sale_margin_pricelist_computation @ git+https://github.com/OCA/margin-analysis.git@refs/pull/262/head#subdirectory=sale_margin_pricelist_computation +odoo-addon-loyalty_margin_computation @ git+https://github.com/OCA/margin-analysis.git@refs/pull/262/head#subdirectory=loyalty_margin_computation