Payment Entry fix: Unallocated amount in payment entry considering deductions
diff --git a/erpnext/accounts/doctype/payment_entry/payment_entry.js b/erpnext/accounts/doctype/payment_entry/payment_entry.js
index d155ecd..e2e8340 100644
--- a/erpnext/accounts/doctype/payment_entry/payment_entry.js
+++ b/erpnext/accounts/doctype/payment_entry/payment_entry.js
@@ -95,13 +95,13 @@
 			frm.doc.paid_to_account_currency != company_currency &&
 			frm.doc.paid_from_account_currency != frm.doc.paid_to_account_currency));
 
-			frm.toggle_display("base_paid_amount", frm.doc.paid_from_account_currency != company_currency);
+		frm.toggle_display("base_paid_amount", frm.doc.paid_from_account_currency != company_currency);
 
 		frm.toggle_display("base_received_amount", (frm.doc.paid_to_account_currency != company_currency &&
 			frm.doc.paid_from_account_currency != frm.doc.paid_to_account_currency));
 
-		frm.toggle_display("received_amount",
-			frm.doc.paid_from_account_currency != frm.doc.paid_to_account_currency)
+		frm.toggle_display("received_amount", (frm.doc.payment_type=="Internal Transfer" ||
+			frm.doc.paid_from_account_currency != frm.doc.paid_to_account_currency))
 
 		frm.toggle_display(["base_total_allocated_amount"],
 			(frm.doc.paid_amount && frm.doc.received_amount && frm.doc.base_total_allocated_amount &&
@@ -601,9 +601,17 @@
 		if(frm.doc.party) {
 			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.total_allocated_amount < party_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);
 
diff --git a/erpnext/accounts/doctype/payment_entry/payment_entry.py b/erpnext/accounts/doctype/payment_entry/payment_entry.py
index 175ebda..c5cf092 100644
--- a/erpnext/accounts/doctype/payment_entry/payment_entry.py
+++ b/erpnext/accounts/doctype/payment_entry/payment_entry.py
@@ -247,8 +247,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)