fix: Multiple fixes in payment entry
diff --git a/erpnext/accounts/doctype/payment_entry/payment_entry.py b/erpnext/accounts/doctype/payment_entry/payment_entry.py
index a131a81..2231b47 100644
--- a/erpnext/accounts/doctype/payment_entry/payment_entry.py
+++ b/erpnext/accounts/doctype/payment_entry/payment_entry.py
@@ -45,7 +45,7 @@
 			self.party_account = self.paid_to
 			self.party_account_currency = self.paid_to_account_currency
 
-	def validate(self):
+	def validate(self, on_reference_unlink=False):
 		self.setup_party_account_field()
 		self.set_missing_values()
 		self.validate_payment_type()
@@ -64,8 +64,9 @@
 		self.set_title()
 		self.set_remarks()
 		self.validate_duplicate_entry()
-		self.validate_allocated_amount()
-		self.validate_paid_invoices()
+		if not on_reference_unlink:
+			self.validate_allocated_amount()
+			self.validate_paid_invoices()
 		self.ensure_supplier_is_not_blocked()
 		self.set_status()
 
@@ -529,8 +530,10 @@
 				base_total_allocated_amount += flt(flt(d.allocated_amount) * flt(d.exchange_rate),
 					self.precision("base_paid_amount"))
 
-		self.total_allocated_amount = abs(total_allocated_amount)
-		self.base_total_allocated_amount = abs(base_total_allocated_amount)
+		# Do not use absolute values as only credit notes could be allocated
+		# and total allocated should be negative in that scenario
+		self.total_allocated_amount = total_allocated_amount
+		self.base_total_allocated_amount = base_total_allocated_amount
 
 	def set_unallocated_amount(self):
 		self.unallocated_amount = 0
diff --git a/erpnext/accounts/utils.py b/erpnext/accounts/utils.py
index 9272bc4..11e113d 100644
--- a/erpnext/accounts/utils.py
+++ b/erpnext/accounts/utils.py
@@ -553,10 +553,14 @@
 			and docstatus < 2""", (now(), frappe.session.user, ref_type, ref_no))
 
 		for pe in linked_pe:
-			pe_doc = frappe.get_doc("Payment Entry", pe)
-			pe_doc.set_total_allocated_amount()
-			pe_doc.set_unallocated_amount()
-			pe_doc.clear_unallocated_reference_document_rows()
+			try:
+				pe_doc = frappe.get_doc("Payment Entry", pe)
+				pe_doc.validate(on_reference_unlink=True)
+			except Exception as e:
+				msg = _("There were issues unlinking payment entry {0}.").format(pe_doc.name)
+				msg += '<br>'
+				msg += _("Please cancel payment entry manually first and then resubmit")
+				frappe.throw(msg, title=_("Payment Unlink Error"))
 
 			frappe.db.sql("""update `tabPayment Entry` set total_allocated_amount=%s,
 				base_total_allocated_amount=%s, unallocated_amount=%s, modified=%s, modified_by=%s