kg to g convertion fix in report

This commit is contained in:
raman 2025-11-05 09:44:02 +05:30
parent f6af6b3260
commit 5316a9a1cb
1 changed files with 88 additions and 104 deletions

View File

@ -16,111 +16,95 @@ 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,
-- Current Cost (from Valuation Layer) -- Opening Stock: includes inventory adjustments before fromDate
COALESCE(( COALESCE(SUM(CASE
SELECT SUM(svl.value) / NULLIF(SUM(svl.quantity), 0) WHEN sm.date < {fromDate} AND sl_dest.usage = 'internal' THEN sm.product_uom_qty * (uom.factor /sm_uom.factor)
FROM stock_valuation_layer svl WHEN sm.date < {fromDate} AND sl_src.usage = 'internal' THEN -sm.product_uom_qty * (uom.factor /sm_uom.factor)
WHERE svl.product_id = pp.id WHEN sm.date < {fromDate} AND (sl_src.usage = 'inventory' OR sl_dest.usage = 'inventory') THEN sm.product_uom_qty * (uom.factor /sm_uom.factor)
), 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_src.usage in ('supplier', 'inventory') THEN sml.quantity
ELSE 0
END
), 0) AS receipts,
-- Production (Production Internal)
COALESCE(SUM(
CASE
WHEN sml.create_date BETWEEN {fromDate} AND {toDate}
AND sl_dest.usage = 'internal'
AND sl_src.usage = 'production' THEN sml.quantity
ELSE 0
END
), 0) AS production,
-- Consumption (Internal Production)
COALESCE(SUM(
CASE
WHEN sml.create_date BETWEEN {fromDate} AND {toDate}
AND sl_src.usage = 'internal'
AND sl_dest.usage = 'production' THEN sml.quantity
ELSE 0
END
), 0) AS consumption,
-- Dispatch (Internal Customer)
COALESCE(SUM(
CASE
WHEN sml.create_date BETWEEN {fromDate} AND {toDate}
AND sl_src.usage = 'internal'
AND sl_dest.usage = 'customer' THEN sml.quantity
ELSE 0
END
), 0) AS dispatch,
-- Closing Stock = Opening + Receipts + Production - Consumption - Dispatch
(
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
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 END), 0) AS opening_stock,
), 0)
) AS closing_stock -- Receipts: from supplier and inventory adjustments in date range
COALESCE(SUM(CASE
FROM WHEN sm.date BETWEEN {fromDate} AND {toDate}
stock_move_line sml AND sl_dest.usage = 'internal'
JOIN AND sl_src.usage in ('supplier','inventory') THEN sm.product_uom_qty * (uom.factor /sm_uom.factor)
product_product pp ON sml.product_id = pp.id WHEN sm.date BETWEEN {fromDate} AND {toDate} AND (sl_src.usage = 'inventory' OR sl_dest.usage = 'inventory') THEN sm.product_uom_qty * (uom.factor /sm_uom.factor)
JOIN ELSE 0
product_template pt ON pp.product_tmpl_id = pt.id END), 0) AS receipts,
JOIN
product_category pc ON pt.categ_id = pc.id -- Production: internal moves from production
JOIN COALESCE(SUM(CASE
uom_uom uom ON pt.uom_id = uom.id WHEN sm.date BETWEEN {fromDate} AND {toDate}
JOIN AND sl_dest.usage = 'internal'
stock_location sl_src ON sml.location_id = sl_src.id AND sl_src.usage = 'production' THEN sm.product_uom_qty * (uom.factor /sm_uom.factor)
JOIN ELSE 0
stock_location sl_dest ON sml.location_dest_id = sl_dest.id END), 0) AS production,
WHERE -- Consumption: internal moves to production
sml.state = 'done' COALESCE(SUM(CASE
AND pt.type IN ('product', 'consu') WHEN sm.date BETWEEN {fromDate} AND {toDate}
AND sl_src.usage IN ('internal', 'supplier', 'production', 'customer','inventory') AND sl_src.usage = 'internal'
AND sl_dest.usage IN ('internal', 'supplier', 'production', 'customer', 'inventory') AND sl_dest.usage = 'production' THEN sm.product_uom_qty * (uom.factor /sm_uom.factor)
ELSE 0
GROUP BY END), 0) AS consumption,
pp.default_code, pt.name, pc.name, uom.name, pp.id
-- Dispatch: internal moves to customer
ORDER BY COALESCE(SUM(CASE
pt.name; WHEN sm.date BETWEEN {fromDate} AND {toDate}
AND sl_src.usage = 'internal'
AND sl_dest.usage = 'customer' THEN sm.product_uom_qty * (uom.factor /sm_uom.factor)
ELSE 0
END), 0) AS dispatch,
-- Closing Stock = Opening + Receipts + Production - Consumption - Dispatch + Inventory adjustments
(
COALESCE(SUM(CASE
WHEN sm.date < {fromDate} AND sl_dest.usage = 'internal' THEN sm.product_uom_qty * (uom.factor /sm_uom.factor)
WHEN sm.date < {fromDate} AND sl_src.usage = 'internal' THEN -sm.product_uom_qty * (uom.factor /sm_uom.factor)
WHEN sm.date < {fromDate} AND (sl_src.usage = 'inventory' OR sl_dest.usage = 'inventory') THEN sm.product_uom_qty * (uom.factor /sm_uom.factor)
WHEN sm.date BETWEEN {fromDate} AND {toDate} AND sl_dest.usage = 'internal' AND sl_src.usage IN ('supplier', 'production') THEN sm.product_uom_qty * (uom.factor /sm_uom.factor)
WHEN sm.date BETWEEN {fromDate} AND {toDate} AND (sl_src.usage = 'inventory' OR sl_dest.usage = 'inventory') THEN sm.product_uom_qty * (uom.factor /sm_uom.factor)
WHEN sm.date BETWEEN {fromDate} AND {toDate} AND sl_src.usage = 'internal' AND sl_dest.usage = 'production' THEN -sm.product_uom_qty * (uom.factor /sm_uom.factor)
WHEN sm.date BETWEEN {fromDate} AND {toDate} AND sl_src.usage = 'internal' AND sl_dest.usage = 'customer' THEN -sm.product_uom_qty * (uom.factor /sm_uom.factor)
ELSE 0
END), 0)
) AS closing_stock
FROM
stock_move sm
JOIN
product_product pp ON sm.product_id = pp.id
JOIN
product_template pt ON pp.product_tmpl_id = pt.id
JOIN
product_category pc ON pt.categ_id = pc.id
JOIN
uom_uom uom ON pt.uom_id = uom.id -- Product default UOM
JOIN
uom_uom sm_uom ON sm.product_uom = sm_uom.id -- Stock move UOM
JOIN
stock_location sl_src ON sm.location_id = sl_src.id
JOIN
stock_location sl_dest ON sm.location_dest_id = sl_dest.id
WHERE
sl_src.usage IN ('internal', 'supplier', 'production', 'customer', 'inventory') AND
sl_dest.usage IN ('internal', 'supplier', 'production', 'customer', 'inventory') AND
sm.state = 'done' AND
pt.type = 'consu'
GROUP BY
pp.default_code, pt.name, pc.name, uom.name
ORDER BY
pt.name;
""" """
self.env.cr.execute(sql) self.env.cr.execute(sql)