diff --git a/product_margin_report_sale_margin/README.rst b/product_margin_report_sale_margin/README.rst new file mode 100644 index 00000000..9bd83199 --- /dev/null +++ b/product_margin_report_sale_margin/README.rst @@ -0,0 +1,108 @@ +========================== +Product report sale margin +========================== + +.. + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! This file is generated by oca-gen-addon-readme !! + !! changes will be overwritten. !! + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! source digest: sha256:5b02242f5dbe90213a92d0d2632daa7f4c6b7553a8057302a5ab4f1f79657f90 + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + +.. |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/17.0/product_margin_report_sale_margin + :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-17-0/margin-analysis-17-0-product_margin_report_sale_margin + :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=17.0 + :alt: Try me on Runboat + +|badge1| |badge2| |badge3| |badge4| |badge5| + +This module adds 2 new fields for calculating the Sales margin. + +**Table of contents** + +.. contents:: + :local: + +Usage +===== + +Sales margin fields +------------------- + +1. Go to *Invoicing* > *Reporting* -> Product Margins + +2. Select the values you want to filter by. + +3. Click on the *Open margins* button. + +4. When the new view is displayed, the two new fields are visible; + similarly, if you select an element, they will be visible in the form + view. + + |FIELDS_VIEW_TREE| + + |FIELDS_VIEW_FORM| + +Field calculations +------------------ + +- Sales margin = Turnover - (# Invoiced in Sale \* Avg. Unit Price + [Purchases]) +- Sales margin rate = (Sales margin - \* 100) / Turnover + +.. |FIELDS_VIEW_TREE| image:: https://raw.githubusercontent.com/OCA/margin-analysis/17.0/product_margin_report_sale_margin/static/img/readme/FIELDS_VIEW_TREE.png +.. |FIELDS_VIEW_FORM| image:: https://raw.githubusercontent.com/OCA/margin-analysis/17.0/product_margin_report_sale_margin/static/img/readme/FIELDS_VIEW_FORM.png + +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 +------- + +* Binhex + +Contributors +------------ + +- [Binhex Cloud] (https://www.binhex.cloud): + + - Edilio Escalona Almira e.escalona@binhex.cloud + +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/product_margin_report_sale_margin/__init__.py b/product_margin_report_sale_margin/__init__.py new file mode 100644 index 00000000..0077c886 --- /dev/null +++ b/product_margin_report_sale_margin/__init__.py @@ -0,0 +1,4 @@ +# Copyright 2025 Binhex +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + +from . import models diff --git a/product_margin_report_sale_margin/__manifest__.py b/product_margin_report_sale_margin/__manifest__.py new file mode 100644 index 00000000..8312de72 --- /dev/null +++ b/product_margin_report_sale_margin/__manifest__.py @@ -0,0 +1,15 @@ +# Copyright 2025 Binhex +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + +{ + "name": "Product report sale margin", + "summary": """Add new fields for sales margin in the Product Margins report.""", + "version": "17.0.1.0.0", + "license": "AGPL-3", + "author": "Binhex,Odoo Community Association (OCA)", + "website": "https://github.com/OCA/margin-analysis", + "depends": [ + "product_margin", + ], + "data": ["views/product_product_views.xml"], +} diff --git a/product_margin_report_sale_margin/models/__init__.py b/product_margin_report_sale_margin/models/__init__.py new file mode 100644 index 00000000..57112ab7 --- /dev/null +++ b/product_margin_report_sale_margin/models/__init__.py @@ -0,0 +1,5 @@ +# Copyright 2025 Binhex +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + + +from . import product_product diff --git a/product_margin_report_sale_margin/models/product_product.py b/product_margin_report_sale_margin/models/product_product.py new file mode 100644 index 00000000..c32baeda --- /dev/null +++ b/product_margin_report_sale_margin/models/product_product.py @@ -0,0 +1,25 @@ +# Copyright 2025 Binhex +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + +from odoo import fields, models + + +class ProductProduct(models.Model): + _inherit = "product.product" + + sales_margin = fields.Float(compute="_compute_product_margin_fields_values") + sales_margin_rate = fields.Float(compute="_compute_product_margin_fields_values") + + def _compute_product_margin_fields_values(self): + res = super()._compute_product_margin_fields_values() + for product in self: + product_values = res[product.id] + product_values["sales_margin"] = product_values["turnover"] - ( + product_values["sale_num_invoiced"] + * product_values["purchase_avg_price"] + ) + product_values["sales_margin_rate"] = ( + product_values["sales_margin"] * 100 + ) / (product_values["turnover"] or 1) + product.update(res[product.id]) + return res diff --git a/product_margin_report_sale_margin/pyproject.toml b/product_margin_report_sale_margin/pyproject.toml new file mode 100644 index 00000000..4231d0cc --- /dev/null +++ b/product_margin_report_sale_margin/pyproject.toml @@ -0,0 +1,3 @@ +[build-system] +requires = ["whool"] +build-backend = "whool.buildapi" diff --git a/product_margin_report_sale_margin/readme/CONTRIBUTORS.md b/product_margin_report_sale_margin/readme/CONTRIBUTORS.md new file mode 100644 index 00000000..0bfe91dd --- /dev/null +++ b/product_margin_report_sale_margin/readme/CONTRIBUTORS.md @@ -0,0 +1,2 @@ +- [Binhex Cloud] (https://www.binhex.cloud): + - Edilio Escalona Almira diff --git a/product_margin_report_sale_margin/readme/DESCRIPTION.md b/product_margin_report_sale_margin/readme/DESCRIPTION.md new file mode 100644 index 00000000..d99fbcab --- /dev/null +++ b/product_margin_report_sale_margin/readme/DESCRIPTION.md @@ -0,0 +1 @@ +This module adds 2 new fields for calculating the Sales margin. diff --git a/product_margin_report_sale_margin/readme/USAGE.md b/product_margin_report_sale_margin/readme/USAGE.md new file mode 100644 index 00000000..153ea96d --- /dev/null +++ b/product_margin_report_sale_margin/readme/USAGE.md @@ -0,0 +1,19 @@ +Sales margin fields +------------------ + +1. Go to *Invoicing* > *Reporting* -> Product Margins +2. Select the values you want to filter by. +3. Click on the *Open margins* button. +4. When the new view is displayed, the two new fields are visible; similarly, + if you select an element, they will be visible in the form view. + + ![FIELDS_VIEW_TREE](../static/img/readme/FIELDS_VIEW_TREE.png) + + ![FIELDS_VIEW_FORM](../static/img/readme/FIELDS_VIEW_FORM.png) + + +Field calculations +------------------ +* Sales margin = Turnover - (# Invoiced in Sale * Avg. Unit Price [Purchases]) +* Sales margin rate = (Sales margin - * 100) / Turnover + diff --git a/product_margin_report_sale_margin/static/description/index.html b/product_margin_report_sale_margin/static/description/index.html new file mode 100644 index 00000000..eada6201 --- /dev/null +++ b/product_margin_report_sale_margin/static/description/index.html @@ -0,0 +1,459 @@ + + + + + +Product report sale margin + + + +
+

Product report sale margin

+ + +

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

+

This module adds 2 new fields for calculating the Sales margin.

+

Table of contents

+ +
+

Usage

+
+

Sales margin fields

+
    +
  1. Go to Invoicing > Reporting -> Product Margins

    +
  2. +
  3. Select the values you want to filter by.

    +
  4. +
  5. Click on the Open margins button.

    +
  6. +
  7. When the new view is displayed, the two new fields are visible; +similarly, if you select an element, they will be visible in the form +view.

    +

    FIELDS_VIEW_TREE

    +

    FIELDS_VIEW_FORM

    +
  8. +
+
+
+

Field calculations

+
    +
  • Sales margin = Turnover - (# Invoiced in Sale * Avg. Unit Price +[Purchases])
  • +
  • Sales margin rate = (Sales margin - * 100) / Turnover
  • +
+
+
+
+

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

+
    +
  • Binhex
  • +
+
+
+

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

+

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

+
+
+
+ + diff --git a/product_margin_report_sale_margin/static/img/readme/FIELDS_VIEW_FORM.png b/product_margin_report_sale_margin/static/img/readme/FIELDS_VIEW_FORM.png new file mode 100644 index 00000000..1d7094b7 Binary files /dev/null and b/product_margin_report_sale_margin/static/img/readme/FIELDS_VIEW_FORM.png differ diff --git a/product_margin_report_sale_margin/static/img/readme/FIELDS_VIEW_TREE.png b/product_margin_report_sale_margin/static/img/readme/FIELDS_VIEW_TREE.png new file mode 100644 index 00000000..6c7c8ddc Binary files /dev/null and b/product_margin_report_sale_margin/static/img/readme/FIELDS_VIEW_TREE.png differ diff --git a/product_margin_report_sale_margin/tests/__init__.py b/product_margin_report_sale_margin/tests/__init__.py new file mode 100644 index 00000000..487088eb --- /dev/null +++ b/product_margin_report_sale_margin/tests/__init__.py @@ -0,0 +1,6 @@ +# Copyright 2025 Binhex +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + + +from . import test_common +from . import test_product diff --git a/product_margin_report_sale_margin/tests/test_common.py b/product_margin_report_sale_margin/tests/test_common.py new file mode 100644 index 00000000..dd86b7f9 --- /dev/null +++ b/product_margin_report_sale_margin/tests/test_common.py @@ -0,0 +1,11 @@ +# Copyright 2025 Binhex +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + +from odoo.tests import TransactionCase + + +class TestReportProductMarginCommon(TransactionCase): + @classmethod + def setUpClass(cls): + super().setUpClass() + cls.Product = cls.env["product.product"] diff --git a/product_margin_report_sale_margin/tests/test_product.py b/product_margin_report_sale_margin/tests/test_product.py new file mode 100644 index 00000000..34b36c1c --- /dev/null +++ b/product_margin_report_sale_margin/tests/test_product.py @@ -0,0 +1,43 @@ +# Copyright 2025 Binhex +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + +from unittest.mock import patch + +from odoo.addons.product_margin.models.product_product import ProductProduct + +from .test_common import TestReportProductMarginCommon + + +class TestReportProductMargin(TestReportProductMarginCommon): + @classmethod + def setUpClass(cls): + super().setUpClass() + cls.product = cls.Product.create( + { + "name": "Test Product", + } + ) + + def test_compute_product_margin_fields_values(self): + mocked_super_return = { + self.product.id: { + "turnover": 1000.0, + "sale_num_invoiced": 10.0, + "purchase_avg_price": 50.0, + } + } + + with patch.object( + ProductProduct, + "_compute_product_margin_fields_values", + return_value=mocked_super_return, + ): + result = self.product._compute_product_margin_fields_values() + + sales_margin = 1000.0 - (10.0 * 50.0) + sales_margin_rate = (sales_margin * 100) / 1000.0 + + self.assertEqual(result[self.product.id]["sales_margin"], sales_margin) + self.assertEqual( + result[self.product.id]["sales_margin_rate"], sales_margin_rate + ) diff --git a/product_margin_report_sale_margin/views/product_product_views.xml b/product_margin_report_sale_margin/views/product_product_views.xml new file mode 100644 index 00000000..96d129ae --- /dev/null +++ b/product_margin_report_sale_margin/views/product_product_views.xml @@ -0,0 +1,39 @@ + + + + + product.margin.form.inherit.product_margin_report_sale_margin + product.product + + + + + + + + + + + + + product.margin.tree.product_margin_report_sale_margin + product.product + + + + + + + + + + +