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