From ea454f1b164f5647bbf4eb781a1be3361d2a3911 Mon Sep 17 00:00:00 2001 From: nareshkannasln Date: Thu, 24 Sep 2026 17:12:07 +0530 Subject: [PATCH 1/6] fix(leave_policy_assignment): cap earned leave schedule to annual allocation --- .../leave_policy_assignment.py | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/hrms/hr/doctype/leave_policy_assignment/leave_policy_assignment.py b/hrms/hr/doctype/leave_policy_assignment/leave_policy_assignment.py index 3cdd8ccdd3e..af0ab3f9499 100644 --- a/hrms/hr/doctype/leave_policy_assignment/leave_policy_assignment.py +++ b/hrms/hr/doctype/leave_policy_assignment/leave_policy_assignment.py @@ -395,9 +395,39 @@ def get_earned_leave_schedule( pro_rated_period_end, ) schedule[0]["number_of_leaves"] = pro_rated_earned_leave + + # yearly earned leaves are exempted from the annual allocation limit + # since a single period already grants the entire annual allocation + if annual_allocation and leave_details.earned_leave_frequency != "Yearly": + schedule = cap_schedule_to_annual_allocation(schedule, annual_allocation) + return schedule +def cap_schedule_to_annual_allocation(schedule, annual_allocation): + from frappe.model.meta import get_field_precision + + precision = get_field_precision(frappe.get_meta("Leave Allocation").get_field("new_leaves_allocated")) + annual_allocation = flt(annual_allocation, precision) + + capped_schedule = [] + scheduled_leaves = 0.0 + + for row in schedule: + # leaves allocated already have ledger entries against them, they cannot be trimmed + if not row.get("is_allocated"): + leaves_left_in_quota = flt(annual_allocation - scheduled_leaves, precision) + if leaves_left_in_quota <= 0: + break + if flt(row["number_of_leaves"], precision) > leaves_left_in_quota: + row["number_of_leaves"] = leaves_left_in_quota + + scheduled_leaves = flt(scheduled_leaves + flt(row["number_of_leaves"], precision), precision) + capped_schedule.append(row) + + return capped_schedule + + def get_pro_rata_period_end_date(consider_current_month): # for earned leave, pro-rata period ends on the last day of the month # pro rata period end date is different for different periods From 3856d556253e84bf0d2d34413c838656f0ba5214 Mon Sep 17 00:00:00 2001 From: nareshkannasln Date: Thu, 24 Sep 2026 17:12:07 +0530 Subject: [PATCH 2/6] fix(hr): allocate leaves left in annual quota instead of skipping allocation --- .../leave_allocation/leave_allocation.py | 61 +++++-------------- hrms/hr/utils.py | 55 +++++++++-------- 2 files changed, 45 insertions(+), 71 deletions(-) diff --git a/hrms/hr/doctype/leave_allocation/leave_allocation.py b/hrms/hr/doctype/leave_allocation/leave_allocation.py index 90fd0ebb6bc..8ab4be966da 100755 --- a/hrms/hr/doctype/leave_allocation/leave_allocation.py +++ b/hrms/hr/doctype/leave_allocation/leave_allocation.py @@ -465,59 +465,28 @@ def retry_failed_allocations(self, failed_allocations: list) -> None: if not frappe.has_permission(doctype="Leave Allocation", ptype="write", user=frappe.session.user): frappe.throw(_("You do not have permission to complete this action"), frappe.PermissionError) - max_leaves_allowed, frequency = frappe.db.get_values( - "Leave Type", self.leave_type, ["max_leaves_allowed", "earned_leave_frequency"] - )[0] + from hrms.hr.utils import update_previous_leave_allocation + leave_type = frappe.get_cached_doc("Leave Type", self.leave_type) annual_allocation = frappe.get_value( "Leave Policy Detail", {"parent": self.leave_policy, "leave_type": self.leave_type}, "annual_allocation", ) - - for allocation in failed_allocations: - new_allocation = flt(self.total_leaves_allocated) + flt(allocation["number_of_leaves"]) - - new_allocation_without_cf = flt(self.get_existing_leave_count()) + flt( - allocation["number_of_leaves"] + requested_dates = {getdate(row["allocation_date"]) for row in failed_allocations} + self.reload() + for row in self.earned_leave_schedule: + if not (row.failed and row.attempted and getdate(row.allocation_date) in requested_dates): + continue + update_previous_leave_allocation( + self, + annual_allocation, + leave_type, + row.number_of_leaves, + row.allocation_date, + allocated_via="Manually", ) - - if new_allocation > max_leaves_allowed and max_leaves_allowed > 0: - frappe.throw( - msg=_( - "Cannot allocate more leaves due to maximum leaves allowed limit of {0} in {1} leave type." - ).format(frappe.bold(max_leaves_allowed), frappe.bold(self.leave_type)), - title=_("Retry Failed"), - ) - - elif new_allocation_without_cf > annual_allocation and frequency != "Yearly": - frappe.throw( - msg=_( - "Cannot allocate more leaves due to maximum leave allocation limit of {0} in leave policy assignment" - ).format(frappe.bold(annual_allocation)), - title=_("Retry Failed"), - ) - - else: - self.db_set("total_leaves_allocated", new_allocation, update_modified=False) - create_additional_leave_ledger_entry( - self, allocation["number_of_leaves"], allocation["allocation_date"] - ) - earned_leave_schedule = frappe.qb.DocType("Earned Leave Schedule") - ( - frappe.qb.update(earned_leave_schedule) - .where( - (earned_leave_schedule.parent == self.name) - & (earned_leave_schedule.allocation_date == allocation["allocation_date"]) - & (earned_leave_schedule.attempted == 1) - & (earned_leave_schedule.failed == 1) - ) - .set(earned_leave_schedule.is_allocated, 1) - .set(earned_leave_schedule.attempted, 1) - .set(earned_leave_schedule.allocated_via, "Manually") - .set(earned_leave_schedule.failed, 0) - .set(earned_leave_schedule.failure_reason, "") - ).run() + self.reload() def get_previous_allocation(from_date, leave_type, employee): diff --git a/hrms/hr/utils.py b/hrms/hr/utils.py index cc9bfc74f38..a1e6521df27 100644 --- a/hrms/hr/utils.py +++ b/hrms/hr/utils.py @@ -415,26 +415,21 @@ def calculate_upcoming_earned_leave(allocation, e_leave_type, date_of_joining): return earned_leave -def update_previous_leave_allocation(allocation, annual_allocation, e_leave_type, earned_leaves, today): +def update_previous_leave_allocation( + allocation, annual_allocation, e_leave_type, earned_leaves, today, allocated_via="Scheduler" +): allocation = frappe.get_doc("Leave Allocation", allocation.name) precision = allocation.precision("total_leaves_allocated") annual_allocation = flt(annual_allocation, precision) earned_leaves = flt(earned_leaves, precision) - new_leaves_to_allocate_without_cf = flt( - flt(allocation.get_existing_leave_count()) + earned_leaves, - precision, - ) - if ( - # annual allocation as per policy should not be exceeded except for yearly leaves - new_leaves_to_allocate_without_cf > annual_allocation - and e_leave_type.earned_leave_frequency != "Yearly" - ): - frappe.throw( - _("Allocation was skipped due to exceeding annual allocation set in leave policy"), - OverAllocationError, + + if earned_leaves > 0 and e_leave_type.earned_leave_frequency != "Yearly": + leaves_left_in_annual_quota = max( + flt(annual_allocation - allocation.get_existing_leave_count(), precision), 0 ) + earned_leaves = min(earned_leaves, leaves_left_in_annual_quota) - if e_leave_type.max_leaves_allowed: + if earned_leaves > 0 and e_leave_type.max_leaves_allowed: leaves_quota = flt(e_leave_type.max_leaves_allowed - allocation.total_leaves_allocated, precision) if leaves_quota <= 0: frappe.throw( @@ -447,18 +442,28 @@ def update_previous_leave_allocation(allocation, annual_allocation, e_leave_type if leaves_quota < earned_leaves: earned_leaves = leaves_quota - allocation.db_set( - "total_leaves_allocated", - earned_leaves + allocation.total_leaves_allocated, - update_modified=False, - ) - create_additional_leave_ledger_entry(allocation, earned_leaves, today) + if earned_leaves: + allocation.db_set( + "total_leaves_allocated", + earned_leaves + allocation.total_leaves_allocated, + update_modified=False, + ) + create_additional_leave_ledger_entry(allocation, earned_leaves, today) + earned_leave_schedule = qb.DocType("Earned Leave Schedule") - qb.update(earned_leave_schedule).where( - (earned_leave_schedule.parent == allocation.name) & (earned_leave_schedule.allocation_date == today) - ).set(earned_leave_schedule.is_allocated, 1).set(earned_leave_schedule.attempted, 1).set( - earned_leave_schedule.allocated_via, "Scheduler" - ).set(earned_leave_schedule.number_of_leaves, earned_leaves).run() + ( + qb.update(earned_leave_schedule) + .where( + (earned_leave_schedule.parent == allocation.name) + & (earned_leave_schedule.allocation_date == today) + ) + .set(earned_leave_schedule.is_allocated, 1) + .set(earned_leave_schedule.attempted, 1) + .set(earned_leave_schedule.allocated_via, allocated_via) + .set(earned_leave_schedule.number_of_leaves, earned_leaves) + .set(earned_leave_schedule.failed, 0) + .set(earned_leave_schedule.failure_reason, "") + ).run() def log_allocation_error(allocation_name, error): From b925fa5cbf54fdad76003c3f8b19f8f630fc6781 Mon Sep 17 00:00:00 2001 From: nareshkannasln Date: Thu, 24 Sep 2026 17:12:07 +0530 Subject: [PATCH 3/6] test: assert earned leave schedule does not exceed annual allocation --- .../test_earned_leave_schedule.py | 38 +++++++ .../leave_allocation/test_earned_leaves.py | 102 +++++++++++++++++- 2 files changed, 139 insertions(+), 1 deletion(-) diff --git a/hrms/hr/doctype/leave_allocation/test_earned_leave_schedule.py b/hrms/hr/doctype/leave_allocation/test_earned_leave_schedule.py index b9774a4838a..c61038eb61e 100644 --- a/hrms/hr/doctype/leave_allocation/test_earned_leave_schedule.py +++ b/hrms/hr/doctype/leave_allocation/test_earned_leave_schedule.py @@ -338,6 +338,42 @@ def test_schedule_when_doj_is_end_of_big_month(self): self.employee.date_of_joining, ) + def test_schedule_does_not_exceed_annual_allocation_when_rounded_up(self): + """Tests that rounding up the periodic leave does not schedule more leaves than the policy allows""" + frappe.flags.current_date = get_year_start(getdate()) + earned_leave_schedule = create_earned_leave_schedule( + self.employee, + allocate_on_day="First Day", + earned_leave_frequency="Monthly", + annual_allocation=19, + assignment_based_on="Leave Period", + start_date=get_year_start(getdate()), + end_date=get_year_ending(getdate()), + rounding=1.0, + ) + + self.assertEqual(len(earned_leave_schedule), 10) + self.assertEqual(sum(row.number_of_leaves for row in earned_leave_schedule), 19) + self.assertEqual(earned_leave_schedule[-1].number_of_leaves, 1) + self.assertEqual(earned_leave_schedule[-1].allocation_date, add_months(get_year_start(getdate()), 9)) + + def test_schedule_for_yearly_earned_leave_is_exempted_from_annual_allocation(self): + """Tests that a yearly schedule spanning multiple years is not capped to the annual allocation""" + frappe.flags.current_date = get_year_start(getdate()) + earned_leave_schedule = create_earned_leave_schedule( + self.employee, + allocate_on_day="First Day", + earned_leave_frequency="Yearly", + annual_allocation=19, + assignment_based_on="Leave Period", + start_date=get_year_start(getdate()), + end_date=add_months(get_year_ending(getdate()), 12), + rounding=1.0, + ) + + self.assertEqual(len(earned_leave_schedule), 2) + self.assertEqual(sum(row.number_of_leaves for row in earned_leave_schedule), 38) + def test_absence_of_earned_leave_schedule_for_non_earned_leave_types(self): leave_policy = frappe.get_doc( { @@ -406,6 +442,7 @@ def create_earned_leave_schedule( assignment_based_on, start_date, end_date, + rounding=0.5, ): assignment = make_policy_assignment( employee, @@ -415,6 +452,7 @@ def create_earned_leave_schedule( assignment_based_on=assignment_based_on, start_date=start_date, end_date=end_date, + rounding=rounding, )[0] leave_allocation = frappe.get_value("Leave Allocation", {"leave_policy_assignment": assignment}, "name") earned_leave_schedule = frappe.get_all( diff --git a/hrms/hr/doctype/leave_allocation/test_earned_leaves.py b/hrms/hr/doctype/leave_allocation/test_earned_leaves.py index d65b60a2e25..401649b7fc8 100644 --- a/hrms/hr/doctype/leave_allocation/test_earned_leaves.py +++ b/hrms/hr/doctype/leave_allocation/test_earned_leaves.py @@ -1,3 +1,5 @@ +from unittest.mock import patch + import frappe from frappe.utils import ( add_days, @@ -207,10 +209,13 @@ def test_earned_leaves_creation(self): ) # validate earned leaves creation without maximum leaves + # the leave period spans 13 allocation dates, but the schedule is capped to the + # annual allocation (6), so the leaves skipped due to the max leaves limit + # are not compensated by an extra allocation and have to be retried instead frappe.db.set_value("Leave Type", self.leave_type, "max_leaves_allowed", 0) allocate_earned_leaves_for_months(6) self.assertEqual( - get_leave_balance_on(self.employee.name, self.leave_type, frappe.flags.current_date), 5 + get_leave_balance_on(self.employee.name, self.leave_type, frappe.flags.current_date), 4.5 ) def test_overallocation(self): @@ -237,6 +242,101 @@ def test_overallocation(self): get_leave_balance_on(self.employee.name, self.leave_type, frappe.flags.current_date), 22 ) + def test_overallocation_when_annual_allocation_is_not_divisible(self): + """Tests earned leave allocation is capped to the annual allocation + when rounding up does not divide the annual allocation evenly""" + frappe.flags.current_date = get_year_start(getdate()) + assignment = make_policy_assignment( + self.employee, + annual_allocation=19, + allocate_on_day="First Day", + start_date=frappe.flags.current_date, + rounding=1.0, + )[0] + + # 19 leaves / 12 months = 1.58 rounded to 2 leaves per month + # the last allocation should be capped to the leaves left in the annual quota + allocate_earned_leaves_for_months(12) + self.assertEqual(get_allocated_leaves(assignment), 19) + + # allocations should not be marked as failed since nothing was skipped + allocation = frappe.db.get_value("Leave Allocation", {"leave_policy_assignment": assignment}, "name") + self.assertEqual(frappe.db.count("Earned Leave Schedule", {"parent": allocation, "failed": 1}), 0) + + def test_overallocation_without_earned_leave_schedule(self): + """Tests earned leave allocation is capped to the annual allocation + for allocations created before the earned leave schedule was introduced""" + frappe.flags.current_date = get_year_start(getdate()) + assignment = make_policy_assignment( + self.employee, + annual_allocation=19, + allocate_on_day="First Day", + start_date=frappe.flags.current_date, + rounding=1.0, + )[0] + allocation = frappe.db.get_value("Leave Allocation", {"leave_policy_assignment": assignment}, "name") + frappe.db.delete("Earned Leave Schedule", {"parent": allocation}) + + allocate_earned_leaves_for_months(12) + self.assertEqual(get_allocated_leaves(assignment), 19) + + def make_capped_monthly_allocation(self): + start_date = get_year_start(getdate()) + frappe.flags.current_date = add_days(start_date, -1) + assignment = make_policy_assignment( + self.employee, + annual_allocation=19, + allocate_on_day="First Day", + start_date=start_date, + end_date=get_year_ending(start_date), + rounding=1.0, + )[0] + frappe.flags.current_date = start_date + return frappe.get_doc("Leave Allocation", {"leave_policy_assignment": assignment}) + + def test_existing_schedule_completes_without_quota_failure(self): + allocation = self.make_capped_monthly_allocation() + # Existing schedules were generated before the annual cap was introduced. + for row in allocation.earned_leave_schedule: + row.db_set("number_of_leaves", 2) + with patch("hrms.hr.utils.send_email_for_failed_allocations") as notify: + allocate_earned_leaves() + allocate_earned_leaves_for_months(11) + notify.assert_not_called() + allocation.reload() + self.assertEqual(allocation.total_leaves_allocated, 19) + self.assertEqual([row.number_of_leaves for row in allocation.earned_leave_schedule[-2:]], [2, 1]) + self.assertTrue(all(row.attempted and not row.failed for row in allocation.earned_leave_schedule)) + + def test_legacy_allocation_without_schedule_completes_without_quota_failure(self): + allocation = self.make_capped_monthly_allocation() + frappe.db.delete("Earned Leave Schedule", {"parent": allocation.name}) + with patch("hrms.hr.utils.send_email_for_failed_allocations") as notify: + allocate_earned_leaves() + allocate_earned_leaves_for_months(11) + notify.assert_not_called() + allocation.reload() + self.assertEqual(allocation.total_leaves_allocated, 19) + + def test_retry_failed_allocation_credits_remaining_annual_quota(self): + allocation = self.make_capped_monthly_allocation() + allocate_earned_leaves() + allocate_earned_leaves_for_months(8) + allocation.reload() + self.assertEqual(allocation.total_leaves_allocated, 18) + # Before upgrading, this two-day credit failed at the annual limit. + allocation.earned_leave_schedule[-1].db_set({"number_of_leaves": 2, "attempted": 1, "failed": 1}) + allocation.reload() + failed = [row.as_dict() for row in allocation.earned_leave_schedule if row.failed] + allocation.retry_failed_allocations(failed) + self.assertEqual(allocation.total_leaves_allocated, 19) + self.assertEqual(allocation.earned_leave_schedule[-1].number_of_leaves, 1) + self.assertFalse(any(row.failed for row in allocation.earned_leave_schedule)) + self.assertEqual(allocation.earned_leave_schedule[-1].allocated_via, "Manually") + # A repeated request must not credit the same failed row again. + allocation.retry_failed_allocations(failed) + self.assertEqual(allocation.total_leaves_allocated, 19) + def test_over_allocation_during_assignment_creation(self): """Tests backdated earned leave allocation does not exceed annual allocation""" start_date = get_first_day(add_months(getdate(), -12)) From 50ddee02e17b84c3cca017d7f9cd119708996c6e Mon Sep 17 00:00:00 2001 From: nareshkannasln Date: Thu, 24 Sep 2026 17:09:19 +0530 Subject: [PATCH 4/6] fix(holiday_list): allow fetching only half day holidays --- hrms/utils/holiday_list.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/hrms/utils/holiday_list.py b/hrms/utils/holiday_list.py index a5e329fe3d2..a808565c0cc 100644 --- a/hrms/utils/holiday_list.py +++ b/hrms/utils/holiday_list.py @@ -12,6 +12,7 @@ def get_holiday_dates_between( skip_weekly_offs: bool = False, as_dict: bool = False, select_weekly_off: bool = False, + only_half_days: bool = False, ) -> list: Holiday = frappe.qb.DocType("Holiday") query = frappe.qb.from_(Holiday).select(Holiday.holiday_date) @@ -26,6 +27,9 @@ def get_holiday_dates_between( if skip_weekly_offs: query = query.where(Holiday.weekly_off == 0) + if only_half_days: + query = query.where(Holiday.is_half_day == 1) + if as_dict: return query.run(as_dict=True) From f9cd9d55a1685cf0452248fd8c31a3a5c69935da Mon Sep 17 00:00:00 2001 From: nareshkannasln Date: Thu, 24 Sep 2026 17:09:19 +0530 Subject: [PATCH 5/6] fix(salary_slip): count a half day holiday as half a working day --- .../doctype/salary_slip/salary_slip.py | 116 +++++++++++++++--- 1 file changed, 97 insertions(+), 19 deletions(-) diff --git a/hrms/payroll/doctype/salary_slip/salary_slip.py b/hrms/payroll/doctype/salary_slip/salary_slip.py index 069d600fc7c..1ea284cd9a1 100644 --- a/hrms/payroll/doctype/salary_slip/salary_slip.py +++ b/hrms/payroll/doctype/salary_slip/salary_slip.py @@ -60,13 +60,20 @@ get_component_eval_context, throw_error_message, ) -from hrms.utils.holiday_list import get_holiday_list_ranges_for_employee, get_holidays_in_ranges_map +from hrms.utils.holiday_list import ( + get_holiday_dates_between, + get_holiday_list_ranges_for_employee, + get_holidays_in_ranges_map, +) # cache keys HOLIDAYS_BETWEEN_DATES = "holidays_between_dates" LEAVE_TYPE_MAP = "leave_type_map" TAX_COMPONENTS_BY_COMPANY = "tax_components_by_company" +# fraction of a working day contributed by a holiday marked as half day +HALF_DAY_HOLIDAY_FRACTION = 0.5 + class SalarySlip(TransactionBase): # begin: auto-generated types @@ -563,12 +570,20 @@ def get_working_days_details(self, lwp=None, for_preview=0, lwp_days_corrected=N return holidays = self.get_holidays_for_employee(self.start_date, self.end_date) + # holidays marked as half day are half a working day, so they are only excluded by half + # when holidays are included in total working days, they are counted in full like any other holiday + half_day_holidays = ( + [] + if cint(payroll_settings.include_holidays_in_total_working_days) + else self.get_half_day_holidays_for_employee(self.start_date, self.end_date) + ) + full_day_holidays = [date for date in holidays if date not in half_day_holidays] working_days_list = [add_days(getdate(self.start_date), days=day) for day in range(0, working_days)] if not cint(payroll_settings.include_holidays_in_total_working_days): - working_days_list = [i for i in working_days_list if i not in holidays] + working_days_list = [i for i in working_days_list if i not in full_day_holidays] - working_days -= len(holidays) + working_days -= len(full_day_holidays) + HALF_DAY_HOLIDAY_FRACTION * len(half_day_holidays) if working_days < 0: frappe.throw(_("There are more holidays than working days this month.")) @@ -577,12 +592,15 @@ def get_working_days_details(self, lwp=None, for_preview=0, lwp_days_corrected=N if payroll_settings.payroll_based_on == "Attendance": actual_lwp, absent = self.calculate_lwp_ppl_and_absent_days_based_on_attendance( - holidays, daily_wages_fraction_for_half_day, consider_marked_attendance_on_holidays + full_day_holidays, + half_day_holidays, + daily_wages_fraction_for_half_day, + consider_marked_attendance_on_holidays, ) self.absent_days = absent else: actual_lwp = self.calculate_lwp_or_ppl_based_on_leave_application( - holidays, working_days_list, daily_wages_fraction_for_half_day + full_day_holidays, half_day_holidays, working_days_list, daily_wages_fraction_for_half_day ) if not lwp: @@ -610,13 +628,15 @@ def get_working_days_details(self, lwp=None, for_preview=0, lwp_days_corrected=N if payroll_settings.payroll_based_on == "Attendance": if consider_unmarked_attendance_as == "Absent": unmarked_days = self.get_unmarked_days( - payroll_settings.include_holidays_in_total_working_days, holidays + payroll_settings.include_holidays_in_total_working_days, + full_day_holidays, + half_day_holidays, ) self.absent_days += unmarked_days # will be treated as absent self.payment_days -= unmarked_days half_absent_days = self.get_half_absent_days( consider_marked_attendance_on_holidays, - holidays, + full_day_holidays, ) self.absent_days += half_absent_days * daily_wages_fraction_for_half_day self.payment_days -= half_absent_days * daily_wages_fraction_for_half_day @@ -628,13 +648,18 @@ def get_working_days_details(self, lwp=None, for_preview=0, lwp_days_corrected=N self.payment_days += lwp_days_corrected def get_unmarked_days( - self, include_holidays_in_total_working_days: bool, holidays: list | None = None + self, + include_holidays_in_total_working_days: bool, + holidays: list | None = None, + half_day_holidays: list | None = None, ) -> float: """Calculates the number of unmarked days for an employee within a date range""" unmarked_days = ( self.total_working_days - - self._get_days_outside_period(include_holidays_in_total_working_days, holidays) - - self._get_marked_attendance_days(holidays) + - self._get_days_outside_period( + include_holidays_in_total_working_days, holidays, half_day_holidays + ) + - self._get_marked_attendance_days(holidays, half_day_holidays) ) if include_holidays_in_total_working_days and holidays: @@ -661,9 +686,13 @@ def get_half_absent_days(self, consider_marked_attendance_on_holidays, holidays) return query.run()[0][0] def _get_days_outside_period( - self, include_holidays_in_total_working_days: bool, holidays: list | None = None + self, + include_holidays_in_total_working_days: bool, + holidays: list | None = None, + half_day_holidays: list | None = None, ): """Returns days before DOJ or after relieving date""" + half_day_holidays = half_day_holidays or [] def _get_days(start_date, end_date): no_of_days = date_diff(end_date, start_date) + 1 @@ -675,7 +704,9 @@ def _get_days(start_date, end_date): end_date = getdate(end_date) for day in range(no_of_days): date = add_days(end_date, -day) - if date not in holidays: + if date in half_day_holidays: + days += HALF_DAY_HOLIDAY_FRACTION + elif date not in holidays: days += 1 return days @@ -699,7 +730,9 @@ def _get_number_of_holidays(self, holidays: list | None = None) -> float: return no_of_holidays - def _get_marked_attendance_days(self, holidays: list | None = None) -> float: + def _get_marked_attendance_days( + self, holidays: list | None = None, half_day_holidays: list | None = None + ) -> float: Attendance = frappe.qb.DocType("Attendance") query = ( frappe.qb.from_(Attendance) @@ -713,7 +746,16 @@ def _get_marked_attendance_days(self, holidays: list | None = None) -> float: if holidays: query = query.where(Attendance.attendance_date.notin(holidays)) - return query.run()[0][0] + marked_days = query.run()[0][0] + + if half_day_holidays: + # attendance marked on a half day holiday accounts for half a working day + marked_days -= ( + HALF_DAY_HOLIDAY_FRACTION + * (query.where(Attendance.attendance_date.isin(half_day_holidays)).run()[0][0]) + ) + + return marked_days def get_payment_days(self, include_holidays_in_total_working_days): if self.joining_date and self.joining_date > getdate(self.end_date): @@ -733,7 +775,11 @@ def get_payment_days(self, include_holidays_in_total_working_days): if not cint(include_holidays_in_total_working_days): holidays = self.get_holidays_for_employee(self.actual_start_date, self.actual_end_date) - payment_days -= len(holidays) + half_day_holidays = self.get_half_day_holidays_for_employee( + self.actual_start_date, self.actual_end_date + ) + # half day holidays are working days for half the day, so only half of them is deducted + payment_days -= len(holidays) - HALF_DAY_HOLIDAY_FRACTION * len(half_day_holidays) return payment_days @@ -754,8 +800,28 @@ def get_holidays_for_employee(self, start_date, end_date): return [holiday_date for holidays in holidays_by_range.values() for holiday_date in holidays] + def get_half_day_holidays_for_employee(self, start_date, end_date): + """Returns holidays marked as half day, they count as half a working day""" + half_day_holidays = [] + for holiday_list_range in get_holiday_list_ranges_for_employee(self.employee, start_date, end_date): + key = "half_day:{holiday_list}:{from_date}:{to_date}".format(**holiday_list_range) + holiday_dates = frappe.cache().hget(HOLIDAYS_BETWEEN_DATES, key) + + if holiday_dates is None: + holiday_dates = get_holiday_dates_between( + holiday_list_range.holiday_list, + holiday_list_range.from_date, + holiday_list_range.to_date, + only_half_days=True, + ) + frappe.cache().hset(HOLIDAYS_BETWEEN_DATES, key, holiday_dates) + + half_day_holidays.extend(holiday_dates) + + return half_day_holidays + def calculate_lwp_or_ppl_based_on_leave_application( - self, holidays, working_days_list, daily_wages_fraction_for_half_day + self, holidays, half_day_holidays, working_days_list, daily_wages_fraction_for_half_day ): lwp = 0 leaves = get_lwp_or_ppl_for_date_range( @@ -790,6 +856,10 @@ def calculate_lwp_or_ppl_based_on_leave_application( (1 - fraction_of_daily_salary_per_leave) if fraction_of_daily_salary_per_leave else 1 ) + if not leave.include_holiday and getdate(d) in half_day_holidays: + # only half of the day was a working day, so only half of it can be unpaid + equivalent_lwp_count *= HALF_DAY_HOLIDAY_FRACTION + lwp += equivalent_lwp_count return lwp @@ -829,7 +899,11 @@ def get_employee_attendance(self, start_date, end_date): return attendance_details def calculate_lwp_ppl_and_absent_days_based_on_attendance( - self, holidays, daily_wages_fraction_for_half_day, consider_marked_attendance_on_holidays + self, + holidays, + half_day_holidays, + daily_wages_fraction_for_half_day, + consider_marked_attendance_on_holidays, ): lwp = 0 absent = 0 @@ -861,6 +935,10 @@ def calculate_lwp_ppl_and_absent_days_based_on_attendance( "fraction_of_daily_salary_per_leave" ] + # only half of the day was a working day, so a full day of leave or absence is unpaid by half + # a half day leave already covers just that half, so it is left as is + day_fraction = HALF_DAY_HOLIDAY_FRACTION if getdate(d.attendance_date) in half_day_holidays else 1 + if d.status == "Half Day" and d.leave_type and d.leave_type in leave_type_map.keys(): equivalent_lwp = 1 - daily_wages_fraction_for_half_day @@ -876,10 +954,10 @@ def calculate_lwp_ppl_and_absent_days_based_on_attendance( equivalent_lwp *= ( fraction_of_daily_salary_per_leave if fraction_of_daily_salary_per_leave else 1 ) - lwp += equivalent_lwp + lwp += equivalent_lwp * day_fraction elif d.status == "Absent": - absent += 1 + absent += day_fraction return lwp, absent From 28d17910190985ffd0eb79e269f6a71d6d74059d Mon Sep 17 00:00:00 2001 From: nareshkannasln Date: Thu, 24 Sep 2026 17:09:19 +0530 Subject: [PATCH 6/6] test(salary_slip): cover half day holidays in working and payment days --- .../doctype/salary_slip/test_salary_slip.py | 174 +++++++++++++++++- 1 file changed, 173 insertions(+), 1 deletion(-) diff --git a/hrms/payroll/doctype/salary_slip/test_salary_slip.py b/hrms/payroll/doctype/salary_slip/test_salary_slip.py index ee263c40c2c..d57a6bc75ea 100644 --- a/hrms/payroll/doctype/salary_slip/test_salary_slip.py +++ b/hrms/payroll/doctype/salary_slip/test_salary_slip.py @@ -47,7 +47,7 @@ make_salary_slip_from_timesheet, ) from hrms.payroll.doctype.salary_structure.salary_structure import make_salary_slip -from hrms.tests.test_utils import get_email_by_subject, get_first_sunday +from hrms.tests.test_utils import add_date_to_holiday_list, get_email_by_subject, get_first_sunday from hrms.tests.utils import HRMSTestSuite @@ -707,6 +707,153 @@ def test_holidays_resolved_from_holiday_list_assigned_during_slip_period(self): self.assertEqual(ss.total_working_days, days_in_month - sundays_before_today) self.assertEqual(ss.payment_days, days_in_month - sundays_before_today) + @HRMSTestSuite.change_settings( + "Payroll Settings", {"include_holidays_in_total_working_days": 0, "payroll_based_on": "Leave"} + ) + def test_salary_slip_with_half_day_holiday(self): + """Tests that a holiday marked as half day is counted as half a working day""" + no_of_days = get_no_of_days() + emp_id, _half_day_holiday = setup_employee_with_half_day_holiday( + "Test Half Day Holiday List", "test_half_day_holiday@salary.com" + ) + + ss = make_employee_salary_slip(emp_id, "Monthly", "Test Salary Slip With Half Day Holiday") + + # weekly offs are excluded in full, the half day holiday is excluded by half only + expected_working_days = no_of_days[0] - no_of_days[1] - 0.5 + self.assertEqual(ss.total_working_days, expected_working_days) + self.assertEqual(ss.payment_days, expected_working_days) + + @HRMSTestSuite.change_settings( + "Payroll Settings", {"include_holidays_in_total_working_days": 0, "payroll_based_on": "Leave"} + ) + def test_lwp_on_half_day_holiday(self): + """Tests that leave without pay on a half day holiday is unpaid for half the day only""" + no_of_days = get_no_of_days() + emp_id, half_day_holiday = setup_employee_with_half_day_holiday( + "Test LWP Half Day Holiday List", "test_lwp_on_half_day_holiday@salary.com" + ) + frappe.db.set_value("Leave Type", "Leave Without Pay", "include_holiday", 0) + + # leave on the half day holiday and on the working day after it + make_leave_application(emp_id, half_day_holiday, add_days(half_day_holiday, 1), "Leave Without Pay") + + ss = make_employee_salary_slip(emp_id, "Monthly", "Test LWP On Half Day Holiday") + + # 0.5 for the half day holiday + 1 for the full working day + expected_working_days = no_of_days[0] - no_of_days[1] - 0.5 + self.assertEqual(ss.leave_without_pay, 1.5) + self.assertEqual(ss.total_working_days, expected_working_days) + self.assertEqual(ss.payment_days, expected_working_days - 1.5) + + @HRMSTestSuite.change_settings( + "Payroll Settings", + { + "include_holidays_in_total_working_days": 0, + "payroll_based_on": "Attendance", + "consider_unmarked_attendance_as": "Present", + }, + ) + def test_absent_on_half_day_holiday(self): + """Tests that being absent on a half day holiday deducts half a day""" + no_of_days = get_no_of_days() + emp_id, half_day_holiday = setup_employee_with_half_day_holiday( + "Test Absent Half Day Holiday List", "test_absent_on_half_day_holiday@salary.com" + ) + + mark_attendance(emp_id, half_day_holiday, "Absent", ignore_validate=True) + + ss = make_employee_salary_slip(emp_id, "Monthly", "Test Absent On Half Day Holiday") + + expected_working_days = no_of_days[0] - no_of_days[1] - 0.5 + self.assertEqual(ss.absent_days, 0.5) + self.assertEqual(ss.total_working_days, expected_working_days) + self.assertEqual(ss.payment_days, expected_working_days - 0.5) + + @HRMSTestSuite.change_settings( + "Payroll Settings", + { + "include_holidays_in_total_working_days": 0, + "payroll_based_on": "Attendance", + "consider_unmarked_attendance_as": "Absent", + }, + ) + def test_marked_attendance_on_half_day_holiday_with_unmarked_days_as_absent(self): + """Tests that attendance marked on a half day holiday accounts for half a working day""" + no_of_days = get_no_of_days() + emp_id, half_day_holiday = setup_employee_with_half_day_holiday( + "Test Marked Half Day Holiday List", "test_marked_on_half_day_holiday@salary.com" + ) + + # only the half day holiday is marked, every other working day stays unmarked + mark_attendance(emp_id, half_day_holiday, "Present", ignore_validate=True) + + ss = make_employee_salary_slip(emp_id, "Monthly", "Test Marked Attendance On Half Day Holiday") + + # the half day holiday is the only day worked, and it is worth half a day + expected_working_days = no_of_days[0] - no_of_days[1] - 0.5 + self.assertEqual(ss.total_working_days, expected_working_days) + self.assertEqual(ss.absent_days, expected_working_days - 0.5) + self.assertEqual(ss.payment_days, 0.5) + + @HRMSTestSuite.change_settings( + "Payroll Settings", + { + "include_holidays_in_total_working_days": 0, + "payroll_based_on": "Attendance", + "consider_unmarked_attendance_as": "Present", + "daily_wages_fraction_for_half_day": 0.5, + }, + ) + def test_half_day_absent_on_half_day_holiday(self): + """Tests that being absent for the working half of a half day holiday deducts half a day""" + no_of_days = get_no_of_days() + emp_id, half_day_holiday = setup_employee_with_half_day_holiday( + "Test Half Absent Half Day Holiday List", "test_half_absent_on_half_day_holiday@salary.com" + ) + + mark_attendance(emp_id, half_day_holiday, "Half Day", ignore_validate=True, half_day_status="Absent") + + ss = make_employee_salary_slip(emp_id, "Monthly", "Test Half Absent On Half Day Holiday") + + expected_working_days = no_of_days[0] - no_of_days[1] - 0.5 + self.assertEqual(ss.absent_days, 0.5) + self.assertEqual(ss.total_working_days, expected_working_days) + self.assertEqual(ss.payment_days, expected_working_days - 0.5) + + @HRMSTestSuite.change_settings( + "Payroll Settings", + { + "include_holidays_in_total_working_days": 0, + "payroll_based_on": "Attendance", + "consider_unmarked_attendance_as": "Present", + "daily_wages_fraction_for_half_day": 0.5, + }, + ) + def test_half_day_lwp_on_half_day_holiday(self): + """Tests that a half day leave without pay on a half day holiday is unpaid for half a day""" + no_of_days = get_no_of_days() + emp_id, half_day_holiday = setup_employee_with_half_day_holiday( + "Test Half LWP Half Day Holiday List", "test_half_lwp_on_half_day_holiday@salary.com" + ) + frappe.db.set_value("Leave Type", "Leave Without Pay", "include_holiday", 0) + + mark_attendance( + emp_id, + half_day_holiday, + "Half Day", + leave_type="Leave Without Pay", + ignore_validate=True, + half_day_status="Present", + ) + + ss = make_employee_salary_slip(emp_id, "Monthly", "Test Half LWP On Half Day Holiday") + + expected_working_days = no_of_days[0] - no_of_days[1] - 0.5 + self.assertEqual(ss.leave_without_pay, 0.5) + self.assertEqual(ss.total_working_days, expected_working_days) + self.assertEqual(ss.payment_days, expected_working_days - 0.5) + @HRMSTestSuite.change_settings( "Payroll Settings", { @@ -2854,6 +3001,31 @@ def make_payroll_period(company=None): create_payroll_period(company=company, name=company_based_payroll_period[company]) +def setup_employee_with_half_day_holiday(list_name: str, employee_email: str) -> tuple[str, str]: + """Creates a holiday list with weekly offs and one half day holiday, assigned to a new employee. + + Returns the employee and the half day holiday date.""" + from hrms.hr.doctype.holiday_list_assignment.test_holiday_list_assignment import ( + create_holiday_list_assignment, + ) + + holiday_list = make_holiday_list(list_name) + # the day after the first weekly off, so that it does not fall on one + half_day_holiday = add_days(get_first_sunday(holiday_list), 1) + add_date_to_holiday_list(half_day_holiday, holiday_list, is_half_day=1) + + emp_id = make_employee( + employee_email, + relieving_date=None, + status="Active", + company="_Test Company", + holiday_list=holiday_list, + ) + create_holiday_list_assignment("Employee", emp_id, holiday_list) + + return emp_id, half_day_holiday + + def make_holiday_list( list_name=None, from_date=None, to_date=None, add_weekly_offs=True, weekly_off_days=None ):