Skip to content

Commit

Permalink
Merge pull request #2299 from frappe/mergify/bp/version-15-hotfix/pr-…
Browse files Browse the repository at this point in the history
…2291

fix(Leave Policy Assignment): calculation of months passed (backport #2291)
  • Loading branch information
krantheman authored Oct 17, 2024
2 parents 5f3220a + 61a8f4d commit 211cc2e
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 14 deletions.
31 changes: 20 additions & 11 deletions hrms/hr/doctype/leave_policy_assignment/leave_policy_assignment.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,11 @@ def _get_months_passed(current_date, from_date, consider_current_month):
months_passed += 1

elif current_date.year > from_date.year:
months_passed = (12 - from_date.month) + current_date.month
months_passed = (
(12 - from_date.month)
+ (current_date.year - from_date.year - 1) * 12
+ current_date.month
)
if consider_current_month:
months_passed += 1

Expand Down Expand Up @@ -286,16 +290,7 @@ def create_assignment_for_multiple_employees(employees, data):
failed = []

for employee in employees:
assignment = frappe.new_doc("Leave Policy Assignment")
assignment.employee = employee
assignment.assignment_based_on = data.assignment_based_on or None
assignment.leave_policy = data.leave_policy
assignment.effective_from = getdate(data.effective_from) or None
assignment.effective_to = getdate(data.effective_to) or None
assignment.leave_period = data.leave_period or None
assignment.carry_forward = data.carry_forward
assignment.save()

assignment = create_assignment(employee, data)
savepoint = "before_assignment_submission"
try:
frappe.db.savepoint(savepoint)
Expand All @@ -313,6 +308,20 @@ def create_assignment_for_multiple_employees(employees, data):
return docs_name


@frappe.whitelist()
def create_assignment(employee, data):
assignment = frappe.new_doc("Leave Policy Assignment")
assignment.employee = employee
assignment.assignment_based_on = data.assignment_based_on or None
assignment.leave_policy = data.leave_policy
assignment.effective_from = getdate(data.effective_from) or None
assignment.effective_to = getdate(data.effective_to) or None
assignment.leave_period = data.leave_period or None
assignment.carry_forward = data.carry_forward
assignment.save()
return assignment


def show_assignment_submission_status(failed):
frappe.clear_messages()
assignment_list = [get_link_to_form("Leave Policy Assignment", entry) for entry in failed]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,13 @@
from frappe.utils import add_months, get_first_day, get_year_ending, getdate

from hrms.hr.doctype.leave_application.test_leave_application import get_employee, get_leave_period
from hrms.hr.doctype.leave_period.test_leave_period import create_leave_period
from hrms.hr.doctype.leave_policy.test_leave_policy import create_leave_policy
from hrms.hr.doctype.leave_policy_assignment.leave_policy_assignment import (
create_assignment,
create_assignment_for_multiple_employees,
)
from hrms.hr.doctype.leave_type.test_leave_type import create_leave_type

test_dependencies = ["Employee"]

Expand All @@ -20,6 +23,7 @@ def setUp(self):
"Leave Period",
"Leave Application",
"Leave Allocation",
"Leave Policy",
"Leave Policy Assignment",
"Leave Ledger Entry",
]:
Expand Down Expand Up @@ -117,6 +121,49 @@ def test_pro_rated_leave_allocation(self):
# pro-rated leave allocation for 9 months
self.assertEqual(allocation, 9)

# tests no of leaves for passed months if assignment is based on Leave Period / Joining Date
def test_get_leaves_for_passed_months(self):
first_day = get_first_day(getdate())
annual_allocation = 10
leave_type = create_leave_type(
leave_type_name="_Test Earned Leave", is_earned_leave=True, allocate_on_day="First Day"
)
leave_policy = create_leave_policy(leave_type=leave_type, annual_allocation=annual_allocation)
leave_policy.submit()

data = {
"assignment_based_on": "Joining Date",
"leave_policy": leave_policy.name,
}

self.employee.date_of_joining = add_months(first_day, -5)
self.employee.save()
assignment = create_assignment(self.employee.name, frappe._dict(data))
new_leaves_allocated = assignment.get_leaves_for_passed_months(
annual_allocation, leave_type, self.employee.date_of_joining
)
self.assertEqual(new_leaves_allocated, 5)

self.employee.date_of_joining = add_months(first_day, -35)
self.employee.save()
assignment = create_assignment(self.employee.name, frappe._dict(data))
new_leaves_allocated = assignment.get_leaves_for_passed_months(
annual_allocation, leave_type, self.employee.date_of_joining
)
self.assertEqual(new_leaves_allocated, 30)

leave_period = create_leave_period(add_months(first_day, -23), first_day)
data = {
"assignment_based_on": "Leave Period",
"leave_policy": leave_policy.name,
"leave_period": leave_period.name,
}
assignment = create_assignment(self.employee.name, frappe._dict(data))
new_leaves_allocated = assignment.get_leaves_for_passed_months(
annual_allocation, leave_type, self.employee.date_of_joining
)
self.assertEqual(new_leaves_allocated, 20)

def test_pro_rated_leave_allocation_for_custom_date_range(self):
leave_type = frappe.get_doc(
{
Expand Down
8 changes: 5 additions & 3 deletions hrms/hr/doctype/leave_type/test_leave_type.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,14 @@

def create_leave_type(**args):
args = frappe._dict(args)
if frappe.db.exists("Leave Type", args.leave_type_name):
frappe.delete_doc("Leave Type", args.leave_type_name, force=True)
leave_type_name = args.leave_type_name or "_Test Leave Type"
if frappe.db.exists("Leave Type", leave_type_name):
frappe.delete_doc("Leave Type", leave_type_name, force=True)

leave_type = frappe.get_doc(
{
"doctype": "Leave Type",
"leave_type_name": args.leave_type_name or "_Test Leave Type",
"leave_type_name": leave_type_name,
"include_holiday": args.include_holidays or 1,
"allow_encashment": args.allow_encashment or 0,
"is_earned_leave": args.is_earned_leave or 0,
Expand All @@ -26,6 +27,7 @@ def create_leave_type(**args):
"earning_component": "Leave Encashment",
"max_leaves_allowed": args.max_leaves_allowed,
"maximum_carry_forwarded_leaves": args.maximum_carry_forwarded_leaves,
"allocate_on_day": args.allocate_on_day or "Last Day",
}
)

Expand Down

0 comments on commit 211cc2e

Please sign in to comment.