diff --git a/erpnext/hr/doctype/leave_application/test_leave_application.py b/erpnext/hr/doctype/leave_application/test_leave_application.py index 8d1adaca62b..494255f4a1e 100644 --- a/erpnext/hr/doctype/leave_application/test_leave_application.py +++ b/erpnext/hr/doctype/leave_application/test_leave_application.py @@ -713,25 +713,31 @@ class TestLeaveApplication(unittest.TestCase): self.assertEqual(details.leave_balance, 30) def test_earned_leaves_creation(self): - from erpnext.hr.utils import allocate_earned_leaves + from erpnext.hr.doctype.leave_policy_assignment.test_leave_policy_assignment import ( + allocate_earned_leaves_for_months, + ) - leave_period = get_leave_period() + year_start = get_year_start(getdate()) + year_end = get_year_ending(getdate()) + frappe.flags.current_date = year_start + + leave_period = get_leave_period(year_start, year_end) employee = get_employee() leave_type = "Test Earned Leave Type" + make_policy_assignment(employee, leave_type, leave_period) - for i in range(0, 14): - allocate_earned_leaves() - - self.assertEqual(get_leave_balance_on(employee.name, leave_type, nowdate()), 6) + # leaves for 6 months = 3, but max leaves restricts allocation to 2 + frappe.db.set_value("Leave Type", leave_type, "max_leaves_allowed", 2) + allocate_earned_leaves_for_months(6) + self.assertEqual(get_leave_balance_on(employee.name, leave_type, frappe.flags.current_date), 2) # validate earned leaves creation without maximum leaves frappe.db.set_value("Leave Type", leave_type, "max_leaves_allowed", 0) + allocate_earned_leaves_for_months(5) + self.assertEqual(get_leave_balance_on(employee.name, leave_type, frappe.flags.current_date), 4.5) - for i in range(0, 6): - allocate_earned_leaves() - - self.assertEqual(get_leave_balance_on(employee.name, leave_type, nowdate()), 9) + frappe.flags.current_date = None # test to not consider current leave in leave balance while submitting def test_current_leave_on_submit(self): @@ -1254,7 +1260,7 @@ def set_leave_approver(): dept_doc.save(ignore_permissions=True) -def get_leave_period(): +def get_leave_period(from_date=None, to_date=None): leave_period_name = frappe.db.exists({"doctype": "Leave Period", "company": "_Test Company"}) if leave_period_name: return frappe.get_doc("Leave Period", leave_period_name[0][0]) @@ -1263,8 +1269,8 @@ def get_leave_period(): dict( name="Test Leave Period", doctype="Leave Period", - from_date=add_months(nowdate(), -6), - to_date=add_months(nowdate(), 6), + from_date=from_date or add_months(nowdate(), -6), + to_date=to_date or add_months(nowdate(), 6), company="_Test Company", is_active=1, ) diff --git a/erpnext/hr/doctype/leave_policy_assignment/leave_policy_assignment.py b/erpnext/hr/doctype/leave_policy_assignment/leave_policy_assignment.py index 79295aa1bd4..8269d98a5a7 100644 --- a/erpnext/hr/doctype/leave_policy_assignment/leave_policy_assignment.py +++ b/erpnext/hr/doctype/leave_policy_assignment/leave_policy_assignment.py @@ -100,7 +100,7 @@ class LeavePolicyAssignment(Document): return leave_allocations def create_leave_allocation( - self, leave_type, new_leaves_allocated, leave_type_details, date_of_joining + self, leave_type, annual_allocation, leave_type_details, date_of_joining ): # Creates leave allocation for the given employee in the provided leave period carry_forward = self.carry_forward @@ -108,7 +108,7 @@ class LeavePolicyAssignment(Document): carry_forward = 0 new_leaves_allocated = self.get_new_leaves( - leave_type, new_leaves_allocated, leave_type_details, date_of_joining + leave_type, annual_allocation, leave_type_details, date_of_joining ) allocation = frappe.get_doc( @@ -129,7 +129,7 @@ class LeavePolicyAssignment(Document): allocation.submit() return allocation.name, new_leaves_allocated - def get_new_leaves(self, leave_type, new_leaves_allocated, leave_type_details, date_of_joining): + def get_new_leaves(self, leave_type, annual_allocation, leave_type_details, date_of_joining): from frappe.model.meta import get_field_precision precision = get_field_precision( @@ -146,20 +146,27 @@ class LeavePolicyAssignment(Document): else: # get leaves for past months if assignment is based on Leave Period / Joining Date new_leaves_allocated = self.get_leaves_for_passed_months( - leave_type, new_leaves_allocated, leave_type_details, date_of_joining + leave_type, annual_allocation, leave_type_details, date_of_joining ) # Calculate leaves at pro-rata basis for employees joining after the beginning of the given leave period - elif getdate(date_of_joining) > getdate(self.effective_from): - remaining_period = (date_diff(self.effective_to, date_of_joining) + 1) / ( - date_diff(self.effective_to, self.effective_from) + 1 - ) - new_leaves_allocated = ceil(new_leaves_allocated * remaining_period) + else: + if getdate(date_of_joining) > getdate(self.effective_from): + remaining_period = (date_diff(self.effective_to, date_of_joining) + 1) / ( + date_diff(self.effective_to, self.effective_from) + 1 + ) + new_leaves_allocated = ceil(annual_allocation * remaining_period) + else: + new_leaves_allocated = annual_allocation + + # leave allocation should not exceed annual allocation as per policy assignment + if new_leaves_allocated > annual_allocation: + new_leaves_allocated = annual_allocation return flt(new_leaves_allocated, precision) def get_leaves_for_passed_months( - self, leave_type, new_leaves_allocated, leave_type_details, date_of_joining + self, leave_type, annual_allocation, leave_type_details, date_of_joining ): from erpnext.hr.utils import get_monthly_earned_leave @@ -184,7 +191,7 @@ class LeavePolicyAssignment(Document): if months_passed > 0: monthly_earned_leave = get_monthly_earned_leave( - new_leaves_allocated, + annual_allocation, leave_type_details.get(leave_type).earned_leave_frequency, leave_type_details.get(leave_type).rounding, ) diff --git a/erpnext/hr/doctype/leave_policy_assignment/test_leave_policy_assignment.py b/erpnext/hr/doctype/leave_policy_assignment/test_leave_policy_assignment.py index ee61b2bfc91..fe6c156a50a 100644 --- a/erpnext/hr/doctype/leave_policy_assignment/test_leave_policy_assignment.py +++ b/erpnext/hr/doctype/leave_policy_assignment/test_leave_policy_assignment.py @@ -5,8 +5,10 @@ import unittest import frappe from frappe.tests.utils import FrappeTestCase -from frappe.utils import add_days, add_months, get_first_day, get_last_day, getdate +from frappe.utils import add_days, add_months, get_first_day, get_last_day, get_year_start, getdate +from erpnext.hr.doctype.leave_allocation.test_leave_allocation import create_leave_allocation +from erpnext.hr.doctype.leave_application.leave_application import get_leave_balance_on from erpnext.hr.doctype.leave_application.test_leave_application import ( get_employee, get_leave_period, @@ -15,6 +17,7 @@ from erpnext.hr.doctype.leave_policy.test_leave_policy import create_leave_polic from erpnext.hr.doctype.leave_policy_assignment.leave_policy_assignment import ( create_assignment_for_multiple_employees, ) +from erpnext.hr.utils import allocate_earned_leaves test_dependencies = ["Employee"] @@ -34,6 +37,8 @@ class TestLeavePolicyAssignment(FrappeTestCase): self.original_doj = employee.date_of_joining self.employee = employee + self.leave_type = "Test Earned Leave" + def test_grant_leaves(self): leave_period = get_leave_period() # allocation = 10 @@ -326,6 +331,90 @@ class TestLeavePolicyAssignment(FrappeTestCase): self.assertEqual(effective_from, self.employee.date_of_joining) self.assertEqual(leaves_allocated, 3) + def test_overallocation(self): + """Tests if earned leave allocation does not exceed annual allocation""" + frappe.flags.current_date = get_year_start(getdate()) + make_policy_assignment( + self.employee, + annual_allocation=22, + allocate_on_day="First Day", + start_date=frappe.flags.current_date, + ) + + # leaves for 12 months = 22 + # With rounding, 22 leaves would be allocated in 11 months only + frappe.db.set_value("Leave Type", self.leave_type, "rounding", 1.0) + allocate_earned_leaves_for_months(11) + self.assertEqual( + get_leave_balance_on(self.employee.name, self.leave_type, frappe.flags.current_date), 22 + ) + + # should not allocate more leaves than annual allocation + allocate_earned_leaves_for_months(1) + self.assertEqual( + get_leave_balance_on(self.employee.name, self.leave_type, frappe.flags.current_date), 22 + ) + + 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)) + + # joining date set to 1Y ago + self.employee.date_of_joining = start_date + self.employee.save() + + # create backdated assignment for last year + frappe.flags.current_date = get_first_day(getdate()) + + leave_policy_assignments = make_policy_assignment( + self.employee, start_date=start_date, allocate_on_day="Date of Joining" + ) + + # 13 months have passed but annual allocation = 12 + # check annual allocation is not exceeded + leaves_allocated = get_allocated_leaves(leave_policy_assignments[0]) + self.assertEqual(leaves_allocated, 12) + + def test_overallocation_with_carry_forwarding(self): + """Tests earned leave allocation with cf leaves does not exceed annual allocation""" + year_start = get_year_start(getdate()) + + # initial leave allocation = 5 + leave_allocation = create_leave_allocation( + employee=self.employee.name, + employee_name=self.employee.employee_name, + leave_type=self.leave_type, + from_date=get_first_day(add_months(year_start, -1)), + to_date=get_last_day(add_months(year_start, -1)), + new_leaves_allocated=5, + carry_forward=0, + ) + leave_allocation.submit() + + frappe.flags.current_date = year_start + # carry forwarded leaves = 5 + make_policy_assignment( + self.employee, + annual_allocation=22, + allocate_on_day="First Day", + start_date=year_start, + carry_forward=True, + ) + + frappe.db.set_value("Leave Type", self.leave_type, "rounding", 1.0) + allocate_earned_leaves_for_months(11) + + # 5 carry forwarded leaves + 22 EL allocated = 27 leaves + self.assertEqual( + get_leave_balance_on(self.employee.name, self.leave_type, frappe.flags.current_date), 27 + ) + + # should not allocate more leaves than annual allocation (22 excluding 5 cf leaves) + allocate_earned_leaves_for_months(1) + self.assertEqual( + get_leave_balance_on(self.employee.name, self.leave_type, frappe.flags.current_date), 27 + ) + def tearDown(self): frappe.db.set_value("Employee", self.employee.name, "date_of_joining", self.original_doj) frappe.flags.current_date = None @@ -376,3 +465,51 @@ def setup_leave_period_and_policy(start_date, based_on_doj=False): ).insert() return leave_period, leave_policy + + +def make_policy_assignment( + employee, + allocate_on_day="Last Day", + earned_leave_frequency="Monthly", + start_date=None, + annual_allocation=12, + carry_forward=0, + assignment_based_on="Leave Period", +): + leave_type = create_earned_leave_type("Test Earned Leave", allocate_on_day) + leave_period = create_leave_period("Test Earned Leave Period", start_date=start_date) + leave_policy = frappe.get_doc( + { + "doctype": "Leave Policy", + "title": "Test Earned Leave Policy", + "leave_policy_details": [ + {"leave_type": leave_type.name, "annual_allocation": annual_allocation} + ], + } + ).insert() + + data = { + "assignment_based_on": assignment_based_on, + "leave_policy": leave_policy.name, + "leave_period": leave_period.name, + "carry_forward": carry_forward, + } + + leave_policy_assignments = create_assignment_for_multiple_employees( + [employee.name], frappe._dict(data) + ) + return leave_policy_assignments + + +def get_allocated_leaves(assignment): + return frappe.db.get_value( + "Leave Allocation", + {"leave_policy_assignment": assignment}, + "total_leaves_allocated", + ) + + +def allocate_earned_leaves_for_months(months): + for i in range(0, months): + frappe.flags.current_date = add_months(frappe.flags.current_date, 1) + allocate_earned_leaves() diff --git a/erpnext/hr/utils.py b/erpnext/hr/utils.py index 181e3b12b98..a2c45c0c0d3 100644 --- a/erpnext/hr/utils.py +++ b/erpnext/hr/utils.py @@ -459,7 +459,7 @@ def generate_leave_encashment(): def allocate_earned_leaves(): """Allocate earned leaves to Employees""" e_leave_types = get_earned_leaves() - today = getdate() + today = frappe.flags.current_date or getdate() for e_leave_type in e_leave_types: @@ -496,18 +496,28 @@ def allocate_earned_leaves(): def update_previous_leave_allocation(allocation, annual_allocation, e_leave_type): + allocation = frappe.get_doc("Leave Allocation", allocation.name) + annual_allocation = flt(annual_allocation, allocation.precision("total_leaves_allocated")) + earned_leaves = get_monthly_earned_leave( annual_allocation, e_leave_type.earned_leave_frequency, e_leave_type.rounding ) - allocation = frappe.get_doc("Leave Allocation", allocation.name) new_allocation = flt(allocation.total_leaves_allocated) + flt(earned_leaves) + new_allocation_without_cf = flt( + flt(allocation.get_existing_leave_count()) + flt(earned_leaves), + allocation.precision("total_leaves_allocated"), + ) if new_allocation > e_leave_type.max_leaves_allowed and e_leave_type.max_leaves_allowed > 0: new_allocation = e_leave_type.max_leaves_allowed - if new_allocation != allocation.total_leaves_allocated: - today_date = today() + if ( + new_allocation != allocation.total_leaves_allocated + # annual allocation as per policy should not be exceeded + and new_allocation_without_cf <= annual_allocation + ): + today_date = frappe.flags.current_date or getdate() allocation.db_set("total_leaves_allocated", new_allocation, update_modified=False) create_additional_leave_ledger_entry(allocation, earned_leaves, today_date)