From 8a55d9a7959609399d4928c0c2704d4ea6225b72 Mon Sep 17 00:00:00 2001 From: Nathan Do Date: Wed, 5 Nov 2014 12:32:57 +0800 Subject: [PATCH] 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 --- .../leave_application/leave_application.js | 10 +++- .../leave_application/leave_application.py | 49 ++++++++++++------- 2 files changed, 39 insertions(+), 20 deletions(-) diff --git a/erpnext/hr/doctype/leave_application/leave_application.js b/erpnext/hr/doctype/leave_application/leave_application.js index 0d8b37e9a4..ecaac4ac37 100755 --- a/erpnext/hr/doctype/leave_application/leave_application.js +++ b/erpnext/hr/doctype/leave_application/leave_application.js @@ -104,7 +104,15 @@ cur_frm.cscript.calculate_total_days = function(doc, dt, dn) { 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 1751eb128d..50d1d6faaa 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 @@ class LeaveApplication(Document): 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 @@ class LeaveApplication(Document): 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