From 84ec1855acc867b8ab263379b0665899609e559e Mon Sep 17 00:00:00 2001 From: Akash Tom Date: Tue, 15 Oct 2024 15:26:47 +0530 Subject: [PATCH] Revert "fix(Leave Policy Assignment): calculation of months passed since from_date" --- .../leave_policy_assignment.py | 31 +++++-------- .../test_leave_policy_assignment.py | 44 ------------------- hrms/hr/doctype/leave_type/test_leave_type.py | 7 ++- 3 files changed, 14 insertions(+), 68 deletions(-) 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 dc151e4cdb..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,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 @@ -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) @@ -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] 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 fee48e23b8..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,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( { diff --git a/hrms/hr/doctype/leave_type/test_leave_type.py b/hrms/hr/doctype/leave_type/test_leave_type.py index 643e4fb648..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, 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,