From 1d7c12022dcffce4792b9eec85706e19338cfa9f Mon Sep 17 00:00:00 2001 From: Denis Ledoux Date: Tue, 16 Feb 2016 16:23:05 +0100 Subject: [PATCH] [FIX] stock_account: valuation history with moves having multiple quant prices The revision 29bd62252158803cdad351a2966af6eeac4cca74 aimed to improve the performance by changing the view index, to use a BIGINT instead of a text. It was assumed that the index used (`stock_move.id`) could not appear multiple times with the JOINs and group bys defined. This was in fact possible, if a stock move is associated to several quants with different costs, because of the JOIN on the many2many table `stock_quant_move_rel` linking quants to moves, and the GROUP BY `price_unit_on_quant` that caused the different moves/quants association not to be merged within one unique line if the costs on the quants are different. Instead of the group by, we now aggregate the quants costs, using the weighted average, so the index used can be unique, as expected. From now, the inventory value per line in this report view can therefore be different than what can be found on the quants, but this report view is based on the stock moves rather than the stock quants, and this is therefore accepted. In other words, the inventory value is computed for the stock move rather than for the stock quant. opw-658903 --- addons/stock_account/wizard/stock_valuation_history.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/addons/stock_account/wizard/stock_valuation_history.py b/addons/stock_account/wizard/stock_valuation_history.py index 5fc94f8106f..fad5a454454 100644 --- a/addons/stock_account/wizard/stock_valuation_history.py +++ b/addons/stock_account/wizard/stock_valuation_history.py @@ -125,7 +125,7 @@ class stock_history(osv.osv): product_categ_id, SUM(quantity) as quantity, date, - price_unit_on_quant, + SUM(price_unit_on_quant * quantity) / SUM(quantity) as price_unit_on_quant, source FROM ((SELECT @@ -193,5 +193,5 @@ class stock_history(osv.osv): dest_location.usage not in ('internal', 'transit')) )) AS foo - GROUP BY move_id, location_id, company_id, product_id, product_categ_id, date, price_unit_on_quant, source + GROUP BY move_id, location_id, company_id, product_id, product_categ_id, date, source )""")