Merge pull request #14628 from Zlash65/fix-pe

Expense Claim - Payment Entry related fix
diff --git a/erpnext/accounts/doctype/payment_entry/payment_entry.js b/erpnext/accounts/doctype/payment_entry/payment_entry.js
index fc3eed3..c353e5e 100644
--- a/erpnext/accounts/doctype/payment_entry/payment_entry.js
+++ b/erpnext/accounts/doctype/payment_entry/payment_entry.js
@@ -832,6 +832,11 @@
 					$.each(r.message, function(field, value) {
 						frappe.model.set_value(cdt, cdn, field, value);
 					})
+
+					let allocated_amount = frm.doc.unallocated_amount > row.outstanding_amount ?
+						row.outstanding_amount : frm.doc.unallocated_amount;
+
+					frappe.model.set_value(cdt, cdn, 'allocated_amount', allocated_amount);
 					frm.refresh_fields();
 				}
 			}
diff --git a/erpnext/accounts/doctype/payment_entry/payment_entry.py b/erpnext/accounts/doctype/payment_entry/payment_entry.py
index 4e99859..9d5adda 100644
--- a/erpnext/accounts/doctype/payment_entry/payment_entry.py
+++ b/erpnext/accounts/doctype/payment_entry/payment_entry.py
@@ -725,8 +725,11 @@
 			exchange_rate = ref_doc.get("conversion_rate") or \
 				get_exchange_rate(party_account_currency, company_currency, ref_doc.posting_date)
 
-		if reference_doctype in ("Sales Invoice", "Purchase Invoice", "Expense Claim"):
+		if reference_doctype in ("Sales Invoice", "Purchase Invoice"):
 			outstanding_amount = ref_doc.get("outstanding_amount")
+		elif reference_doctype == "Expense Claim":
+			outstanding_amount = flt(ref_doc.get("total_sanctioned_amount")) \
+				- flt(ref_doc.get("total_amount+reimbursed")) - flt(ref_doc.get("total_advance_amount"))
 		elif reference_doctype == "Employee Advance":
 			outstanding_amount = ref_doc.advance_amount - flt(ref_doc.paid_amount)
 		else: