chore: move functions to a separate file in utils
diff --git a/erpnext/accounts/doctype/payment_entry/payment_entry.js b/erpnext/accounts/doctype/payment_entry/payment_entry.js
index 5f7e96f..794a4ef 100644
--- a/erpnext/accounts/doctype/payment_entry/payment_entry.js
+++ b/erpnext/accounts/doctype/payment_entry/payment_entry.js
@@ -154,6 +154,7 @@
 		frm.events.set_dynamic_labels(frm);
 		frm.events.show_general_ledger(frm);
 		erpnext.accounts.ledger_preview.show_accounting_ledger_preview(frm);
+		erpnext.accounts.unreconcile_payments.add_unreconcile_btn(frm);
 	},
 
 	validate_company: (frm) => {
diff --git a/erpnext/accounts/doctype/sales_invoice/sales_invoice.js b/erpnext/accounts/doctype/sales_invoice/sales_invoice.js
index 6856d25..d4d9239 100644
--- a/erpnext/accounts/doctype/sales_invoice/sales_invoice.js
+++ b/erpnext/accounts/doctype/sales_invoice/sales_invoice.js
@@ -184,22 +184,7 @@
 			}
 		}
 
-		if (doc.docstatus == 1) {
-			frappe.call({
-				"method": "erpnext.accounts.doctype.unreconcile_payments.unreconcile_payments.doc_has_payments",
-				"args": {
-					"doctype": this.frm.doc.doctype,
-					"docname": this.frm.doc.name
-				},
-				callback: function(r) {
-					if (r.message) {
-						me.frm.add_custom_button(__("Un-Reconcile"), function() {
-							erpnext.utils.build_unreconcile_dialog(cur_frm);
-						});
-					}
-				}
-			});
-		}
+		erpnext.accounts.unreconcile_payments.add_unreconcile_btn(me.frm);
 	}
 
 
diff --git a/erpnext/accounts/doctype/unreconcile_payments/unreconcile_payments.py b/erpnext/accounts/doctype/unreconcile_payments/unreconcile_payments.py
index cced2b3..c80365b 100644
--- a/erpnext/accounts/doctype/unreconcile_payments/unreconcile_payments.py
+++ b/erpnext/accounts/doctype/unreconcile_payments/unreconcile_payments.py
@@ -117,15 +117,22 @@
 
 
 @frappe.whitelist()
-def create_unreconcile_doc_for_selection(
-	company: str = None, dt: str = None, dn: str = None, selections: list = None
-):
+def create_unreconcile_doc_for_selection(selections=None):
 	if selections:
+		selections = frappe.json.loads(selections)
 		# assuming each row is a unique voucher
 		for row in selections:
 			unrecon = frappe.new_doc("Unreconcile Payments")
-			unrecon.company = company
-			unrecon.voucher_type = dt
-			unrecon.voucher_type = dn
+			unrecon.company = row.get("company")
+			unrecon.voucher_type = row.get("voucher_type")
+			unrecon.voucher_no = row.get("voucher_no")
 			unrecon.add_references()
+
 			# remove unselected references
+			unrecon.allocations = [
+				x
+				for x in unrecon.allocations
+				if x.reference_doctype == row.get("against_voucher_type")
+				and x.reference_name == row.get("against_voucher_no")
+			]
+			unrecon.save().submit()
diff --git a/erpnext/public/js/erpnext.bundle.js b/erpnext/public/js/erpnext.bundle.js
index 966a9e1..0e1b23b 100644
--- a/erpnext/public/js/erpnext.bundle.js
+++ b/erpnext/public/js/erpnext.bundle.js
@@ -16,7 +16,8 @@
 import "./utils/supplier_quick_entry";
 import "./call_popup/call_popup";
 import "./utils/dimension_tree_filter";
-import "./utils/ledger_preview.js"
+import "./utils/ledger_preview.js";
+import "./utils/unreconcile.js";
 import "./utils/barcode_scanner";
 import "./telephony";
 import "./templates/call_link.html";
diff --git a/erpnext/public/js/utils.js b/erpnext/public/js/utils.js
index d3442af..d435711 100755
--- a/erpnext/public/js/utils.js
+++ b/erpnext/public/js/utils.js
@@ -769,61 +769,8 @@
 	dialog.show();
 }
 
