Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: mark absence for default shift on non-overlapping dates if another shift assignment exists (backport #2602) #2665

Merged
merged 4 commits into from
Jan 20, 2025
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fix: added check for 0 absent employees
chore: better test scenario

(cherry picked from commit 83281c6)
  • Loading branch information
asmitahase authored and mergify[bot] committed Jan 20, 2025
commit 0e278d0a6a6b404bf5afe1bc3562676ace4b5c5d
9 changes: 5 additions & 4 deletions hrms/hr/doctype/shift_type/shift_type.py
Original file line number Diff line number Diff line change
@@ -254,23 +254,24 @@ def get_employees_with_default_shift(self, filters: dict) -> list:
default_shift_employees = frappe.get_all(
"Employee", filters={"default_shift": self.name, "status": "Active"}, pluck="name"
)

if not default_shift_employees:
return []

# exclude employees from default shift list if any other valid shift assignment exists such that
# the shift start and end dates overlap with absent dates
absent_employees = {}
for employee in default_shift_employees:
if absent_dates := self.get_dates_for_attendance(employee):
absent_employees[employee] = absent_dates
del filters["shift_type"]
if not absent_employees:
return []

filters["employee"] = ("in", absent_employees.keys())
del filters["shift_type"]
active_shift_assignments = frappe.get_all(
"Shift Assignment", filters=filters, fields=["employee", "start_date", "end_date"]
)
for shift in active_shift_assignments:
if self.is_overlap_between_dates(shift, absent_employees[shift.employee]):
if self.is_overlap_between_dates(shift, absent_employees.get(shift.employee)):
default_shift_employees.remove(shift.employee)
return default_shift_employees

12 changes: 5 additions & 7 deletions hrms/hr/doctype/shift_type/test_shift_type.py
Original file line number Diff line number Diff line change
@@ -612,19 +612,22 @@ def test_skip_auto_attendance_for_overlapping_shift(self):
self.assertEqual(log_in.skip_auto_attendance, 1)
self.assertEqual(log_out.skip_auto_attendance, 1)

def test_mark_aattendance_for_default_shift_when_shift_assignment_is_not_overlapping(self):
def test_mark_attendance_for_default_shift_when_shift_assignment_is_not_overlapping(self):
shift_1 = setup_shift_type(shift_type="Deafult Shift", start_time="08:00:00", end_time="12:00:00")
shift_2 = setup_shift_type(shift_type="Not Default Shift", start_time="10:00:00", end_time="18:00:00")
employee = make_employee(
"test_employee_attendance@example.com", company="_Test Company", default_shift=shift_1.name
)
shift_assigned_date = add_days(getdate(), +1)
make_shift_assignment(shift_2.name, employee, shift_assigned_date)
from hrms.hr.doctype.attendance.attendance import mark_attendance

mark_attendance(employee, add_days(getdate(), -1), "Present", shift=shift_1.name)
shift_1.process_auto_attendance()
self.assertEqual(
frappe.db.get_value(
"Attendance",
{"employee": employee, "attendance_date": add_days(getdate(), -1), "shift": shift_1.name},
{"employee": employee, "attendance_date": getdate(), "shift": shift_1.name},
"status",
),
"Absent",
@@ -686,8 +689,3 @@ def make_shift_assignment(shift_type, employee, start_date, end_date=None, do_no
shift_assignment.submit()

return shift_assignment


def clear_exsiting_shift_assignments(employee):
for shift in frappe.get_all("Shift Assignment", filters={"employee": employee}):
frappe.delete_doc("Shift Assignment", shift.name)