diff --git a/sale_report_margin/README.rst b/sale_report_margin/README.rst index 52c56a659..142d37de9 100644 --- a/sale_report_margin/README.rst +++ b/sale_report_margin/README.rst @@ -32,9 +32,15 @@ Sale Report Margin |badge1| |badge2| |badge3| |badge4| |badge5| -Adds purchase_price field to sale report. This field comes from the -*Cost* field configured in the products themselves and on the defined -inventory valuation method. +Adds margin information to the sales analysis report: + +- ``Purchase Price``: the unit cost of the sold products, in the company + currency. It comes from the *Cost* field configured in the products + themselves and on the defined inventory valuation method. +- ``Margin (%)``: the margin over the untaxed total. + +The purchase price, margin and margin percentage are also shown as +columns in the list view of the report. **Table of contents** @@ -45,7 +51,10 @@ Usage ===== Go to *Sales > Reporting > Sales* and you'll find the new *Purchase -Price* measure. +Price* and *Margin (%)* measures. + +In the list view, the *Purchase Price* column is displayed next to the +unit price, and the *Margin* and *Margin (%)* columns next to the total. Known issues / Roadmap ====================== @@ -81,6 +90,10 @@ Contributors - SodexisTeam +- `Camptocamp `__: + + - Iván Todorovich + Maintainers ----------- diff --git a/sale_report_margin/__manifest__.py b/sale_report_margin/__manifest__.py index e4ece00ea..845b1bfa6 100644 --- a/sale_report_margin/__manifest__.py +++ b/sale_report_margin/__manifest__.py @@ -10,5 +10,6 @@ "category": "Sales", "license": "AGPL-3", "depends": ["sale_margin"], + "data": ["views/sale_report_views.xml"], "installable": True, } diff --git a/sale_report_margin/readme/CONTRIBUTORS.md b/sale_report_margin/readme/CONTRIBUTORS.md index 8736c3949..f8bbd6168 100644 --- a/sale_report_margin/readme/CONTRIBUTORS.md +++ b/sale_report_margin/readme/CONTRIBUTORS.md @@ -4,4 +4,8 @@ > - David Vidal > - Carlos Roca -- SodexisTeam \<\> \ No newline at end of file +- SodexisTeam \<\> + +- [Camptocamp](https://www.camptocamp.com): + + > - Iván Todorovich \<\> diff --git a/sale_report_margin/readme/DESCRIPTION.md b/sale_report_margin/readme/DESCRIPTION.md index d1112d818..2956b587d 100644 --- a/sale_report_margin/readme/DESCRIPTION.md +++ b/sale_report_margin/readme/DESCRIPTION.md @@ -1,3 +1,9 @@ -Adds purchase_price field to sale report. This field comes from the -*Cost* field configured in the products themselves and on the defined -inventory valuation method. +Adds margin information to the sales analysis report: + +- `Purchase Price`: the unit cost of the sold products, in the company + currency. It comes from the *Cost* field configured in the products + themselves and on the defined inventory valuation method. +- `Margin (%)`: the margin over the untaxed total. + +The purchase price, margin and margin percentage are also shown as columns +in the list view of the report. diff --git a/sale_report_margin/readme/USAGE.md b/sale_report_margin/readme/USAGE.md index 833ee81d6..99fd291f3 100644 --- a/sale_report_margin/readme/USAGE.md +++ b/sale_report_margin/readme/USAGE.md @@ -1,2 +1,5 @@ Go to *Sales \> Reporting \> Sales* and you'll find the new *Purchase -Price* measure. +Price* and *Margin (%)* measures. + +In the list view, the *Purchase Price* column is displayed next to the +unit price, and the *Margin* and *Margin (%)* columns next to the total. diff --git a/sale_report_margin/reports/sale_report.py b/sale_report_margin/reports/sale_report.py index be3f576e6..a42449a60 100644 --- a/sale_report_margin/reports/sale_report.py +++ b/sale_report_margin/reports/sale_report.py @@ -1,4 +1,5 @@ # Copyright 2018 Tecnativa - Sergio Teruel +# Copyright 2026 Camptocamp SA (https://www.camptocamp.com). # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). from odoo import fields, models @@ -7,9 +8,19 @@ class SaleReport(models.Model): _inherit = "sale.report" - purchase_price = fields.Float(readonly=True) + purchase_price = fields.Float(readonly=True, aggregator="avg") + # Grouped rows average the per-row ratios, like ``sale.order.margin_percent``. + margin_percent = fields.Float(string="Margin (%)", readonly=True, aggregator="avg") def _select_additional_fields(self): res = super()._select_additional_fields() - res["purchase_price"] = "SUM(l.purchase_price)" + # Unit cost, converted to the company currency like ``price_unit``. + res["purchase_price"] = f"""AVG(l.purchase_price + / {self._case_value_or_one("s.currency_rate")} + * {self._case_value_or_one("account_currency_table.rate")})""" + # Ratio of the grouped lines' margin over their untaxed total. The currency + # conversion applied to both amounts in the report cancels out. + res["margin_percent"] = """ + CASE WHEN SUM(l.price_subtotal) = 0 THEN 0 + ELSE SUM(l.margin) / SUM(l.price_subtotal) END""" return res diff --git a/sale_report_margin/static/description/index.html b/sale_report_margin/static/description/index.html index d84f66ba8..7a108e136 100644 --- a/sale_report_margin/static/description/index.html +++ b/sale_report_margin/static/description/index.html @@ -375,9 +375,15 @@

Sale Report Margin

!! source digest: sha256:3ba588ce7e07b01b5bc3c5c65dcb27d213765f309a80e4222d264c3b85fccef6 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! -->

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

-

Adds purchase_price field to sale report. This field comes from the -Cost field configured in the products themselves and on the defined -inventory valuation method.

+

Adds margin information to the sales analysis report:

+
    +
  • Purchase Price: the unit cost of the sold products, in the company +currency. It comes from the Cost field configured in the products +themselves and on the defined inventory valuation method.
  • +
  • Margin (%): the margin over the untaxed total.
  • +
+

The purchase price, margin and margin percentage are also shown as +columns in the list view of the report.

Table of contents

diff --git a/sale_report_margin/tests/test_sale_report_margin.py b/sale_report_margin/tests/test_sale_report_margin.py index ca00ba9a3..2accd5bdb 100644 --- a/sale_report_margin/tests/test_sale_report_margin.py +++ b/sale_report_margin/tests/test_sale_report_margin.py @@ -1,6 +1,7 @@ # Copyright 2023 Álvaro Marcos # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). +from odoo import fields from odoo.orm.commands import Command from odoo.addons.base.tests.common import BaseCommon @@ -15,7 +16,14 @@ def setUpClass(cls): ) def test_sale_report_margin(self): - """Check purchase_price in sale report""" + """Check the purchase price and margin of a line in the sale report. + + Scenario: + 1. Confirm an order with one line sold at 10 with a cost of 8. + Expected: + - The report row shows a purchase price of 8. + - The report row shows a margin of 2, that is 20% of the untaxed total. + """ order = self.env["sale.order"].create( { "name": "Test Order", @@ -40,3 +48,127 @@ def test_sale_report_margin(self): ] ) self.assertEqual(report.purchase_price, 8.0) + self.assertEqual(report.margin, 2.0) + self.assertEqual(report.margin_percent, 0.2) + + def test_sale_report_margin_lines_in_one_row(self): + """Lines merged in one report row show their average unit cost. + + Scenario: + 1. Confirm an order with two lines of the same product, both sold + at 10, one with a cost of 2 and the other with a cost of 4. + Expected: + - Both lines fall into a single report row. + - The row shows a purchase price of 3, not the sum of both costs. + - The row shows a margin of 14, that is 70% of the untaxed total. + """ + order = self.env["sale.order"].create( + { + "name": "Test Order", + "partner_id": self.partner.id, + "order_line": [ + Command.create( + { + "product_id": self.product_product.id, + "price_unit": 10.0, + "product_uom_qty": 1.0, + "purchase_price": cost, + }, + ) + for cost in (2.0, 4.0) + ], + } + ) + order.action_confirm() + report = self.env["sale.report"].search( + [ + ("order_reference", "=", f"{order._name},{order.id}"), + ("product_id", "=", self.product_product.id), + ] + ) + self.assertEqual(len(report), 1) + self.assertEqual(report.purchase_price, 3.0) + self.assertEqual(report.margin, 14.0) + self.assertEqual(report.margin_percent, 0.7) + + def test_sale_report_margin_percent_no_subtotal(self): + """A free line has no margin percentage instead of a division error. + + Scenario: + 1. Confirm an order with one line sold at 0 with a cost of 8. + Expected: + - The report row shows a margin of -8 and a margin percentage of 0. + """ + order = self.env["sale.order"].create( + { + "name": "Test Order", + "partner_id": self.partner.id, + "order_line": [ + Command.create( + { + "product_id": self.product_product.id, + "price_unit": 0.0, + "product_uom_qty": 1.0, + "purchase_price": 8.0, + }, + ) + ], + } + ) + order.action_confirm() + report = self.env["sale.report"].search( + [("order_reference", "=", f"{order._name},{order.id}")] + ) + self.assertEqual(report.margin, -8.0) + self.assertEqual(report.margin_percent, 0.0) + + def test_sale_report_margin_other_currency(self): + """Amounts of an order in another currency are reported in the company one. + + Scenario: + 1. Set up a currency worth half of the company currency. + 2. Confirm an order in that currency with one line sold at 10 with + a cost of 8. + Expected: + - The report row shows a unit price of 5, a purchase price of 4 and + a margin of 1, all in the company currency. + - The margin percentage is still 20%. + """ + currency = self.quick_ref("base.EUR") + currency.active = True + self.env["res.currency.rate"].create( + { + "currency_id": currency.id, + "company_id": self.env.company.id, + "name": fields.Date.today(), + "rate": 2.0, + } + ) + pricelist = self.env["product.pricelist"].create( + {"name": "Test Pricelist", "currency_id": currency.id} + ) + order = self.env["sale.order"].create( + { + "name": "Test Order", + "partner_id": self.partner.id, + "pricelist_id": pricelist.id, + "order_line": [ + Command.create( + { + "product_id": self.product_product.id, + "price_unit": 10.0, + "product_uom_qty": 1.0, + "purchase_price": 8.0, + }, + ) + ], + } + ) + order.action_confirm() + report = self.env["sale.report"].search( + [("order_reference", "=", f"{order._name},{order.id}")] + ) + self.assertEqual(report.price_unit, 5.0) + self.assertEqual(report.purchase_price, 4.0) + self.assertEqual(report.margin, 1.0) + self.assertEqual(report.margin_percent, 0.2) diff --git a/sale_report_margin/views/sale_report_views.xml b/sale_report_margin/views/sale_report_views.xml new file mode 100644 index 000000000..6f8cb5e59 --- /dev/null +++ b/sale_report_margin/views/sale_report_views.xml @@ -0,0 +1,25 @@ + + + + + sale.report + + + + + + + + + + + + diff --git a/sale_report_margin_security/README.rst b/sale_report_margin_security/README.rst new file mode 100644 index 000000000..e360a8a80 --- /dev/null +++ b/sale_report_margin_security/README.rst @@ -0,0 +1,86 @@ +.. image:: https://odoo-community.org/readme-banner-image + :target: https://odoo-community.org/get-involved?utm_source=readme + :alt: Odoo Community Association + +=========================== +Sale Report Margin Security +=========================== + +.. + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! This file is generated by oca-gen-addon-readme !! + !! changes will be overwritten. !! + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! source digest: sha256:b670de2e3d0c30b3fe61199f53e6fc38f38595aec492cee8b4dad84fe251fda5 + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + +.. |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/license-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/19.0/sale_report_margin_security + :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-19-0/margin-analysis-19-0-sale_report_margin_security + :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=19.0 + :alt: Try me on Runboat + +|badge1| |badge2| |badge3| |badge4| |badge5| + +Glue module between ``sale_report_margin`` and ``sale_margin_security``. + +Restricts the purchase price and the margin percentage of the sales +analysis to the users allowed to see the product cost, as +``sale_margin_security`` does for the margin. + +**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 +------- + +* Camptocamp + +Contributors +------------ + +- `Camptocamp `__: + + - Iván Todorovich + +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_report_margin_security/__init__.py b/sale_report_margin_security/__init__.py new file mode 100644 index 000000000..65323532b --- /dev/null +++ b/sale_report_margin_security/__init__.py @@ -0,0 +1 @@ +from . import reports diff --git a/sale_report_margin_security/__manifest__.py b/sale_report_margin_security/__manifest__.py new file mode 100644 index 000000000..b2f95f163 --- /dev/null +++ b/sale_report_margin_security/__manifest__.py @@ -0,0 +1,13 @@ +# Copyright 2026 Camptocamp SA (https://www.camptocamp.com). +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). +{ + "name": "Sale Report Margin Security", + "summary": "Hide the cost and margin percentage of the sales analysis", + "version": "19.0.1.0.0", + "author": "Camptocamp, Odoo Community Association (OCA)", + "website": "https://github.com/OCA/margin-analysis", + "category": "Sales", + "license": "AGPL-3", + "depends": ["sale_report_margin", "sale_margin_security"], + "auto_install": True, +} diff --git a/sale_report_margin_security/pyproject.toml b/sale_report_margin_security/pyproject.toml new file mode 100644 index 000000000..4231d0ccc --- /dev/null +++ b/sale_report_margin_security/pyproject.toml @@ -0,0 +1,3 @@ +[build-system] +requires = ["whool"] +build-backend = "whool.buildapi" diff --git a/sale_report_margin_security/readme/CONTRIBUTORS.md b/sale_report_margin_security/readme/CONTRIBUTORS.md new file mode 100644 index 000000000..b8464aa7a --- /dev/null +++ b/sale_report_margin_security/readme/CONTRIBUTORS.md @@ -0,0 +1,2 @@ +- [Camptocamp](https://www.camptocamp.com): + - Iván Todorovich \<\> diff --git a/sale_report_margin_security/readme/DESCRIPTION.md b/sale_report_margin_security/readme/DESCRIPTION.md new file mode 100644 index 000000000..aa3ecd86b --- /dev/null +++ b/sale_report_margin_security/readme/DESCRIPTION.md @@ -0,0 +1,5 @@ +Glue module between `sale_report_margin` and `sale_margin_security`. + +Restricts the purchase price and the margin percentage of the sales +analysis to the users allowed to see the product cost, as +`sale_margin_security` does for the margin. diff --git a/sale_report_margin_security/reports/__init__.py b/sale_report_margin_security/reports/__init__.py new file mode 100644 index 000000000..cd23411b8 --- /dev/null +++ b/sale_report_margin_security/reports/__init__.py @@ -0,0 +1 @@ +from . import sale_report diff --git a/sale_report_margin_security/reports/sale_report.py b/sale_report_margin_security/reports/sale_report.py new file mode 100644 index 000000000..9cd81db3f --- /dev/null +++ b/sale_report_margin_security/reports/sale_report.py @@ -0,0 +1,11 @@ +# Copyright 2026 Camptocamp SA (https://www.camptocamp.com). +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + +from odoo import fields, models + + +class SaleReport(models.Model): + _inherit = "sale.report" + + purchase_price = fields.Float(groups="product_cost_security.group_product_cost") + margin_percent = fields.Float(groups="product_cost_security.group_product_cost") diff --git a/sale_report_margin_security/static/description/icon.png b/sale_report_margin_security/static/description/icon.png new file mode 100644 index 000000000..3a0328b51 Binary files /dev/null and b/sale_report_margin_security/static/description/icon.png differ diff --git a/sale_report_margin_security/static/description/index.html b/sale_report_margin_security/static/description/index.html new file mode 100644 index 000000000..a36f25b5c --- /dev/null +++ b/sale_report_margin_security/static/description/index.html @@ -0,0 +1,435 @@ + + + + + +Sale Report Margin Security + + + +
+ + + +Odoo Community Association + +
+

Sale Report Margin Security

+ +

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

+

Glue module between sale_report_margin and sale_margin_security.

+

Restricts the purchase price and the margin percentage of the sales +analysis to the users allowed to see the product cost, as +sale_margin_security does for the margin.

+

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

+
    +
  • Camptocamp
  • +
+
+
+

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/sale_report_margin_security/tests/__init__.py b/sale_report_margin_security/tests/__init__.py new file mode 100644 index 000000000..4c5535408 --- /dev/null +++ b/sale_report_margin_security/tests/__init__.py @@ -0,0 +1 @@ +from . import test_sale_report_margin_security diff --git a/sale_report_margin_security/tests/test_sale_report_margin_security.py b/sale_report_margin_security/tests/test_sale_report_margin_security.py new file mode 100644 index 000000000..33797fccc --- /dev/null +++ b/sale_report_margin_security/tests/test_sale_report_margin_security.py @@ -0,0 +1,45 @@ +# Copyright 2026 Camptocamp SA (https://www.camptocamp.com). +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + +from odoo.orm.commands import Command +from odoo.tests.common import new_test_user + +from odoo.addons.base.tests.common import BaseCommon + + +class TestSaleReportMarginSecurity(BaseCommon): + @classmethod + def setUpClass(cls): + super().setUpClass() + cls.user_cost = new_test_user( + cls.env, + login="sale_manager_cost", + groups="sales_team.group_sale_manager,product_cost_security.group_product_cost", + ) + cls.product = cls.env["product.product"].create({"name": "Product Test"}) + cls.order = cls.env["sale.order"].create( + { + "partner_id": cls.partner.id, + "order_line": [ + Command.create( + { + "product_id": cls.product.id, + "price_unit": 10.0, + "product_uom_qty": 1.0, + "purchase_price": 8.0, + }, + ) + ], + } + ) + cls.order.action_confirm() + cls.domain = [ + ("order_reference", "=", f"{cls.order._name},{cls.order.id}"), + ("product_id", "=", cls.product.id), + ] + + def test_cost_user_reads_margin_data(self): + """A user allowed to see the cost reads the margin data of the report.""" + report = self.env["sale.report"].with_user(self.user_cost).search(self.domain) + self.assertEqual(report.purchase_price, 8.0) + self.assertEqual(report.margin_percent, 0.2)