This commit is contained in:
raman 2025-11-04 13:00:08 +05:30
parent fae4c322ba
commit 808e0e6baa
1 changed files with 95 additions and 71 deletions

View File

@ -15,87 +15,111 @@ class SamashtiDashboard(models.AbstractModel):
toDate = "'"+str(to_date)+" 23:59:59'" toDate = "'"+str(to_date)+" 23:59:59'"
sql = f""" sql = f"""
SELECT SELECT
pp.default_code AS product_code, pp.default_code AS product_code,
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 -- Current Cost (from Valuation Layer)
WHEN sm.date < {fromDate} AND sl_dest.usage = 'internal' THEN sm.product_uom_qty COALESCE((
WHEN sm.date < {fromDate} AND sl_src.usage = 'internal' THEN -sm.product_uom_qty 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 ELSE 0
END), 0) AS opening_stock, END
), 0) AS opening_stock,
-- Receipts (to internal from supplier/purchase)
COALESCE(SUM(CASE -- Receipts (Supplier Internal)
WHEN sm.date BETWEEN {fromDate} AND {toDate} 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)
COALESCE(SUM(CASE -- Production (Production Internal)
WHEN sm.date BETWEEN {fromDate} AND {toDate} 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 = '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)
COALESCE(SUM(CASE -- Consumption (Internal Production)
WHEN sm.date BETWEEN {fromDate} AND {toDate} COALESCE(SUM(
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)
COALESCE(SUM(CASE -- Dispatch (Internal Customer)
WHEN sm.date BETWEEN {fromDate} AND {toDate} COALESCE(SUM(
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 (
WHEN sm.date < {fromDate} AND sl_dest.usage = 'internal' THEN sm.product_uom_qty COALESCE(SUM(
WHEN sm.date < {fromDate} AND sl_src.usage = 'internal' THEN -sm.product_uom_qty CASE
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_dest.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 < {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 = 'customer' THEN -sm.product_uom_qty WHEN sml.create_date BETWEEN {fromDate} AND {toDate}
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
) AS closing_stock ), 0)
) AS closing_stock
FROM
stock_move sm FROM
JOIN stock_move_line sml
product_product pp ON sm.product_id = pp.id JOIN
JOIN product_product pp ON sml.product_id = pp.id
product_template pt ON pp.product_tmpl_id = pt.id JOIN
JOIN product_template pt ON pp.product_tmpl_id = pt.id
product_category pc ON pt.categ_id = pc.id JOIN
JOIN product_category pc ON pt.categ_id = pc.id
uom_uom uom ON pt.uom_id = uom.id JOIN
JOIN uom_uom uom ON pt.uom_id = uom.id
stock_location sl_src ON sm.location_id = sl_src.id JOIN
JOIN stock_location sl_src ON sml.location_id = sl_src.id
stock_location sl_dest ON sm.location_dest_id = sl_dest.id JOIN
stock_location sl_dest ON sml.location_dest_id = sl_dest.id
WHERE
sl_src.usage IN ('internal', 'supplier', 'production', 'customer') AND WHERE
sl_dest.usage IN ('internal', 'supplier', 'production', 'customer') AND sml.state = 'done'
sm.state = 'done' AND pt.type = 'consu' AND pt.type IN ('product', 'consu')
AND sl_src.usage IN ('internal', 'supplier', 'production', 'customer')
GROUP BY AND sl_dest.usage IN ('internal', 'supplier', 'production', 'customer')
pp.default_code, pt.name, pc.name, uom.name
GROUP BY
ORDER BY pp.default_code, pt.name, pc.name, uom.name, pp.id
pt.name;
ORDER BY
pt.name;
""" """
self.env.cr.execute(sql) self.env.cr.execute(sql)