Merge pull request #4212 from anandpdoshi/leave-application-js-clenup

[minor] leave application cleanup
diff --git a/erpnext/hr/doctype/leave_application/leave_application.js b/erpnext/hr/doctype/leave_application/leave_application.js
index 7e72064..904fc5a 100755
--- a/erpnext/hr/doctype/leave_application/leave_application.js
+++ b/erpnext/hr/doctype/leave_application/leave_application.js
@@ -3,113 +3,116 @@
 
 cur_frm.add_fetch('employee','employee_name','employee_name');
 
-frappe.ui.form.on("Leave Application", "leave_approver", function(frm) {
-	frm.set_value("leave_approver_name", frappe.user.full_name(frm.doc.leave_approver));
-});
+frappe.ui.form.on("Leave Application", {
+	onload: function(frm) {
+		if (!frm.doc.posting_date) {
+			frm.set_value("posting_date", get_today());
+		}
 
-cur_frm.cscript.onload = function(doc, dt, dn) {
-	if(!doc.posting_date)
-		set_multiple(dt,dn,{posting_date:get_today()});
-	if(doc.__islocal) {
-		cur_frm.set_value("status", "Open");
-		cur_frm.cscript.calculate_total_days(doc, dt, dn);
-	}
+		frm.set_query("leave_approver", function() {
+			return {
+				query: "erpnext.hr.doctype.leave_application.leave_application.get_approvers",
+				filters: {
+					employee: frm.doc.employee
+				}
+			};
+		});
 
-	cur_frm.set_query("leave_approver", function() {
-		return {
-			query: "erpnext.hr.doctype.leave_application.leave_application.get_approvers",
-			filters: {
-				employee: cur_frm.doc.employee
-			}
-		};
-	});
+		frm.set_query("employee", erpnext.queries.employee);
 
-	cur_frm.cscript.get_leave_balance(cur_frm.doc);
-}
+	},
 
-cur_frm.cscript.refresh = function(doc, dt, dn) {
-	if(doc.__islocal) {
-		cur_frm.set_value("status", "Open")
-	}
-	cur_frm.set_intro("");
-	if(doc.__islocal && !in_list(user_roles, "HR User")) {
-		cur_frm.set_intro(__("Fill the form and save it"))
-	} else {
-		if(doc.docstatus==0 && doc.status=="Open") {
-			if(user==doc.leave_approver) {
-				cur_frm.set_intro(__("You are the Leave Approver for this record. Please Update the 'Status' and Save"));
-				cur_frm.toggle_enable("status", true);
-			} else {
-				cur_frm.set_intro(__("This Leave Application is pending approval. Only the Leave Approver can update status."))
-				cur_frm.toggle_enable("status", false);
+	refresh: function(frm) {
+		if (frm.is_new()) {
+			frm.set_value("status", "Open");
+			frm.trigger("calculate_total_days");
+		}
+
+		frm.set_intro("");
+		if (frm.is_new() && !in_list(user_roles, "HR User")) {
+			frm.set_intro(__("Fill the form and save it"));
+		} else {
+			if(frm.doc.docstatus==0 && frm.doc.status=="Open") {
+				if(user==frm.doc.leave_approver) {
+					frm.set_intro(__("You are the Leave Approver for this record. Please Update the 'Status' and Save"));
+					frm.toggle_enable("status", true);
+				} else {
+					frm.set_intro(__("This Leave Application is pending approval. Only the Leave Approver can update status."))
+					frm.toggle_enable("status", false);
+				}
 			}
 		}
-	}
-}
+	},
 
-cur_frm.cscript.employee = function (doc, dt, dn){
-	cur_frm.cscript.get_leave_balance(doc, dt, dn);
-}
+	leave_approver: function(frm) {
+		frm.set_value("leave_approver_name", frappe.user.full_name(frm.doc.leave_approver));
+	},
 
-cur_frm.cscript.fiscal_year = function (doc, dt, dn){
-	cur_frm.cscript.get_leave_balance(doc, dt, dn);
-}
+	employee: function(frm) {
+		frm.trigger("get_leave_balance");
+	},
 
-cur_frm.cscript.leave_type = function (doc, dt, dn){
-	cur_frm.cscript.get_leave_balance(doc, dt, dn);
-}
+	fiscal_year: function(frm) {
+		frm.trigger("get_leave_balance");
+	},
 
-cur_frm.cscript.half_day = function(doc, dt, dn) {
-	if(doc.from_date) {
-		set_multiple(dt,dn,{to_date:doc.from_date});
-		cur_frm.cscript.calculate_total_days(doc, dt, dn);
-	}
-}
+	leave_type: function(frm) {
+		frm.trigger("get_leave_balance");
+	},
 
-cur_frm.cscript.from_date = function(doc, dt, dn) {
-	if(cint(doc.half_day) == 1){
-		set_multiple(dt,dn,{to_date:doc.from_date});
-	}
-	cur_frm.cscript.calculate_total_days(doc, dt, dn);
-}
+	half_day: function(frm) {
+		if (frm.doc.from_date) {
+			frm.set_value("to_date", frm.doc.from_date);
+			frm.trigger("calculate_total_days");
+		}
+	},
 
-cur_frm.cscript.to_date = function(doc, dt, dn) {
-	if(cint(doc.half_day) == 1 && cstr(doc.from_date) && doc.from_date != doc.to_date){
-		msgprint(__("To Date should be same as From Date for Half Day leave"));
-		set_multiple(dt,dn,{to_date:doc.from_date});
-	}
-	cur_frm.cscript.calculate_total_days(doc, dt, dn);
-}
+	from_date: function(frm) {
+		if (cint(frm.doc.half_day)==1) {
+			frm.set_value("to_date", frm.doc.from_date);
+		}
+		frm.trigger("calculate_total_days");
+	},
 
-cur_frm.cscript.get_leave_balance = function(doc, dt, dn) {
-	if(doc.docstatus==0 && doc.employee && doc.leave_type && doc.fiscal_year) {
-		return cur_frm.call({
-			method: "get_leave_balance",
-			args: {
-				employee: doc.employee,
-				fiscal_year: doc.fiscal_year,
-				leave_type: doc.leave_type
-			}
-		});
-	}
-}
+	to_date: function(frm) {
+		if (cint(frm.doc.half_day)==1 && cstr(frm.doc.from_date) && frm.doc.from_date != frm.doc.to_date) {
+			msgprint(__("To Date should be same as From Date for Half Day leave"));
+			frm.set_value("to_date", frm.doc.from_date);
+		}
 
-cur_frm.cscript.calculate_total_days = function(doc, dt, dn) {
-	if(doc.from_date && doc.to_date){
-		if(cint(doc.half_day) == 1) set_multiple(dt,dn,{total_leave_days:0.5});
-		else{
-			// server call is done to include holidays in leave days calculations
-			return frappe.call({
-				method: 'erpnext.hr.doctype.leave_application.leave_application.get_total_leave_days',
-				args: {leave_app: doc},
-				callback: function(response) {
-					if (response && response.message) {
-						cur_frm.set_value('total_leave_days', response.message.total_leave_days);
-					}
+		frm.trigger("calculate_total_days");
+	},
+
+	get_leave_balance: function(frm) {
+		if(frm.doc.docstatus==0 && frm.doc.employee && frm.doc.leave_type && frm.doc.fiscal_year) {
+			return frm.call({
+				method: "get_leave_balance",
+				args: {
+					employee: frm.doc.employee,
+					fiscal_year: frm.doc.fiscal_year,
+					leave_type: frm.doc.leave_type
 				}
 			});
 		}
-	}
-}
+	},
 
-cur_frm.fields_dict.employee.get_query = erpnext.queries.employee;
+	calculate_total_days: function(frm) {
+		if(frm.doc.from_date && frm.doc.to_date) {
+			if (cint(frm.doc.half_day)==1) {
+				frm.set_value("total_leave_days", 0.5);
+			} else {
+				// server call is done to include holidays in leave days calculations
+				return frappe.call({
+					method: 'erpnext.hr.doctype.leave_application.leave_application.get_total_leave_days',
+					args: { leave_app: frm.doc },
+					callback: function(response) {
+						if (response && response.message) {
+							frm.set_value('total_leave_days', response.message.total_leave_days);
+						}
+					}
+				});
+			}
+		}
+	},
+
+});