refactor: adding 'Get Allocations' button
diff --git a/erpnext/accounts/doctype/unreconcile_payment_entries/unreconcile_payment_entries.json b/erpnext/accounts/doctype/unreconcile_payment_entries/unreconcile_payment_entries.json
index f70f4db..c4afaa8 100644
--- a/erpnext/accounts/doctype/unreconcile_payment_entries/unreconcile_payment_entries.json
+++ b/erpnext/accounts/doctype/unreconcile_payment_entries/unreconcile_payment_entries.json
@@ -30,7 +30,8 @@
    "fieldname": "unlinked",
    "fieldtype": "Check",
    "in_list_view": 1,
-   "label": "Unlinked"
+   "label": "Unlinked",
+   "read_only": 1
   },
   {
    "fieldname": "reference_doctype",
@@ -43,7 +44,7 @@
  "index_web_pages_for_search": 1,
  "istable": 1,
  "links": [],
- "modified": "2023-08-22 15:00:33.203161",
+ "modified": "2023-08-24 14:48:10.018574",
  "modified_by": "Administrator",
  "module": "Accounts",
  "name": "Unreconcile Payment Entries",
diff --git a/erpnext/accounts/doctype/unreconcile_payments/unreconcile_payments.js b/erpnext/accounts/doctype/unreconcile_payments/unreconcile_payments.js
index 03a8253..ef7c958 100644
--- a/erpnext/accounts/doctype/unreconcile_payments/unreconcile_payments.js
+++ b/erpnext/accounts/doctype/unreconcile_payments/unreconcile_payments.js
@@ -22,4 +22,20 @@
 		});
 
 	},
+	get_allocations: function(frm) {
+		frm.clear_table("allocations");
+		frappe.call({
+			method: "get_allocations_from_payment",
+			doc: frm.doc,
+			callback: function(r) {
+				if (r.message) {
+					r.message.forEach(x => {
+						frm.add_child("allocations", x)
+					})
+					frm.refresh_fields();
+				}
+			}
+		})
+
+	}
 });
diff --git a/erpnext/accounts/doctype/unreconcile_payments/unreconcile_payments.json b/erpnext/accounts/doctype/unreconcile_payments/unreconcile_payments.json
index f4b3cd7..68af5dc 100644
--- a/erpnext/accounts/doctype/unreconcile_payments/unreconcile_payments.json
+++ b/erpnext/accounts/doctype/unreconcile_payments/unreconcile_payments.json
@@ -11,7 +11,8 @@
   "company",
   "voucher_type",
   "voucher_no",
-  "references",
+  "get_allocations",
+  "allocations",
   "amended_from"
  ],
  "fields": [
@@ -43,16 +44,21 @@
    "options": "voucher_type"
   },
   {
-   "fieldname": "references",
+   "fieldname": "get_allocations",
+   "fieldtype": "Button",
+   "label": "Get Allocations"
+  },
+  {
+   "fieldname": "allocations",
    "fieldtype": "Table",
-   "label": "References",
+   "label": "Allocations",
    "options": "Unreconcile Payment Entries"
   }
  ],
  "index_web_pages_for_search": 1,
  "is_submittable": 1,
  "links": [],
- "modified": "2023-08-22 14:11:13.073414",
+ "modified": "2023-08-24 16:53:50.767700",
  "modified_by": "Administrator",
  "module": "Accounts",
  "name": "Unreconcile Payments",
diff --git a/erpnext/accounts/doctype/unreconcile_payments/unreconcile_payments.py b/erpnext/accounts/doctype/unreconcile_payments/unreconcile_payments.py
index df08d79..ab2cc71 100644
--- a/erpnext/accounts/doctype/unreconcile_payments/unreconcile_payments.py
+++ b/erpnext/accounts/doctype/unreconcile_payments/unreconcile_payments.py
@@ -2,25 +2,44 @@
 # For license information, please see license.txt
 
 import frappe
+from frappe import qb
 from frappe.model.document import Document
+from frappe.query_builder.functions import Sum
 
 from erpnext.accounts.utils import unlink_ref_doc_from_payment_entries, update_voucher_outstanding
 
 
 class UnreconcilePayments(Document):
-	def before_save(self):
-		if self.voucher_type == "Payment Entry":
-			references = frappe.db.get_all(
-				"Payment Entry Reference",
-				filters={"docstatus": 1, "parent": self.voucher_no},
-				fields=["reference_doctype", "reference_name", "allocated_amount"],
-			)
+	# def validate(self):
+	# 	parent = set([alloc.parent for alloc in self.allocations])
+	# 	if len(parent) != 1:
+	# 		pass
 
-			self.set("references", [])
-			for ref in references:
-				self.append("references", ref)
+	@frappe.whitelist()
+	def get_allocations_from_payment(self):
+		if self.voucher_type == "Payment Entry":
+			per = qb.DocType("Payment Entry Reference")
+			allocated_references = (
+				qb.from_(per)
+				.select(
+					per.reference_doctype, per.reference_name, Sum(per.allocated_amount).as_("allocated_amount")
+				)
+				.where((per.docstatus == 1) & (per.parent == self.voucher_no))
+				.groupby(per.reference_name)
+				.run(as_dict=True)
+			)
+			return allocated_references
+
+	def add_references(self):
+		allocations = self.get_allocations_from_payment()
+
+		for alloc in allocations:
+			self.append("allocations", alloc)
 
 	def on_submit(self):
+		# todo: add more granular unlinking
+		# different amounts for same invoice should be individually unlinkable
+
 		payment_type, paid_from, paid_to, party_type, party = frappe.db.get_all(
 			self.voucher_type,
 			filters={"name": self.voucher_no},
@@ -29,10 +48,10 @@
 		)[0]
 		account = paid_from if payment_type == "Receive" else paid_to
 
-		for ref in self.references:
-			doc = frappe.get_doc(ref.reference_doctype, ref.reference_name)
+		for alloc in self.allocations:
+			doc = frappe.get_doc(alloc.reference_doctype, alloc.reference_name)
 			unlink_ref_doc_from_payment_entries(doc)
 			update_voucher_outstanding(
-				ref.reference_doctype, ref.reference_name, account, party_type, party
+				alloc.reference_doctype, alloc.reference_name, account, party_type, party
 			)
-			frappe.db.set_value("Unreconcile Payment Entries", ref.name, "unlinked", True)
+			frappe.db.set_value("Unreconcile Payment Entries", alloc.name, "unlinked", True)