From 7d2223da080cb1c7606ac98441e5f2a64d7f6251 Mon Sep 17 00:00:00 2001 From: Josse Colpaert Date: Wed, 19 Aug 2015 10:06:29 +0200 Subject: [PATCH] [FIX] stock: Report stock level forecast correction Should take minimum id instead of max and different ids for stock moves and quants. The report was by day and the view by week, which gave wrong results for the cumulative quantity when having multiple values in a week (as it was summing them) We changed the SQL view to be by week also. Expected dates in the past are all reduced to the current date. --- addons/stock/report/report_stock_forecast.py | 46 +++++++++----------- 1 file changed, 20 insertions(+), 26 deletions(-) diff --git a/addons/stock/report/report_stock_forecast.py b/addons/stock/report/report_stock_forecast.py index 61397d5a25c..1161e5fc209 100644 --- a/addons/stock/report/report_stock_forecast.py +++ b/addons/stock/report/report_stock_forecast.py @@ -17,36 +17,24 @@ class report_stock_forecast(models.Model): def init(self, cr): tools.drop_view_if_exists(cr, 'report_stock_forecast') cr.execute("""CREATE or REPLACE VIEW report_stock_forecast AS (SELECT - max(id) as id, + MIN(id) as id, product_id as product_id, date as date, sum(product_qty) AS quantity, - sum(sum(product_qty)) OVER(PARTITION BY product_id ORDER BY date ) AS cumulative_quantity + sum(sum(product_qty)) OVER (PARTITION BY product_id ORDER BY date) AS cumulative_quantity FROM (SELECT - max(id) as id, + MIN(id) as id, MAIN.product_id as product_id, SUB.date as date, CASE WHEN MAIN.date = SUB.date THEN sum(MAIN.product_qty) ELSE 0 END as product_qty FROM (SELECT - max(sq.id) as id, + MIN(sq.id) as id, sq.product_id, - CASE WHEN sm.date < CURRENT_DATE - THEN to_date(to_char(sm.date, 'YYYY/MM/DD'), 'YYYY/MM/DD') - ELSE to_date(to_char(CURRENT_DATE, 'YYYY/MM/DD'), 'YYYY/MM/DD') - END AS date, + date_trunc('week', to_date(to_char(CURRENT_DATE, 'YYYY/MM/DD'), 'YYYY/MM/DD')) as date, SUM(sq.qty) AS product_qty FROM - (SELECT min(date_expected) AS date FROM stock_move AS sm - LEFT JOIN - stock_location source_location ON sm.location_id = source_location.id - LEFT JOIN - stock_location dest_location ON sm.location_dest_id = dest_location.id - WHERE - sm.state IN ('confirmed','assigned','waiting') AND - (dest_location.usage = 'internal' AND source_location.usage != 'internal') - or (source_location.usage = 'internal' AND dest_location.usage != 'internal')) AS sm, stock_quant as sq LEFT JOIN product_product ON product_product.id = sq.product_id @@ -57,9 +45,12 @@ class report_stock_forecast(models.Model): GROUP BY date, sq.product_id UNION ALL SELECT - max(sm.id) as id, + MIN(-sm.id) as id, sm.product_id, - to_date(to_char(sm.date_expected, 'YYYY/MM/DD'), 'YYYY/MM/DD') AS date, + CASE WHEN sm.date_expected > CURRENT_DATE + THEN date_trunc('week', to_date(to_char(sm.date_expected, 'YYYY/MM/DD'), 'YYYY/MM/DD')) + ELSE date_trunc('week', to_date(to_char(CURRENT_DATE, 'YYYY/MM/DD'), 'YYYY/MM/DD')) END + AS date, SUM(sm.product_qty) AS product_qty FROM stock_move as sm @@ -75,9 +66,12 @@ class report_stock_forecast(models.Model): GROUP BY sm.date_expected,sm.product_id UNION ALL SELECT - max(sm.id) as id, + MIN(-sm.id) as id, sm.product_id, - to_date(to_char(sm.date_expected, 'YYYY/MM/DD'), 'YYYY/MM/DD') AS date, + CASE WHEN sm.date_expected > CURRENT_DATE + THEN date_trunc('week', to_date(to_char(sm.date_expected, 'YYYY/MM/DD'), 'YYYY/MM/DD')) + ELSE date_trunc('week', to_date(to_char(CURRENT_DATE, 'YYYY/MM/DD'), 'YYYY/MM/DD')) END + AS date, SUM(-(sm.product_qty)) AS product_qty FROM stock_move as sm @@ -96,18 +90,18 @@ class report_stock_forecast(models.Model): (SELECT DISTINCT date FROM ( - SELECT CURRENT_DATE AS DATE + SELECT date_trunc('week', CURRENT_DATE) AS DATE UNION ALL - SELECT to_date(to_char(sm.date_expected, 'YYYY/MM/DD'), 'YYYY/MM/DD') AS date + SELECT date_trunc('week', to_date(to_char(sm.date_expected, 'YYYY/MM/DD'), 'YYYY/MM/DD')) AS date FROM stock_move sm LEFT JOIN stock_location source_location ON sm.location_id = source_location.id LEFT JOIN stock_location dest_location ON sm.location_dest_id = dest_location.id WHERE - sm.state IN ('confirmed','assigned','waiting') and - (dest_location.usage = 'internal' AND source_location.usage != 'internal') - or (source_location.usage = 'internal' AND dest_location.usage != 'internal')) AS DATE_SEARCH) + sm.state IN ('confirmed','assigned','waiting') and sm.date_expected > CURRENT_DATE and + ((dest_location.usage = 'internal' AND source_location.usage != 'internal') + or (source_location.usage = 'internal' AND dest_location.usage != 'internal'))) AS DATE_SEARCH) SUB ON (SUB.date IS NOT NULL) GROUP BY MAIN.product_id,SUB.date, MAIN.date ) AS FINAL