Leave Allocation Tool and Employee. Fixes #1555

This commit is contained in:
Anand Doshi 2014-05-02 16:20:45 +05:30
parent 2cebf9b499
commit b71a18cd1e
5 changed files with 28 additions and 54 deletions

View File

@ -86,33 +86,32 @@ class Employee(Document):
def update_user(self):
# add employee role if missing
if not "Employee" in frappe.db.sql_list("""select role from tabUserRole
where parent=%s""", self.user_id):
from frappe.utils.user import add_role
add_role(self.user_id, "Employee")
user = frappe.get_doc("User", self.user_id)
user.ignore_permissions = True
user_wrapper = frappe.get_doc("User", self.user_id)
if "Employee" not in user.get("user_roles"):
user.add_roles("Employee")
# copy details like Fullname, DOB and Image to User
if self.employee_name:
employee_name = self.employee_name.split(" ")
if len(employee_name) >= 3:
user_wrapper.last_name = " ".join(employee_name[2:])
user_wrapper.middle_name = employee_name[1]
user.last_name = " ".join(employee_name[2:])
user.middle_name = employee_name[1]
elif len(employee_name) == 2:
user_wrapper.last_name = employee_name[1]
user.last_name = employee_name[1]
user_wrapper.first_name = employee_name[0]
user.first_name = employee_name[0]
if self.date_of_birth:
user_wrapper.birth_date = self.date_of_birth
user.birth_date = self.date_of_birth
if self.gender:
user_wrapper.gender = self.gender
user.gender = self.gender
if self.image:
if not user_wrapper.user_image == self.image:
user_wrapper.user_image = self.image
if not user.user_image:
user.user_image = self.image
try:
frappe.get_doc({
"doctype": "File Data",
@ -123,8 +122,8 @@ class Employee(Document):
except frappe.DuplicateEntryError, e:
# already exists
pass
user_wrapper.ignore_permissions = True
user_wrapper.save()
user.save()
def validate_date(self):
if self.date_of_birth and self.date_of_joining and getdate(self.date_of_birth) >= getdate(self.date_of_joining):

View File

@ -4,20 +4,20 @@
cur_frm.add_fetch('employee','employee_name','employee_name');
cur_frm.cscript.onload = function(doc, dt, dn) {
if(!doc.posting_date)
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);
}
var leave_approver = doc.leave_approver;
return cur_frm.call({
method: "erpnext.hr.utils.get_leave_approver_list",
callback: function(r) {
cur_frm.set_df_property("leave_approver", "options", $.map(r.message,
function(user) {
return {value: user, label: frappe.user_info(user).fullname};
cur_frm.set_df_property("leave_approver", "options", $.map(r.message,
function(user) {
return {value: user, label: frappe.user_info(user).fullname};
}));
if(leave_approver) cur_frm.set_value("leave_approver", leave_approver);
cur_frm.cscript.get_leave_balance(cur_frm.doc);
@ -54,7 +54,7 @@ cur_frm.cscript.refresh = function(doc, dt, dn) {
cur_frm.set_intro(__("Leave application has been rejected."));
}
}
}
}
}
cur_frm.cscript.employee = function (doc, dt, dn){
@ -86,11 +86,11 @@ cur_frm.cscript.from_date = function(doc, dt, dn) {
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});
set_multiple(dt,dn,{to_date:doc.from_date});
}
cur_frm.cscript.calculate_total_days(doc, dt, dn);
}
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({
@ -114,8 +114,4 @@ cur_frm.cscript.calculate_total_days = function(doc, dt, dn) {
}
}
cur_frm.fields_dict.employee.get_query = function() {
return {
query: "erpnext.hr.doctype.leave_application.leave_application.query_for_permitted_employees"
};
}
cur_frm.fields_dict.employee.get_query = erpnext.queries.employee;

View File

@ -318,28 +318,3 @@ def add_holidays(events, start, end, employee, company):
"title": _("Holiday") + ": " + cstr(holiday.description),
"name": holiday.name
})
@frappe.whitelist()
def query_for_permitted_employees(doctype, txt, searchfield, start, page_len, filters):
txt = "%" + cstr(txt) + "%"
if "Leave Approver" in frappe.user.get_roles():
user = frappe.session.user.replace('"', '\"')
condition = """and (exists(select ela.name from `tabEmployee Leave Approver` ela
where ela.parent=`tabEmployee`.name and ela.leave_approver= "%s") or
not exists(select ela.name from `tabEmployee Leave Approver` ela
where ela.parent=`tabEmployee`.name)
or user_id = "%s")""" % (user, user)
else:
from frappe.widgets.reportview import build_match_conditions
condition = build_match_conditions("Employee")
condition = ("and " + condition) if condition else ""
return frappe.db.sql("""select name, employee_name from `tabEmployee`
where status = 'Active' and docstatus < 2 and
(`%s` like %s or employee_name like %s) %s
order by
case when name like %s then 0 else 1 end,
case when employee_name like %s then 0 else 1 end,
name limit %s, %s""" % tuple([searchfield] + ["%s"]*2 + [condition] + ["%s"]*4),
(txt, txt, txt, txt, start, page_len))

View File

@ -40,7 +40,7 @@ class LeaveControlPanel(Document):
for d in self.get_employees():
try:
la = frappe.get_doc('Leave Allocation')
la = frappe.new_doc('Leave Allocation')
la.set("__islocal", 1)
la.employee = cstr(d[0])
la.employee_name = frappe.db.get_value('Employee',cstr(d[0]),'employee_name')

View File

@ -64,4 +64,8 @@ $.extend(erpnext.queries, {
not_a_group_filter: function() {
return { filters: { is_group: "No" } };
},
employee: function() {
return { query: "erpnext.controllers.queries.employee_query" }
}
});