fix: avoid over allocation during backdated assignment creation

This commit is contained in:
Rucha Mahabal
2023-12-07 13:04:23 +05:30
parent ee7c9add39
commit fee4eae96c

View File

@@ -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,7 +146,7 @@ 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
@@ -156,10 +156,14 @@ class LeavePolicyAssignment(Document):
)
new_leaves_allocated = ceil(new_leaves_allocated * remaining_period)
# 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 +188,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,
)