Skip to content

Commit

Permalink
fix: incorrect pro-rated earned leave calculation for passed months
Browse files Browse the repository at this point in the history
(cherry picked from commit db4aca9)
  • Loading branch information
ruchamahabal authored and mergify[bot] committed May 24, 2023
1 parent d00a985 commit cec0960
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 18 deletions.
41 changes: 31 additions & 10 deletions hrms/hr/doctype/leave_policy_assignment/leave_policy_assignment.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,23 +203,44 @@ def _get_pro_rata_period_end_date(consider_current_month):

return period_end_date

def _calculate_leaves_for_passed_months(consider_current_month):
monthly_earned_leave = get_monthly_earned_leave(
date_of_joining,
annual_allocation,
leave_details.earned_leave_frequency,
leave_details.rounding,
pro_rated=False,
)

period_end_date = _get_pro_rata_period_end_date(consider_current_month)

if 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
leaves = get_monthly_earned_leave(
date_of_joining,
annual_allocation,
leave_details.earned_leave_frequency,
leave_details.rounding,
get_first_day(date_of_joining),
get_last_day(date_of_joining),
)

leaves += monthly_earned_leave * (months_passed - 1)
else:
leaves = monthly_earned_leave * months_passed

return leaves

consider_current_month = is_earned_leave_applicable_for_current_month(
date_of_joining, leave_details.allocate_on_day
)
current_date, from_date = _get_current_and_from_date()
months_passed = _get_months_passed(current_date, from_date, consider_current_month)

if months_passed > 0:
period_end_date = _get_pro_rata_period_end_date(consider_current_month)
monthly_earned_leave = get_monthly_earned_leave(
date_of_joining,
annual_allocation,
leave_details.earned_leave_frequency,
leave_details.rounding,
self.effective_from,
period_end_date,
)
new_leaves_allocated = monthly_earned_leave * months_passed
new_leaves_allocated = _calculate_leaves_for_passed_months(consider_current_month)
else:
new_leaves_allocated = 0

Expand Down
22 changes: 14 additions & 8 deletions hrms/hr/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,10 @@ def update_previous_leave_allocation(allocation, annual_allocation, e_leave_type
annual_allocation = flt(annual_allocation, allocation.precision("total_leaves_allocated"))

earned_leaves = get_monthly_earned_leave(
date_of_joining, annual_allocation, e_leave_type.earned_leave_frequency, e_leave_type.rounding
date_of_joining,
annual_allocation,
e_leave_type.earned_leave_frequency,
e_leave_type.rounding,
)

new_allocation = flt(allocation.total_leaves_allocated) + flt(earned_leaves)
Expand Down Expand Up @@ -404,20 +407,23 @@ def get_monthly_earned_leave(
rounding,
period_start_date=None,
period_end_date=None,
pro_rated=True,
):
earned_leaves = 0.0
divide_by_frequency = {"Yearly": 1, "Half-Yearly": 2, "Quarterly": 4, "Monthly": 12}
if annual_leaves:
earned_leaves = flt(annual_leaves) / divide_by_frequency[frequency]

if not (period_start_date or period_end_date):
today_date = frappe.flags.current_date or getdate()
period_end_date = get_last_day(today_date)
period_start_date = get_first_day(today_date)
if pro_rated:
if not (period_start_date or period_end_date):
today_date = frappe.flags.current_date or getdate()
period_end_date = get_last_day(today_date)
period_start_date = get_first_day(today_date)

earned_leaves = calculate_pro_rated_leaves(
earned_leaves, date_of_joining, period_start_date, period_end_date, is_earned_leave=True
)

earned_leaves = calculate_pro_rated_leaves(
earned_leaves, date_of_joining, period_start_date, period_end_date, is_earned_leave=True
)
earned_leaves = round_earned_leaves(earned_leaves, rounding)

return earned_leaves
Expand Down

0 comments on commit cec0960

Please sign in to comment.