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

@ -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" }
}
});