blob: 06d4385569ba482ed9fc98e388b3c94d1f6ec38e [file] [log] [blame]
Anand Doshi486f9df2012-07-19 13:40:31 +05301from __future__ import unicode_literals
Rushabh Mehta5eb37642013-02-14 17:34:51 +05302
3import webnotes
Rushabh Mehta62275272013-02-14 20:42:33 +05304from webnotes import _
Rushabh Mehta5eb37642013-02-14 17:34:51 +05305
6@webnotes.whitelist()
Rushabh Mehta62275272013-02-14 20:42:33 +05307def get_events(start, end, employee=None, company=None):
Rushabh Mehta5eb37642013-02-14 17:34:51 +05308 roles = webnotes.get_roles()
9 events = webnotes.conn.sql("""select name as `id`, subject as title,
Rushabh Mehta62275272013-02-14 20:42:33 +053010 starts_on as `start`, ends_on as `end`, "Event" as doctype, owner,
11 all_day as allDay, event_type
12 from tabEvent where (
13 (starts_on between %s and %s)
14 or (ends_on between %s and %s)
15 )
Rushabh Mehta5eb37642013-02-14 17:34:51 +053016 and (event_type='Public' or owner=%s
17 or exists(select * from `tabEvent User` where
18 `tabEvent User`.parent=tabEvent.name and person=%s)
19 or exists(select * from `tabEvent Role` where
20 `tabEvent Role`.parent=tabEvent.name
Rushabh Mehta62275272013-02-14 20:42:33 +053021 and `tabEvent Role`.role in ('%s')))""" % ('%s', '%s', '%s', '%s', '%s', '%s',
22 "', '".join(roles)), (start, end, start, end,
23 webnotes.session.user, webnotes.session.user), as_dict=1)
24
25
26 if employee:
27 add_block_dates(events, start, end, employee, company)
28 add_department_leaves(events, start, end, employee, company)
29
Rushabh Mehta5eb37642013-02-14 17:34:51 +053030 return events
Rushabh Mehta62275272013-02-14 20:42:33 +053031
32def add_department_leaves(events, start, end, employee, company):
33 department = webnotes.conn.get_value("Employee", employee, "department")
34
35 if not department:
36 return
37
38 # department leaves
39 department_employees = webnotes.conn.sql_list("select name from tabEmployee where department=%s",
40 department)
41
42 for d in webnotes.conn.sql("""select name, from_date, to_date, employee_name, half_day,
43 status, employee
44 from `tabLeave Application` where
45 (from_date between %s and %s or to_date between %s and %s)
46 and docstatus < 2
47 and status!="Rejected"
48 and employee in ('%s')""" % ("%s", "%s", "%s", "%s", "', '".join(department_employees)),
49 (start, end, start, end), as_dict=True):
50 events.append({
51 "id": d.name,
52 "employee": d.employee,
53 "doctype": "Leave Application",
54 "start": d.from_date,
55 "end": d.to_date,
56 "allDay": True,
57 "status": d.status,
58 "title": _("Leave by") + " " + d.employee_name + \
59 (d.half_day and _(" (Half Day)") or "")
60 })
61
62
63def add_block_dates(events, start, end, employee, company):
64 # block days
65 from hr.doctype.leave_block_list.leave_block_list import get_applicable_block_dates
66
67 cnt = 0
68 block_dates = get_applicable_block_dates(start, end, employee, company, all_lists=True)
69
70 for block_date in block_dates:
71 events.append({
72 "doctype": "Leave Block List Date",
73 "start": block_date.block_date,
74 "title": _("Leave Blocked") + ": " + block_date.reason,
75 "id": "_" + str(cnt),
76 "allDay": True
77 })
78 cnt+=1
79
80
Rushabh Mehta5eb37642013-02-14 17:34:51 +053081@webnotes.whitelist()
82def update_event(name, start, end):
83 webnotes.conn.sql("""update tabEvent set starts_on=%s, ends_on=%s where
84 name=%s""", (start, end, name))
85