Skip to content

Commit

Permalink
fix: filter out old allocation's cf leaves while fetching leave detai…
Browse files Browse the repository at this point in the history
…ls (frappe#34723)
  • Loading branch information
ruchamahabal authored Apr 3, 2023
1 parent 313aecf commit 50de045
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 5 deletions.
3 changes: 3 additions & 0 deletions erpnext/hr/doctype/leave_application/leave_application.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
)
)
)
Expand Down
36 changes: 31 additions & 5 deletions erpnext/hr/doctype/leave_application/test_leave_application.py
Original file line number Diff line number Diff line change
Expand Up @@ -1170,25 +1170,51 @@ def test_get_leave_allocation_records(self):
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()
Expand Down

0 comments on commit 50de045

Please sign in to comment.