Skip to content

Commit

Permalink
fix: calculate leaves for months passed even when policy assignment i…
Browse files Browse the repository at this point in the history
…s based on custom range (#1697)

* fix: remove condition( if not self.assignment_based_on:)

* chore: pre-commit run

* fix: test case

* chore: pre_commit_run

* chore: fix test name

---------

Co-authored-by: Rucha Mahabal <[email protected]>
  • Loading branch information
Jeevansyriac and ruchamahabal authored Jun 12, 2024
1 parent 06e1d94 commit 4adf500
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -140,13 +140,9 @@ def get_new_leaves(self, annual_allocation, leave_details, date_of_joining):
new_leaves_allocated = 0

elif leave_details.is_earned_leave:
if not self.assignment_based_on:
new_leaves_allocated = 0
else:
# get leaves for past months if assignment is based on Leave Period / Joining Date
new_leaves_allocated = self.get_leaves_for_passed_months(
annual_allocation, leave_details, date_of_joining
)
new_leaves_allocated = self.get_leaves_for_passed_months(
annual_allocation, leave_details, date_of_joining
)

else:
# calculate pro-rated leaves for other leave types
Expand Down Expand Up @@ -213,7 +209,7 @@ def _calculate_leaves_for_passed_months(consider_current_month):

period_end_date = _get_pro_rata_period_end_date(consider_current_month)

if self.effective_from < date_of_joining <= period_end_date:
if getdate(self.effective_from) <= date_of_joining <= period_end_date:
# if the employee joined within the allocation period in some previous month,
# calculate pro-rated leave for that month
# and normal monthly earned leave for remaining passed months
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

import frappe
from frappe.tests.utils import FrappeTestCase
from frappe.utils import add_months, get_first_day, getdate
from frappe.utils import add_months, get_first_day, get_year_ending, getdate

from hrms.hr.doctype.leave_application.test_leave_application import get_employee, get_leave_period
from hrms.hr.doctype.leave_policy.test_leave_policy import create_leave_policy
Expand Down Expand Up @@ -117,5 +117,49 @@ def test_pro_rated_leave_allocation(self):
# pro-rated leave allocation for 9 months
self.assertEqual(allocation, 9)

def test_pro_rated_leave_allocation_for_custom_date_range(self):
leave_type = frappe.get_doc(
{
"doctype": "Leave Type",
"leave_type_name": "_Test Leave Type_",
"include_holiday": 1,
"is_earned_leave": 1,
"allocate_on_day": "First Day",
}
).submit()

leave_policy = frappe.get_doc(
{
"doctype": "Leave Policy",
"title": "Test Leave Policy",
"leave_policy_details": [
{
"leave_type": leave_type.name,
"annual_allocation": 12,
}
],
}
).submit()

today_date = getdate()

leave_policy_assignment = frappe.new_doc("Leave Policy Assignment")
leave_policy_assignment.employee = self.employee
leave_policy_assignment.leave_policy = leave_policy.name
leave_policy_assignment.effective_from = getdate(get_first_day(today_date))
leave_policy_assignment.effective_to = getdate(get_year_ending(today_date))
leave_policy_assignment.submit()

new_leaves_allocated = frappe.db.get_value(
"Leave Allocation",
{
"employee": leave_policy_assignment.employee,
"leave_policy_assignment": leave_policy_assignment.name,
},
"new_leaves_allocated",
)

self.assertGreater(new_leaves_allocated, 0)

def tearDown(self):
frappe.db.set_value("Employee", self.employee.name, "date_of_joining", self.original_doj)

0 comments on commit 4adf500

Please sign in to comment.