Skip to content
Merged
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
11 changes: 4 additions & 7 deletions hrms/api/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
4 changes: 2 additions & 2 deletions hrms/controllers/employee_boarding_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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,
Expand Down
59 changes: 38 additions & 21 deletions hrms/hr/doctype/leave_application/leave_application.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)

Expand All @@ -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)
Expand All @@ -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):
Expand Down Expand Up @@ -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",
Expand Down
Loading
Loading