[FIX] stock: Forecasted stock report is not multi-company compliant

To take the company into account in the report.stock.forecast.

opw:777270
This commit is contained in:
Goffin Simon
2017-10-25 11:15:07 +02:00
parent b67e18bcb5
commit 135823a4d8
2 changed files with 18 additions and 12 deletions
+16 -10
View File
@@ -12,6 +12,7 @@ class ReportStockForecat(models.Model):
product_tmpl_id = fields.Many2one('product.template', string='Product Template', related='product_id.product_tmpl_id', readonly=True)
cumulative_quantity = fields.Float(string='Cumulative Quantity', readonly=True)
quantity = fields.Float(readonly=True)
company_id = fields.Many2one('res.company', string='Company', readonly=True)
@api.model_cr
def init(self):
@@ -21,19 +22,22 @@ class ReportStockForecat(models.Model):
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,
company_id
FROM
(SELECT
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
CASE WHEN MAIN.date = SUB.date THEN sum(MAIN.product_qty) ELSE 0 END as product_qty,
MAIN.company_id as company_id
FROM
(SELECT
MIN(sq.id) as id,
sq.product_id,
date_trunc('week', to_date(to_char(CURRENT_DATE, 'YYYY/MM/DD'), 'YYYY/MM/DD')) as date,
SUM(sq.quantity) AS product_qty
SUM(sq.quantity) AS product_qty,
sq.company_id
FROM
stock_quant as sq
LEFT JOIN
@@ -42,7 +46,7 @@ class ReportStockForecat(models.Model):
stock_location location_id ON sq.location_id = location_id.id
WHERE
location_id.usage = 'internal'
GROUP BY date, sq.product_id
GROUP BY date, sq.product_id, sq.company_id
UNION ALL
SELECT
MIN(-sm.id) as id,
@@ -51,7 +55,8 @@ class ReportStockForecat(models.Model):
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
SUM(sm.product_qty) AS product_qty,
sm.company_id
FROM
stock_move as sm
LEFT JOIN
@@ -63,7 +68,7 @@ class ReportStockForecat(models.Model):
WHERE
sm.state IN ('confirmed','assigned','waiting') and
source_location.usage != 'internal' and dest_location.usage = 'internal'
GROUP BY sm.date_expected,sm.product_id
GROUP BY sm.date_expected,sm.product_id, sm.company_id
UNION ALL
SELECT
MIN(-sm.id) as id,
@@ -72,7 +77,8 @@ class ReportStockForecat(models.Model):
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
SUM(-(sm.product_qty)) AS product_qty,
sm.company_id
FROM
stock_move as sm
LEFT JOIN
@@ -84,7 +90,7 @@ class ReportStockForecat(models.Model):
WHERE
sm.state IN ('confirmed','assigned','waiting') and
source_location.usage = 'internal' and dest_location.usage != 'internal'
GROUP BY sm.date_expected,sm.product_id)
GROUP BY sm.date_expected,sm.product_id, sm.company_id)
as MAIN
LEFT JOIN
(SELECT DISTINCT date
@@ -103,6 +109,6 @@ class ReportStockForecat(models.Model):
((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
GROUP BY MAIN.product_id,SUB.date, MAIN.date, MAIN.company_id
) AS FINAL
GROUP BY product_id,date)""")
GROUP BY product_id,date,company_id)""")
@@ -45,7 +45,7 @@
<field name="view_type">form</field>
<field name="view_mode">graph,pivot</field>
<field name="search_view_id" ref="view_stock_level_forecast_filter"/>
<field name="context">{'search_default_pivot_by':1, 'search_default_graph_by':1, 'search_default_product_tmpl_id': active_id}</field>
<field name="context">{'search_default_pivot_by':1, 'search_default_graph_by':1, 'search_default_product_tmpl_id': active_id, group_by': ['company_id']}</field>
<field name="view_id" ref="view_stock_level_forecast_pivot"/>
</record>
@@ -55,7 +55,7 @@
<field name="view_type">form</field>
<field name="view_mode">graph,pivot</field>
<field name="search_view_id" ref="view_stock_level_forecast_filter"/>
<field name="context">{'search_default_pivot_by':1, 'search_default_graph_by':1, 'search_default_product_id': active_id}</field>
<field name="context">{'search_default_pivot_by':1, 'search_default_graph_by':1, 'search_default_product_id': active_id, group_by': ['company_id']}</field>
<field name="view_id" ref="view_stock_level_forecast_pivot"/>
</record>
</odoo>