fix: consider leaves for past months if assignment is based on joining date too
This commit is contained in:
parent
89fa0bb73f
commit
c7be9ef5d2
@ -8,7 +8,7 @@ from math import ceil
|
|||||||
import frappe
|
import frappe
|
||||||
from frappe import _, bold
|
from frappe import _, bold
|
||||||
from frappe.model.document import Document
|
from frappe.model.document import Document
|
||||||
from frappe.utils import date_diff, flt, formatdate, get_datetime, get_last_day, getdate
|
from frappe.utils import date_diff, flt, formatdate, get_last_day, getdate
|
||||||
|
|
||||||
|
|
||||||
class LeavePolicyAssignment(Document):
|
class LeavePolicyAssignment(Document):
|
||||||
@ -94,10 +94,12 @@ class LeavePolicyAssignment(Document):
|
|||||||
new_leaves_allocated = 0
|
new_leaves_allocated = 0
|
||||||
|
|
||||||
elif leave_type_details.get(leave_type).is_earned_leave == 1:
|
elif leave_type_details.get(leave_type).is_earned_leave == 1:
|
||||||
if self.assignment_based_on == "Leave Period":
|
if not self.assignment_based_on:
|
||||||
new_leaves_allocated = self.get_leaves_for_passed_months(leave_type, new_leaves_allocated, leave_type_details, date_of_joining)
|
|
||||||
else:
|
|
||||||
new_leaves_allocated = 0
|
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(leave_type, new_leaves_allocated, leave_type_details, date_of_joining)
|
||||||
|
|
||||||
# Calculate leaves at pro-rata basis for employees joining after the beginning of the given leave period
|
# 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):
|
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))
|
remaining_period = ((date_diff(self.effective_to, date_of_joining) + 1) / (date_diff(self.effective_to, self.effective_from) + 1))
|
||||||
@ -108,25 +110,23 @@ class LeavePolicyAssignment(Document):
|
|||||||
def get_leaves_for_passed_months(self, leave_type, new_leaves_allocated, leave_type_details, date_of_joining):
|
def get_leaves_for_passed_months(self, leave_type, new_leaves_allocated, leave_type_details, date_of_joining):
|
||||||
from erpnext.hr.utils import get_monthly_earned_leave
|
from erpnext.hr.utils import get_monthly_earned_leave
|
||||||
|
|
||||||
current_month = get_datetime(frappe.flags.current_date).month or get_datetime().month
|
current_date = frappe.flags.current_date or getdate()
|
||||||
current_year = get_datetime(frappe.flags.current_date).year or get_datetime().year
|
if current_date > getdate(self.effective_to):
|
||||||
|
current_date = getdate(self.effective_to)
|
||||||
|
|
||||||
from_date = frappe.db.get_value("Leave Period", self.leave_period, "from_date")
|
from_date = getdate(self.effective_from)
|
||||||
if getdate(date_of_joining) > getdate(from_date):
|
if getdate(date_of_joining) > from_date:
|
||||||
from_date = date_of_joining
|
from_date = getdate(date_of_joining)
|
||||||
|
|
||||||
from_date_month = get_datetime(from_date).month
|
|
||||||
from_date_year = get_datetime(from_date).year
|
|
||||||
|
|
||||||
months_passed = 0
|
months_passed = 0
|
||||||
based_on_doj = leave_type_details.get(leave_type).based_on_date_of_joining
|
based_on_doj = leave_type_details.get(leave_type).based_on_date_of_joining
|
||||||
|
|
||||||
if current_year == from_date_year and current_month >= from_date_month:
|
if current_date.year == from_date.year and current_date.month >= from_date.month:
|
||||||
months_passed = current_month - from_date_month
|
months_passed = current_date.month - from_date.month
|
||||||
months_passed = add_current_month_if_applicable(months_passed, date_of_joining, based_on_doj)
|
months_passed = add_current_month_if_applicable(months_passed, date_of_joining, based_on_doj)
|
||||||
|
|
||||||
elif current_year > from_date_year:
|
elif current_date.year > from_date.year:
|
||||||
months_passed = (12 - from_date_month) + current_month
|
months_passed = (12 - from_date.month) + current_date.month
|
||||||
months_passed = add_current_month_if_applicable(months_passed, date_of_joining, based_on_doj)
|
months_passed = add_current_month_if_applicable(months_passed, date_of_joining, based_on_doj)
|
||||||
|
|
||||||
if months_passed > 0:
|
if months_passed > 0:
|
||||||
@ -143,8 +143,7 @@ def add_current_month_if_applicable(months_passed, date_of_joining, based_on_doj
|
|||||||
date = getdate(frappe.flags.current_date) or getdate()
|
date = getdate(frappe.flags.current_date) or getdate()
|
||||||
|
|
||||||
if based_on_doj:
|
if based_on_doj:
|
||||||
# if leave type allocation is based on DOJ,
|
# if leave type allocation is based on DOJ, and the date of assignment creation is same as DOJ,
|
||||||
# and the date of assignment creation is same as DOJ,
|
|
||||||
# then the month should be considered
|
# then the month should be considered
|
||||||
if date == date_of_joining:
|
if date == date_of_joining:
|
||||||
months_passed += 1
|
months_passed += 1
|
||||||
|
Loading…
Reference in New Issue
Block a user