Anand Doshi | 486f9df | 2012-07-19 13:40:31 +0530 | [diff] [blame] | 1 | from __future__ import unicode_literals |
Rushabh Mehta | 5eb3764 | 2013-02-14 17:34:51 +0530 | [diff] [blame] | 2 | |
| 3 | import webnotes |
Rushabh Mehta | 6227527 | 2013-02-14 20:42:33 +0530 | [diff] [blame^] | 4 | from webnotes import _ |
Rushabh Mehta | 5eb3764 | 2013-02-14 17:34:51 +0530 | [diff] [blame] | 5 | |
| 6 | @webnotes.whitelist() |
Rushabh Mehta | 6227527 | 2013-02-14 20:42:33 +0530 | [diff] [blame^] | 7 | def get_events(start, end, employee=None, company=None): |
Rushabh Mehta | 5eb3764 | 2013-02-14 17:34:51 +0530 | [diff] [blame] | 8 | roles = webnotes.get_roles() |
| 9 | events = webnotes.conn.sql("""select name as `id`, subject as title, |
Rushabh Mehta | 6227527 | 2013-02-14 20:42:33 +0530 | [diff] [blame^] | 10 | 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 Mehta | 5eb3764 | 2013-02-14 17:34:51 +0530 | [diff] [blame] | 16 | 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 Mehta | 6227527 | 2013-02-14 20:42:33 +0530 | [diff] [blame^] | 21 | 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 Mehta | 5eb3764 | 2013-02-14 17:34:51 +0530 | [diff] [blame] | 30 | return events |
Rushabh Mehta | 6227527 | 2013-02-14 20:42:33 +0530 | [diff] [blame^] | 31 | |
| 32 | def 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 | |
| 63 | def 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 Mehta | 5eb3764 | 2013-02-14 17:34:51 +0530 | [diff] [blame] | 81 | @webnotes.whitelist() |
| 82 | def 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 | |