fix(patch): remove dead links to ProdPlan Item
diff --git a/erpnext/patches.txt b/erpnext/patches.txt
index ebda805..16d8c73 100644
--- a/erpnext/patches.txt
+++ b/erpnext/patches.txt
@@ -358,4 +358,5 @@
erpnext.patches.v14_0.update_batch_valuation_flag
erpnext.patches.v14_0.delete_non_profit_doctypes
erpnext.patches.v14_0.update_employee_advance_status
-erpnext.patches.v13_0.add_cost_center_in_loans
\ No newline at end of file
+erpnext.patches.v13_0.add_cost_center_in_loans
+erpnext.patches.v13_0.remove_unknown_links_to_prod_plan_items
diff --git a/erpnext/patches/v13_0/remove_unknown_links_to_prod_plan_items.py b/erpnext/patches/v13_0/remove_unknown_links_to_prod_plan_items.py
new file mode 100644
index 0000000..317e85e
--- /dev/null
+++ b/erpnext/patches/v13_0/remove_unknown_links_to_prod_plan_items.py
@@ -0,0 +1,34 @@
+import frappe
+
+
+def execute():
+ """
+ Remove "production_plan_item" field where linked field doesn't exist in tha table.
+ """
+
+ work_order = frappe.qb.DocType("Work Order")
+ pp_item = frappe.qb.DocType("Production Plan Item")
+
+ broken_work_orders = (
+ frappe.qb
+ .from_(work_order)
+ .left_join(pp_item).on(work_order.production_plan_item == pp_item.name)
+ .select(work_order.name)
+ .where(
+ (work_order.docstatus == 0)
+ & (work_order.production_plan_item.notnull())
+ & (work_order.production_plan_item.like("new-production-plan%"))
+ & (pp_item.name.isnull())
+ )
+ ).run(pluck=True)
+
+ if not broken_work_orders:
+ return
+
+ (frappe.qb
+ .update(work_order)
+ .set(work_order.production_plan_item, None)
+ .where(work_order.name.isin(broken_work_orders))
+ ).run()
+
+