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: show validation message before changing shift start time #2742

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
17 changes: 17 additions & 0 deletions hrms/hr/doctype/shift_type/shift_type.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from itertools import groupby

import frappe
from frappe import _
from frappe.model.document import Document
from frappe.utils import cint, create_batch, get_datetime, get_time, getdate

Expand All @@ -25,6 +26,22 @@


class ShiftType(Document):
def validate(self):
if self.is_field_modified("start_time") and self.unlinked_checkins_exist():
frappe.throw(
title=_("Unmarked Check-in Logs Found"),
msg=_("Mark attendance for exsiting check-in/out logs before changing shift settings"),
Copy link
Member

@ruchamahabal ruchamahabal Jan 31, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
msg=_("Mark attendance for exsiting check-in/out logs before changing shift settings"),
msg=_("Mark attendance for existing check-in/out logs before changing shift settings"),

Typo

)

def is_field_modified(self, fieldname):
return not self.is_new() and self.has_value_changed(fieldname)

def unlinked_checkins_exist(self):
return frappe.db.exists(
"Employee Checkin",
{"shift": self.name, "attendance": ["is", "not set"], "skip_auto_attendance": 0},
)

@frappe.whitelist()
def process_auto_attendance(self):
if (
Expand Down
36 changes: 36 additions & 0 deletions hrms/hr/doctype/shift_type/test_shift_type.py
Original file line number Diff line number Diff line change
Expand Up @@ -688,6 +688,42 @@ def test_mark_attendance_for_default_shift_when_shift_assignment_is_not_overlapp
"Absent",
)

def test_validation_for_unlinked_logs_before_changing_important_shift_configuration(self):
# the important shift configuration is start time, it is used to sort logs chronologically
shift = setup_shift_type(shift_type="Test Shift", start_time="10:00:00", end_time="18:00:00")
employee = make_employee(
"[email protected]", company="_Test Company", default_shift=shift.name
)

from hrms.hr.doctype.employee_checkin.test_employee_checkin import make_checkin

in_time = datetime.combine(getdate(), get_time("10:00:00"))
check_in = make_checkin(employee, in_time)
check_in.fetch_shift()
# Case 1: raise valdiation error if shift time is being changed and checkin logs exists
shift.start_time = get_time("10:15:00")
self.assertRaises(frappe.ValidationError, shift.save)

# don't raise validation error if something else is being changed
# even if checkin logs exists, it's probably fine
shift.reload()
shift.begin_check_in_before_shift_start_time = 120
shift.save()
self.assertEqual(
frappe.get_value("Shift Type", shift.name, "begin_check_in_before_shift_start_time"), 120
)
out_time = datetime.combine(getdate(), get_time("18:00:00"))
check_out = make_checkin(employee, out_time)
check_out.fetch_shift()
shift.process_auto_attendance()

# Case 2: allow shift time to change if no unlinked logs exist
shift.start_time = get_time("10:15:00")
shift.save()
self.assertEqual(
get_time(frappe.get_value("Shift Type", shift.name, "start_time")), get_time("10:15:00")
)


def setup_shift_type(**args):
args = frappe._dict(args)
Expand Down
Loading