diff --git a/erpnext/hr/doctype/leave_application/leave_application.py b/erpnext/hr/doctype/leave_application/leave_application.py index 08bc93760a3..d6f8c25b424 100755 --- a/erpnext/hr/doctype/leave_application/leave_application.py +++ b/erpnext/hr/doctype/leave_application/leave_application.py @@ -873,6 +873,9 @@ def get_leave_allocation_records(employee, date, leave_type=None): | ( (Ledger.is_carry_forward == 1) & (Ledger.to_date.between(LeaveAllocation.from_date, LeaveAllocation.to_date)) + # only consider cf leaves from current allocation + & (LeaveAllocation.from_date <= date) + & (date <= LeaveAllocation.to_date) ) ) ) diff --git a/erpnext/hr/doctype/leave_application/test_leave_application.py b/erpnext/hr/doctype/leave_application/test_leave_application.py index e30b84bbf34..8d1adaca62b 100644 --- a/erpnext/hr/doctype/leave_application/test_leave_application.py +++ b/erpnext/hr/doctype/leave_application/test_leave_application.py @@ -1170,25 +1170,51 @@ class TestLeaveApplication(unittest.TestCase): details = get_leave_allocation_records(employee.name, add_days(cf_expiry, 1), leave_type.name) self.assertEqual(details.get(leave_type.name), expected_data) + @set_holiday_list("Salary Slip Test Holiday List", "_Test Company") + def test_filtered_old_cf_entries_in_get_leave_allocation_records(self): + """Tests whether old cf entries are ignored while fetching current allocation records""" + employee = get_employee() + leave_type = create_leave_type( + leave_type_name="_Test_CF_leave_expiry", + is_carry_forward=1, + expire_carry_forwarded_leaves_after_days=90, + ) + + # old allocation with cf leaves + create_carry_forwarded_allocation(employee, leave_type, date="2019-01-01") + # new allocation with cf leaves + leave_alloc = create_carry_forwarded_allocation(employee, leave_type) + cf_expiry = frappe.db.get_value( + "Leave Ledger Entry", {"transaction_name": leave_alloc.name, "is_carry_forward": 1}, "to_date" + ) + + # test total leaves allocated before cf leave expiry + details = get_leave_allocation_records(employee.name, add_days(cf_expiry, -1), leave_type.name) + # filters out old CF leaves (15 i.e total 45) + self.assertEqual(details[leave_type.name]["total_leaves_allocated"], 30.0) + + +def create_carry_forwarded_allocation(employee, leave_type, date=None): + date = date or nowdate() -def create_carry_forwarded_allocation(employee, leave_type): # initial leave allocation leave_allocation = create_leave_allocation( leave_type="_Test_CF_leave_expiry", employee=employee.name, employee_name=employee.employee_name, - from_date=add_months(nowdate(), -24), - to_date=add_months(nowdate(), -12), + from_date=add_months(date, -24), + to_date=add_months(date, -12), carry_forward=0, ) leave_allocation.submit() + # carry forward leave allocation leave_allocation = create_leave_allocation( leave_type="_Test_CF_leave_expiry", employee=employee.name, employee_name=employee.employee_name, - from_date=add_days(nowdate(), -84), - to_date=add_days(nowdate(), 100), + from_date=add_days(date, -84), + to_date=add_days(date, 100), carry_forward=1, ) leave_allocation.submit()