From 26247be3b872a28f425ce51af663eeb0e0899799 Mon Sep 17 00:00:00 2001 From: Rucha Mahabal Date: Thu, 6 Jan 2022 11:21:59 +0530 Subject: [PATCH] fix: Earned Leave allocation from Leave Policy Assignment (#29163) * fix: Earned Leave allocation from Leave Policy Assignment * test: Earned Leave Allocation from Leave Policy Assignment --- .../leave_policy_assignment.py | 2 + .../test_leave_policy_assignment.py | 61 +++++++++++++++++-- 2 files changed, 57 insertions(+), 6 deletions(-) diff --git a/erpnext/hr/doctype/leave_policy_assignment/leave_policy_assignment.py b/erpnext/hr/doctype/leave_policy_assignment/leave_policy_assignment.py index 655e3ac53e..355370f3a4 100644 --- a/erpnext/hr/doctype/leave_policy_assignment/leave_policy_assignment.py +++ b/erpnext/hr/doctype/leave_policy_assignment/leave_policy_assignment.py @@ -128,6 +128,8 @@ class LeavePolicyAssignment(Document): monthly_earned_leave = get_monthly_earned_leave(new_leaves_allocated, leave_type_details.get(leave_type).earned_leave_frequency, leave_type_details.get(leave_type).rounding) new_leaves_allocated = monthly_earned_leave * months_passed + else: + new_leaves_allocated = 0 return new_leaves_allocated diff --git a/erpnext/hr/doctype/leave_policy_assignment/test_leave_policy_assignment.py b/erpnext/hr/doctype/leave_policy_assignment/test_leave_policy_assignment.py index b1861ad4d8..8953a51e8b 100644 --- a/erpnext/hr/doctype/leave_policy_assignment/test_leave_policy_assignment.py +++ b/erpnext/hr/doctype/leave_policy_assignment/test_leave_policy_assignment.py @@ -4,6 +4,7 @@ import unittest import frappe +from frappe.utils import add_months, get_first_day, getdate from erpnext.hr.doctype.leave_application.test_leave_application import ( get_employee, @@ -17,9 +18,8 @@ from erpnext.hr.doctype.leave_policy_assignment.leave_policy_assignment import ( test_dependencies = ["Employee"] class TestLeavePolicyAssignment(unittest.TestCase): - def setUp(self): - for doctype in ["Leave Application", "Leave Allocation", "Leave Policy Assignment", "Leave Ledger Entry"]: + for doctype in ["Leave Period", "Leave Application", "Leave Allocation", "Leave Policy Assignment", "Leave Ledger Entry"]: frappe.db.sql("delete from `tab{0}`".format(doctype)) #nosec def test_grant_leaves(self): @@ -54,8 +54,8 @@ class TestLeavePolicyAssignment(unittest.TestCase): self.assertEqual(leave_alloc_doc.new_leaves_allocated, 10) self.assertEqual(leave_alloc_doc.leave_type, "_Test Leave Type") - self.assertEqual(leave_alloc_doc.from_date, leave_period.from_date) - self.assertEqual(leave_alloc_doc.to_date, leave_period.to_date) + self.assertEqual(getdate(leave_alloc_doc.from_date), getdate(leave_period.from_date)) + self.assertEqual(getdate(leave_alloc_doc.to_date), getdate(leave_period.to_date)) self.assertEqual(leave_alloc_doc.leave_policy, leave_policy.name) self.assertEqual(leave_alloc_doc.leave_policy_assignment, leave_policy_assignments[0]) @@ -101,6 +101,55 @@ class TestLeavePolicyAssignment(unittest.TestCase): # User are now allowed to grant leave self.assertEqual(leave_policy_assignment_doc.leaves_allocated, 0) + def test_earned_leave_allocation(self): + leave_period = create_leave_period("Test Earned Leave Period") + employee = get_employee() + leave_type = create_earned_leave_type("Test Earned Leave") + + leave_policy = frappe.get_doc({ + "doctype": "Leave Policy", + "leave_policy_details": [{"leave_type": leave_type.name, "annual_allocation": 6}] + }).insert() + + data = { + "assignment_based_on": "Leave Period", + "leave_policy": leave_policy.name, + "leave_period": leave_period.name + } + leave_policy_assignments = create_assignment_for_multiple_employees([employee.name], frappe._dict(data)) + + # leaves allocated should be 0 since it is an earned leave and allocation happens via scheduler based on set frequency + leaves_allocated = frappe.db.get_value("Leave Allocation", { + "leave_policy_assignment": leave_policy_assignments[0] + }, "total_leaves_allocated") + self.assertEqual(leaves_allocated, 0) + def tearDown(self): - for doctype in ["Leave Application", "Leave Allocation", "Leave Policy Assignment", "Leave Ledger Entry"]: - frappe.db.sql("delete from `tab{0}`".format(doctype)) #nosec + frappe.db.rollback() + + +def create_earned_leave_type(leave_type): + frappe.delete_doc_if_exists("Leave Type", leave_type, force=1) + + return frappe.get_doc(dict( + leave_type_name=leave_type, + doctype="Leave Type", + is_earned_leave=1, + earned_leave_frequency="Monthly", + rounding=0.5, + max_leaves_allowed=6 + )).insert() + + +def create_leave_period(name): + frappe.delete_doc_if_exists("Leave Period", name, force=1) + start_date = get_first_day(getdate()) + + return frappe.get_doc(dict( + name=name, + doctype="Leave Period", + from_date=start_date, + to_date=add_months(start_date, 12), + company="_Test Company", + is_active=1 + )).insert() \ No newline at end of file