From 98cf3587ebaa699ff75a46b5cb90b410be242372 Mon Sep 17 00:00:00 2001 From: Mihir Kandoi Date: Sun, 2 Aug 2026 19:41:22 +0530 Subject: [PATCH] fix(manufacturing): compute BOM item amount per line get_bom_items_as_dict groups BOM lines by item_code, so a BOM listing the same item on more than one line collapses to a single row. The amount column multiplied the summed quantity by a single line's rate: Sum(stock_qty / bom.quantity) * Max(rate) * qty That is neither line's amount and not their total. The Max() was added to satisfy Postgres' strict GROUP BY on the assumption that rate is constant per item, but rate is editable per line. Fold the rate into the sum so every line contributes its own: Sum(stock_qty / bom.quantity * rate) * qty Identical for the common single-line item, correct for duplicates, and valid on both engines. Same class as the fix applied to budget_controller's requested amount. --- erpnext/manufacturing/doctype/bom/bom.py | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/erpnext/manufacturing/doctype/bom/bom.py b/erpnext/manufacturing/doctype/bom/bom.py index 6583e28889a..88d537f0a82 100644 --- a/erpnext/manufacturing/doctype/bom/bom.py +++ b/erpnext/manufacturing/doctype/bom/bom.py @@ -1264,11 +1264,9 @@ def _build_base_bom_items_query(bom, company, qty, t): def _add_bom_item_columns(query, t, bom, opts, track_semi_finished_goods): is_stock_item = cint(not opts.include_non_stock_items) stock_item_condition = t.item_doc.is_stock_item.isin([1, is_stock_item]) - # rate is constant per grouped item -> Max() keeps it out of the Sum (preserving the original - # Sum(...) * rate * qty arithmetic) while making the expression postgres-valid under GROUP BY. - amount_col = ( - Sum(t.bom_item.stock_qty / IfNull(t.bom_doc.quantity, 1)) * Max(t.bom_item.rate) * opts.qty - ).as_("amount") + amount_col = (Sum(t.bom_item.stock_qty / IfNull(t.bom_doc.quantity, 1) * t.bom_item.rate) * opts.qty).as_( + "amount" + ) if cint(opts.fetch_exploded): return _add_exploded_item_columns(query, t, bom, amount_col, stock_item_condition)