Add Calendar View to Attendance doctype (#13919)
* [fix] #13917
* codacy fixes
* remove company
* add status
* fix date issue
* code optimizations
* change sql
diff --git a/erpnext/hr/doctype/attendance/attendance.py b/erpnext/hr/doctype/attendance/attendance.py
index 7b04f7d..7a4cb64 100644
--- a/erpnext/hr/doctype/attendance/attendance.py
+++ b/erpnext/hr/doctype/attendance/attendance.py
@@ -8,6 +8,7 @@
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 @@
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 0000000..b21afe5
--- /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