Skip to content
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

[IMP] average calculations in group by's #157293

Conversation

lennartvdd
Copy link

Description of the issue/feature this PR addresses:

In Odoo group by / aggregation averages can not be correctly calculated for certain values such as an average price per unit, which requires taking into account the number of items sold, or percentual values such as margin percentages.
The current method is to sum their values and dividing by the number of values summed, but this leads to incorrect results. Averaging percentages is almost never a good idea.

Instead it is necessary to calculate the sum of a different value and divide by the summed total to get the right result.
This change supports adding custom numerator and divisor field to get the right average value in grouped tree views

Current behavior before PR:

Example

  1. Go to Orders > Orders.
  2. Edit Sales Order Tree View
  3. Add "Margin %" field (use, widget: "Percentage", Aggregate: "Average") & Add "Margin €" field (Widget: "Monetary", Aggregate: "Sum") for example with Odoo Studio.
    5 Select/show the Untaxed Amount column.
  4. Group by for example: "YourCompany, Marc Demo". Contains two orders, one with a margin of 0 (0%) and the other margin is 10.25 (33.33%)
  5. Current view shows the Sum of Margin € is 10.25 (correct) Sum of Total Untaxed amount is 130.75 (correct) Average of margin % is 16.67% (calculated by Odoo as (0% + 33.33%) / 2, wich is incorrect)

A similar issue occurs when trying to get the average unit cost from sales lines.
Simply taking al the unit_cost values from the line and averaging them would only give a valid result if we only sell 1 item on each line. To get the actual valid average unit cost we need to take the sum of the untaxed amount and divide it by the sum of the quantity.

Desired behavior after PR is merged:

Average Margin % in reality is: 10.25 / 130.75 = 7.84%

This PR proposes to resolve the issue for developers in a more straightforward and easy to use manner.

To apply a custom average calculation, when using group_operator='avg' on a field, simply specifying the relevant fields on which to base the calculation, by adding the parameter(s) 'average_numerator' and/or 'average_divisor',
The grouped query will return the result by calculating it as: sum({average_numerator}) / sum({average_divisor}) as 'name'
This yields the correct result in both tree (list) and pivot views. Perhaps also in other views that use the group by aggregation method, but I didn't test that.

for example:

def SaleOrderLine(models.Model):
	_inherit="sale.order.line"
	price_unit = fields.Field(..., group_operator='avg', average_numerator='price_subtotal', average_divisor='product_uom_qty'])

The value of the average_* parameters must contain the name of an existing field on the model.

Some disclaimers:

Perhaps not too many people take too notice on the issue, seeing as I couldn't find much people complaining, but since it's such a foundational calculation error that's omnipresent in the system I felt like there should be some quick fix available and spent a few hours of my spare time over the weekend drafting this.

I'm not an experienced python developer, let alone Odoo developer, and didn't bother to write tests, I only performed some quick hallway usability testing by extending the sale report, and it seemed to work like a charm.
This PR is probably also not for the right branch/version, but it would be nice if this fix can be backported to the oldest stable version.

Therefor consider this PR as a push in the right direction, or just a source of inspiration, that MAY even usable in production, but really needs someone with deep Odoo Development knowledge/experience to have a look at, test, improve, etc.


I confirm I have signed the CLA and read the PR guidelines at www.odoo.com/submit-pr

In Odoo grouped lists averages can not be correctly calculated
for certain values such as an average price per unit, which requires
taking into account the number of items sold, or percentual values such
as margin percentages.

The current method is to sum their values and deviding by the number of
values summed, but this leads to incorrect results. Averaging
percentages is almost never a good idea.

Instead it is necessary to calculate the sum of a different value
and devide by the summed total to get the right result.

This change supports adding custom numerator and divisor field to get
the right average value in grouped tree views
@robodoo
Copy link
Contributor

robodoo commented Mar 11, 2024

@C3POdoo C3POdoo requested review from a team, rco-odoo, sleepy-monax, BastienFafchamps and fdardenne and removed request for a team March 11, 2024 17:08
@lennartvdd lennartvdd changed the title [FIX] average calcuation in list views [FIX] average calculations in group by's Mar 11, 2024
@lennartvdd lennartvdd changed the title [FIX] average calculations in group by's [IMP] average calculations in group by's Mar 11, 2024
@C3POdoo C3POdoo requested a review from a team March 12, 2024 08:58
@rco-odoo
Copy link
Member

What you ask for is a kind of weighted average. This clearly is a new feature, not a bugfix.
Please check our policy https://github.com/odoo/odoo/wiki/Contributing#what-does-stable-mean

@rco-odoo rco-odoo closed this Mar 13, 2024
@rco-odoo
Copy link
Member

FYI this kind of feature is now implementable by models with a simple override since #110737.

@lennartvdd
Copy link
Author

What you ask for is a kind of weighted average. This clearly is a new feature, not a bugfix. Please check our policy https://github.com/odoo/odoo/wiki/Contributing#what-does-stable-mean

I expected one of you would say that. As mentioned, I assumed this was for the wrong branch.

It is a little sad that although Odoo is technically a wonderful product, something as simple as showing valid numbers in generally available reports seems to not be possible for a really long time at least not since version 14 and still not in version 17 and a suggestion to somehow fix it is simply shot down / closed without giving it much thought or room for discussion..

I'm going to leave it at this. It's not my job and don't have time to do more deep dive into the odoo code to fix this.

FYI this kind of feature is now implementable by models with a simple override since #110737.

I am curious though.
Does this still apply?
https://www.odoo.com/nl_NL/forum/help-1/group-by-function-computed-field-with-out-store-true-in-odoo-11-153145

How would you fix the sale.report for example discount %?
Or when adding a useful/valuable measure such as avg. unit cost or margin %
I don't see how you would fix it there with this.

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants