[Fix] Unallocated amount considering deductions (#11314)

diff --git a/erpnext/accounts/doctype/payment_entry/payment_entry.js b/erpnext/accounts/doctype/payment_entry/payment_entry.js
index 04db9e2..fc9fc97 100644
--- a/erpnext/accounts/doctype/payment_entry/payment_entry.js
+++ b/erpnext/accounts/doctype/payment_entry/payment_entry.js
@@ -660,8 +660,15 @@
 			var party_amount = frm.doc.payment_type=="Receive" ?
 				frm.doc.paid_amount : frm.doc.received_amount;
 
+			var total_deductions = frappe.utils.sum($.map(frm.doc.deductions || [],
+				function(d) { return flt(d.amount) }));
+
 			if(frm.doc.total_allocated_amount < party_amount) {
-				unallocated_amount = party_amount - frm.doc.total_allocated_amount;
+				if(frm.doc.payment_type == "Receive") {
+					unallocated_amount = party_amount - (frm.doc.total_allocated_amount - total_deductions);
+				} else {
+					unallocated_amount = party_amount - (frm.doc.total_allocated_amount + total_deductions);
+				}
 			}
 		}
 		frm.set_value("unallocated_amount", unallocated_amount);
@@ -680,9 +687,6 @@
 			difference_amount = flt(frm.doc.base_paid_amount) - flt(frm.doc.base_received_amount);
 		}
 
-		var total_deductions = frappe.utils.sum($.map(frm.doc.deductions || [],
-			function(d) { return flt(d.amount) }));
-
 		frm.set_value("difference_amount", difference_amount - total_deductions);
 
 		frm.events.hide_unhide_fields(frm);
diff --git a/erpnext/accounts/doctype/payment_entry/payment_entry.py b/erpnext/accounts/doctype/payment_entry/payment_entry.py
index 9d24261..56bdfba 100644
--- a/erpnext/accounts/doctype/payment_entry/payment_entry.py
+++ b/erpnext/accounts/doctype/payment_entry/payment_entry.py
@@ -285,8 +285,13 @@
 		if self.party:
 			party_amount = self.paid_amount if self.payment_type=="Receive" else self.received_amount
 
+			total_deductions = sum([flt(d.amount) for d in self.get("deductions")])
+
 			if self.total_allocated_amount < party_amount:
-				self.unallocated_amount = party_amount - self.total_allocated_amount
+				if self.payment_type == "Receive":
+					self.unallocated_amount = party_amount - (self.total_allocated_amount - total_deductions)
+				else:
+					self.unallocated_amount = party_amount - (self.total_allocated_amount + total_deductions)
 
 	def set_difference_amount(self):
 		base_unallocated_amount = flt(self.unallocated_amount) * (flt(self.source_exchange_rate)