fixup! feat(payment): add advance payment status to advance payment doctypes to better track advance payments
diff --git a/erpnext/buying/doctype/purchase_order/purchase_order.json b/erpnext/buying/doctype/purchase_order/purchase_order.json
index f1ecdf5..97f2310 100644
--- a/erpnext/buying/doctype/purchase_order/purchase_order.json
+++ b/erpnext/buying/doctype/purchase_order/purchase_order.json
@@ -1272,7 +1272,6 @@
    "show_dashboard": 1
   },
   {
-   "default": "Not Initiated",
    "fieldname": "advance_payment_status",
    "fieldtype": "Select",
    "hidden": 1,
diff --git a/erpnext/patches.txt b/erpnext/patches.txt
index e9c056e..4c7d8e5 100644
--- a/erpnext/patches.txt
+++ b/erpnext/patches.txt
@@ -344,5 +344,6 @@
 erpnext.patches.v14_0.migrate_deferred_accounts_to_item_defaults
 erpnext.patches.v14_0.update_invoicing_period_in_subscription
 execute:frappe.delete_doc("Page", "welcome-to-erpnext")
+erpnext.patches.v15_0.create_advance_payment_status
 # below migration patch should always run last
 erpnext.patches.v14_0.migrate_gl_to_payment_ledger
diff --git a/erpnext/patches/v15_0/create_advance_payment_status.py b/erpnext/patches/v15_0/create_advance_payment_status.py
new file mode 100644
index 0000000..ff5ba8f
--- /dev/null
+++ b/erpnext/patches/v15_0/create_advance_payment_status.py
@@ -0,0 +1,54 @@
+import frappe
+
+
+def execute():
+	"""
+	Description:
+	Calculate the new Advance Payment Statuse column in SO & PO
+	"""
+
+	if frappe.reload_doc("selling", "doctype", "Sales Order"):
+		so = frappe.qb.DocType("Sales Order")
+		frappe.qb.update(so).set(so.advance_payment_status, "Not Requested").where(
+			so.docstatus == 1
+		).where(so.advance_paid == 0.0).run()
+
+		frappe.qb.update(so).set(so.advance_payment_status, "Partially Paid").where(
+			so.docstatus == 1
+		).where(so.advance_payment_status.isnull()).where(
+			so.advance_paid < (so.rounded_total or so.grand_total)
+		).run()
+
+		frappe.qb.update(so).set(so.advance_payment_status, "Paid").where(so.docstatus == 1).where(
+			so.advance_payment_status.isnull()
+		).where(so.advance_paid == (so.rounded_total or so.grand_total)).run()
+
+		pr = frappe.qb.DocType("Payment Request")
+		frappe.qb.update(so).join(pr).on(so.name == pr.reference_name).set(
+			so.advance_payment_status, "Requested"
+		).where(so.docstatus == 1).where(pr.docstatus == 1).where(
+			so.advance_payment_status == "Not Requested"
+		).run()
+
+	if frappe.reload_doc("buying", "doctype", "Purchase Order"):
+		po = frappe.qb.DocType("Purchase Order")
+		frappe.qb.update(po).set(po.advance_payment_status, "Not Initiated").where(
+			po.docstatus == 1
+		).where(po.advance_paid == 0.0).run()
+
+		frappe.qb.update(po).set(po.advance_payment_status, "Partially Paid").where(
+			po.docstatus == 1
+		).where(po.advance_payment_status.isnull()).where(
+			po.advance_paid < (po.rounded_total or po.grand_total)
+		).run()
+
+		frappe.qb.update(po).set(po.advance_payment_status, "Paid").where(po.docstatus == 1).where(
+			po.advance_payment_status.isnull()
+		).where(po.advance_paid == (po.rounded_total or po.grand_total)).run()
+
+		pr = frappe.qb.DocType("Payment Request")
+		frappe.qb.update(po).join(pr).on(po.name == pr.reference_name).set(
+			po.advance_payment_status, "Initiated"
+		).where(po.docstatus == 1).where(pr.docstatus == 1).where(
+			po.advance_payment_status == "Not Initiated"
+		).run()
diff --git a/erpnext/selling/doctype/sales_order/sales_order.json b/erpnext/selling/doctype/sales_order/sales_order.json
index a046808..7fb49a9 100644
--- a/erpnext/selling/doctype/sales_order/sales_order.json
+++ b/erpnext/selling/doctype/sales_order/sales_order.json
@@ -1642,7 +1642,6 @@
    "report_hide": 1
   },
   {
-   "default": "Not Requested",
    "fieldname": "advance_payment_status",
    "fieldtype": "Select",
    "hidden": 1,