leave application calendar: add leaves based on permission; add holidays applicable to the employee

This commit is contained in:
Anand Doshi 2013-02-19 15:09:21 +05:30
parent 22d3a8b41b
commit a95e2db5b6

View File

@ -103,9 +103,7 @@ class DocType(DocListController):
raise Exception
def validate_balance_leaves(self):
if self.doc.from_date and self.doc.to_date and not is_lwp(self.doc.leave_type):
self.doc.leave_balance = get_leave_balance(self.doc.employee,
self.doc.leave_type, self.doc.fiscal_year)["leave_balance"]
if self.doc.from_date and self.doc.to_date:
self.doc.total_leave_days = self.get_total_leave_days()["total_leave_days"]
if self.doc.total_leave_days == 0:
@ -113,6 +111,10 @@ class DocType(DocListController):
coincide with holiday(s). You need not apply for leave."),
raise_exception=1)
if not is_lwp(self.doc.leave_type):
self.doc.leave_balance = get_leave_balance(self.doc.employee,
self.doc.leave_type, self.doc.fiscal_year)["leave_balance"]
if self.doc.leave_balance - self.doc.total_leave_days < 0:
msgprint("There is not enough leave balance for Leave Type: %s" % \
(self.doc.leave_type,), raise_exception=1)
@ -222,8 +224,21 @@ def get_events(start, end):
employee = webnotes.conn.get_default("employee", webnotes.session.user)
company = webnotes.conn.get_default("company", webnotes.session.user)
from webnotes.widgets.reportview import build_match_conditions
match_conditions = build_match_conditions({"doctype": "Leave Application"})
# show department leaves for employee
show_department_leaves = match_conditions and \
len(match_conditions.split("or"))==1 and "employee" in match_conditions
if show_department_leaves:
add_department_leaves(events, start, end, employee, company)
else:
add_leaves(events, start, end, employee, company, match_conditions)
add_block_dates(events, start, end, employee, company)
add_holidays(events, start, end, employee, company)
return events
def add_department_leaves(events, start, end, employee, company):
@ -233,17 +248,23 @@ def add_department_leaves(events, start, end, employee, company):
return
# department leaves
department_employees = webnotes.conn.sql_list("select name from tabEmployee where department=%s",
department)
department_employees = webnotes.conn.sql_list("""select name from tabEmployee where department=%s
and company=%s""", (department, company))
for d in webnotes.conn.sql("""select name, from_date, to_date, employee_name, half_day,
match_conditions = "employee in (\"%s\")" % '", "'.join(department_employees)
add_leaves(events, start, end, employee, company, match_conditions=match_conditions)
def add_leaves(events, start, end, employee, company, match_conditions=None):
query = """select name, from_date, to_date, employee_name, half_day,
status, employee, docstatus
from `tabLeave Application` where
(from_date between %s and %s or to_date between %s and %s)
and docstatus < 2
and status!="Rejected"
and employee in ('%s')""" % ("%s", "%s", "%s", "%s", "', '".join(department_employees)),
(start, end, start, end), as_dict=True):
and status!="Rejected" """
if match_conditions:
query += " and " + match_conditions
for d in webnotes.conn.sql(query, (start, end, start, end), as_dict=True):
events.append({
"name": d.name,
"doctype": "Leave Application",
@ -255,7 +276,6 @@ def add_department_leaves(events, start, end, employee, company):
"docstatus": d.docstatus
})
def add_block_dates(events, start, end, employee, company):
# block days
from hr.doctype.leave_block_list.leave_block_list import get_applicable_block_dates
@ -272,3 +292,17 @@ def add_block_dates(events, start, end, employee, company):
})
cnt+=1
def add_holidays(events, start, end, employee, company):
applicable_holiday_list = webnotes.conn.get_value("Employee", employee, "holiday_list")
if not applicable_holiday_list:
return
for holiday in webnotes.conn.sql("""select name, holiday_date, description
from `tabHoliday` where parent=%s and holiday_date between %s and %s""",
(applicable_holiday_list, start, end), as_dict=True):
events.append({
"doctype": "Holiday",
"from_date": holiday.holiday_date,
"title": _("Holiday") + ": " + cstr(holiday.description),
"name": holiday.name
})