Repository navigation
[19.0] [FIX] sale_report_margin: report the purchase price as a unit cost + add other useful columns #287
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ivantodorovich
wants to merge
4
commits into
OCA:19.0
Choose a base branch
from
camptocamp:19.0-imp-sale_report_margin-list-view
base: 19.0
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
[19.0] [FIX] sale_report_margin: report the purchase price as a unit cost + add other useful columns #287
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
cbcc73c
[FIX] sale_report_margin: report the purchase price as a unit cost
ivantodorovich b6dc2d7
[IMP] sale_report_margin: add the margin percentage to the sale report
ivantodorovich 1354321
[IMP] sale_report_margin: show the margin in the list view
ivantodorovich 62175db
[ADD] sale_report_margin_security
ivantodorovich File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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 <[email protected]> | ||
|
|
||
| - `Camptocamp <https://www.camptocamp.com>`__: | ||
|
|
||
| - Iván Todorovich <[email protected]> | ||
|
|
||
| Maintainers | ||
| ----------- | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,4 +4,8 @@ | |
| > - David Vidal | ||
| > - Carlos Roca | ||
|
|
||
| - SodexisTeam \<<[email protected]>\> | ||
| - SodexisTeam \<<[email protected]>\> | ||
|
|
||
| - [Camptocamp](https://www.camptocamp.com): | ||
|
|
||
| > - Iván Todorovich \<<[email protected]>\> | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,7 @@ | ||
| # Copyright 2023 Álvaro Marcos <[email protected]> | ||
| # 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) | ||
|
ivantodorovich marked this conversation as resolved.
Resolved
|
||
| 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) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| <?xml version="1.0" encoding="utf-8" ?> | ||
| <!-- | ||
| Copyright 2026 Camptocamp SA (https://www.camptocamp.com). | ||
| License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). | ||
| --> | ||
| <odoo> | ||
| <record id="sale_report_view_tree" model="ir.ui.view"> | ||
| <field name="model">sale.report</field> | ||
| <field name="inherit_id" ref="sale.sale_report_view_tree" /> | ||
| <field name="arch" type="xml"> | ||
| <field name="price_unit" position="after"> | ||
| <field name="purchase_price" optional="show" widget="monetary" /> | ||
| </field> | ||
| <field name="price_total" position="after"> | ||
| <field | ||
| name="margin" | ||
| optional="show" | ||
| widget="monetary" | ||
| sum="Sum of Margin" | ||
| /> | ||
| <field name="margin_percent" optional="show" widget="percentage" /> | ||
| </field> | ||
| </field> | ||
| </record> | ||
| </odoo> |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.