new fix
This commit is contained in:
parent
fae4c322ba
commit
808e0e6baa
|
|
@ -20,61 +20,84 @@ class SamashtiDashboard(models.AbstractModel):
|
||||||
pt.name AS product_name,
|
pt.name AS product_name,
|
||||||
pc.name AS category,
|
pc.name AS category,
|
||||||
uom.name AS uom,
|
uom.name AS uom,
|
||||||
-- Opening Stock
|
|
||||||
COALESCE(SUM(CASE
|
|
||||||
WHEN sm.date < {fromDate} AND sl_dest.usage = 'internal' THEN sm.product_uom_qty
|
|
||||||
WHEN sm.date < {fromDate} AND sl_src.usage = 'internal' THEN -sm.product_uom_qty
|
|
||||||
ELSE 0
|
|
||||||
END), 0) AS opening_stock,
|
|
||||||
|
|
||||||
-- Receipts (to internal from supplier/purchase)
|
-- Current Cost (from Valuation Layer)
|
||||||
COALESCE(SUM(CASE
|
COALESCE((
|
||||||
WHEN sm.date BETWEEN {fromDate} AND {toDate}
|
SELECT SUM(svl.value) / NULLIF(SUM(svl.quantity), 0)
|
||||||
|
FROM stock_valuation_layer svl
|
||||||
|
WHERE svl.product_id = pp.id
|
||||||
|
), 0) AS current_cost,
|
||||||
|
|
||||||
|
-- Opening Stock (before start date)
|
||||||
|
COALESCE(SUM(
|
||||||
|
CASE
|
||||||
|
WHEN sml.create_date < {fromDate}
|
||||||
|
AND sl_dest.usage = 'internal' THEN sml.quantity
|
||||||
|
WHEN sml.create_date < {fromDate}
|
||||||
|
AND sl_src.usage = 'internal' THEN -sml.quantity
|
||||||
|
ELSE 0
|
||||||
|
END
|
||||||
|
), 0) AS opening_stock,
|
||||||
|
|
||||||
|
-- Receipts (Supplier → Internal)
|
||||||
|
COALESCE(SUM(
|
||||||
|
CASE
|
||||||
|
WHEN sml.create_date BETWEEN {fromDate} AND {toDate}
|
||||||
AND sl_dest.usage = 'internal'
|
AND sl_dest.usage = 'internal'
|
||||||
AND sl_src.usage = 'supplier' THEN sm.product_uom_qty
|
AND sl_src.usage = 'supplier' THEN sml.quantity
|
||||||
ELSE 0
|
ELSE 0
|
||||||
END), 0) AS receipts,
|
END
|
||||||
|
), 0) AS receipts,
|
||||||
|
|
||||||
-- Production (to internal from Production)
|
-- Production (Production → Internal)
|
||||||
COALESCE(SUM(CASE
|
COALESCE(SUM(
|
||||||
WHEN sm.date BETWEEN {fromDate} AND {toDate}
|
CASE
|
||||||
|
WHEN sml.create_date BETWEEN {fromDate} AND {toDate}
|
||||||
AND sl_dest.usage = 'internal'
|
AND sl_dest.usage = 'internal'
|
||||||
AND sl_src.usage = 'production' THEN sm.product_uom_qty
|
AND sl_src.usage = 'production' THEN sml.quantity
|
||||||
ELSE 0
|
ELSE 0
|
||||||
END), 0) AS production,
|
END
|
||||||
|
), 0) AS production,
|
||||||
|
|
||||||
-- Consumption (to production only)
|
-- Consumption (Internal → Production)
|
||||||
COALESCE(SUM(CASE
|
COALESCE(SUM(
|
||||||
WHEN sm.date BETWEEN {fromDate} AND {toDate}
|
CASE
|
||||||
|
WHEN sml.create_date BETWEEN {fromDate} AND {toDate}
|
||||||
AND sl_src.usage = 'internal'
|
AND sl_src.usage = 'internal'
|
||||||
AND sl_dest.usage = 'production' THEN sm.product_uom_qty
|
AND sl_dest.usage = 'production' THEN sml.quantity
|
||||||
ELSE 0
|
ELSE 0
|
||||||
END), 0) AS consumption,
|
END
|
||||||
|
), 0) AS consumption,
|
||||||
|
|
||||||
-- Dispatch (to customer only)
|
-- Dispatch (Internal → Customer)
|
||||||
COALESCE(SUM(CASE
|
COALESCE(SUM(
|
||||||
WHEN sm.date BETWEEN {fromDate} AND {toDate}
|
CASE
|
||||||
|
WHEN sml.create_date BETWEEN {fromDate} AND {toDate}
|
||||||
AND sl_src.usage = 'internal'
|
AND sl_src.usage = 'internal'
|
||||||
AND sl_dest.usage = 'customer' THEN sm.product_uom_qty
|
AND sl_dest.usage = 'customer' THEN sml.quantity
|
||||||
ELSE 0
|
ELSE 0
|
||||||
END), 0) AS dispatch,
|
END
|
||||||
|
), 0) AS dispatch,
|
||||||
|
|
||||||
-- Closing Stock = Opening + Receipts + Production - Consumption - Dispatch
|
-- Closing Stock = Opening + Receipts + Production - Consumption - Dispatch
|
||||||
(
|
(
|
||||||
COALESCE(SUM(CASE
|
COALESCE(SUM(
|
||||||
WHEN sm.date < {fromDate} AND sl_dest.usage = 'internal' THEN sm.product_uom_qty
|
CASE
|
||||||
WHEN sm.date < {fromDate} AND sl_src.usage = 'internal' THEN -sm.product_uom_qty
|
WHEN sml.create_date < {fromDate} AND sl_dest.usage = 'internal' THEN sml.quantity
|
||||||
WHEN sm.date BETWEEN {fromDate} AND {toDate} AND sl_dest.usage = 'internal' AND sl_src.usage IN ('supplier', 'production') THEN sm.product_uom_qty
|
WHEN sml.create_date < {fromDate} AND sl_src.usage = 'internal' THEN -sml.quantity
|
||||||
WHEN sm.date BETWEEN {fromDate} AND {toDate} AND sl_src.usage = 'internal' AND sl_dest.usage = 'production' THEN -sm.product_uom_qty
|
WHEN sml.create_date BETWEEN {fromDate} AND {toDate}
|
||||||
WHEN sm.date BETWEEN {fromDate} AND {toDate} AND sl_src.usage = 'internal' AND sl_dest.usage = 'customer' THEN -sm.product_uom_qty
|
AND sl_dest.usage = 'internal' AND sl_src.usage IN ('supplier', 'production') THEN sml.quantity
|
||||||
|
WHEN sml.create_date BETWEEN {fromDate} AND {toDate}
|
||||||
|
AND sl_src.usage = 'internal' AND sl_dest.usage IN ('production', 'customer') THEN -sml.quantity
|
||||||
ELSE 0
|
ELSE 0
|
||||||
END), 0)
|
END
|
||||||
|
), 0)
|
||||||
) AS closing_stock
|
) AS closing_stock
|
||||||
|
|
||||||
FROM
|
FROM
|
||||||
stock_move sm
|
stock_move_line sml
|
||||||
JOIN
|
JOIN
|
||||||
product_product pp ON sm.product_id = pp.id
|
product_product pp ON sml.product_id = pp.id
|
||||||
JOIN
|
JOIN
|
||||||
product_template pt ON pp.product_tmpl_id = pt.id
|
product_template pt ON pp.product_tmpl_id = pt.id
|
||||||
JOIN
|
JOIN
|
||||||
|
|
@ -82,17 +105,18 @@ class SamashtiDashboard(models.AbstractModel):
|
||||||
JOIN
|
JOIN
|
||||||
uom_uom uom ON pt.uom_id = uom.id
|
uom_uom uom ON pt.uom_id = uom.id
|
||||||
JOIN
|
JOIN
|
||||||
stock_location sl_src ON sm.location_id = sl_src.id
|
stock_location sl_src ON sml.location_id = sl_src.id
|
||||||
JOIN
|
JOIN
|
||||||
stock_location sl_dest ON sm.location_dest_id = sl_dest.id
|
stock_location sl_dest ON sml.location_dest_id = sl_dest.id
|
||||||
|
|
||||||
WHERE
|
WHERE
|
||||||
sl_src.usage IN ('internal', 'supplier', 'production', 'customer') AND
|
sml.state = 'done'
|
||||||
sl_dest.usage IN ('internal', 'supplier', 'production', 'customer') AND
|
AND pt.type IN ('product', 'consu')
|
||||||
sm.state = 'done' AND pt.type = 'consu'
|
AND sl_src.usage IN ('internal', 'supplier', 'production', 'customer')
|
||||||
|
AND sl_dest.usage IN ('internal', 'supplier', 'production', 'customer')
|
||||||
|
|
||||||
GROUP BY
|
GROUP BY
|
||||||
pp.default_code, pt.name, pc.name, uom.name
|
pp.default_code, pt.name, pc.name, uom.name, pp.id
|
||||||
|
|
||||||
ORDER BY
|
ORDER BY
|
||||||
pt.name;
|
pt.name;
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue