fix: earned leaves not allocated if assignment is created on month-end

(cherry picked from commit 25c7f850b1)

# Conflicts:
#	erpnext/hr/doctype/leave_policy_assignment/leave_policy_assignment.py
This commit is contained in:
Rucha Mahabal
2022-02-05 16:05:46 +05:30
committed by mergify-bot
parent 48a9d985e8
commit 5363b1922a
2 changed files with 41 additions and 4 deletions

View File

@@ -8,8 +8,12 @@ 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
<<<<<<< HEAD
from frappe.utils import date_diff, flt, formatdate, get_datetime, getdate from frappe.utils import date_diff, flt, formatdate, get_datetime, getdate
from six import string_types from six import string_types
=======
from frappe.utils import date_diff, flt, formatdate, get_datetime, get_last_day, getdate
>>>>>>> 25c7f850b1 (fix: earned leaves not allocated if assignment is created on month-end)
class LeavePolicyAssignment(Document): class LeavePolicyAssignment(Document):
@@ -109,8 +113,8 @@ 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().month current_month = get_datetime(frappe.flags.current_date).month or get_datetime().month
current_year = get_datetime().year current_year = get_datetime(frappe.flags.current_date).year or get_datetime().year
from_date = frappe.db.get_value("Leave Period", self.leave_period, "from_date") from_date = frappe.db.get_value("Leave Period", self.leave_period, "from_date")
if getdate(date_of_joining) > getdate(from_date): if getdate(date_of_joining) > getdate(from_date):
@@ -120,10 +124,14 @@ class LeavePolicyAssignment(Document):
from_date_year = get_datetime(from_date).year from_date_year = get_datetime(from_date).year
months_passed = 0 months_passed = 0
if current_year == from_date_year and current_month > from_date_month: if current_year == from_date_year and current_month > from_date_month:
months_passed = current_month - from_date_month months_passed = current_month - from_date_month
months_passed = add_current_month_if_applicable(months_passed)
elif current_year > from_date_year: elif current_year > from_date_year:
months_passed = (12 - from_date_month) + current_month months_passed = (12 - from_date_month) + current_month
months_passed = add_current_month_if_applicable(months_passed)
if months_passed > 0: if months_passed > 0:
monthly_earned_leave = get_monthly_earned_leave(new_leaves_allocated, monthly_earned_leave = get_monthly_earned_leave(new_leaves_allocated,
@@ -135,6 +143,17 @@ class LeavePolicyAssignment(Document):
return new_leaves_allocated return new_leaves_allocated
def add_current_month_if_applicable(months_passed):
date = getdate(frappe.flags.current_date) or getdate()
last_day_of_month = get_last_day(date)
# if its the last day of the month, then that month should also be considered
if last_day_of_month == date:
months_passed += 1
return months_passed
@frappe.whitelist() @frappe.whitelist()
def create_assignment_for_multiple_employees(employees, data): def create_assignment_for_multiple_employees(employees, data):

View File

@@ -393,9 +393,12 @@ def update_previous_leave_allocation(allocation, annual_allocation, e_leave_type
new_allocation = e_leave_type.max_leaves_allowed new_allocation = e_leave_type.max_leaves_allowed
if new_allocation != allocation.total_leaves_allocated: if new_allocation != allocation.total_leaves_allocated:
allocation.db_set("total_leaves_allocated", new_allocation, update_modified=False)
today_date = today() today_date = today()
create_additional_leave_ledger_entry(allocation, earned_leaves, today_date)
if not is_earned_leave_already_allocated(allocation, annual_allocation):
allocation.db_set("total_leaves_allocated", new_allocation, update_modified=False)
create_additional_leave_ledger_entry(allocation, earned_leaves, today_date)
def get_monthly_earned_leave(annual_leaves, frequency, rounding): def get_monthly_earned_leave(annual_leaves, frequency, rounding):
earned_leaves = 0.0 earned_leaves = 0.0
@@ -413,6 +416,21 @@ def get_monthly_earned_leave(annual_leaves, frequency, rounding):
return earned_leaves return earned_leaves
def is_earned_leave_already_allocated(allocation, annual_allocation):
from erpnext.hr.doctype.leave_policy_assignment.leave_policy_assignment import get_leave_type_details
leave_type_details = get_leave_type_details()
date_of_joining = frappe.db.get_value("Employee", allocation.employee, "date_of_joining")
assignment = frappe.get_doc("Leave Policy Assignment", allocation.leave_policy_assignment)
leaves_for_passed_months = assignment.get_leaves_for_passed_months(allocation.leave_type,
annual_allocation, leave_type_details, date_of_joining)
if allocation.total_leaves_allocated >= leaves_for_passed_months:
return True
return False
def get_leave_allocations(date, leave_type): def get_leave_allocations(date, leave_type):
return frappe.db.sql("""select name, employee, from_date, to_date, leave_policy_assignment, leave_policy return frappe.db.sql("""select name, employee, from_date, to_date, leave_policy_assignment, leave_policy
from `tabLeave Allocation` from `tabLeave Allocation`