set the reference document in the payment entry while creating from fees (#11861)

* set the reference document in the payment entry while creating from fees

* allocate the paid amount in the reference document
diff --git a/erpnext/accounts/doctype/payment_entry/payment_entry.js b/erpnext/accounts/doctype/payment_entry/payment_entry.js
index d8995a9..8ecedb5 100644
--- a/erpnext/accounts/doctype/payment_entry/payment_entry.js
+++ b/erpnext/accounts/doctype/payment_entry/payment_entry.js
@@ -512,7 +512,7 @@
 						c.outstanding_amount = d.outstanding_amount;
 						c.bill_no = d.bill_no;
 
-						if(!in_list(["Sales Order", "Purchase Order", "Expense Claim"], d.voucher_type)) {
+						if(!in_list(["Sales Order", "Purchase Order", "Expense Claim", "Fees"], d.voucher_type)) {
 							if(flt(d.outstanding_amount) > 0)
 								total_positive_outstanding += flt(d.outstanding_amount);
 							else
@@ -527,7 +527,7 @@
 						} else {
 							c.exchange_rate = 1;
 						}
-						if (in_list(['Sales Invoice', 'Purchase Invoice', "Expense Claim"], d.reference_doctype)){
+						if (in_list(['Sales Invoice', 'Purchase Invoice', "Expense Claim", "Fees"], d.reference_doctype)){
 							c.due_date = d.due_date;
 						}
 					});
@@ -535,7 +535,8 @@
 					if(
 						(frm.doc.payment_type=="Receive" && frm.doc.party_type=="Customer") ||
 						(frm.doc.payment_type=="Pay" && frm.doc.party_type=="Supplier")  ||
-						(frm.doc.payment_type=="Pay" && frm.doc.party_type=="Employee")
+						(frm.doc.payment_type=="Pay" && frm.doc.party_type=="Employee") ||
+						(frm.doc.payment_type=="Receive" && frm.doc.party_type=="Student") 
 					) {
 						if(total_positive_outstanding > total_negative_outstanding)
 							frm.set_value("paid_amount",
@@ -579,13 +580,16 @@
 		})
 
 		var allocated_negative_outstanding = 0;
-		if ((frm.doc.payment_type=="Receive" && frm.doc.party_type=="Customer") ||
+		if (
+				(frm.doc.payment_type=="Receive" && frm.doc.party_type=="Customer") ||
 				(frm.doc.payment_type=="Pay" && frm.doc.party_type=="Supplier") ||
-				(frm.doc.payment_type=="Pay" && frm.doc.party_type=="Employee")) {
-			if(total_positive_outstanding_including_order > paid_amount) {
-				var remaining_outstanding = total_positive_outstanding_including_order - paid_amount;
-				allocated_negative_outstanding = total_negative_outstanding < remaining_outstanding ?
-					total_negative_outstanding : remaining_outstanding;
+				(frm.doc.payment_type=="Pay" && frm.doc.party_type=="Employee") ||
+				(frm.doc.payment_type=="Receive" && frm.doc.party_type=="Student")
+			) {
+				if(total_positive_outstanding_including_order > paid_amount) {
+					var remaining_outstanding = total_positive_outstanding_including_order - paid_amount;
+					allocated_negative_outstanding = total_negative_outstanding < remaining_outstanding ?
+						total_negative_outstanding : remaining_outstanding;
 			}
 
 			var allocated_positive_outstanding =  paid_amount + allocated_negative_outstanding;
diff --git a/erpnext/accounts/doctype/payment_entry/payment_entry.py b/erpnext/accounts/doctype/payment_entry/payment_entry.py
index 36ff0ac..99ff295 100644
--- a/erpnext/accounts/doctype/payment_entry/payment_entry.py
+++ b/erpnext/accounts/doctype/payment_entry/payment_entry.py
@@ -310,9 +310,10 @@
 		self.difference_amount = flt(self.difference_amount - total_deductions,
 			self.precision("difference_amount"))
 
+	# Paid amount is auto allocated in the reference document by default.
+	# Clear the reference document which doesn't have allocated amount on validate so that form can be loaded fast
 	def clear_unallocated_reference_document_rows(self):
 		self.set("references", self.get("references", {"allocated_amount": ["not in", [0, None, ""]]}))
-
 		frappe.db.sql("""delete from `tabPayment Entry Reference`
 			where parent = %s and allocated_amount = 0""", self.name)
 
diff --git a/erpnext/accounts/utils.py b/erpnext/accounts/utils.py
index ba5d8b3..abdadb3 100644
--- a/erpnext/accounts/utils.py
+++ b/erpnext/accounts/utils.py
@@ -576,14 +576,13 @@
 	outstanding_invoices = []
 	precision = frappe.get_precision("Sales Invoice", "outstanding_amount")
 
-	if party_type == "Customer":
+	if party_type == "Customer" or party_type == "Student":
 		dr_or_cr = "debit_in_account_currency - credit_in_account_currency"
 		payment_dr_or_cr = "payment_gl_entry.credit_in_account_currency - payment_gl_entry.debit_in_account_currency"
 	else:
 		dr_or_cr = "credit_in_account_currency - debit_in_account_currency"
 		payment_dr_or_cr = "payment_gl_entry.debit_in_account_currency - payment_gl_entry.credit_in_account_currency"
 
-	invoice = 'Sales Invoice' if party_type == 'Customer' else 'Purchase Invoice'
 	invoice_list = frappe.db.sql("""
 		select
 			voucher_no,	voucher_type, posting_date, ifnull(sum({dr_or_cr}), 0) as invoice_amount, due_date,
@@ -613,7 +612,6 @@
 		having (invoice_amount - payment_amount) > 0.005
 		order by posting_date, name, due_date""".format(
 			dr_or_cr=dr_or_cr,
-			invoice=invoice,
 			payment_dr_or_cr=payment_dr_or_cr,
 			condition=condition or ""
 		), {
diff --git a/erpnext/education/doctype/fees/fees.json b/erpnext/education/doctype/fees/fees.json
index 85c2471..c4e1f82 100644
--- a/erpnext/education/doctype/fees/fees.json
+++ b/erpnext/education/doctype/fees/fees.json
@@ -1263,35 +1263,6 @@
    "search_index": 0, 
    "set_only_once": 0, 
    "unique": 0
-  }, 
-  {
-   "allow_bulk_edit": 0, 
-   "allow_on_submit": 0, 
-   "bold": 0, 
-   "collapsible": 0, 
-   "columns": 0, 
-   "fieldname": "data_42", 
-   "fieldtype": "Data", 
-   "hidden": 0, 
-   "ignore_user_permissions": 0, 
-   "ignore_xss_filter": 0, 
-   "in_filter": 0, 
-   "in_global_search": 0, 
-   "in_list_view": 0, 
-   "in_standard_filter": 0, 
-   "length": 0, 
-   "no_copy": 0, 
-   "permlevel": 0, 
-   "precision": "", 
-   "print_hide": 0, 
-   "print_hide_if_no_value": 0, 
-   "read_only": 0, 
-   "remember_last_selected_value": 0, 
-   "report_hide": 0, 
-   "reqd": 0, 
-   "search_index": 0, 
-   "set_only_once": 0, 
-   "unique": 0
   }
  ], 
  "has_web_view": 0, 
@@ -1305,7 +1276,7 @@
  "istable": 0, 
  "max_attachments": 0, 
  "menu_index": 0, 
- "modified": "2017-11-10 18:57:12.021112", 
+ "modified": "2017-12-06 05:55:10.502567", 
  "modified_by": "Administrator", 
  "module": "Education", 
  "name": "Fees",