diff --git a/hrms/hr/doctype/leave_policy_assignment/leave_policy_assignment.py b/hrms/hr/doctype/leave_policy_assignment/leave_policy_assignment.py index d2017a4c29..9fab7276b6 100644 --- a/hrms/hr/doctype/leave_policy_assignment/leave_policy_assignment.py +++ b/hrms/hr/doctype/leave_policy_assignment/leave_policy_assignment.py @@ -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 @@ -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) @@ -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] diff --git a/hrms/hr/doctype/leave_policy_assignment/test_leave_policy_assignment.py b/hrms/hr/doctype/leave_policy_assignment/test_leave_policy_assignment.py index f57a49df89..7fd0cf62db 100644 --- a/hrms/hr/doctype/leave_policy_assignment/test_leave_policy_assignment.py +++ b/hrms/hr/doctype/leave_policy_assignment/test_leave_policy_assignment.py @@ -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"] @@ -24,7 +21,6 @@ def setUp(self): "Leave Application", "Leave Allocation", "Leave Policy Assignment", - "Leave Policy", "Leave Ledger Entry", ]: frappe.db.delete(doctype) @@ -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) diff --git a/hrms/hr/doctype/leave_type/test_leave_type.py b/hrms/hr/doctype/leave_type/test_leave_type.py index 1dfdea0a33..7700013ca1 100644 --- a/hrms/hr/doctype/leave_type/test_leave_type.py +++ b/hrms/hr/doctype/leave_type/test_leave_type.py @@ -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,