From c7be9ef5d24a3e03efde64a45302baca76e8107f Mon Sep 17 00:00:00 2001 From: Rucha Mahabal Date: Fri, 11 Feb 2022 13:52:46 +0530 Subject: [PATCH] fix: consider leaves for past months if assignment is based on joining date too --- .../leave_policy_assignment.py | 35 +++++++++---------- 1 file changed, 17 insertions(+), 18 deletions(-) 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 6168db8502..fa73bdaccf 100644 --- a/erpnext/hr/doctype/leave_policy_assignment/leave_policy_assignment.py +++ b/erpnext/hr/doctype/leave_policy_assignment/leave_policy_assignment.py @@ -8,7 +8,7 @@ from math import ceil import frappe from frappe import _, bold 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): @@ -94,10 +94,12 @@ class LeavePolicyAssignment(Document): new_leaves_allocated = 0 elif leave_type_details.get(leave_type).is_earned_leave == 1: - if self.assignment_based_on == "Leave Period": - new_leaves_allocated = self.get_leaves_for_passed_months(leave_type, new_leaves_allocated, leave_type_details, date_of_joining) - else: + 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(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 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)) @@ -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): from erpnext.hr.utils import get_monthly_earned_leave - current_month = get_datetime(frappe.flags.current_date).month or get_datetime().month - current_year = get_datetime(frappe.flags.current_date).year or get_datetime().year + current_date = frappe.flags.current_date or getdate() + 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") - if getdate(date_of_joining) > getdate(from_date): - from_date = date_of_joining - - from_date_month = get_datetime(from_date).month - from_date_year = get_datetime(from_date).year + from_date = getdate(self.effective_from) + if getdate(date_of_joining) > from_date: + from_date = getdate(date_of_joining) months_passed = 0 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: - months_passed = current_month - from_date_month + if current_date.year == from_date.year and current_date.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) - elif current_year > from_date_year: - months_passed = (12 - from_date_month) + current_month + elif current_date.year > from_date.year: + months_passed = (12 - from_date.month) + current_date.month months_passed = add_current_month_if_applicable(months_passed, date_of_joining, based_on_doj) 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() if based_on_doj: - # if leave type allocation is based on DOJ, - # and the date of assignment creation is same as DOJ, + # if leave type allocation is based on DOJ, and the date of assignment creation is same as DOJ, # then the month should be considered if date == date_of_joining: months_passed += 1