Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 80 additions & 0 deletions sale_loyalty_margin_computation/README.rst
Original file line number Diff line number Diff line change
@@ -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%2Fsale--promotion-lightgray.png?logo=github
:target: https://github.com/OCA/sale-promotion/tree/18.0/sale_loyalty_margin_computation
: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-18-0/sale-promotion-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/sale-promotion&target_branch=18.0
:alt: Try me on Runboat

|badge1| |badge2| |badge3| |badge4| |badge5|

This module allows to configure a formula (Python code to use in sales,
pos). It's a technical base to be extended with top level functionality.

**Table of contents**

.. contents::
:local:

Bug Tracker
===========

Bugs are tracked on `GitHub Issues <https://github.com/OCA/sale-promotion/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 <https://github.com/OCA/sale-promotion/issues/new?body=module:%20sale_loyalty_margin_computation%0Aversion:%2018.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**>`_.

Do not contact contributors directly about support or help with technical issues.

Credits
=======

Authors
-------

* Tecnativa

Contributors
------------

- `Tecnativa <https://www.tecnativa.com>`__:

- 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/sale-promotion <https://github.com/OCA/sale-promotion/tree/18.0/sale_loyalty_margin_computation>`_ project on GitHub.

You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute.
1 change: 1 addition & 0 deletions sale_loyalty_margin_computation/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import models
19 changes: 19 additions & 0 deletions sale_loyalty_margin_computation/__manifest__.py
Original file line number Diff line number Diff line change
@@ -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": "Allow to use a formula from reward for calculate sale margin",
"version": "18.0.1.0.0",
"category": "Sale",
"website": "https://github.com/OCA/sale-promotion",
"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",
],
}
38 changes: 38 additions & 0 deletions sale_loyalty_margin_computation/i18n/es.po
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# 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-04-30 14:27+0000\n"
"PO-Revision-Date: 2026-04-30 16:28+0200\n"
"Last-Translator: Andrii Kompaniiets <andrii.kompaniiets@tecnativa.com>\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 "<code>context</code>: The context"
msgstr "<code>context</code>: 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 "<code>origin_lines</code>: The origin sale order lines"
msgstr "<code>origin_lines</code>: Las líneas 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 "<code>reward_line</code>: The Reward line"
msgstr "<code>reward_line</code>: La línea de recompensas"

#. 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"
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# 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-04-30 14:27+0000\n"
"PO-Revision-Date: 2026-04-30 14:27+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 "<code>context</code>: 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 "<code>origin_lines</code>: The origin sale order lines"
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 "<code>reward_line</code>: The Reward line"
msgstr ""

#. module: sale_loyalty_margin_computation
#: model:ir.model,name:sale_loyalty_margin_computation.model_sale_order_line
msgid "Sales Order Line"
msgstr ""
2 changes: 2 additions & 0 deletions sale_loyalty_margin_computation/models/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
from . import loyalty_reward
from . import sale_order_line
61 changes: 61 additions & 0 deletions sale_loyalty_margin_computation/models/loyalty_reward.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
# 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"

def _get_sale_margin_formula_eval_context(self):
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,
}
)
],
}
)
return {
"env": self.env,
"context": self.env.context,
"user": self.env.user,
"origin_lines": order.order_line,
"reward": self,
"reward_line": order.order_line[0],
"float_compare": float_compare,
}

@api.constrains("sale_margin_formula")
def _check_sale_margin_formula(self):
res = super()._check_sale_margin_formula()
for reward in self:
if not reward.sale_margin_formula:
continue
try:
safe_eval(
str(reward.sale_margin_formula).strip(),
reward._get_sale_margin_formula_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
39 changes: 39 additions & 0 deletions sale_loyalty_margin_computation/models/sale_order_line.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
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_origin_generated_line_ids", "reward_id")
def _compute_margin(self):
res = super()._compute_margin()
for line in self.filtered(lambda x: x.reward_origin_generated_line_ids):
if not line.reward_id.sale_margin_formula:
continue
origin_lines = line.reward_origin_generated_line_ids
eval_context = line._get_reward_eval_context()
safe_eval(
str(line.reward_id.sale_margin_formula).strip(),
eval_context,
mode="exec",
nocopy=True,
)
origin_lines[0].margin = eval_context.get("result", 0)
origin_lines[0].margin_percent = (
origin_lines[0].margin / origin_lines[0].price_subtotal
)
break
return res

def _get_reward_eval_context(self):
return {
"env": self.env,
"context": self.env.context,
"user": self.env.user,
"origin_lines": self.reward_origin_generated_line_ids,
"reward": self.reward_id,
"reward_line": self,
"float_compare": float_compare,
}
3 changes: 3 additions & 0 deletions sale_loyalty_margin_computation/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[build-system]
requires = ["whool"]
build-backend = "whool.buildapi"
3 changes: 3 additions & 0 deletions sale_loyalty_margin_computation/readme/CONTRIBUTORS.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
- [Tecnativa](https://www.tecnativa.com):
- Pedro M. Baeza
- Andrii Kompaniiets
2 changes: 2 additions & 0 deletions sale_loyalty_margin_computation/readme/DESCRIPTION.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
This module allows to configure a formula (Python code to use in sales, pos).
It's a technical base to be extended with top level functionality.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Loading