Merge pull request #33548 from rohitwaghchaure/fixed-incorrect-wo-status

fix: incorrect status in the work order
diff --git a/erpnext/manufacturing/doctype/work_order/test_work_order.py b/erpnext/manufacturing/doctype/work_order/test_work_order.py
index 76040b2..729ed42 100644
--- a/erpnext/manufacturing/doctype/work_order/test_work_order.py
+++ b/erpnext/manufacturing/doctype/work_order/test_work_order.py
@@ -890,6 +890,9 @@
 		self.assertEqual(se.process_loss_percentage, 10)
 		self.assertEqual(se.process_loss_qty, 1)
 
+		wo.load_from_db()
+		self.assertEqual(wo.status, "In Process")
+
 	@timeout(seconds=60)
 	def test_job_card_scrap_item(self):
 		items = [
diff --git a/erpnext/manufacturing/doctype/work_order/work_order.py b/erpnext/manufacturing/doctype/work_order/work_order.py
index 2b30641..ae9e9c6 100644
--- a/erpnext/manufacturing/doctype/work_order/work_order.py
+++ b/erpnext/manufacturing/doctype/work_order/work_order.py
@@ -246,21 +246,11 @@
 			status = "Draft"
 		elif self.docstatus == 1:
 			if status != "Stopped":
-				stock_entries = frappe._dict(
-					frappe.db.sql(
-						"""select purpose, sum(fg_completed_qty)
-					from `tabStock Entry` where work_order=%s and docstatus=1
-					group by purpose""",
-						self.name,
-					)
-				)
-
 				status = "Not Started"
-				if stock_entries:
+				if flt(self.material_transferred_for_manufacturing) > 0:
 					status = "In Process"
-					produced_qty = stock_entries.get("Manufacture")
-					if flt(produced_qty) >= flt(self.qty):
-						status = "Completed"
+				if flt(self.produced_qty) >= flt(self.qty):
+					status = "Completed"
 		else:
 			status = "Cancelled"
 
@@ -309,12 +299,15 @@
 
 	def get_transferred_or_manufactured_qty(self, purpose):
 		table = frappe.qb.DocType("Stock Entry")
-		query = (
-			frappe.qb.from_(table)
-			.select(Sum(table.fg_completed_qty))
-			.where((table.work_order == self.name) & (table.docstatus == 1) & (table.purpose == purpose))
+		query = frappe.qb.from_(table).where(
+			(table.work_order == self.name) & (table.docstatus == 1) & (table.purpose == purpose)
 		)
 
+		if purpose == "Manufacture":
+			query = query.select(Sum(table.fg_completed_qty) - Sum(table.process_loss_qty))
+		else:
+			query = query.select(Sum(table.fg_completed_qty))
+
 		return flt(query.run()[0][0])
 
 	def set_process_loss_qty(self):
@@ -346,6 +339,7 @@
 
 			produced_qty = total_qty[0][0] if total_qty else 0
 
+		self.update_status()
 		production_plan.run_method(
 			"update_produced_pending_qty", produced_qty, self.production_plan_item
 		)
diff --git a/erpnext/stock/doctype/stock_entry/stock_entry.py b/erpnext/stock/doctype/stock_entry/stock_entry.py
index 500ec04..352ef57 100644
--- a/erpnext/stock/doctype/stock_entry/stock_entry.py
+++ b/erpnext/stock/doctype/stock_entry/stock_entry.py
@@ -1244,7 +1244,6 @@
 		if self.work_order:
 			pro_doc = frappe.get_doc("Work Order", self.work_order)
 			_validate_work_order(pro_doc)
-			pro_doc.run_method("update_status")
 
 			if self.fg_completed_qty:
 				pro_doc.run_method("update_work_order_qty")
@@ -1252,6 +1251,7 @@
 					pro_doc.run_method("update_planned_qty")
 					pro_doc.update_batch_produced_qty(self)
 
+			pro_doc.run_method("update_status")
 			if not pro_doc.operations:
 				pro_doc.set_actual_dates()
 
@@ -1494,9 +1494,10 @@
 			return
 
 		self.process_loss_qty = 0.0
-		self.process_loss_percentage = frappe.get_cached_value(
-			"BOM", self.bom_no, "process_loss_percentage"
-		)
+		if not self.process_loss_percentage:
+			self.process_loss_percentage = frappe.get_cached_value(
+				"BOM", self.bom_no, "process_loss_percentage"
+			)
 
 		if self.process_loss_percentage:
 			self.process_loss_qty = flt(