Skip to content

Commit

Permalink
feat(UX): Show warnings for skipping/overwriting attendance in request
Browse files Browse the repository at this point in the history
(cherry picked from commit e112cf8)
  • Loading branch information
ruchamahabal authored and mergify[bot] committed May 3, 2023
1 parent 6e232f9 commit f165abc
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 28 deletions.
26 changes: 18 additions & 8 deletions hrms/hr/doctype/attendance_request/attendance_request.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,24 @@
// Copyright (c) 2018, Frappe Technologies Pvt. Ltd. and contributors
// For license information, please see license.txt
cur_frm.add_fetch('employee', 'company', 'company');
frappe.ui.form.on("Attendance Request", {
refresh(frm) {
frm.trigger("show_attendance_warnings");
},

frappe.ui.form.on('Attendance Request', {
half_day: function(frm) {
if(frm.doc.half_day == 1){
frm.set_df_property('half_day_date', 'reqd', true);
}
else{
frm.set_df_property('half_day_date', 'reqd', false);
show_attendance_warnings(frm) {
if (!frm.is_new() && frm.doc.docstatus === 0) {
frm.dashboard.clear_headline();

frm.call("get_attendance_warnings").then((r) => {
frm.dashboard.reset();
frm.dashboard.add_section(
frappe.render_template("attendance_warnings", {
warnings: r.message || [],
}),
__("Attendance Warnings")
);
frm.dashboard.show();
})
}
}
});
8 changes: 6 additions & 2 deletions hrms/hr/doctype/attendance_request/attendance_request.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
"read_only": 1
},
{
"fetch_from": "employee.company",
"fieldname": "company",
"fieldtype": "Link",
"label": "Company",
Expand Down Expand Up @@ -83,7 +84,8 @@
"depends_on": "half_day",
"fieldname": "half_day_date",
"fieldtype": "Date",
"label": "Half Day Date"
"label": "Half Day Date",
"mandatory_depends_on": "half_day"
},
{
"fieldname": "reason_section",
Expand Down Expand Up @@ -119,10 +121,11 @@
],
"is_submittable": 1,
"links": [],
"modified": "2019-12-16 11:49:26.943173",
"modified": "2023-05-03 09:31:53.325203",
"modified_by": "Administrator",
"module": "HR",
"name": "Attendance Request",
"naming_rule": "Expression (old style)",
"owner": "Administrator",
"permissions": [
{
Expand Down Expand Up @@ -185,6 +188,7 @@
],
"sort_field": "modified",
"sort_order": "DESC",
"states": [],
"title_field": "employee_name",
"track_changes": 1
}
71 changes: 53 additions & 18 deletions hrms/hr/doctype/attendance_request/attendance_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def on_submit(self):
self.create_attendance_records()

def on_cancel(self):
attendance_list = frappe.get_list(
attendance_list = frappe.get_all(
"Attendance", {"employee": self.employee, "attendance_request": self.name, "docstatus": 1}
)
if attendance_list:
Expand All @@ -34,15 +34,13 @@ def on_cancel(self):

def create_attendance_records(self):
request_days = date_diff(self.to_date, self.from_date) + 1
for number in range(request_days):
attendance_date = add_days(self.from_date, number)
for day in range(request_days):
attendance_date = add_days(self.from_date, day)
if self.should_mark_attendance(attendance_date):
self.create_or_update_attendance(attendance_date)

def create_or_update_attendance(self, date):
attendance_name = frappe.db.exists(
"Attendance", dict(employee=self.employee, attendance_date=date, docstatus=("!=", 2))
)
def create_or_update_attendance(self, date: str):
attendance_name = self.get_attendance_record(date)

if self.half_day and date_diff(getdate(self.half_day_date), getdate(date)) == 0:
status = "Half Day"
Expand All @@ -55,12 +53,12 @@ def create_or_update_attendance(self, date):
# update existing attendance, change the status
doc = frappe.get_doc("Attendance", attendance_name)
if doc.status != status:
text = _("updated status from {0} to {1} via Attendance Request").format(
text = _("Status updated from {0} to {1} via Attendance Request").format(
frappe.bold(doc.status), frappe.bold(status)
)

doc.db_set({"status": status, "attendance_request": self.name})
doc.add_comment(comment_type="Info", text=text)
doc.add_comment(text=text)
else:
# submit a new attendance record
doc = frappe.new_doc("Attendance")
Expand All @@ -83,7 +81,18 @@ def should_mark_attendance(self, attendance_date: str) -> bool:
return False

# Check if employee is on leave
leave_record = frappe.db.exists(
if self.has_leave_record(attendance_date):
frappe.msgprint(
_("Attendance not submitted for {0} as {1} is on leave.").format(
frappe.bold(format_date(attendance_date)), frappe.bold(self.employee)
)
)
return False

return True

def has_leave_record(self, attendance_date: str) -> str | None:
return frappe.db.exists(
"Leave Application",
{
"employee": self.employee,
Expand All @@ -93,12 +102,38 @@ def should_mark_attendance(self, attendance_date: str) -> bool:
},
)

if leave_record:
frappe.msgprint(
_("Attendance not submitted for {0} as {1} is on leave.").format(
frappe.bold(format_date(attendance_date)), frappe.bold(self.employee)
)
)
return False
def get_attendance_record(self, attendance_date: str) -> str | None:
return frappe.db.exists(
"Attendance",
{
"employee": self.employee,
"attendance_date": attendance_date,
"docstatus": 1,
},
)

return True
@frappe.whitelist()
def get_attendance_warnings(self) -> list:
attendance_warnings = []
request_days = date_diff(self.to_date, self.from_date) + 1

for day in range(request_days):
attendance_date = add_days(self.from_date, day)

if is_holiday(self.employee, attendance_date):
attendance_warnings.append({"date": attendance_date, "reason": "Holiday", "action": "Skip"})
elif self.has_leave_record(attendance_date):
attendance_warnings.append({"date": attendance_date, "reason": "On Leave", "action": "Skip"})
else:
attendance = self.get_attendance_record(attendance_date)
if attendance:
attendance_warnings.append(
{
"date": attendance_date,
"reason": "Attendance already marked",
"record": attendance,
"action": "Overwrite",
}
)

return attendance_warnings
24 changes: 24 additions & 0 deletions hrms/hr/doctype/attendance_request/attendance_warnings.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<div class="form-message yellow">
<div>{{__("Attendance for the following dates will be skipped/overwritten on submission")}}</div>
</div>
<table class="table table-bordered table-hover">
<thead>
<tr>
<th style="width: 20%">{{ __("Date") }}</th>
<th style="width: 20%">{{ __("Action on Submission") }}</th>
<th style="width: 20%">{{ __("Reason") }}</th>
<th style="width: 20%">{{ __("Existing Record") }}</th>
</tr>
</thead>

<tbody>
{% for(var i=0; i < warnings.length; i++) { %}
<tr>
<td class="small">{{ frappe.datetime.str_to_user(warnings[i].date) }}</td>
<td class="small"> {{ __(warnings[i].action) }} </td>
<td class="small"> {{ __(warnings[i].reason) }} </td>
<td class="small"> {{ warnings[i].record }} </td>
</tr>
{% } %}
</tbody>
</table>

0 comments on commit f165abc

Please sign in to comment.