diff --git a/hrms/api/__init__.py b/hrms/api/__init__.py index 23d025ca123..97b6028b11d 100644 --- a/hrms/api/__init__.py +++ b/hrms/api/__init__.py @@ -167,14 +167,11 @@ def get_attendance_for_calendar(employee: str, from_date: str, to_date: str) -> def get_holidays_for_calendar(employee: str, from_date: str, to_date: str) -> list[str]: - if holiday_list := get_holiday_list_for_employee(employee, raise_exception=False): - return frappe.get_all( - "Holiday", - filters={"parent": holiday_list, "holiday_date": ["between", [from_date, to_date]]}, - pluck="holiday_date", - ) + from hrms.utils.holiday_list import get_holiday_dates_between_range - return [] + return get_holiday_dates_between_range( + employee, from_date, to_date, raise_exception_for_holiday_list=False + ) @frappe.whitelist() diff --git a/hrms/controllers/employee_boarding_controller.py b/hrms/controllers/employee_boarding_controller.py index 18a8857f82b..b3dcb3adf2e 100644 --- a/hrms/controllers/employee_boarding_controller.py +++ b/hrms/controllers/employee_boarding_controller.py @@ -101,10 +101,10 @@ def create_task_and_notify_user(self): def get_holiday_list(self): if self.doctype == "Employee Separation": - return get_holiday_list_for_employee(self.employee) + return get_holiday_list_for_employee(self.employee, as_on=self.boarding_begins_on) else: if self.employee: - return get_holiday_list_for_employee(self.employee) + return get_holiday_list_for_employee(self.employee, as_on=self.boarding_begins_on) else: if not self.holiday_list: frappe.throw(_("Please set the Holiday List."), frappe.MandatoryError) diff --git a/hrms/hr/doctype/holiday_list_assignment/test_holiday_list_assignment.py b/hrms/hr/doctype/holiday_list_assignment/test_holiday_list_assignment.py index 7f2a58cbb7d..73600434883 100644 --- a/hrms/hr/doctype/holiday_list_assignment/test_holiday_list_assignment.py +++ b/hrms/hr/doctype/holiday_list_assignment/test_holiday_list_assignment.py @@ -4,14 +4,19 @@ from contextlib import contextmanager import frappe -from frappe.utils import add_months, get_year_ending, get_year_start, getdate +from frappe.utils import add_days, add_months, get_year_ending, get_year_start, getdate from erpnext.setup.doctype.employee.test_employee import make_employee from hrms.payroll.doctype.salary_slip.test_salary_slip import make_holiday_list from hrms.payroll.doctype.salary_structure_assignment.salary_structure_assignment import DuplicateAssignment from hrms.tests.utils import HRMSTestSuite -from hrms.utils.holiday_list import get_holiday_list_for_employee +from hrms.utils.holiday_list import ( + get_holiday_list_for_employee, + get_holiday_list_ranges_for_employee, + get_holiday_list_ranges_for_employees, + get_holidays_in_ranges_map, +) class IntegrationTestHolidayListAssignment(HRMSTestSuite): @@ -74,6 +79,238 @@ def test_default_to_company_holiday_list_assignment(self): holiday_list = get_holiday_list_for_employee(employee, as_on=getdate()) self.assertEqual(holiday_list, self.holiday_list) + def test_dates_before_first_assignment_fall_back_to_earliest_assignment(self): + employee = make_employee("test_hla_fallback@example.com", company="_Test Company") + year_start = get_year_start(getdate()) + create_holiday_list_assignment( + "Employee", employee, self.holiday_list, from_date=add_months(year_start, 6) + ) + + self.assertEqual(get_holiday_list_for_employee(employee, as_on=year_start), self.holiday_list) + self.assertEqual( + get_holiday_list_for_employee(employee, as_on=add_months(year_start, -12)), self.holiday_list + ) + + def test_holidays_in_ranges_map_restricts_each_key_to_its_ranges(self): + year_start = get_year_start(getdate()) + other_holiday_list = make_holiday_list( + list_name="Test HLA Holidays Map", from_date=year_start, to_date=get_year_ending(getdate()) + ) + for holiday_list in (self.holiday_list, other_holiday_list): + doc = frappe.get_doc("Holiday List", holiday_list) + doc.append("holidays", {"holiday_date": add_days(year_start, 1), "description": holiday_list}) + doc.append("holidays", {"holiday_date": add_days(year_start, 40), "description": holiday_list}) + doc.save() + + holidays = get_holidays_in_ranges_map( + { + "first": [ + { + "holiday_list": self.holiday_list, + "from_date": year_start, + "to_date": add_days(year_start, 30), + }, + { + "holiday_list": other_holiday_list, + "from_date": add_days(year_start, 31), + "to_date": add_days(year_start, 60), + }, + ], + "second": [ + { + "holiday_list": other_holiday_list, + "from_date": year_start, + "to_date": add_days(year_start, 30), + } + ], + "none": [], + }, + skip_weekly_offs=True, + ) + + self.assertEqual( + [(h.parent, h.holiday_date) for h in holidays["first"]], + [(self.holiday_list, add_days(year_start, 1)), (other_holiday_list, add_days(year_start, 40))], + ) + self.assertEqual( + [(h.parent, h.holiday_date) for h in holidays["second"]], + [(other_holiday_list, add_days(year_start, 1))], + ) + self.assertNotIn("none", holidays) + + def test_bulk_and_scalar_resolution_agree_for_backdated_periods(self): + employee = make_employee("test_hla_backdated@example.com", company="_Test Company") + year_start = get_year_start(getdate()) + period_start, period_end = add_months(year_start, -12), add_days(add_months(year_start, -11), -1) + create_holiday_list_assignment( + "Employee", employee, self.holiday_list, from_date=add_months(year_start, 6) + ) + + # no company assignment: both paths fall back to the employee's earliest assignment + ranges = get_holiday_list_ranges_for_employees({employee: "_Test Company"}, period_start, period_end) + self.assertEqual( + ranges, + { + employee: [ + {"holiday_list": self.holiday_list, "from_date": period_start, "to_date": period_end} + ] + }, + ) + self.assertEqual( + get_holiday_list_for_employee(employee, as_on=period_start), ranges[employee][0]["holiday_list"] + ) + + # company assignment covering the period: both paths use it + company_holiday_list = make_holiday_list( + list_name="Test HLA Backdated Company", from_date=period_start, to_date=period_end + ) + create_holiday_list_assignment( + "Company", "_Test Company", company_holiday_list, from_date=period_start + ) + ranges = get_holiday_list_ranges_for_employees({employee: "_Test Company"}, period_start, period_end) + self.assertEqual(ranges[employee][0]["holiday_list"], company_holiday_list) + self.assertEqual(get_holiday_list_for_employee(employee, as_on=period_start), company_holiday_list) + + def test_holiday_list_ranges_split_on_assignment_change(self): + employee = make_employee("test_hla_ranges@example.com", company="_Test Company") + year_start = get_year_start(getdate()) + new_holiday_list = make_holiday_list( + list_name="Test HLA Ranges", from_date=year_start, to_date=get_year_ending(getdate()) + ) + create_holiday_list_assignment("Employee", employee, self.holiday_list, from_date=year_start) + create_holiday_list_assignment( + "Employee", employee, new_holiday_list, from_date=add_months(year_start, 6) + ) + + ranges = get_holiday_list_ranges_for_employee( + employee, add_months(year_start, 5), add_days(add_months(year_start, 7), -1) + ) + self.assertEqual( + ranges, + [ + { + "holiday_list": self.holiday_list, + "from_date": add_months(year_start, 5), + "to_date": add_days(add_months(year_start, 6), -1), + }, + { + "holiday_list": new_holiday_list, + "from_date": add_months(year_start, 6), + "to_date": add_days(add_months(year_start, 7), -1), + }, + ], + ) + + ranges = get_holiday_list_ranges_for_employee( + employee, add_months(year_start, 1), add_months(year_start, 2) + ) + self.assertEqual( + ranges, + [ + { + "holiday_list": self.holiday_list, + "from_date": add_months(year_start, 1), + "to_date": add_months(year_start, 2), + } + ], + ) + + def test_holiday_list_ranges_include_assignments_inside_the_range(self): + employee = make_employee("test_hla_inner_ranges@example.com", company="_Test Company") + year_start = get_year_start(getdate()) + year_end = get_year_ending(getdate()) + inner_holiday_list = make_holiday_list( + list_name="Test HLA Inner", from_date=year_start, to_date=year_end + ) + # A -> B -> A: B is active only in the middle of the range + create_holiday_list_assignment("Employee", employee, self.holiday_list, from_date=year_start) + create_holiday_list_assignment( + "Employee", employee, inner_holiday_list, from_date=add_months(year_start, 3) + ) + frappe.get_doc( + { + "doctype": "Holiday List Assignment", + "applicable_for": "Employee", + "assigned_to": employee, + "holiday_list": self.holiday_list, + "from_date": add_months(year_start, 6), + } + ).submit() + + ranges = get_holiday_list_ranges_for_employee(employee, year_start, year_end) + self.assertEqual( + ranges, + [ + { + "holiday_list": self.holiday_list, + "from_date": year_start, + "to_date": add_days(add_months(year_start, 3), -1), + }, + { + "holiday_list": inner_holiday_list, + "from_date": add_months(year_start, 3), + "to_date": add_days(add_months(year_start, 6), -1), + }, + { + "holiday_list": self.holiday_list, + "from_date": add_months(year_start, 6), + "to_date": year_end, + }, + ], + ) + + def test_holiday_list_ranges_after_holiday_list_expiry(self): + employee = make_employee("test_hla_expired_ranges@example.com", company="_Test Company") + year_start = get_year_start(getdate()) + year_end = get_year_ending(getdate()) + expiring_holiday_list = make_holiday_list( + list_name="Test HLA Expiring", + from_date=year_start, + to_date=add_days(add_months(year_start, 6), -1), + ) + create_holiday_list_assignment("Employee", employee, expiring_holiday_list, from_date=year_start) + + # the expired list stays in effect until the next assignment starts, even when a company + # assignment covers the remaining period, matching get_holiday_list_for_employee + create_holiday_list_assignment("Company", "_Test Company", self.holiday_list, from_date=year_start) + ranges = get_holiday_list_ranges_for_employee(employee, year_start, year_end) + self.assertEqual( + ranges, + [{"holiday_list": expiring_holiday_list, "from_date": year_start, "to_date": year_end}], + ) + self.assertEqual(get_holiday_list_for_employee(employee, as_on=year_end), expiring_holiday_list) + + def test_bulk_holiday_list_ranges_fall_back_to_company(self): + employee = make_employee("test_hla_bulk_ranges@example.com", company="_Test Company") + year_start = get_year_start(getdate()) + year_end = get_year_ending(getdate()) + employee_holiday_list = make_holiday_list( + list_name="Test HLA Bulk Ranges", from_date=year_start, to_date=year_end + ) + create_holiday_list_assignment("Company", "_Test Company", self.holiday_list, from_date=year_start) + create_holiday_list_assignment( + "Employee", employee, employee_holiday_list, from_date=add_months(year_start, 6) + ) + + ranges = get_holiday_list_ranges_for_employees({employee: "_Test Company"}, year_start, year_end) + self.assertEqual( + ranges, + { + employee: [ + { + "holiday_list": self.holiday_list, + "from_date": year_start, + "to_date": add_days(add_months(year_start, 6), -1), + }, + { + "holiday_list": employee_holiday_list, + "from_date": add_months(year_start, 6), + "to_date": year_end, + }, + ] + }, + ) + def create_holiday_list_assignment( applicable_for, diff --git a/hrms/hr/doctype/leave_application/leave_application.py b/hrms/hr/doctype/leave_application/leave_application.py index 222e2f34a2b..63a1eb5a5a3 100755 --- a/hrms/hr/doctype/leave_application/leave_application.py +++ b/hrms/hr/doctype/leave_application/leave_application.py @@ -808,7 +808,9 @@ def create_leave_ledger_entry(self, submit=True): from_date=self.from_date, to_date=self.to_date, is_lwp=lwp, - holiday_list=get_holiday_list_for_employee(self.employee, raise_exception=raise_exception) + holiday_list=get_holiday_list_for_employee( + self.employee, raise_exception=raise_exception, as_on=self.from_date + ) or "", ) create_leave_ledger_entry(self, args, submit) @@ -869,20 +871,33 @@ def create_separate_ledger_entries(self, alloc_on_from_date, alloc_on_to_date, s self.half_day_date, ) - args = dict( - is_lwp=lwp, - holiday_list=get_holiday_list_for_employee(self.employee, raise_exception=raise_exception) or "", - ) + args = dict(is_lwp=lwp) if leaves_in_first_alloc: args.update( - dict(from_date=self.from_date, to_date=first_alloc_end, leaves=leaves_in_first_alloc * -1) + dict( + from_date=self.from_date, + to_date=first_alloc_end, + leaves=leaves_in_first_alloc * -1, + holiday_list=get_holiday_list_for_employee( + self.employee, raise_exception=raise_exception, as_on=self.from_date + ) + or "", + ) ) create_leave_ledger_entry(self, args, submit) if leaves_in_second_alloc: args.update( - dict(from_date=second_alloc_start, to_date=self.to_date, leaves=leaves_in_second_alloc * -1) + dict( + from_date=second_alloc_start, + to_date=self.to_date, + leaves=leaves_in_second_alloc * -1, + holiday_list=get_holiday_list_for_employee( + self.employee, raise_exception=raise_exception, as_on=second_alloc_start + ) + or "", + ) ) create_leave_ledger_entry(self, args, submit) @@ -900,7 +915,9 @@ def create_ledger_entry_for_intermediate_allocation_expiry(self, expiry_date, su to_date=expiry_date, leaves=leaves * -1, is_lwp=lwp, - holiday_list=get_holiday_list_for_employee(self.employee, raise_exception=raise_exception) + holiday_list=get_holiday_list_for_employee( + self.employee, raise_exception=raise_exception, as_on=self.from_date + ) or "", ) create_leave_ledger_entry(self, args, submit) @@ -912,7 +929,16 @@ def create_ledger_entry_for_intermediate_allocation_expiry(self, expiry_date, su ) if leaves: - args.update(dict(from_date=start_date, to_date=self.to_date, leaves=leaves * -1)) + args = dict( + from_date=start_date, + to_date=self.to_date, + leaves=leaves * -1, + is_lwp=lwp, + holiday_list=get_holiday_list_for_employee( + self.employee, raise_exception=raise_exception, as_on=start_date + ) + or "", + ) create_leave_ledger_entry(self, args, submit) def validate_leave_approver(self): @@ -1524,18 +1550,9 @@ def add_block_dates(events, start, end, employee, company): def add_holidays(events, start, end, employee, company): - applicable_holiday_list = get_holiday_list_for_employee(employee, company) - if not applicable_holiday_list: - return - - Holiday = frappe.qb.DocType("Holiday") - holidays = ( - frappe.qb.from_(Holiday) - .select(Holiday.name, Holiday.holiday_date, Holiday.description) - .where((Holiday.parent == applicable_holiday_list) & (Holiday.holiday_date.between(start, end))) - ).run(as_dict=True) - - for holiday in holidays: + from hrms.hr.utils import get_holidays_for_employee + + for holiday in get_holidays_for_employee(employee, start, end, raise_exception=False): events.append( { "doctype": "Holiday", diff --git a/hrms/hr/doctype/overtime_slip/overtime_slip.py b/hrms/hr/doctype/overtime_slip/overtime_slip.py index b1b759e4c98..fa136be4e47 100644 --- a/hrms/hr/doctype/overtime_slip/overtime_slip.py +++ b/hrms/hr/doctype/overtime_slip/overtime_slip.py @@ -372,13 +372,10 @@ def calculate_overtime_amount( return amount def get_holiday_map(self): - from erpnext.setup.doctype.employee.employee import get_holiday_list_for_employee + from hrms.utils.holiday_list import get_holiday_dates_between_range - from hrms.utils.holiday_list import get_holiday_dates_between - - holiday_list = get_holiday_list_for_employee(self.employee) - holiday_dates = get_holiday_dates_between( - holiday_list, self.start_date, self.end_date, select_weekly_off=True, as_dict=True + holiday_dates = get_holiday_dates_between_range( + self.employee, self.start_date, self.end_date, select_weekly_offs=True, as_dict=True ) holiday_date_map = {} diff --git a/hrms/hr/doctype/shift_type/shift_type.py b/hrms/hr/doctype/shift_type/shift_type.py index 45125dfc5ac..890486e230f 100644 --- a/hrms/hr/doctype/shift_type/shift_type.py +++ b/hrms/hr/doctype/shift_type/shift_type.py @@ -35,7 +35,7 @@ get_valid_shifts_for_time, ) from hrms.utils import get_date_range -from hrms.utils.holiday_list import get_holiday_dates_between +from hrms.utils.holiday_list import get_holiday_dates_between, get_holiday_dates_between_range EMPLOYEE_CHUNK_SIZE = 50 @@ -353,8 +353,12 @@ def get_dates_for_attendance(self, employee: str) -> list[str]: date_range = get_date_range(start_date, end_date) # skip marking absent on holidays - holiday_list = self.get_holiday_list(employee) - holiday_dates = get_holiday_dates_between(holiday_list, start_date, end_date) + if self.holiday_list: + holiday_dates = get_holiday_dates_between(self.holiday_list, start_date, end_date) + else: + holiday_dates = get_holiday_dates_between_range( + employee, start_date, end_date, raise_exception_for_holiday_list=False + ) # skip dates with attendance marked_attendance_dates = self.get_marked_attendance_dates_between(employee, start_date, end_date) diff --git a/hrms/hr/report/employees_working_on_a_holiday/employees_working_on_a_holiday.py b/hrms/hr/report/employees_working_on_a_holiday/employees_working_on_a_holiday.py index 3fa87892cb6..74fa57f3f43 100644 --- a/hrms/hr/report/employees_working_on_a_holiday/employees_working_on_a_holiday.py +++ b/hrms/hr/report/employees_working_on_a_holiday/employees_working_on_a_holiday.py @@ -4,8 +4,9 @@ import frappe from frappe import _ +from frappe.utils import getdate -from erpnext.setup.doctype.employee.employee import get_holiday_list_for_employee +from hrms.utils.holiday_list import get_holiday_list_ranges_for_employees, get_holidays_in_ranges_map def execute(filters=None): @@ -51,40 +52,37 @@ def get_columns(): def get_data(filters): - Attendance = frappe.qb.DocType("Attendance") - Holiday = frappe.qb.DocType("Holiday") - - data = [] - employee_filters = {"company": filters.company} if filters.department: employee_filters["department"] = filters.department - for employee in frappe.get_list("Employee", filters=employee_filters, pluck="name"): - holiday_list = get_holiday_list_for_employee(employee, raise_exception=False) - if not holiday_list or (filters.holiday_list and filters.holiday_list != holiday_list): - continue + employees = frappe.get_list("Employee", filters=employee_filters, fields=["name", "company"]) + holiday_list_ranges = get_holiday_list_ranges_for_employees( + {employee.name: employee.company for employee in employees}, filters.from_date, filters.to_date + ) + holiday_map = { + (employee, getdate(holiday.holiday_date)): holiday + for employee, holidays in get_holidays_in_ranges_map(holiday_list_ranges).items() + for holiday in holidays + if not filters.holiday_list or holiday.parent == filters.holiday_list + } + if not holiday_map: + return [] - working_days = ( - frappe.qb.from_(Attendance) - .inner_join(Holiday) - .on(Attendance.attendance_date == Holiday.holiday_date) - .select( - Attendance.employee, - Attendance.employee_name, - Attendance.attendance_date, - Attendance.status, - Holiday.description, - ) - .where( - (Attendance.employee == employee) - & (Attendance.attendance_date[filters.from_date : filters.to_date]) - & (Attendance.status.notin(["Absent", "On Leave"])) - & (Attendance.docstatus == 1) - & (Holiday.parent == holiday_list) - ) - .run(as_list=True) - ) - data.extend(working_days) + Attendance = frappe.qb.DocType("Attendance") + attendance = ( + frappe.qb.from_(Attendance) + .select(Attendance.employee, Attendance.employee_name, Attendance.attendance_date, Attendance.status) + .where(Attendance.employee.isin(list({employee for employee, _ in holiday_map}))) + .where(Attendance.attendance_date[filters.from_date : filters.to_date]) + .where(Attendance.status.notin(["Absent", "On Leave"])) + .where(Attendance.docstatus == 1) + .orderby(Attendance.employee) + .orderby(Attendance.attendance_date) + ).run(as_dict=True) - return data + return [ + [row.employee, row.employee_name, row.attendance_date, row.status, holiday.description] + for row in attendance + if (holiday := holiday_map.get((row.employee, getdate(row.attendance_date)))) + ] diff --git a/hrms/hr/report/monthly_attendance_sheet/monthly_attendance_sheet.py b/hrms/hr/report/monthly_attendance_sheet/monthly_attendance_sheet.py index 54c304b408c..75a9a33ae70 100644 --- a/hrms/hr/report/monthly_attendance_sheet/monthly_attendance_sheet.py +++ b/hrms/hr/report/monthly_attendance_sheet/monthly_attendance_sheet.py @@ -17,10 +17,7 @@ from frappe.utils.nestedset import get_descendants_of from hrms.utils import date_diff, get_date_range -from hrms.utils.holiday_list import ( - fill_employee_holiday_list_date_gaps_with_company_holiday_list, - get_assigned_holiday_lists_to_employee_and_company, -) +from hrms.utils.holiday_list import get_holiday_list_ranges_for_employees, get_holidays_in_ranges_map Filters = frappe._dict @@ -383,7 +380,6 @@ def get_employee_related_details(filters: Filters) -> tuple[dict, list]: Employee.department, Employee.branch, Employee.company, - Employee.holiday_list, (Employee.date_of_joining).as_("joined_date"), Case() .when( @@ -431,68 +427,17 @@ def get_employee_related_details(filters: Filters) -> tuple[dict, list]: def get_employee_holiday_map(employee_details: dict, filters: Filters) -> dict[str, list[dict]]: - """ - Builds {employee: [holidays]} for all employees in two queries. - - Query 1 — bulk HLA fetch for all employees + their companies. - Query 2 — holidays for only the holiday lists employees are actually assigned to. - - Per-employee lookup after this call is an O(1) dict access. - """ + """Builds {employee: [holidays]} for all employees in two queries: one for assignments, one for holidays""" if not employee_details: return {} start_date, end_date = get_date_range_from_filters(filters) - - employees = list(employee_details.keys()) - companies = list({d.company for d in employee_details.values() if d.get("company")}) - - assigned_holiday_lists = get_assigned_holiday_lists_to_employee_and_company( - employees + companies, start_date, end_date + employee_holiday_list_ranges = get_holiday_list_ranges_for_employees( + {employee: details.get("company") for employee, details in employee_details.items()}, + start_date, + end_date, ) - - # gaps in employee-level assignments are filled with company-level assignments, - employee_hl_ranges = {} - for employee, details in employee_details.items(): - employee_ranges = assigned_holiday_lists.get(employee, []) - company_ranges = assigned_holiday_lists.get(details.get("company"), []) - ranges = fill_employee_holiday_list_date_gaps_with_company_holiday_list( - employee_ranges, company_ranges, start_date, end_date - ) - if ranges: - employee_hl_ranges[employee] = ranges - - if not employee_hl_ranges: - return {} - - # collect only the HL names employees are actually assigned to - used_hl_names = {r["holiday_list"] for ranges in employee_hl_ranges.values() for r in ranges} - - Holiday = frappe.qb.DocType("Holiday") - holiday_rows = ( - frappe.qb.from_(Holiday) - .select(Holiday.parent, Holiday.holiday_date, Holiday.weekly_off) - .where(Holiday.parent.isin(list(used_hl_names))) - .where(Holiday.holiday_date.between(start_date, end_date)) - ).run(as_dict=True) - - hl_holidays = {} - for h in holiday_rows: - hl_holidays.setdefault(h.parent, []).append(h) - - # filter holidays to each employee's effective ranges - employee_holiday_map = {} - for employee, ranges in employee_hl_ranges.items(): - holidays = [ - h - for r in ranges - for h in hl_holidays.get(r["holiday_list"], []) - if r["from_date"] <= getdate(h.holiday_date) <= r["to_date"] - ] - if holidays: - employee_holiday_map[employee] = holidays - - return employee_holiday_map + return get_holidays_in_ranges_map(employee_holiday_list_ranges) def get_date_range_from_filters(filters: Filters) -> tuple: diff --git a/hrms/hr/utils.py b/hrms/hr/utils.py index 11fe15869a7..cc9bfc74f38 100644 --- a/hrms/hr/utils.py +++ b/hrms/hr/utils.py @@ -33,10 +33,7 @@ import erpnext from erpnext import get_company_currency -from erpnext.setup.doctype.employee.employee import ( - InactiveEmployeeStatusError, - get_holiday_list_for_employee, -) +from erpnext.setup.doctype.employee.employee import InactiveEmployeeStatusError from hrms.hr.doctype.leave_policy_assignment.leave_policy_assignment import ( calculate_pro_rated_leaves, @@ -728,21 +725,12 @@ def get_holidays_for_employee(employee, start_date, end_date, raise_exception=Tr return: list of dicts with `holiday_date` and `description` """ - holiday_list = get_holiday_list_for_employee(employee, raise_exception=raise_exception) - - if not holiday_list: - return [] - - filters = {"parent": holiday_list, "holiday_date": ("between", [start_date, end_date])} + from hrms.utils.holiday_list import get_holiday_list_ranges_for_employee, get_holidays_in_ranges - if only_non_weekly: - filters["weekly_off"] = False - - holidays = frappe.get_all( - "Holiday", fields=["description", "holiday_date"], filters=filters, order_by="holiday_date" + holiday_list_ranges = get_holiday_list_ranges_for_employee( + employee, start_date, end_date, raise_exception=raise_exception ) - - return holidays + return get_holidays_in_ranges(holiday_list_ranges, skip_weekly_offs=only_non_weekly) @erpnext.allow_regional diff --git a/hrms/payroll/doctype/payroll_entry/payroll_entry.py b/hrms/payroll/doctype/payroll_entry/payroll_entry.py index 3149a40108c..b6890291433 100644 --- a/hrms/payroll/doctype/payroll_entry/payroll_entry.py +++ b/hrms/payroll/doctype/payroll_entry/payroll_entry.py @@ -31,6 +31,11 @@ from hrms.payroll.doctype.salary_slip.salary_slip_loan_utils import if_lending_app_installed from hrms.payroll.doctype.salary_withholding.salary_withholding import link_bank_entry_in_salary_withholdings +from hrms.utils.holiday_list import ( + clip_holiday_list_ranges, + get_holiday_list_ranges_for_employees, + get_holidays_in_ranges_map, +) class PayrollEntry(Document): @@ -1288,22 +1293,30 @@ def get_employees_with_unmarked_attendance(self) -> list[dict] | None: return unmarked_attendance = [] - employee_details = self.get_employee_and_attendance_details() - default_holiday_list = frappe.db.get_value( - "Company", self.company, "default_holiday_list", cache=True + employee_details = {record.name: record for record in self.get_employee_and_attendance_details()} + holiday_list_ranges = get_holiday_list_ranges_for_employees( + {name: details.company for name, details in employee_details.items()}, + self.start_date, + self.end_date, ) + payroll_dates = {} + for employee, details in employee_details.items(): + payroll_dates[employee] = self.get_payroll_dates_for_employee(details) + holiday_list_ranges[employee] = clip_holiday_list_ranges( + holiday_list_ranges.get(employee, []), *payroll_dates[employee] + ) + + holidays = get_holidays_in_ranges_map(holiday_list_ranges) + for emp in self.employees: - details = next((record for record in employee_details if record.name == emp.employee), None) + details = employee_details.get(emp.employee) if not details: continue - start_date, end_date = self.get_payroll_dates_for_employee(details) - holidays = self.get_holidays_count( - details.holiday_list or default_holiday_list, start_date, end_date - ) + start_date, end_date = payroll_dates[emp.employee] payroll_days = date_diff(end_date, start_date) + 1 - unmarked_days = payroll_days - (holidays + details.attendance_count) + unmarked_days = payroll_days - (len(holidays.get(emp.employee, [])) + details.attendance_count) if unmarked_days > 0: unmarked_attendance.append( @@ -1323,7 +1336,7 @@ def get_employee_and_attendance_details(self) -> list[dict]: "name": "HREMP00001", "date_of_joining": "2019-01-01", "relieving_date": "2022-01-01", - "holiday_list": "Holiday List Company", + "company": "_Test Company", "attendance_count": 22 } ] @@ -1345,7 +1358,7 @@ def get_employee_and_attendance_details(self) -> list[dict]: Employee.name, Employee.date_of_joining, Employee.relieving_date, - Employee.holiday_list, + Employee.company, Count(Attendance.name).as_("attendance_count"), ) .where(Employee.name.isin(employees)) @@ -1363,26 +1376,6 @@ def get_payroll_dates_for_employee(self, employee_details: dict) -> tuple[str, s return start_date, end_date - def get_holidays_count(self, holiday_list: str, start_date: str, end_date: str) -> float: - """Returns number of holidays between start and end dates in the holiday list""" - if not hasattr(self, "_holidays_between_dates"): - self._holidays_between_dates = {} - - key = f"{start_date}-{end_date}-{holiday_list}" - if key in self._holidays_between_dates: - return self._holidays_between_dates[key] - - holidays = frappe.db.get_all( - "Holiday", - filters={"parent": holiday_list, "holiday_date": ("between", [start_date, end_date])}, - fields=[{"COUNT": "*", "as": "holidays_count"}], - )[0] - - if holidays: - self._holidays_between_dates[key] = holidays.holidays_count - - return self._holidays_between_dates.get(key) or 0 - @frappe.whitelist(methods=["POST"]) def create_overtime_slips(self) -> None: self.check_permission("write") diff --git a/hrms/payroll/doctype/payroll_entry/test_payroll_entry.py b/hrms/payroll/doctype/payroll_entry/test_payroll_entry.py index 661953013a0..74c6dc5f88e 100644 --- a/hrms/payroll/doctype/payroll_entry/test_payroll_entry.py +++ b/hrms/payroll/doctype/payroll_entry/test_payroll_entry.py @@ -785,6 +785,44 @@ def test_validate_attendance(self): employees = payroll_entry.get_employees_with_unmarked_attendance() self.assertFalse(employees) + def test_validate_attendance_with_holiday_list_assignment(self): + from hrms.hr.doctype.holiday_list_assignment.test_holiday_list_assignment import ( + create_holiday_list_assignment, + ) + from hrms.payroll.doctype.salary_slip.test_salary_slip import make_holiday_list + + company = frappe.get_doc("Company", "_Test Company") + employee = make_employee("test_validate_attendance_hla@payroll.com", company=company.name) + setup_salary_structure(employee, company) + + dates = get_start_end_dates("Monthly", nowdate()) + weekend_off_list = make_holiday_list( + "Test Payroll Entry Weekend Off", + dates.start_date, + dates.end_date, + weekly_off_days=["Saturday", "Sunday"], + ) + create_holiday_list_assignment("Employee", employee, weekend_off_list, from_date=dates.start_date) + + payroll_entry = get_payroll_entry( + start_date=dates.start_date, + end_date=dates.end_date, + payable_account=company.default_payroll_payable_account, + currency=company.default_currency, + company=company.name, + cost_center="Main - _TC", + ) + payroll_entry.validate_attendance = True + + unmarked = next( + d for d in payroll_entry.get_employees_with_unmarked_attendance() if d["employee"] == employee + ) + weekend_days = len( + [d for d in get_date_range(dates.start_date, dates.end_date) if getdate(d).weekday() >= 5] + ) + payroll_days = date_diff(dates.end_date, dates.start_date) + 1 + self.assertEqual(unmarked["unmarked_days"], payroll_days - weekend_days) + @HRMSTestSuite.change_settings( "Payroll Settings", { diff --git a/hrms/payroll/doctype/salary_slip/salary_slip.py b/hrms/payroll/doctype/salary_slip/salary_slip.py index 62571324e9e..069d600fc7c 100644 --- a/hrms/payroll/doctype/salary_slip/salary_slip.py +++ b/hrms/payroll/doctype/salary_slip/salary_slip.py @@ -31,7 +31,6 @@ import erpnext from erpnext.accounts.utils import get_fiscal_year -from erpnext.setup.doctype.employee.employee import get_holiday_list_for_employee from erpnext.utilities.transaction_base import TransactionBase import hrms @@ -61,7 +60,7 @@ get_component_eval_context, throw_error_message, ) -from hrms.utils.holiday_list import get_holiday_dates_between +from hrms.utils.holiday_list import get_holiday_list_ranges_for_employee, get_holidays_in_ranges_map # cache keys HOLIDAYS_BETWEEN_DATES = "holidays_between_dates" @@ -739,15 +738,21 @@ def get_payment_days(self, include_holidays_in_total_working_days): return payment_days def get_holidays_for_employee(self, start_date, end_date): - holiday_list = get_holiday_list_for_employee(self.employee) - key = f"{holiday_list}:{start_date}:{end_date}" - holiday_dates = frappe.cache().hget(HOLIDAYS_BETWEEN_DATES, key) - - if not holiday_dates: - holiday_dates = get_holiday_dates_between(holiday_list, start_date, end_date) - frappe.cache().hset(HOLIDAYS_BETWEEN_DATES, key, holiday_dates) - - return holiday_dates + holidays_by_range = {} + uncached_ranges = {} + for holiday_list_range in get_holiday_list_ranges_for_employee(self.employee, start_date, end_date): + key = "{holiday_list}:{from_date}:{to_date}".format(**holiday_list_range) + holidays_by_range[key] = frappe.cache().hget(HOLIDAYS_BETWEEN_DATES, key) + if not holidays_by_range[key]: + uncached_ranges[key] = [holiday_list_range] + + if uncached_ranges: + fetched = get_holidays_in_ranges_map(uncached_ranges) + for key in uncached_ranges: + holidays_by_range[key] = [holiday.holiday_date for holiday in fetched.get(key, [])] + frappe.cache().hset(HOLIDAYS_BETWEEN_DATES, key, holidays_by_range[key]) + + return [holiday_date for holidays in holidays_by_range.values() for holiday_date in holidays] def calculate_lwp_or_ppl_based_on_leave_application( self, holidays, working_days_list, daily_wages_fraction_for_half_day diff --git a/hrms/payroll/doctype/salary_slip/test_salary_slip.py b/hrms/payroll/doctype/salary_slip/test_salary_slip.py index 43422542ee6..ee263c40c2c 100644 --- a/hrms/payroll/doctype/salary_slip/test_salary_slip.py +++ b/hrms/payroll/doctype/salary_slip/test_salary_slip.py @@ -673,6 +673,40 @@ def test_salary_slip_with_holidays_excluded(self): self.assertEqual(ss.earnings[1].amount, 3000) self.assertEqual(ss.gross_pay, 78000) + @HRMSTestSuite.change_settings("Payroll Settings", {"include_holidays_in_total_working_days": 0}) + def test_holidays_resolved_from_holiday_list_assigned_during_slip_period(self): + from hrms.hr.doctype.holiday_list_assignment.test_holiday_list_assignment import ( + create_holiday_list_assignment, + ) + + emp_id = make_employee( + "test_holiday_list_assigned_during_slip_period@salary.com", + relieving_date=None, + status="Active", + company="_Test Company", + ) + year_start, year_end = get_year_start(getdate()), get_year_ending(getdate()) + sunday_off_list = make_holiday_list("Test Slip Period Sunday Off", year_start, year_end) + no_off_list = make_holiday_list( + "Test Slip Period No Off", year_start, year_end, add_weekly_offs=False + ) + create_holiday_list_assignment("Employee", emp_id, sunday_off_list, from_date=year_start) + create_holiday_list_assignment("Employee", emp_id, no_off_list, from_date=getdate()) + + ss = make_employee_salary_slip(emp_id, "Monthly", "Test Salary Slip Holiday List Assigned Period") + + # holidays before today come from the Sunday off list, none after the assignment changes + sundays_before_today = len( + [ + d + for d in range(date_diff(getdate(), get_first_day(getdate()))) + if add_days(get_first_day(getdate()), d).weekday() == 6 + ] + ) + days_in_month = get_no_of_days()[0] + 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", { diff --git a/hrms/utils/holiday_list.py b/hrms/utils/holiday_list.py index df497be4b33..a5e329fe3d2 100644 --- a/hrms/utils/holiday_list.py +++ b/hrms/utils/holiday_list.py @@ -39,169 +39,267 @@ def get_holiday_dates_between_range( skip_weekly_offs: bool = False, select_weekly_offs: bool = False, raise_exception_for_holiday_list: bool = True, + as_dict: bool = False, ) -> list: - start_date = getdate(start_date) - end_date = getdate(end_date) - - from_holiday_list = ( - get_holiday_list_for_employee( - assigned_to, as_on=start_date, as_dict=True, raise_exception=raise_exception_for_holiday_list - ) - or {} - ) - to_holiday_list = ( - get_holiday_list_for_employee( - assigned_to, as_on=end_date, as_dict=True, raise_exception=raise_exception_for_holiday_list - ) - or {} + """Returns holidays between the dates, honouring holiday list changes within the range""" + holiday_list_ranges = get_holiday_list_ranges_for_employee( + assigned_to, start_date, end_date, raise_exception=raise_exception_for_holiday_list ) + holidays = get_holidays_in_ranges(holiday_list_ranges, skip_weekly_offs=skip_weekly_offs) - if ( - from_holiday_list - and to_holiday_list - and from_holiday_list.holiday_list != to_holiday_list.holiday_list - ): - return list( - set( - get_holiday_dates_between( - holiday_list=from_holiday_list.holiday_list, - start_date=start_date, - end_date=add_days(to_holiday_list.from_date, -1), - select_weekly_off=select_weekly_offs, - skip_weekly_offs=skip_weekly_offs, - ) - + get_holiday_dates_between( - holiday_list=to_holiday_list.holiday_list, - start_date=to_holiday_list.from_date, - end_date=end_date, - select_weekly_off=select_weekly_offs, - skip_weekly_offs=skip_weekly_offs, - ) - ) - ) - elif holiday_list := from_holiday_list.get("holiday_list", None) or to_holiday_list.get( - "holiday_list", None - ): - return get_holiday_dates_between( - holiday_list=holiday_list, - start_date=start_date, - end_date=end_date, - select_weekly_off=select_weekly_offs, - skip_weekly_offs=skip_weekly_offs, - ) - else: - return [] + if as_dict: + return holidays + return [holiday.holiday_date for holiday in holidays] -def get_holiday_list_for_employee( - employee: str, raise_exception: bool = True, as_on: date | str | None = None, as_dict: bool = False -) -> str: - as_on = frappe.utils.getdate(as_on) - holiday_list = get_assigned_holiday_list(employee, as_on, as_dict) - if not holiday_list: - company = frappe.db.get_value("Employee", employee, "company") - holiday_list = get_assigned_holiday_list(company, as_on, as_dict) - - if not holiday_list and raise_exception: - frappe.throw( - _( - "No Holiday List was found for Employee {0} or their company {1} for date {2}. Please assign through {3}" - ).format( - frappe.bold(employee), - frappe.bold(company), - frappe.bold(formatdate(as_on)), - get_link_to_form("Holiday List Assignment", label="Holiday List Assignment"), - ) - ) - return holiday_list +def get_holidays_in_ranges( + holiday_list_ranges: list[dict], skip_weekly_offs: bool = False +) -> list[frappe._dict]: + """Returns holidays falling within the given holiday list ranges in a single query""" + return get_holidays_in_ranges_map({None: holiday_list_ranges}, skip_weekly_offs).get(None, []) -def get_assigned_holiday_list(assigned_to: str, as_on=None, as_dict: bool = False) -> str: - as_on = frappe.utils.getdate(as_on) - HLA = frappe.qb.DocType("Holiday List Assignment") + +def get_holidays_in_ranges_map( + holiday_list_ranges_map: dict[str, list[dict]], skip_weekly_offs: bool = False +) -> dict[str, list[frappe._dict]]: + """ + Fetches holidays for many keys (usually employees) in a single query, each restricted to its own + holiday list ranges. + + {"EMP-001": [{"name", "parent", "holiday_date", "description", "weekly_off"}, ...]} + """ + all_ranges = [ + holiday_list_range + for holiday_list_ranges in holiday_list_ranges_map.values() + for holiday_list_range in holiday_list_ranges + ] + if not all_ranges: + return {} + + Holiday = frappe.qb.DocType("Holiday") query = ( - frappe.qb.from_(HLA) - .select(HLA.holiday_list) - .where(HLA.assigned_to == assigned_to) - .where(HLA.from_date <= as_on) - .where(HLA.docstatus == 1) - .orderby(HLA.from_date, order=frappe.qb.desc) - .limit(1) + frappe.qb.from_(Holiday) + .select(Holiday.name, Holiday.parent, Holiday.holiday_date, Holiday.description, Holiday.weekly_off) + .where(Holiday.parent.isin(list({r["holiday_list"] for r in all_ranges}))) + .where( + Holiday.holiday_date.between( + min(getdate(r["from_date"]) for r in all_ranges), + max(getdate(r["to_date"]) for r in all_ranges), + ) + ) + .orderby(Holiday.holiday_date) ) - if as_dict: - query = query.select(HLA.from_date) - holiday_list = query.run(as_dict=True) - return holiday_list[0] if holiday_list else None + if skip_weekly_offs: + query = query.where(Holiday.weekly_off == 0) + + holidays_by_list = {} + for holiday in query.run(as_dict=True): + holidays_by_list.setdefault(holiday.parent, []).append(holiday) - result = query.run() - holiday_list = result[0][0] if result else None + holidays_map = {} + for key, holiday_list_ranges in holiday_list_ranges_map.items(): + holidays = [ + holiday + for holiday_list_range in sorted(holiday_list_ranges, key=lambda r: r["from_date"]) + for holiday in holidays_by_list.get(holiday_list_range["holiday_list"], []) + if getdate(holiday_list_range["from_date"]) + <= getdate(holiday.holiday_date) + <= getdate(holiday_list_range["to_date"]) + ] + if holidays: + holidays_map[key] = holidays - return holiday_list + return holidays_map -def get_assigned_holiday_lists_to_employee_and_company( - assigned_to_list: list[str], +def get_holiday_list_ranges_for_employee( + employee: str, start_date: date | str, end_date: date | str, -) -> dict[str, list[dict]]: + raise_exception: bool = True, +) -> list[frappe._dict]: """ - Returns effective holiday list ranges for multiple assigned_to values (employees/companies) in one query. + Splits [start_date, end_date] into ranges, one per holiday list assigned during it. - { - "EMP-001": [{"holiday_list": "HL-1", "from_date": date, "to_date": date}, ...], - "EMP-002": [...], - } + [{"holiday_list": "HL-1", "from_date": date, "to_date": date}, ...] """ - if not assigned_to_list: + company = frappe.db.get_value("Employee", employee, "company") + return get_holiday_list_ranges_for_employees( + {employee: company}, start_date, end_date, raise_exception=raise_exception + ).get(employee, []) + + +def get_holiday_list_ranges_for_employees( + employee_company_map: dict[str, str], + start_date: date | str, + end_date: date | str, + raise_exception: bool = False, +) -> dict[str, list[frappe._dict]]: + """ + Resolves the holiday list in effect on every date of [start_date, end_date] for many employees, + fetching all of their and their companies' assignments in a single query. An assignment stays in + effect until the next assignment starts, exactly as in `get_holiday_list_for_employee`. Dates before + the employee's first assignment use the company's assignments, and dates before both use the + earliest assignment. + + {"EMP-001": [{"holiday_list": "HL-1", "from_date": date, "to_date": date}, ...]} + """ + if not employee_company_map: return {} start_date = getdate(start_date) end_date = getdate(end_date) + companies = list({company for company in employee_company_map.values() if company}) + + assignments = get_holiday_list_assignments(list(employee_company_map) + companies) + effective_ranges = build_effective_date_ranges_for_holiday_assignments(assignments, start_date, end_date) + + employee_holiday_list_ranges = {} + for employee, company in employee_company_map.items(): + ranges = fill_employee_holiday_list_date_gaps_with_company_holiday_list( + effective_ranges.get(employee, []), effective_ranges.get(company, []), start_date, end_date + ) + ranges = fill_uncovered_dates_with_assigned_holiday_list( + ranges, assignments.get(employee, []), assignments.get(company, []), start_date, end_date + ) + + if ranges: + employee_holiday_list_ranges[employee] = ranges + elif raise_exception: + throw_no_holiday_list_assigned(employee, company, start_date) - return build_holiday_list_map(assigned_to_list, start_date, end_date) + return employee_holiday_list_ranges -def build_holiday_list_map( - assigned_to_list: list[str], +def fill_uncovered_dates_with_assigned_holiday_list( + ranges: list[dict], + employee_assignments: list[dict], + company_assignments: list[dict], start_date: date, end_date: date, -) -> dict[str, list[dict]]: +) -> list[frappe._dict]: + """ + Dates before the first assignment resolve like a single date would. Adjacent ranges using the + same holiday list are merged. """ - Single query + compute effective HLA ranges per assigned_to, clipped to start_date/end_date. - effective_to_date = MIN(HL.to_date, next assignment's from_date - 1 day) + filled = [] + + def add_range(holiday_list, from_date, to_date): + last = filled[-1] if filled else None + if last and last.holiday_list == holiday_list and add_days(last.to_date, 1) == from_date: + last.to_date = to_date + else: + filled.append(frappe._dict(holiday_list=holiday_list, from_date=from_date, to_date=to_date)) + + current = start_date + for holiday_list_range in [*sorted(ranges, key=lambda r: r["from_date"]), None]: + gap_end = add_days(holiday_list_range["from_date"], -1) if holiday_list_range else end_date + if current <= gap_end: + assignment = resolve_holiday_list_assignment(employee_assignments, company_assignments, current) + if assignment: + add_range(assignment.holiday_list, current, gap_end) + + if holiday_list_range: + add_range( + holiday_list_range["holiday_list"], + getdate(holiday_list_range["from_date"]), + getdate(holiday_list_range["to_date"]), + ) + current = add_days(holiday_list_range["to_date"], 1) + + return filled + - { - "EMP-001": [{"holiday_list": "HL-1", "from_date": date, "to_date": date}, ...], - } +def resolve_holiday_list_assignment( + employee_assignments: list[dict], company_assignments: list[dict], as_on: date +) -> frappe._dict | None: """ - HLA = frappe.qb.DocType("Holiday List Assignment") - HolidayList = frappe.qb.DocType("Holiday List") + Latest assignment starting on or before `as_on`, employee first and company second. + Dates before the first assignment fall back to the earliest one. + """ + for assignments in (employee_assignments, company_assignments): + active = [a for a in assignments if getdate(a.from_date) <= as_on] + if active: + return active[-1] + + for assignments in (employee_assignments, company_assignments): + if assignments: + return assignments[0] + + return None + + +def get_holiday_list_assignments(assigned_to_list: list[str]) -> dict[str, list[frappe._dict]]: + """Submitted assignments of the given employees and companies, ordered by from_date""" + assigned_to_list = [assigned_to for assigned_to in assigned_to_list if assigned_to] + if not assigned_to_list: + return {} - holiday_list_assignments = ( + HLA = frappe.qb.DocType("Holiday List Assignment") + assignments = ( frappe.qb.from_(HLA) - .join(HolidayList) - .on(HLA.holiday_list == HolidayList.name) - .select( - HLA.assigned_to, - HLA.holiday_list, - HLA.from_date, - HolidayList.to_date.as_("holiday_list_to_date"), - ) + .select(HLA.assigned_to, HLA.holiday_list, HLA.from_date) .where(HLA.assigned_to.isin(assigned_to_list)) .where(HLA.docstatus == 1) - .where(HLA.from_date <= end_date) - .where(HolidayList.to_date >= start_date) .orderby(HLA.assigned_to) .orderby(HLA.from_date) ).run(as_dict=True) - holiday_assignment_map = {} - for assignment in holiday_list_assignments: - holiday_assignment_map.setdefault(assignment.assigned_to, []).append(assignment) + assignment_map = {} + for assignment in assignments: + assignment_map.setdefault(assignment.assigned_to, []).append(assignment) - result = build_effective_date_ranges_for_holiday_assignments(holiday_assignment_map, start_date, end_date) + return assignment_map - return result + +def get_holiday_list_for_employee( + employee: str, raise_exception: bool = True, as_on: date | str | None = None, as_dict: bool = False +) -> str: + as_on = getdate(as_on) + company = frappe.db.get_value("Employee", employee, "company") + assignments = get_holiday_list_assignments([employee, company]) + assignment = resolve_holiday_list_assignment( + assignments.get(employee, []), assignments.get(company, []), as_on + ) + + if not assignment: + if raise_exception: + throw_no_holiday_list_assigned(employee, company, as_on) + return None + + if as_dict: + return frappe._dict(holiday_list=assignment.holiday_list, from_date=assignment.from_date) + + return assignment.holiday_list + + +def throw_no_holiday_list_assigned(employee: str, company: str, as_on: date): + frappe.throw( + _( + "No Holiday List was found for Employee {0} or their company {1} for date {2}. Please assign through {3}" + ).format( + frappe.bold(employee), + frappe.bold(company), + frappe.bold(formatdate(as_on)), + get_link_to_form("Holiday List Assignment", label="Holiday List Assignment"), + ) + ) + + +def get_assigned_holiday_list(assigned_to: str, as_on=None, as_dict: bool = False) -> str: + """Holiday list assigned directly to the employee or company on `as_on`""" + as_on = getdate(as_on) + assignments = get_holiday_list_assignments([assigned_to]).get(assigned_to, []) + active = [a for a in assignments if getdate(a.from_date) <= as_on] + if not active: + return None + + assignment = active[-1] + if as_dict: + return frappe._dict(holiday_list=assignment.holiday_list, from_date=assignment.from_date) + + return assignment.holiday_list def build_effective_date_ranges_for_holiday_assignments( @@ -210,20 +308,15 @@ def build_effective_date_ranges_for_holiday_assignments( end_date: date, ) -> dict[str, list[dict]]: """ - Returns map of {assigned_to: [raw_holiday_list_assignment_rows]}, - effective_to_date = MIN(HL.to_date, next assignment's from_date - 1 day) + Returns map of {assigned_to: [{"holiday_list", "from_date", "to_date"}]} clipped to [start_date, end_date]. + Each assignment stays in effect until the day before the next assignment starts. """ result = {} for assigned_to, assignments in holiday_assignment_map.items(): ranges = [] for idx, assignment in enumerate(assignments): - hl_to_date = getdate(assignment.holiday_list_to_date) next_assignment = assignments[idx + 1] if idx + 1 < len(assignments) else None - - if next_assignment: - effective_to_date = min(hl_to_date, add_days(next_assignment.from_date, -1)) - else: - effective_to_date = hl_to_date + effective_to_date = add_days(next_assignment.from_date, -1) if next_assignment else end_date from_date = max(getdate(assignment.from_date), start_date) effective_to_date = min(getdate(effective_to_date), end_date) @@ -296,6 +389,22 @@ def fill_employee_holiday_list_date_gaps_with_company_holiday_list( return sorted(result, key=lambda r: r["from_date"]) +def clip_holiday_list_ranges(holiday_list_ranges: list[dict], start_date: date, end_date: date) -> list[dict]: + """Restricts the ranges to [start_date, end_date]""" + clipped = [] + for holiday_list_range in holiday_list_ranges: + from_date = max(getdate(holiday_list_range["from_date"]), getdate(start_date)) + to_date = min(getdate(holiday_list_range["to_date"]), getdate(end_date)) + if from_date <= to_date: + clipped.append( + frappe._dict( + holiday_list=holiday_list_range["holiday_list"], from_date=from_date, to_date=to_date + ) + ) + + return clipped + + def invalidate_cache(doc, method=None): from hrms.payroll.doctype.salary_slip.salary_slip import HOLIDAYS_BETWEEN_DATES