Remove get_server_field from leave_application
- Make get_holidays and get_total_leave_days global to module
- Replace get_server_field with using whitelisted function
get_total_leave_days
diff --git a/erpnext/hr/doctype/leave_application/leave_application.js b/erpnext/hr/doctype/leave_application/leave_application.js
index 0d8b37e..ecaac4a 100755
--- a/erpnext/hr/doctype/leave_application/leave_application.js
+++ b/erpnext/hr/doctype/leave_application/leave_application.js
@@ -104,7 +104,15 @@
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 get_server_fields('get_total_leave_days', '', '', doc, dt, dn, 1);
+ 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);
+ }
+ }
+ });
}
}
}
diff --git a/erpnext/hr/doctype/leave_application/leave_application.py b/erpnext/hr/doctype/leave_application/leave_application.py
index 1751eb1..50d1d6f 100755
--- a/erpnext/hr/doctype/leave_application/leave_application.py
+++ b/erpnext/hr/doctype/leave_application/leave_application.py
@@ -2,7 +2,7 @@
# License: GNU General Public License v3. See license.txt
from __future__ import unicode_literals
-import frappe
+import frappe, json
from frappe import _
from frappe.utils import cint, cstr, date_diff, flt, formatdate, getdate, get_url_to_form, \
@@ -77,26 +77,10 @@
LeaveDayBlockedError)
def get_holidays(self):
- tot_hol = frappe.db.sql("""select count(*) from `tabHoliday` h1, `tabHoliday List` h2, `tabEmployee` e1
- where e1.name = %s and h1.parent = h2.name and e1.holiday_list = h2.name
- and h1.holiday_date between %s and %s""", (self.employee, self.from_date, self.to_date))
- if not tot_hol:
- tot_hol = frappe.db.sql("""select count(*) from `tabHoliday` h1, `tabHoliday List` h2
- where h1.parent = h2.name and h1.holiday_date between %s and %s
- and ifnull(h2.is_default,0) = 1 and h2.fiscal_year = %s""",
- (self.from_date, self.to_date, self.fiscal_year))
- return tot_hol and flt(tot_hol[0][0]) or 0
+ return get_holidays(self)
def get_total_leave_days(self):
- """Calculates total leave days based on input and holidays"""
- ret = {'total_leave_days' : 0.5}
- if not self.half_day:
- tot_days = date_diff(self.to_date, self.from_date) + 1
- holidays = self.get_holidays()
- ret = {
- 'total_leave_days' : flt(tot_days)-flt(holidays)
- }
- return ret
+ return get_total_leave_days(self)
def validate_to_date(self):
if self.from_date and self.to_date and \
@@ -216,6 +200,33 @@
post(**{"txt": args.message, "contact": args.message_to, "subject": args.subject,
"notify": cint(self.follow_via_email)})
+def get_holidays(leave_app):
+ tot_hol = frappe.db.sql("""select count(*) from `tabHoliday` h1, `tabHoliday List` h2, `tabEmployee` e1
+ where e1.name = %s and h1.parent = h2.name and e1.holiday_list = h2.name
+ and h1.holiday_date between %s and %s""", (leave_app.employee, leave_app.from_date, leave_app.to_date))
+ if not tot_hol:
+ tot_hol = frappe.db.sql("""select count(*) from `tabHoliday` h1, `tabHoliday List` h2
+ where h1.parent = h2.name and h1.holiday_date between %s and %s
+ and ifnull(h2.is_default,0) = 1 and h2.fiscal_year = %s""",
+ (leave_app.from_date, leave_app.to_date, leave_app.fiscal_year))
+ return tot_hol and flt(tot_hol[0][0]) or 0
+
+@frappe.whitelist()
+def get_total_leave_days(leave_app):
+ # Parse Leave Application if neccessary
+ if isinstance(leave_app, str) or isinstance(leave_app, unicode):
+ leave_app = frappe.get_doc(json.loads(leave_app))
+
+ """Calculates total leave days based on input and holidays"""
+ ret = {'total_leave_days' : 0.5}
+ if not leave_app.half_day:
+ tot_days = date_diff(leave_app.to_date, leave_app.from_date) + 1
+ holidays = leave_app.get_holidays()
+ ret = {
+ 'total_leave_days' : flt(tot_days)-flt(holidays)
+ }
+ return ret
+
@frappe.whitelist()
def get_leave_balance(employee, leave_type, fiscal_year):
leave_all = frappe.db.sql("""select total_leaves_allocated