From d32f21cbc847f2315d807e6ee8a4c55501ea2da7 Mon Sep 17 00:00:00 2001 From: William Henrotin Date: Fri, 26 Jul 2019 11:22:01 +0000 Subject: [PATCH] [IMP] stock: forcast report improvements The forecast report added in b311d9c always shows the stock in all locations even in multicompany/multiwarehouse. This commit change the SQL query that gather the product quantities over time to take the warehouse into account and be able to filter on it. regarding stock.move, the warehouse is the source location one in cast of outgoing move and the destination location one in case of the ingoing move. Furthermore, the SQL query doesn't filter the product on their type. That means a stock move on consumable product will be shown even if this product Task : 2003485 --- addons/stock/report/report_stock_quantity.py | 33 +++++++++++++++----- 1 file changed, 26 insertions(+), 7 deletions(-) diff --git a/addons/stock/report/report_stock_quantity.py b/addons/stock/report/report_stock_quantity.py index d06a1a96b4d..57f6507b5c2 100644 --- a/addons/stock/report/report_stock_quantity.py +++ b/addons/stock/report/report_stock_quantity.py @@ -20,6 +20,7 @@ class ReportStockQuantity(models.Model): product_qty = fields.Float(string='Quantity', readonly=True) move_ids = fields.One2many('stock.move', readonly=True) company_id = fields.Many2one('res.company', readonly=True) + warehouse_id = fields.Many2one('stock.warehouse', readonly=True) def init(self): tools.drop_view_if_exists(self._cr, 'report_stock_quantity') @@ -37,33 +38,42 @@ SELECT WHEN (ls.usage = 'internal' OR ls.usage = 'transit' AND ls.company_id IS NOT NULL) AND ld.usage != 'internal' THEN -product_qty WHEN ls.usage != 'internal' AND (ld.usage = 'internal' OR ld.usage = 'transit' AND ld.company_id IS NOT NULL) THEN product_qty END AS product_qty, - m.company_id + m.company_id, + CASE + WHEN (ls.usage = 'internal' OR ls.usage = 'transit' AND ls.company_id IS NOT NULL) AND ld.usage != 'internal' THEN whs.id + WHEN ls.usage != 'internal' AND (ld.usage = 'internal' OR ld.usage = 'transit' AND ld.company_id IS NOT NULL) THEN whd.id + END AS warehouse_id FROM stock_move m LEFT JOIN stock_location ls on (ls.id=m.location_id) LEFT JOIN stock_location ld on (ld.id=m.location_dest_id) +LEFT JOIN stock_warehouse whs ON ls.parent_path like concat('%/', whs.view_location_id, '/%') +LEFT JOIN stock_warehouse whd ON ld.parent_path like concat('%/', whd.view_location_id, '/%') +LEFT JOIN product_product pp on pp.id=m.product_id +LEFT JOIN product_template pt on pt.id=pp.product_tmpl_id WHERE + pt.type = 'product' AND product_qty != 0 AND ( (ls.usage = 'internal' OR ls.usage = 'transit' AND ls.company_id IS NOT NULL) AND ld.usage != 'internal' OR ls.usage != 'internal' AND (ld.usage = 'internal' OR ld.usage = 'transit' AND ld.company_id IS NOT NULL)) AND m.state NOT IN ('cancel', 'draft', 'done') UNION SELECT - -MIN(q.id) as id, + -q.id as id, product_id, 'forecast' as state, date.*::date, - SUM(quantity) as product_qty, - q.company_id + quantity as product_qty, + q.company_id, + wh.id as warehouse_id FROM GENERATE_SERIES((now() at time zone 'utc')::date - interval '3month', (now() at time zone 'utc')::date + interval '3 month', '1 day'::interval) date, stock_quant q LEFT JOIN stock_location l on (l.id=q.location_id) +LEFT JOIN stock_warehouse wh ON l.parent_path like concat('%/', wh.view_location_id, '/%') WHERE l.usage = 'internal' -GROUP BY - product_id, date, q.company_id UNION SELECT m.id, @@ -84,12 +94,21 @@ SELECT WHEN (ls.usage = 'internal' OR ls.usage = 'transit' AND ls.company_id IS NOT NULL) AND ld.usage != 'internal' THEN -product_qty WHEN ls.usage != 'internal' AND (ld.usage = 'internal' OR ld.usage = 'transit' AND ld.company_id IS NOT NULL) THEN product_qty END AS product_qty, - m.company_id + m.company_id, + CASE + WHEN (ls.usage = 'internal' OR ls.usage = 'transit' AND ls.company_id IS NOT NULL) AND ld.usage != 'internal' THEN whs.id + WHEN ls.usage != 'internal' AND (ld.usage = 'internal' OR ld.usage = 'transit' AND ld.company_id IS NOT NULL) THEN whd.id + END AS warehouse_id FROM stock_move m LEFT JOIN stock_location ls on (ls.id=m.location_id) LEFT JOIN stock_location ld on (ld.id=m.location_dest_id) +LEFT JOIN stock_warehouse whs ON ls.parent_path like concat('%/', whs.view_location_id, '/%') +LEFT JOIN stock_warehouse whd ON ld.parent_path like concat('%/', whd.view_location_id, '/%') +LEFT JOIN product_product pp on pp.id=m.product_id +LEFT JOIN product_template pt on pt.id=pp.product_tmpl_id WHERE + pt.type = 'product' AND product_qty != 0 AND ( (ls.usage = 'internal' OR ls.usage = 'transit' AND ls.company_id IS NOT NULL) AND ld.usage != 'internal' OR ls.usage != 'internal' AND (ld.usage = 'internal' OR ld.usage = 'transit' AND ld.company_id IS NOT NULL)) AND