[IMP] average calculations in group by's #157293
Closed
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.
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
5 Select/show the Untaxed Amount column.
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:
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