From 8910f2fdeea31b4fdbde203d3c116100ba1f02fa Mon Sep 17 00:00:00 2001 From: Andrii9090-tecnativa Date: Tue, 28 Apr 2026 16:18:54 +0200 Subject: [PATCH] [ADD] sale_margin_pricelist_computation: New module TT62183 --- sale_margin_pricelist_computation/README.rst | 92 ++++ sale_margin_pricelist_computation/__init__.py | 2 + .../__manifest__.py | 15 + sale_margin_pricelist_computation/i18n/es.po | 134 ++++++ .../sale_margin_pricelist_computation.pot | 124 +++++ .../models/__init__.py | 2 + .../models/product_pricelist_item.py | 85 ++++ .../models/sale_order_line.py | 69 +++ .../pyproject.toml | 3 + .../readme/CONTRIBUTORS.md | 3 + .../readme/DESCRIPTION.md | 1 + .../readme/USAGE.md | 8 + .../static/description/index.html | 442 ++++++++++++++++++ .../tests/__init__.py | 1 + .../test_sale_margin_pricelist_computation.py | 60 +++ .../views/product_pricelist_item_views.xml | 56 +++ 16 files changed, 1097 insertions(+) create mode 100644 sale_margin_pricelist_computation/README.rst create mode 100644 sale_margin_pricelist_computation/__init__.py create mode 100644 sale_margin_pricelist_computation/__manifest__.py create mode 100644 sale_margin_pricelist_computation/i18n/es.po create mode 100644 sale_margin_pricelist_computation/i18n/sale_margin_pricelist_computation.pot create mode 100644 sale_margin_pricelist_computation/models/__init__.py create mode 100644 sale_margin_pricelist_computation/models/product_pricelist_item.py create mode 100644 sale_margin_pricelist_computation/models/sale_order_line.py create mode 100644 sale_margin_pricelist_computation/pyproject.toml create mode 100644 sale_margin_pricelist_computation/readme/CONTRIBUTORS.md create mode 100644 sale_margin_pricelist_computation/readme/DESCRIPTION.md create mode 100644 sale_margin_pricelist_computation/readme/USAGE.md create mode 100644 sale_margin_pricelist_computation/static/description/index.html create mode 100644 sale_margin_pricelist_computation/tests/__init__.py create mode 100644 sale_margin_pricelist_computation/tests/test_sale_margin_pricelist_computation.py create mode 100644 sale_margin_pricelist_computation/views/product_pricelist_item_views.xml diff --git a/sale_margin_pricelist_computation/README.rst b/sale_margin_pricelist_computation/README.rst new file mode 100644 index 000000000..d8f600e71 --- /dev/null +++ b/sale_margin_pricelist_computation/README.rst @@ -0,0 +1,92 @@ +================================= +Sale Margin Pricelist Computation +================================= + +.. + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! This file is generated by oca-gen-addon-readme !! + !! changes will be overwritten. !! + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! source digest: sha256:485c92d8fb4cce354e556411e031f431d9c1589f6dfb17229f8a40d3fe14d835 + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + +.. |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_margin_pricelist_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_margin_pricelist_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 the use of formulas to calculate the margin of sale +order lines. + +**Table of contents** + +.. contents:: + :local: + +Usage +===== + +1. Go to *Sales > Products > Pricelists* and select one or create a new + one. +2. Add a new Price Rule or select an existing one. +3. Fill in the fields *Margin Cost Price Formula* and/or *Margin Sale + Price Formula*. +4. Create a new Sales Order and set the previously created pricelist. +5. You will see that the margin is calculated using the formulas from + the pricelist. + +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_margin_pricelist_computation/__init__.py b/sale_margin_pricelist_computation/__init__.py new file mode 100644 index 000000000..3275ac2ad --- /dev/null +++ b/sale_margin_pricelist_computation/__init__.py @@ -0,0 +1,2 @@ +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). +from . import models diff --git a/sale_margin_pricelist_computation/__manifest__.py b/sale_margin_pricelist_computation/__manifest__.py new file mode 100644 index 000000000..65578c0a8 --- /dev/null +++ b/sale_margin_pricelist_computation/__manifest__.py @@ -0,0 +1,15 @@ +# Copyright 2021 Tecnativa - Sergio Teruel +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). +{ + "name": "Sale Margin Pricelist Computation", + "summary": "Calculation of margins based on price lists", + "version": "18.0.1.0.0", + "category": "Sale", + "website": "https://github.com/OCA/margin-analysis", + "author": "Tecnativa, Odoo Community Association (OCA)", + "license": "AGPL-3", + "application": False, + "installable": True, + "depends": ["sale_margin"], + "data": ["views/product_pricelist_item_views.xml"], +} diff --git a/sale_margin_pricelist_computation/i18n/es.po b/sale_margin_pricelist_computation/i18n/es.po new file mode 100644 index 000000000..6e023b736 --- /dev/null +++ b/sale_margin_pricelist_computation/i18n/es.po @@ -0,0 +1,134 @@ +# Translation of Odoo Server. +# This file contains the translation of the following modules: +# * sale_margin_pricelist_computation +# +msgid "" +msgstr "" +"Project-Id-Version: Odoo Server 18.0\n" +"Report-Msgid-Bugs-To: \n" +"POT-Creation-Date: 2026-04-28 14:14+0000\n" +"PO-Revision-Date: 2026-04-28 16:16+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_margin_pricelist_computation +#: model_terms:ir.ui.view,arch_db:sale_margin_pricelist_computation.margin_product_pricelist_item_form_view +msgid "env: The Sale Order Line environment" +msgstr "env: El entorno de la línea de pedido de venta" + +#. module: sale_margin_pricelist_computation +#: model_terms:ir.ui.view,arch_db:sale_margin_pricelist_computation.margin_product_pricelist_item_form_view +msgid "" +"float_compare(): utility function to compare floats based on a " +"specific precision" +msgstr "" +"float_compare(): función para comparar números de tipo float en " +"función de una precisión específica" + +#. module: sale_margin_pricelist_computation +#: model_terms:ir.ui.view,arch_db:sale_margin_pricelist_computation.margin_product_pricelist_item_form_view +msgid "" +"line.product_uom_qty, line.qty_delivered: Ordered " +"and delivered quantities" +msgstr "" +"line.product_uom_qty, line.qty_delivered: " +"Cantidades pedidas y entregadas" + +#. module: sale_margin_pricelist_computation +#: model_terms:ir.ui.view,arch_db:sale_margin_pricelist_computation.margin_product_pricelist_item_form_view +msgid "line: Order Line (sale.order.line)" +msgstr "line: Línea de pedido de venta (sale.order.line)" + +#. module: sale_margin_pricelist_computation +#: model_terms:ir.ui.view,arch_db:sale_margin_pricelist_computation.margin_product_pricelist_item_form_view +msgid "pricelist: Current pricelist" +msgstr "pricelist: Lista de precios" + +#. module: sale_margin_pricelist_computation +#: model_terms:ir.ui.view,arch_db:sale_margin_pricelist_computation.margin_product_pricelist_item_form_view +msgid "pricelist_item: Current pricelist rule" +msgstr "item: Regla de precio de la linea" + +#. module: sale_margin_pricelist_computation +#: model_terms:ir.ui.view,arch_db:sale_margin_pricelist_computation.margin_product_pricelist_item_form_view +msgid "product.list_price: Product sale price" +msgstr "product.list_price: Precio de venta del producto" + +#. module: sale_margin_pricelist_computation +#: model_terms:ir.ui.view,arch_db:sale_margin_pricelist_computation.margin_product_pricelist_item_form_view +msgid "product.standard_price: Product cost" +msgstr "product.standard_price: Precio del producto" + +#. module: sale_margin_pricelist_computation +#: model_terms:ir.ui.view,arch_db:sale_margin_pricelist_computation.margin_product_pricelist_item_form_view +msgid "product: The Sale Order Line product" +msgstr "product: El producto de la línea de pedido de venta" + +#. module: sale_margin_pricelist_computation +#: model_terms:ir.ui.view,arch_db:sale_margin_pricelist_computation.margin_product_pricelist_item_form_view +msgid "Example of Python code:" +msgstr "Ejemplo de código en Python:" + +#. module: sale_margin_pricelist_computation +#: model:ir.model.fields,field_description:sale_margin_pricelist_computation.field_product_pricelist_item__margin_cost_price_formula +msgid "Margin Cost Price Formula" +msgstr "Margen del precio de coste" + +#. module: sale_margin_pricelist_computation +#: model:ir.model.fields,field_description:sale_margin_pricelist_computation.field_product_pricelist_item__margin_sale_price_formula +msgid "Margin Sale Price Formula" +msgstr "Margen del precio de pedido de venta " + +#. module: sale_margin_pricelist_computation +#: model_terms:ir.ui.view,arch_db:sale_margin_pricelist_computation.margin_product_pricelist_item_form_view +msgid "Margin's Formulas" +msgstr "Fórmulas de margen" + +#. module: sale_margin_pricelist_computation +#: model:ir.model,name:sale_margin_pricelist_computation.model_product_pricelist_item +msgid "Pricelist Rule" +msgstr "Regla de precio" + +#. module: sale_margin_pricelist_computation +#: model:ir.model,name:sale_margin_pricelist_computation.model_sale_order_line +msgid "Sales Order Line" +msgstr "Línea de pedido de venta" + +#. module: sale_margin_pricelist_computation +#: model_terms:ir.ui.view,arch_db:sale_margin_pricelist_computation.margin_product_pricelist_item_form_view +msgid "The following variables can be used:" +msgstr "Se pueden utilizar las siguientes variables:" + +#. module: sale_margin_pricelist_computation +#: model:ir.model.fields,help:sale_margin_pricelist_computation.field_product_pricelist_item__margin_cost_price_formula +msgid "" +"This formula is used to calculate the cost price for the purposes of " +"calculating the margin" +msgstr "" +"Esta fórmula se utiliza para calcular el precio de coste a efectos del " +"cálculo del margen" + +#. module: sale_margin_pricelist_computation +#: model:ir.model.fields,help:sale_margin_pricelist_computation.field_product_pricelist_item__margin_sale_price_formula +msgid "" +"This formula is used to calculate the sales order price for the purposes of " +"margin calculation" +msgstr "" +"Esta fórmula se utiliza para calcular el precio del pedido de venta a " +"efectos del cálculo del margen" + +#. module: sale_margin_pricelist_computation +#: model_terms:ir.ui.view,arch_db:sale_margin_pricelist_computation.margin_product_pricelist_item_form_view +msgid "To return a result, assign: result = {...}" +msgstr "Para devolver un resultado, asigna: result = {...}" + +#. module: sale_margin_pricelist_computation +#: model_terms:ir.ui.view,arch_db:sale_margin_pricelist_computation.margin_product_pricelist_item_form_view +msgid "result = 20 if line.price_unit > 30 else 10" +msgstr "" diff --git a/sale_margin_pricelist_computation/i18n/sale_margin_pricelist_computation.pot b/sale_margin_pricelist_computation/i18n/sale_margin_pricelist_computation.pot new file mode 100644 index 000000000..bfa5bfb73 --- /dev/null +++ b/sale_margin_pricelist_computation/i18n/sale_margin_pricelist_computation.pot @@ -0,0 +1,124 @@ +# Translation of Odoo Server. +# This file contains the translation of the following modules: +# * sale_margin_pricelist_computation +# +msgid "" +msgstr "" +"Project-Id-Version: Odoo Server 18.0\n" +"Report-Msgid-Bugs-To: \n" +"POT-Creation-Date: 2026-04-28 14:14+0000\n" +"PO-Revision-Date: 2026-04-28 14:14+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_margin_pricelist_computation +#: model_terms:ir.ui.view,arch_db:sale_margin_pricelist_computation.margin_product_pricelist_item_form_view +msgid "env: The Sale Order Line environment" +msgstr "" + +#. module: sale_margin_pricelist_computation +#: model_terms:ir.ui.view,arch_db:sale_margin_pricelist_computation.margin_product_pricelist_item_form_view +msgid "" +"float_compare(): utility function to compare floats based on a " +"specific precision" +msgstr "" + +#. module: sale_margin_pricelist_computation +#: model_terms:ir.ui.view,arch_db:sale_margin_pricelist_computation.margin_product_pricelist_item_form_view +msgid "" +"line.product_uom_qty, line.qty_delivered: Ordered " +"and delivered quantities" +msgstr "" + +#. module: sale_margin_pricelist_computation +#: model_terms:ir.ui.view,arch_db:sale_margin_pricelist_computation.margin_product_pricelist_item_form_view +msgid "line: Order Line (sale.order.line)" +msgstr "" + +#. module: sale_margin_pricelist_computation +#: model_terms:ir.ui.view,arch_db:sale_margin_pricelist_computation.margin_product_pricelist_item_form_view +msgid "pricelist: Current pricelist" +msgstr "" + +#. module: sale_margin_pricelist_computation +#: model_terms:ir.ui.view,arch_db:sale_margin_pricelist_computation.margin_product_pricelist_item_form_view +msgid "pricelist_item: Current pricelist rule" +msgstr "" + +#. module: sale_margin_pricelist_computation +#: model_terms:ir.ui.view,arch_db:sale_margin_pricelist_computation.margin_product_pricelist_item_form_view +msgid "product.list_price: Product sale price" +msgstr "" + +#. module: sale_margin_pricelist_computation +#: model_terms:ir.ui.view,arch_db:sale_margin_pricelist_computation.margin_product_pricelist_item_form_view +msgid "product.standard_price: Product cost" +msgstr "" + +#. module: sale_margin_pricelist_computation +#: model_terms:ir.ui.view,arch_db:sale_margin_pricelist_computation.margin_product_pricelist_item_form_view +msgid "product: The Sale Order Line product" +msgstr "" + +#. module: sale_margin_pricelist_computation +#: model_terms:ir.ui.view,arch_db:sale_margin_pricelist_computation.margin_product_pricelist_item_form_view +msgid "Example of Python code:" +msgstr "" + +#. module: sale_margin_pricelist_computation +#: model:ir.model.fields,field_description:sale_margin_pricelist_computation.field_product_pricelist_item__margin_cost_price_formula +msgid "Margin Cost Price Formula" +msgstr "" + +#. module: sale_margin_pricelist_computation +#: model:ir.model.fields,field_description:sale_margin_pricelist_computation.field_product_pricelist_item__margin_sale_price_formula +msgid "Margin Sale Price Formula" +msgstr "" + +#. module: sale_margin_pricelist_computation +#: model_terms:ir.ui.view,arch_db:sale_margin_pricelist_computation.margin_product_pricelist_item_form_view +msgid "Margin's Formulas" +msgstr "" + +#. module: sale_margin_pricelist_computation +#: model:ir.model,name:sale_margin_pricelist_computation.model_product_pricelist_item +msgid "Pricelist Rule" +msgstr "" + +#. module: sale_margin_pricelist_computation +#: model:ir.model,name:sale_margin_pricelist_computation.model_sale_order_line +msgid "Sales Order Line" +msgstr "" + +#. module: sale_margin_pricelist_computation +#: model_terms:ir.ui.view,arch_db:sale_margin_pricelist_computation.margin_product_pricelist_item_form_view +msgid "The following variables can be used:" +msgstr "" + +#. module: sale_margin_pricelist_computation +#: model:ir.model.fields,help:sale_margin_pricelist_computation.field_product_pricelist_item__margin_cost_price_formula +msgid "" +"This formula is used to calculate the cost price for the purposes of " +"calculating the margin" +msgstr "" + +#. module: sale_margin_pricelist_computation +#: model:ir.model.fields,help:sale_margin_pricelist_computation.field_product_pricelist_item__margin_sale_price_formula +msgid "" +"This formula is used to calculate the sales order price for the purposes of " +"margin calculation" +msgstr "" + +#. module: sale_margin_pricelist_computation +#: model_terms:ir.ui.view,arch_db:sale_margin_pricelist_computation.margin_product_pricelist_item_form_view +msgid "To return a result, assign: result = {...}" +msgstr "" + +#. module: sale_margin_pricelist_computation +#: model_terms:ir.ui.view,arch_db:sale_margin_pricelist_computation.margin_product_pricelist_item_form_view +msgid "result = 20 if line.price_unit > 30 else 10" +msgstr "" diff --git a/sale_margin_pricelist_computation/models/__init__.py b/sale_margin_pricelist_computation/models/__init__.py new file mode 100644 index 000000000..7372b1353 --- /dev/null +++ b/sale_margin_pricelist_computation/models/__init__.py @@ -0,0 +1,2 @@ +from . import product_pricelist_item +from . import sale_order_line diff --git a/sale_margin_pricelist_computation/models/product_pricelist_item.py b/sale_margin_pricelist_computation/models/product_pricelist_item.py new file mode 100644 index 000000000..bc35cff5a --- /dev/null +++ b/sale_margin_pricelist_computation/models/product_pricelist_item.py @@ -0,0 +1,85 @@ +from odoo import Command, api, fields, models +from odoo.exceptions import ValidationError +from odoo.tools.float_utils import float_compare +from odoo.tools.safe_eval import safe_eval + + +class PricelistItem(models.Model): + _inherit = "product.pricelist.item" + + margin_cost_price_formula = fields.Text( + help="This formula is used to calculate the cost price " + "for the purposes of calculating the margin" + ) + margin_sale_price_formula = fields.Text( + help="This formula is used to calculate the sales order price " + "for the purposes of margin calculation" + ) + + @api.constrains("margin_sale_price_formula", "margin_cost_price_formula") + def _check_margin_formula(self): + for rec in self: + if not rec.margin_cost_price_formula and rec.margin_cost_price_formula: + continue + if rec.margin_sale_price_formula: + self._eval_python_code_margin_formula(rec.margin_sale_price_formula) + if rec.margin_cost_price_formula: + self._eval_python_code_margin_formula(rec.margin_cost_price_formula) + + def _eval_python_code_margin_formula(self, test_string): + 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, + } + ) + ], + } + ) + pricelist = self.env["product.pricelist"].new( + { + "name": "Test Pricelist", + } + ) + pricelist_item = self.env["product.pricelist.item"].new( + { + "pricelist_id": pricelist.id, + "applied_on": "3_global", + "base": "list_price", + "compute_price": "percentage", + "percent_price": 10, + } + ) + test_eval_context = { + "env": self.env, + "context": self.env.context, + "user": self.env.user, + "line": order.order_line, + "pricelist": pricelist, + "pricelist_item": pricelist_item, + "product": main_product, + "float_compare": float_compare, + } + try: + safe_eval( + str(test_string).strip(), + test_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 diff --git a/sale_margin_pricelist_computation/models/sale_order_line.py b/sale_margin_pricelist_computation/models/sale_order_line.py new file mode 100644 index 000000000..270a03ea8 --- /dev/null +++ b/sale_margin_pricelist_computation/models/sale_order_line.py @@ -0,0 +1,69 @@ +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("pricelist_item_id", "price_unit") + def _compute_purchase_price(self): + for line in self.filtered( + lambda line: line.pricelist_item_id.margin_cost_price_formula + ): + if not line.product_id: + line.purchase_price = 0.0 + continue + line = line.with_company(line.company_id) + eval_context = line._get_margin_pricelist_eval_context() + safe_eval( + str(line.pricelist_item_id.margin_cost_price_formula).strip(), + eval_context, + mode="exec", + nocopy=True, + ) + line.purchase_price = line._convert_to_sol_currency( + eval_context.get("result", 0), line.product_id.cost_currency_id + ) + self = self.filtered( + lambda line: not line.pricelist_item_id.margin_cost_price_formula + ) + return super()._compute_purchase_price() + + @api.depends("pricelist_item_id") + def _compute_margin(self): + self_formula = self.filtered( + lambda line: line.pricelist_item_id.margin_sale_price_formula + ) + for line in self_formula: + eval_context = line._get_margin_pricelist_eval_context() + safe_eval( + str(line.pricelist_item_id.margin_sale_price_formula).strip(), + eval_context, + mode="exec", + nocopy=True, + ) + quantity = ( + line.qty_delivered + if line.qty_delivered and not line.product_uom_qty + else line.product_uom_qty + ) + calculated_subtotal = eval_context.get("result", 0) * quantity + line.margin = calculated_subtotal - (line.purchase_price * quantity) + line.margin_percent = ( + calculated_subtotal and line.margin / calculated_subtotal + ) + self -= self_formula + return super()._compute_margin() + + def _get_margin_pricelist_eval_context(self): + return { + "env": self.env, + "context": self.env.context, + "user": self.env.user, + "line": self, + "pricelist": self.pricelist_item_id.pricelist_id, + "pricelist_item": self.pricelist_item_id, + "product": self.product_id, + "float_compare": float_compare, + } diff --git a/sale_margin_pricelist_computation/pyproject.toml b/sale_margin_pricelist_computation/pyproject.toml new file mode 100644 index 000000000..4231d0ccc --- /dev/null +++ b/sale_margin_pricelist_computation/pyproject.toml @@ -0,0 +1,3 @@ +[build-system] +requires = ["whool"] +build-backend = "whool.buildapi" diff --git a/sale_margin_pricelist_computation/readme/CONTRIBUTORS.md b/sale_margin_pricelist_computation/readme/CONTRIBUTORS.md new file mode 100644 index 000000000..71701213b --- /dev/null +++ b/sale_margin_pricelist_computation/readme/CONTRIBUTORS.md @@ -0,0 +1,3 @@ +- [Tecnativa](https://www.tecnativa.com): + - Pedro M. Baeza + - Andrii Kompaniiets diff --git a/sale_margin_pricelist_computation/readme/DESCRIPTION.md b/sale_margin_pricelist_computation/readme/DESCRIPTION.md new file mode 100644 index 000000000..87ed80ca2 --- /dev/null +++ b/sale_margin_pricelist_computation/readme/DESCRIPTION.md @@ -0,0 +1 @@ +This module allows the use of formulas to calculate the margin of sale order lines. \ No newline at end of file diff --git a/sale_margin_pricelist_computation/readme/USAGE.md b/sale_margin_pricelist_computation/readme/USAGE.md new file mode 100644 index 000000000..8eb51a292 --- /dev/null +++ b/sale_margin_pricelist_computation/readme/USAGE.md @@ -0,0 +1,8 @@ +1. Go to *Sales > Products > Pricelists* and select one or create a new + one. +1. Add a new Price Rule or select an existing one. +1. Fill in the fields *Margin Cost Price Formula* and/or *Margin Sale + Price Formula*. +1. Create a new Sales Order and set the previously created pricelist. +1. You will see that the margin is calculated using the formulas from + the pricelist. \ No newline at end of file diff --git a/sale_margin_pricelist_computation/static/description/index.html b/sale_margin_pricelist_computation/static/description/index.html new file mode 100644 index 000000000..27a9e9a4b --- /dev/null +++ b/sale_margin_pricelist_computation/static/description/index.html @@ -0,0 +1,442 @@ + + + + + +Sale Margin Pricelist Computation + + + +
+

Sale Margin Pricelist Computation

+ + +

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

+

This module allows the use of formulas to calculate the margin of sale +order lines.

+

Table of contents

+ +
+

Usage

+
    +
  1. Go to Sales > Products > Pricelists and select one or create a new +one.
  2. +
  3. Add a new Price Rule or select an existing one.
  4. +
  5. Fill in the fields Margin Cost Price Formula and/or Margin Sale +Price Formula.
  6. +
  7. Create a new Sales Order and set the previously created pricelist.
  8. +
  9. You will see that the margin is calculated using the formulas from +the pricelist.
  10. +
+
+
+

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_margin_pricelist_computation/tests/__init__.py b/sale_margin_pricelist_computation/tests/__init__.py new file mode 100644 index 000000000..695920439 --- /dev/null +++ b/sale_margin_pricelist_computation/tests/__init__.py @@ -0,0 +1 @@ +from . import test_sale_margin_pricelist_computation diff --git a/sale_margin_pricelist_computation/tests/test_sale_margin_pricelist_computation.py b/sale_margin_pricelist_computation/tests/test_sale_margin_pricelist_computation.py new file mode 100644 index 000000000..de38826e0 --- /dev/null +++ b/sale_margin_pricelist_computation/tests/test_sale_margin_pricelist_computation.py @@ -0,0 +1,60 @@ +from odoo.fields import Command + +from odoo.addons.sale_margin.tests.test_sale_margin import TestSaleMargin + + +class TestSaleMarginPricelistComputation(TestSaleMargin): + @classmethod + def setUpClass(cls): + super().setUpClass() + cls.product.write({"list_price": 20.0, "standard_price": 10.0}) + cls.margin_pricelist = cls.env["product.pricelist"].create( + { + "name": "Margin Pricelist", + "item_ids": [ + Command.create( + { + "name": "Test Margin", + "applied_on": "1_product", + "product_tmpl_id": cls.product.product_tmpl_id.id, + "compute_price": "fixed", + "margin_sale_price_formula": "result=line.price_unit", + } + ), + ], + } + ) + + def _create_order(self, qty=10): + order = self.env["sale.order"].create( + { + "partner_id": self.partner.id, + "pricelist_id": self.margin_pricelist.id, + "order_line": [ + Command.create( + { + "price_unit": 30.0, + "product_uom_qty": qty, + "product_id": self.product.id, + } + ), + ], + } + ) + return order + + def test_pricelist_margin_with_formulas(self): + order = self._create_order() + self.assertEqual(order.margin, 200.00) + self.assertAlmostEqual(order.margin_percent, 0.66, delta=0.01) + pricelist_rule = self.margin_pricelist.item_ids[0] + pricelist_rule.margin_cost_price_formula = ( + "result=product.list_price/4 if " + "line.product_uom_qty < 15 else product.list_price/6" + ) + order = self._create_order() + self.assertEqual(order.margin, 250.00) + self.assertAlmostEqual(order.margin_percent, 0.83, delta=0.01) + order = self._create_order(20) + self.assertAlmostEqual(order.margin, 533.4, delta=0.1) + self.assertAlmostEqual(order.margin_percent, 0.89, delta=0.1) diff --git a/sale_margin_pricelist_computation/views/product_pricelist_item_views.xml b/sale_margin_pricelist_computation/views/product_pricelist_item_views.xml new file mode 100644 index 000000000..92be6d44e --- /dev/null +++ b/sale_margin_pricelist_computation/views/product_pricelist_item_views.xml @@ -0,0 +1,56 @@ + + + + product.pricelist.item + + + + + + + + + + + + + + + +