From 33cc081c3b0aee20e4a9fa64bbfb8ecb789db361 Mon Sep 17 00:00:00 2001 From: Valmik Jangla Date: Mon, 14 Mar 2016 05:07:22 -0700 Subject: [PATCH] Improved Leave Application / Approver feature --- erpnext/hr/doctype/employee/employee.js | 7 +++++-- .../employee_leave_approver.py | 9 ++++++++- .../doctype/leave_application/leave_application.js | 4 +++- .../doctype/leave_application/leave_application.py | 13 ++++++++++++- 4 files changed, 28 insertions(+), 5 deletions(-) mode change 100644 => 100755 erpnext/hr/doctype/employee/employee.js mode change 100644 => 100755 erpnext/hr/doctype/employee_leave_approver/employee_leave_approver.py diff --git a/erpnext/hr/doctype/employee/employee.js b/erpnext/hr/doctype/employee/employee.js old mode 100644 new mode 100755 index 55bc7aa16c..0fedfd6e61 --- a/erpnext/hr/doctype/employee/employee.js +++ b/erpnext/hr/doctype/employee/employee.js @@ -11,9 +11,12 @@ erpnext.hr.EmployeeController = frappe.ui.form.Controller.extend({ }, onload: function() { - this.frm.set_query("leave_approver", "leave_approvers", function() { + this.frm.set_query("leave_approver", "leave_approvers", function(doc) { return { - filters: [["UserRole", "role", "=", "Leave Approver"]] + query:"erpnext.hr.doctype.employee_leave_approver.employee_leave_approver.get_approvers", + filters:{ + user: doc.user_id + } } }); }, diff --git a/erpnext/hr/doctype/employee_leave_approver/employee_leave_approver.py b/erpnext/hr/doctype/employee_leave_approver/employee_leave_approver.py old mode 100644 new mode 100755 index 4d0178fee8..d6a1a97136 --- a/erpnext/hr/doctype/employee_leave_approver/employee_leave_approver.py +++ b/erpnext/hr/doctype/employee_leave_approver/employee_leave_approver.py @@ -9,4 +9,11 @@ import frappe from frappe.model.document import Document class EmployeeLeaveApprover(Document): - pass \ No newline at end of file + pass + +def get_approvers(doctype, txt, searchfield, start, page_len, filters): + return frappe.db.sql("""select user.name, user.first_name, user.last_name from + tabUser user, tabUserRole user_role where + user.name != %s + and user_role.role = "Leave Approver" + and user_role.parent = user.name""", filters.get("user")) \ No newline at end of file diff --git a/erpnext/hr/doctype/leave_application/leave_application.js b/erpnext/hr/doctype/leave_application/leave_application.js index 9d440179d6..79c6d959c0 100755 --- a/erpnext/hr/doctype/leave_application/leave_application.js +++ b/erpnext/hr/doctype/leave_application/leave_application.js @@ -31,7 +31,9 @@ frappe.ui.form.on("Leave Application", { }, leave_approver: function(frm) { - frm.set_value("leave_approver_name", frappe.user.full_name(frm.doc.leave_approver)); + if(frm.doc.leave_approver){ + frm.set_value("leave_approver_name", frappe.user.full_name(frm.doc.leave_approver)); + } }, employee: function(frm) { diff --git a/erpnext/hr/doctype/leave_application/leave_application.py b/erpnext/hr/doctype/leave_application/leave_application.py index 950400df44..e40745385a 100755 --- a/erpnext/hr/doctype/leave_application/leave_application.py +++ b/erpnext/hr/doctype/leave_application/leave_application.py @@ -247,12 +247,23 @@ def get_approvers(doctype, txt, searchfield, start, page_len, filters): if not filters.get("employee"): frappe.throw(_("Please select Employee Record first.")) - return frappe.db.sql("""select user.name, user.first_name, user.last_name from + employee_user = frappe.get_value("Employee", filters.get("employee"), "user_id") + + approvers_list = frappe.db.sql("""select user.name, user.first_name, user.last_name from tabUser user, `tabEmployee Leave Approver` approver where approver.parent = %s and user.name like %s and approver.leave_approver=user.name""", (filters.get("employee"), "%" + txt + "%")) + if not approvers_list: + approvers_list = frappe.db.sql("""select user.name, user.first_name, user.last_name from + tabUser user, tabUserRole user_role where + user_role.role = "Leave Approver" + and user_role.parent = user.name + and user.name != %s + """, employee_user) + return approvers_list + @frappe.whitelist() def get_number_of_leave_days(employee, leave_type, from_date, to_date, half_day=None): if half_day: