Skip to content
Snippets Groups Projects
Commit 1369cd18 authored by Touati Djamel (otd)'s avatar Touati Djamel (otd)
Browse files

[FIX] purchase: correctly calculate average price


Steps to reproduce the bug:
- Create a storable product “P1”
    - costing method: avco
- Create a PO:
    - Add the product “P1”:
        - Line 1: Qty= 10, price= $50
        - Line 2: Qty=1, price= $10
    - Confirm the PO and receive the product

- Go to purchase → Reporting → Purchase Analysis

Problem:
The average price is incorrect, the current calculation is:

(50 + 10) / 2 = 30

The average should take into account the quantities purchased in each
line, And not simply the number of line, so the correct calculation
should be:

((10 * 50) + (10 * 1)) / 11 = 46.36

The SQL query is correct, it is when applying the read_group that the
calculation is incorrect, we should override it to make a personalized
calculation of the average.

opw-3136406

closes odoo/odoo#110740

Signed-off-by: default avatarWilliam Henrotin (whe) <whe@odoo.com>
parent 3c43edcb
Branches
Tags
No related merge requests found
......@@ -168,10 +168,24 @@ class PurchaseReport(models.Model):
if any(field.split(':')[1].split('(')[0] != 'avg' for field in [avg_days_to_purchase] if field):
raise UserError("Value: 'avg_days_to_purchase' should only be used to show an average. If you are seeing this message then it is being accessed incorrectly.")
if 'price_average:avg' in fields:
fields.extend(['aggregated_qty_ordered:array_agg(qty_ordered)'])
fields.extend(['aggregated_price_average:array_agg(price_average)'])
res = []
if fields:
res = super(PurchaseReport, self).read_group(domain, fields, groupby, offset=offset, limit=limit, orderby=orderby, lazy=lazy)
if 'price_average:avg' in fields:
qties = 'aggregated_qty_ordered'
special_field = 'aggregated_price_average'
for data in res:
if data[special_field] and data[qties]:
total_unit_cost = sum(float(value) * float(qty) for value, qty in zip(data[special_field], data[qties]) if qty and value)
total_qty_ordered = sum(float(qty) for qty in data[qties] if qty)
data['price_average'] = (total_unit_cost / total_qty_ordered) if total_qty_ordered else 0
del data[special_field]
del data[qties]
if not res and avg_days_to_purchase:
res = [{}]
......
......@@ -88,3 +88,38 @@ class TestPurchaseOrderReport(AccountTestInvoicingCommon):
)
self.assertEqual(round(report[0]['delay']), -10, msg="The PO has been confirmed 10 days in advance")
self.assertEqual(round(report[0]['delay_pass']), 5, msg="There are 5 days between the order date and the planned date")
def test_avg_price_calculation(self):
"""
Check that the average price is calculated based on the quantity ordered in each line
PO:
- 10 unit of product A -> price $50
- 1 unit of product A -> price $10
Total qty_ordered: 11
avergae price: 46.36 = ((10 * 50) + (10 * 1)) / 11
"""
po = self.env['purchase.order'].create({
'partner_id': self.partner_a.id,
'order_line': [
(0, 0, {
'product_id': self.product_a.id,
'product_qty': 10.0,
'price_unit': 50.0,
}),
(0, 0, {
'product_id': self.product_a.id,
'product_qty': 1.0,
'price_unit': 10.0,
}),
],
})
po.button_confirm()
po.flush()
report = self.env['purchase.report'].read_group(
[('product_id', '=', self.product_a.id)],
['qty_ordered', 'price_average:avg'],
['product_id'],
)
self.assertEqual(report[0]['qty_ordered'], 11)
self.assertEqual(round(report[0]['price_average'], 2), 46.36)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment