Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

revert: "fix(Leave Policy Assignment): calculation of months passed since from_date" #2290

Merged
merged 1 commit into from
Oct 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -182,11 +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 @@ -290,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 @@ -308,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,46 +117,6 @@ 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()

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

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)

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_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
)
self.assertEqual(new_leaves_allocated, 20)

def test_pro_rated_leave_allocation_for_custom_date_range(self):
leave_type = frappe.get_doc(
{
Expand Down
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, force=True)
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
Loading