Skip to content

Commit

Permalink
Revert "fix(Leave Policy Assignment): calculation of months passed si…
Browse files Browse the repository at this point in the history
…nce from_date"
  • Loading branch information
krantheman authored and iamejaaz committed Oct 16, 2024
1 parent 5e8fbc3 commit f3c4423
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 60 deletions.
29 changes: 11 additions & 18 deletions hrms/hr/doctype/leave_policy_assignment/leave_policy_assignment.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,9 +182,7 @@ 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.year - from_date.year - 1) * 12 + current_date.month
)
months_passed = (12 - from_date.month) + current_date.month
if consider_current_month:
months_passed += 1

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

for employee in employees:
assignment = 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()

savepoint = "before_assignment_submission"
try:
frappe.db.savepoint(savepoint)
Expand All @@ -306,20 +313,6 @@ 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,13 +6,10 @@
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 @@ -24,7 +21,6 @@ def setUp(self):
"Leave Application",
"Leave Allocation",
"Leave Policy Assignment",
"Leave Policy",
"Leave Ledger Entry",
]:
frappe.db.delete(doctype)
Expand Down Expand Up @@ -121,45 +117,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):
annual_allocation = 10
leave_type = create_leave_type(leave_type_name="_Test Earned Leave", is_earned_leave=True)
leave_policy = create_leave_policy(leave_type=leave_type, annual_allocation=annual_allocation)
leave_policy.submit()
def test_pro_rated_leave_allocation_for_custom_date_range(self):
leave_type = frappe.get_doc(
{
"doctype": "Leave Type",
"leave_type_name": "_Test Leave Type_",
"include_holiday": 1,
"is_earned_leave": 1,
"allocate_on_day": "First Day",
}
).submit()

data = {
"assignment_based_on": "Joining Date",
"leave_policy": leave_policy.name,
}
leave_policy = frappe.get_doc(
{
"doctype": "Leave Policy",
"title": "Test Leave Policy",
"leave_policy_details": [
{
"leave_type": leave_type.name,
"annual_allocation": 12,
}
],
}
).submit()

self.employee.date_of_joining = add_months(getdate(), -6)
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)
today_date = getdate()

self.employee.date_of_joining = add_months(getdate(), -36)
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_policy_assignment = frappe.new_doc("Leave Policy Assignment")
leave_policy_assignment.employee = self.employee
leave_policy_assignment.leave_policy = leave_policy.name
leave_policy_assignment.effective_from = getdate(get_first_day(today_date))
leave_policy_assignment.effective_to = getdate(get_year_ending(today_date))
leave_policy_assignment.submit()

leave_period = create_leave_period(add_months(getdate(), -24), getdate())
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
new_leaves_allocated = frappe.db.get_value(
"Leave Allocation",
{
"employee": leave_policy_assignment.employee,
"leave_policy_assignment": leave_policy_assignment.name,
},
"new_leaves_allocated",
)
self.assertEqual(new_leaves_allocated, 20)

self.assertGreater(new_leaves_allocated, 0)

def tearDown(self):
frappe.db.set_value("Employee", self.employee.name, "date_of_joining", self.original_doj)
7 changes: 3 additions & 4 deletions hrms/hr/doctype/leave_type/test_leave_type.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,13 @@

def create_leave_type(**args):
args = frappe._dict(args)
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)
if frappe.db.exists("Leave Type", args.leave_type_name):
frappe.delete_doc("Leave Type", args.leave_type_name, force=True)

leave_type = frappe.get_doc(
{
"doctype": "Leave Type",
"leave_type_name": leave_type_name,
"leave_type_name": args.leave_type_name or "_Test Leave Type",
"include_holiday": args.include_holidays or 1,
"allow_encashment": args.allow_encashment or 0,
"is_earned_leave": args.is_earned_leave or 0,
Expand Down

0 comments on commit f3c4423

Please sign in to comment.