From 5e7b2eb33808ae00fe045bf191e497fc73cec45f Mon Sep 17 00:00:00 2001 From: Pawan Mehta Date: Fri, 4 May 2018 18:12:35 +0530 Subject: [PATCH] Add Calendar View to Attendance doctype (#13919) * [fix] #13917 * codacy fixes * remove company * add status * fix date issue * code optimizations * change sql --- erpnext/hr/doctype/attendance/attendance.py | 34 +++++++++++++++++++ .../doctype/attendance/attendance_calendar.js | 18 ++++++++++ 2 files changed, 52 insertions(+) create mode 100644 erpnext/hr/doctype/attendance/attendance_calendar.js diff --git a/erpnext/hr/doctype/attendance/attendance.py b/erpnext/hr/doctype/attendance/attendance.py index 7b04f7dbae..7a4cb64c2d 100644 --- a/erpnext/hr/doctype/attendance/attendance.py +++ b/erpnext/hr/doctype/attendance/attendance.py @@ -8,6 +8,7 @@ from frappe.utils import getdate, nowdate from frappe import _ from frappe.model.document import Document from erpnext.hr.utils import set_employee_name +from frappe.utils import cstr class Attendance(Document): def validate_duplicate_record(self): @@ -54,3 +55,36 @@ class Attendance(Document): self.validate_attendance_date() self.validate_duplicate_record() self.check_leave_record() + +@frappe.whitelist() +def get_events(start, end, filters=None): + events = [] + + employee = frappe.db.get_value("Employee", {"user_id": frappe.session.user}) + + if not employee: + return events + + from frappe.desk.reportview import get_filters_cond + conditions = get_filters_cond("Attendance", filters, []) + add_attendance(events, start, end, conditions=conditions) + return events + +def add_attendance(events, start, end, conditions=None): + query = """select name, attendance_date, status + from `tabAttendance` where + attendance_date between %(from_date)s and %(to_date)s + and docstatus < 2""" + if conditions: + query += conditions + + for d in frappe.db.sql(query, {"from_date":start, "to_date":end}, as_dict=True): + e = { + "name": d.name, + "doctype": "Attendance", + "date": d.attendance_date, + "title": cstr(d.status), + "docstatus": d.docstatus + } + if e not in events: + events.append(e) \ No newline at end of file diff --git a/erpnext/hr/doctype/attendance/attendance_calendar.js b/erpnext/hr/doctype/attendance/attendance_calendar.js new file mode 100644 index 0000000000..b21afe5eae --- /dev/null +++ b/erpnext/hr/doctype/attendance/attendance_calendar.js @@ -0,0 +1,18 @@ +// Copyright (c) 2018, Frappe Technologies Pvt. Ltd. and contributors +// For license information, please see license.txt +frappe.views.calendar["Attendance"] = { + field_map: { + "start": "date", + "end": "date", + "id": "name", + "docstatus": 1 + }, + options: { + header: { + left: 'prev,next today', + center: 'title', + right: 'month' + } + }, + get_events_method: "erpnext.hr.doctype.attendance.attendance.get_events" +}; \ No newline at end of file