-erpnext.utils.build_unreconcile_dialog = function(frm) {
-	if (['Sales Invoice', 'Purchase Invoice', 'Payment Entry', 'Journal Entry'].includes(frm.doc.doctype)) {
-		let child_table_fields = [
-			{ label: __("Voucher Type"), fieldname: "voucher_type", fieldtype: "Dynamic Link", options: "DocType", in_list_view: 1, read_only: 1},
-			{ label: __("Voucher No"), fieldname: "voucher_no", fieldtype: "Link", options: "voucher_type", in_list_view: 1, read_only: 1 },
-			{ label: __("Allocated Amount"), fieldname: "allocated_amount", fieldtype: "Float", in_list_view: 1, read_only: 1 },
-		]
-		let unreconcile_dialog_fields = [
-			{
-				label: __('Allocations'),
-				fieldname: 'allocations',
-				fieldtype: 'Table',
-				read_only: 1,
-				fields: child_table_fields,
-			},
-		];
 
-		// get linked payments
-		frappe.call({
-			"method": "erpnext.accounts.doctype.unreconcile_payments.unreconcile_payments.get_linked_payments_for_doc",
-			"args": {
-				"company": frm.doc.company,
-				"doctype": frm.doc.doctype,
-				"docname": frm.doc.name
-			},
-			callback: function(r) {
-				if (r.message) {
-					// populate child table with allocations
-					unreconcile_dialog_fields[0].data = r.message;
-					unreconcile_dialog_fields[0].get_data = function(){ return r.message};
 
-					let d = new frappe.ui.Dialog({
-						title: 'Un-Reconcile Allocations',
-						fields: unreconcile_dialog_fields,
-						size: 'large',
-						cannot_add_rows: 1,
-						primary_action_label: 'Un-Reconcile',
-						primary_action(values) {
-
-							let selected_allocations = values.allocations.filter(x=>x.__checked);
-							if (selected_allocations.length > 0) {
-								// assuming each row is an individual voucher
-								// pass this to server side method that created unreconcile doc for row
-							} else {
-								frappe.msgprint("No Selection");
-							}
-						}
-					});
-
-					d.show();
-				}
-			}
-		});
-	}
-}
 
 erpnext.utils.map_current_doc = function(opts) {
 	function _map() {
diff --git a/erpnext/public/js/utils/unreconcile.js b/erpnext/public/js/utils/unreconcile.js
new file mode 100644
index 0000000..509cd39
--- /dev/null
+++ b/erpnext/public/js/utils/unreconcile.js
@@ -0,0 +1,106 @@
+frappe.provide('erpnext.accounts');
+
+erpnext.accounts.unreconcile_payments = {
+	add_unreconcile_btn(frm) {
+		if (frm.doc.docstatus == 1) {
+			frappe.call({
+				"method": "erpnext.accounts.doctype.unreconcile_payments.unreconcile_payments.doc_has_payments",
+				"args": {
+					"doctype": frm.doc.doctype,
+					"docname": frm.doc.name
+				},
+				callback: function(r) {
+					if (r.message) {
+						frm.add_custom_button(__("Un-Reconcile"), function() {
+							erpnext.accounts.unreconcile_payments.build_unreconcile_dialog(frm);
+						});
+					}
+				}
+			});
+		}
+	},
+
+	build_unreconcile_dialog(frm) {
+		if (['Sales Invoice', 'Purchase Invoice', 'Payment Entry', 'Journal Entry'].includes(frm.doc.doctype)) {
+			let child_table_fields = [
+				{ label: __("Voucher Type"), fieldname: "voucher_type", fieldtype: "Dynamic Link", options: "DocType", in_list_view: 1, read_only: 1},
+				{ label: __("Voucher No"), fieldname: "voucher_no", fieldtype: "Link", options: "voucher_type", in_list_view: 1, read_only: 1 },
+				{ label: __("Allocated Amount"), fieldname: "allocated_amount", fieldtype: "Float", in_list_view: 1, read_only: 1 },
+			]
+			let unreconcile_dialog_fields = [
+				{
+					label: __('Allocations'),
+					fieldname: 'allocations',
+					fieldtype: 'Table',
+					read_only: 1,
+					fields: child_table_fields,
+				},
+			];
+
+			// get linked payments
+			frappe.call({
+				"method": "erpnext.accounts.doctype.unreconcile_payments.unreconcile_payments.get_linked_payments_for_doc",
+				"args": {
+					"company": frm.doc.company,
+					"doctype": frm.doc.doctype,
+					"docname": frm.doc.name
+				},
+				callback: function(r) {
+					if (r.message) {
+						// populate child table with allocations
+						unreconcile_dialog_fields[0].data = r.message;
+						unreconcile_dialog_fields[0].get_data = function(){ return r.message};
+
+						let d = new frappe.ui.Dialog({
+							title: 'Un-Reconcile Allocations',
+							fields: unreconcile_dialog_fields,
+							size: 'large',
+							cannot_add_rows: 1,
+							primary_action_label: 'Un-Reconcile',
+							primary_action(values) {
+
+								let selected_allocations = values.allocations.filter(x=>x.__checked);
+								if (selected_allocations.length > 0) {
+									// assuming each row is an individual voucher
+									// pass this to server side method that creates unreconcile doc for each row
+									if (['Sales Invoice', 'Purchase Invoice'].includes(frm.doc.doctype)) {
+										let selection_map = selected_allocations.map(function(elem) {
+											return {
+												company: elem.company,
+												voucher_type: elem.voucher_type,
+												voucher_no: elem.voucher_no,
+												against_voucher_type: frm.doc.doctype,
+												against_voucher_no: frm.doc.name
+											};
+
+										});
+
+										erpnext.utils.create_unreconcile_docs(selection_map);
+										d.hide();
+									}
+
+								} else {
+									frappe.msgprint("No Selection");
+								}
+							}
+						});
+
+						d.show();
+					}
+				}
+			});
+		}
+	},
+
+	create_unreconcile_docs(selection_map) {
+		frappe.call({
+			"method": "erpnext.accounts.doctype.unreconcile_payments.unreconcile_payments.create_unreconcile_doc_for_selection",
+			"args": {
+				"selections": selection_map
+			},
+		});
+	}
+
+
+
+}