Skip to content

Commit

Permalink
Merge pull request #1636 from frappe/mergify/bp/version-15-hotfix/pr-…
Browse files Browse the repository at this point in the history
…1595

feat: show holidays and colour code in attendance calendar (backport #1595)
  • Loading branch information
ruchamahabal authored Apr 5, 2024
2 parents d5a45f2 + 1a0ec54 commit dda4149
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 11 deletions.
12 changes: 11 additions & 1 deletion hrms/hr/doctype/attendance/attendance.json
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@
"idx": 1,
"is_submittable": 1,
"links": [],
"modified": "2023-06-15 20:09:21.730465",
"modified": "2024-04-05 20:55:02.905452",
"modified_by": "Administrator",
"module": "HR",
"name": "Attendance",
Expand Down Expand Up @@ -254,6 +254,16 @@
"share": 1,
"submit": 1,
"write": 1
},
{
"email": 1,
"export": 1,
"print": 1,
"read": 1,
"report": 1,
"role": "Employee",
"select": 1,
"share": 1
}
],
"search_fields": "employee,employee_name,attendance_date,status",
Expand Down
35 changes: 30 additions & 5 deletions hrms/hr/doctype/attendance/attendance.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@
)

from hrms.hr.doctype.shift_assignment.shift_assignment import has_overlapping_timings
from hrms.hr.utils import get_holiday_dates_for_employee, validate_active_employee
from hrms.hr.utils import (
get_holiday_dates_for_employee,
get_holidays_for_employee,
validate_active_employee,
)


class DuplicateAttendanceError(frappe.ValidationError):
Expand Down Expand Up @@ -227,25 +231,27 @@ def unlink_attendance_from_checkins(self):

@frappe.whitelist()
def get_events(start, end, filters=None):
from frappe.desk.reportview import get_filters_cond

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)
add_holidays(events, start, end, employee)
return events


def add_attendance(events, start, end, conditions=None):
query = """select name, attendance_date, status
query = """select name, attendance_date, status, employee_name
from `tabAttendance` where
attendance_date between %(from_date)s and %(to_date)s
and docstatus < 2"""

if conditions:
query += conditions

Expand All @@ -255,13 +261,32 @@ def add_attendance(events, start, end, conditions=None):
"doctype": "Attendance",
"start": d.attendance_date,
"end": d.attendance_date,
"title": cstr(d.status),
"title": f"{d.employee_name}: {cstr(d.status)}",
"status": d.status,
"docstatus": d.docstatus,
}
if e not in events:
events.append(e)


def add_holidays(events, start, end, employee=None):
holidays = get_holidays_for_employee(employee, start, end)
if not holidays:
return

for holiday in holidays:
events.append(
{
"doctype": "Holiday",
"start": holiday.holiday_date,
"end": holiday.holiday_date,
"title": _("Holiday") + ": " + cstr(holiday.description),
"name": holiday.name,
"allDay": 1,
}
)


def mark_attendance(
employee,
attendance_date,
Expand Down
28 changes: 23 additions & 5 deletions hrms/hr/doctype/attendance/attendance_calendar.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,30 @@
// Copyright (c) 2018, Frappe Technologies Pvt. Ltd. and contributors
// For license information, please see license.txt
frappe.views.calendar["Attendance"] = {
field_map: {
start: "start",
end: "end",
id: "name",
title: "title",
allDay: "allDay",
color: "color",
},
get_css_class: function (data) {
if (data.doctype === "Holiday") return "default";
else if (data.doctype === "Attendance") {
if (data.status === "Absent" || data.status === "On Leave") {
return "danger";
}
if (data.status === "Half Day") return "warning";
return "success";
}
},
options: {
header: {
left: 'prev,next today',
center: 'title',
right: 'month'
}
left: "prev,next today",
center: "title",
right: "month",
},
},
get_events_method: "hrms.hr.doctype.attendance.attendance.get_events"
get_events_method: "hrms.hr.doctype.attendance.attendance.get_events",
};

0 comments on commit dda4149

Please sign in to comment.