diff --git a/erpnext/manufacturing/doctype/work_order/work_order.py b/erpnext/manufacturing/doctype/work_order/work_order.py index 2a987313345..dcdc00fd660 100644 --- a/erpnext/manufacturing/doctype/work_order/work_order.py +++ b/erpnext/manufacturing/doctype/work_order/work_order.py @@ -500,7 +500,7 @@ class WorkOrder(Document): # already ordered qty ordered_qty_against_so = frappe.db.sql( """select sum(qty) from `tabWork Order` - where production_item = %s and sales_order = %s and docstatus < 2 and name != %s""", + where production_item = %s and sales_order = %s and docstatus < 2 and status != 'Closed' and name != %s""", (self.production_item, self.sales_order, self.name), )[0][0] @@ -768,6 +768,9 @@ class WorkOrder(Document): self.validate_cancel() self.db_set("status", "Cancelled") + self.on_close_or_cancel() + + def on_close_or_cancel(self): if self.production_plan and frappe.db.exists( "Production Plan Item Reference", {"parent": self.production_plan} ): @@ -1170,7 +1173,7 @@ class WorkOrder(Document): qty = frappe.db.sql( f""" select sum(qty) from - `tabWork Order` where sales_order = %s and docstatus = 1 and {cond} + `tabWork Order` where sales_order = %s and docstatus = 1 and status <> 'Closed' and {cond} """, (self.sales_order, (self.product_bundle_item or self.production_item)), as_list=1, @@ -2010,7 +2013,9 @@ def make_stock_reservation_entries(doc, items=None, is_transfer=True, notify=Fal items = parse_json(items) sre = StockReservation(doc, items=items, notify=notify) - if doc.docstatus == 1: + if doc.docstatus == 2 or doc.status == "Closed": + sre.cancel_stock_reservation_entries() + elif doc.docstatus == 1: if doc.production_plan and is_transfer: sre.transfer_reservation_entries_to( doc.production_plan, from_doctype="Production Plan", to_doctype="Work Order" @@ -2028,9 +2033,6 @@ def make_stock_reservation_entries(doc, items=None, is_transfer=True, notify=Fal if sre_created: frappe.msgprint(_("Stock Reservation Entries Created"), alert=True) - elif doc.docstatus == 2: - sre.cancel_stock_reservation_entries() - doc.reload() doc.db_set("status", doc.get_status()) @@ -2422,8 +2424,8 @@ def close_work_order(work_order, status): ) ) + work_order.on_close_or_cancel() work_order.update_status(status) - work_order.update_planned_qty() frappe.msgprint(_("Work Order has been {0}").format(status)) work_order.notify_update() return work_order.status diff --git a/erpnext/selling/doctype/sales_order/sales_order.py b/erpnext/selling/doctype/sales_order/sales_order.py index fb684155d45..d24ed088e37 100755 --- a/erpnext/selling/doctype/sales_order/sales_order.py +++ b/erpnext/selling/doctype/sales_order/sales_order.py @@ -1966,6 +1966,7 @@ def get_work_order_items(sales_order, for_raw_material_request=0): & (wo.sales_order == so.name) & (wo.sales_order_item == i.name) & (wo.docstatus.lt(2)) + & (wo.status != "Closed") ) .run()[0][0] ) diff --git a/erpnext/stock/doctype/material_request/material_request.py b/erpnext/stock/doctype/material_request/material_request.py index 6f12fd05ea7..54372fa656a 100644 --- a/erpnext/stock/doctype/material_request/material_request.py +++ b/erpnext/stock/doctype/material_request/material_request.py @@ -304,6 +304,9 @@ class MaterialRequest(BuyingController): .groupby(doctype.material_request_item) ) + if self.material_request_type == "Manufacture": + query = query.where(doctype.status != "Closed") + mr_items_ordered_qty = frappe._dict(query.run()) return mr_items_ordered_qty