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.
This commit is contained in:
Mihir Kandoi
2026-08-02 19:41:22 +05:30
parent 8f227ad80e
commit 98cf3587eb

View File

@@ -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)