diff --git a/erpnext/manufacturing/doctype/production_plan/production_plan.py b/erpnext/manufacturing/doctype/production_plan/production_plan.py index 94b533fbecd..579dca4a8df 100644 --- a/erpnext/manufacturing/doctype/production_plan/production_plan.py +++ b/erpnext/manufacturing/doctype/production_plan/production_plan.py @@ -385,6 +385,15 @@ class ProductionPlan(Document): pi = frappe.qb.DocType("Packed Item") + pending_qty = ( + frappe.qb.terms.Case() + .when( + (so_item.work_order_qty > so_item.delivered_qty), + (((so_item.qty - so_item.work_order_qty) * pi.qty) / so_item.qty), + ) + .else_(((so_item.qty - so_item.delivered_qty) * pi.qty) / so_item.qty) + ) + packed_items_query = ( frappe.qb.from_(so_item) .from_(pi) @@ -392,7 +401,7 @@ class ProductionPlan(Document): pi.parent, pi.item_code, pi.warehouse.as_("warehouse"), - (((so_item.qty - so_item.work_order_qty) * pi.qty) / so_item.qty).as_("pending_qty"), + pending_qty.as_("pending_qty"), pi.parent_item, pi.description, so_item.name, @@ -403,7 +412,16 @@ class ProductionPlan(Document): & (so_item.docstatus == 1) & (pi.parent_item == so_item.item_code) & (so_item.parent.isin(so_list)) - & (so_item.qty > so_item.work_order_qty) + & ( + ( + (so_item.work_order_qty > so_item.delivered_qty) + & (so_item.qty > so_item.work_order_qty) + ) + | ( + (so_item.work_order_qty <= so_item.delivered_qty) + & (so_item.qty > so_item.delivered_qty) + ) + ) & ( ExistsCriterion( frappe.qb.from_(bom)