From 37b120bf69adab1d1f266678f8ae50302f6b7128 Mon Sep 17 00:00:00 2001 From: Mihir Kandoi Date: Wed, 19 Nov 2025 15:56:13 +0530 Subject: [PATCH] fix: modify for new changes --- erpnext/manufacturing/doctype/bom/bom.py | 18 ++++++--- .../stock/doctype/stock_entry/stock_entry.py | 37 ++++++++++--------- 2 files changed, 32 insertions(+), 23 deletions(-) diff --git a/erpnext/manufacturing/doctype/bom/bom.py b/erpnext/manufacturing/doctype/bom/bom.py index 753d5a6ec4d..2c3d8b1279b 100644 --- a/erpnext/manufacturing/doctype/bom/bom.py +++ b/erpnext/manufacturing/doctype/bom/bom.py @@ -1503,7 +1503,7 @@ def add_non_stock_items_cost(stock_entry, work_order, expense_account, job_card= def add_operating_cost_component_wise( - stock_entry, work_order=None, operating_cost_per_unit=None, op_expense_account=None, job_card=None + stock_entry, work_order=None, consumed_operating_cost=None, op_expense_account=None, job_card=None ): if not work_order: return False @@ -1527,11 +1527,11 @@ def add_operating_cost_component_wise( get_component_account(wc.operating_component, stock_entry.company) or op_expense_account ) actual_cp_operating_cost = flt( - flt(wc.operating_cost) * flt(flt(row.actual_operation_time) / 60.0), + flt(wc.operating_cost) * flt(flt(row.actual_operation_time) / 60.0) - consumed_operating_cost, row.precision("actual_operating_cost"), ) - per_unit_cost = flt(actual_cp_operating_cost) / flt(row.completed_qty) + per_unit_cost = flt(actual_cp_operating_cost) / flt(row.completed_qty - work_order.produced_qty) if per_unit_cost and expense_account: stock_entry.append( @@ -1542,6 +1542,7 @@ def add_operating_cost_component_wise( wc.operating_component, row.operation ), "amount": per_unit_cost * flt(stock_entry.fg_completed_qty), + "has_operating_cost": 1, }, ) @@ -1558,13 +1559,20 @@ def get_component_account(parent, company): def add_operations_cost(stock_entry, work_order=None, expense_account=None, job_card=None): - from erpnext.stock.doctype.stock_entry.stock_entry import get_operating_cost_per_unit + from erpnext.stock.doctype.stock_entry.stock_entry import ( + get_consumed_operating_cost, + get_operating_cost_per_unit, + ) operating_cost_per_unit = get_operating_cost_per_unit(work_order, stock_entry.bom_no) if operating_cost_per_unit: cost_added = add_operating_cost_component_wise( - stock_entry, work_order, operating_cost_per_unit, expense_account, job_card=job_card + stock_entry, + work_order, + get_consumed_operating_cost(work_order.name, stock_entry.bom_no), + expense_account, + job_card=job_card, ) if not cost_added: diff --git a/erpnext/stock/doctype/stock_entry/stock_entry.py b/erpnext/stock/doctype/stock_entry/stock_entry.py index 540de088c42..b386955f9a5 100644 --- a/erpnext/stock/doctype/stock_entry/stock_entry.py +++ b/erpnext/stock/doctype/stock_entry/stock_entry.py @@ -3417,26 +3417,27 @@ def get_work_order_details(work_order, company): } -def get_operating_cost_per_unit(work_order=None, bom_no=None): - def get_consumed_operating_cost(wo_name, bom_no): - table = frappe.qb.DocType("Stock Entry") - child_table = frappe.qb.DocType("Landed Cost Taxes and Charges") - query = ( - frappe.qb.from_(child_table) - .join(table) - .on(child_table.parent == table.name) - .select(Sum(child_table.amount).as_("consumed_cost")) - .where( - (table.docstatus == 1) - & (table.work_order == wo_name) - & (table.purpose == "Manufacture") - & (table.bom_no == bom_no) - & (child_table.has_operating_cost == 1) - ) +def get_consumed_operating_cost(wo_name, bom_no): + table = frappe.qb.DocType("Stock Entry") + child_table = frappe.qb.DocType("Landed Cost Taxes and Charges") + query = ( + frappe.qb.from_(child_table) + .join(table) + .on(child_table.parent == table.name) + .select(Sum(child_table.amount).as_("consumed_cost")) + .where( + (table.docstatus == 1) + & (table.work_order == wo_name) + & (table.purpose == "Manufacture") + & (table.bom_no == bom_no) + & (child_table.has_operating_cost == 1) ) - cost = query.run(pluck="consumed_cost") - return cost[0] if cost and cost[0] else 0 + ) + cost = query.run(pluck="consumed_cost") + return cost[0] if cost and cost[0] else 0 + +def get_operating_cost_per_unit(work_order=None, bom_no=None): operating_cost_per_unit = 0 if work_order: if (