fix: remove check for already allocated earned leaves (#30931) (#30932)

* fix: remove check for already allocated earned leaves

* fix: do not set New Leaves Allocated field as read-only for earned leaves

- removing this until there's a better way to update existing allocations

(cherry picked from commit f92bc4dd33)

Co-authored-by: Rucha Mahabal <ruchamahabal2@gmail.com>
This commit is contained in:
mergify[bot]
2022-05-09 12:34:21 +05:30
committed by GitHub
parent 9189653f2e
commit 80f7d66255
4 changed files with 21 additions and 99 deletions

View File

@@ -34,15 +34,6 @@ frappe.ui.form.on("Leave Allocation", {
}); });
} }
} }
// make new leaves allocated field read only if allocation is created via leave policy assignment
// and leave type is earned leave, since these leaves would be allocated via the scheduler
if (frm.doc.leave_policy_assignment) {
frappe.db.get_value("Leave Type", frm.doc.leave_type, "is_earned_leave", (r) => {
if (r && cint(r.is_earned_leave))
frm.set_df_property("new_leaves_allocated", "read_only", 1);
});
}
}, },
expire_allocation: function(frm) { expire_allocation: function(frm) {

View File

@@ -744,7 +744,7 @@ class TestLeaveApplication(unittest.TestCase):
i = 0 i = 0
while i < 14: while i < 14:
allocate_earned_leaves(ignore_duplicates=True) allocate_earned_leaves()
i += 1 i += 1
self.assertEqual(get_leave_balance_on(employee.name, leave_type, nowdate()), 6) self.assertEqual(get_leave_balance_on(employee.name, leave_type, nowdate()), 6)
@@ -752,7 +752,7 @@ class TestLeaveApplication(unittest.TestCase):
frappe.db.set_value("Leave Type", leave_type, "max_leaves_allowed", 0) frappe.db.set_value("Leave Type", leave_type, "max_leaves_allowed", 0)
i = 0 i = 0
while i < 6: while i < 6:
allocate_earned_leaves(ignore_duplicates=True) allocate_earned_leaves()
i += 1 i += 1
self.assertEqual(get_leave_balance_on(employee.name, leave_type, nowdate()), 9) self.assertEqual(get_leave_balance_on(employee.name, leave_type, nowdate()), 9)

View File

@@ -4,6 +4,7 @@
import unittest import unittest
import frappe import frappe
from frappe.tests.utils import FrappeTestCase
from frappe.utils import add_days, add_months, get_first_day, get_last_day, getdate from frappe.utils import add_days, add_months, get_first_day, get_last_day, getdate
from erpnext.hr.doctype.leave_application.test_leave_application import ( from erpnext.hr.doctype.leave_application.test_leave_application import (
@@ -18,7 +19,7 @@ from erpnext.hr.doctype.leave_policy_assignment.leave_policy_assignment import (
test_dependencies = ["Employee"] test_dependencies = ["Employee"]
class TestLeavePolicyAssignment(unittest.TestCase): class TestLeavePolicyAssignment(FrappeTestCase):
def setUp(self): def setUp(self):
for doctype in [ for doctype in [
"Leave Period", "Leave Period",
@@ -39,6 +40,9 @@ class TestLeavePolicyAssignment(unittest.TestCase):
leave_policy = create_leave_policy() leave_policy = create_leave_policy()
leave_policy.submit() leave_policy.submit()
self.employee.date_of_joining = get_first_day(leave_period.from_date)
self.employee.save()
data = { data = {
"assignment_based_on": "Leave Period", "assignment_based_on": "Leave Period",
"leave_policy": leave_policy.name, "leave_policy": leave_policy.name,
@@ -187,19 +191,6 @@ class TestLeavePolicyAssignment(unittest.TestCase):
) )
self.assertEqual(leaves_allocated, 3) self.assertEqual(leaves_allocated, 3)
# if the daily job is not completed yet, there is another check present
# to ensure leave is not already allocated to avoid duplication
from erpnext.hr.utils import allocate_earned_leaves
allocate_earned_leaves()
leaves_allocated = frappe.db.get_value(
"Leave Allocation",
{"leave_policy_assignment": leave_policy_assignments[0]},
"total_leaves_allocated",
)
self.assertEqual(leaves_allocated, 3)
def test_earned_leave_alloc_for_passed_months_with_cf_leaves_based_on_leave_period(self): def test_earned_leave_alloc_for_passed_months_with_cf_leaves_based_on_leave_period(self):
from erpnext.hr.doctype.leave_allocation.test_leave_allocation import create_leave_allocation from erpnext.hr.doctype.leave_allocation.test_leave_allocation import create_leave_allocation
@@ -241,20 +232,6 @@ class TestLeavePolicyAssignment(unittest.TestCase):
self.assertEqual(details.unused_leaves, 5) self.assertEqual(details.unused_leaves, 5)
self.assertEqual(details.total_leaves_allocated, 7) self.assertEqual(details.total_leaves_allocated, 7)
# if the daily job is not completed yet, there is another check present
# to ensure leave is not already allocated to avoid duplication
from erpnext.hr.utils import is_earned_leave_already_allocated
frappe.flags.current_date = get_last_day(getdate())
allocation = frappe.get_doc("Leave Allocation", details.name)
# 1 leave is still pending to be allocated, irrespective of carry forwarded leaves
self.assertFalse(
is_earned_leave_already_allocated(
allocation, leave_policy.leave_policy_details[0].annual_allocation
)
)
def test_earned_leave_alloc_for_passed_months_based_on_joining_date(self): def test_earned_leave_alloc_for_passed_months_based_on_joining_date(self):
# tests leave alloc for earned leaves for assignment based on joining date in policy assignment # tests leave alloc for earned leaves for assignment based on joining date in policy assignment
leave_type = create_earned_leave_type("Test Earned Leave") leave_type = create_earned_leave_type("Test Earned Leave")
@@ -287,19 +264,6 @@ class TestLeavePolicyAssignment(unittest.TestCase):
self.assertEqual(effective_from, self.employee.date_of_joining) self.assertEqual(effective_from, self.employee.date_of_joining)
self.assertEqual(leaves_allocated, 3) self.assertEqual(leaves_allocated, 3)
# to ensure leave is not already allocated to avoid duplication
from erpnext.hr.utils import allocate_earned_leaves
frappe.flags.current_date = get_last_day(getdate())
allocate_earned_leaves()
leaves_allocated = frappe.db.get_value(
"Leave Allocation",
{"leave_policy_assignment": leave_policy_assignments[0]},
"total_leaves_allocated",
)
self.assertEqual(leaves_allocated, 3)
def test_grant_leaves_on_doj_for_earned_leaves_based_on_leave_period(self): def test_grant_leaves_on_doj_for_earned_leaves_based_on_leave_period(self):
# tests leave alloc based on leave period for earned leaves with "based on doj" configuration in leave type # tests leave alloc based on leave period for earned leaves with "based on doj" configuration in leave type
leave_period, leave_policy = setup_leave_period_and_policy( leave_period, leave_policy = setup_leave_period_and_policy(
@@ -329,20 +293,6 @@ class TestLeavePolicyAssignment(unittest.TestCase):
) )
self.assertEqual(leaves_allocated, 3) self.assertEqual(leaves_allocated, 3)
# if the daily job is not completed yet, there is another check present
# to ensure leave is not already allocated to avoid duplication
from erpnext.hr.utils import allocate_earned_leaves
frappe.flags.current_date = get_first_day(getdate())
allocate_earned_leaves()
leaves_allocated = frappe.db.get_value(
"Leave Allocation",
{"leave_policy_assignment": leave_policy_assignments[0]},
"total_leaves_allocated",
)
self.assertEqual(leaves_allocated, 3)
def test_grant_leaves_on_doj_for_earned_leaves_based_on_joining_date(self): def test_grant_leaves_on_doj_for_earned_leaves_based_on_joining_date(self):
# tests leave alloc based on joining date for earned leaves with "based on doj" configuration in leave type # tests leave alloc based on joining date for earned leaves with "based on doj" configuration in leave type
leave_type = create_earned_leave_type("Test Earned Leave", based_on_doj=True) leave_type = create_earned_leave_type("Test Earned Leave", based_on_doj=True)
@@ -376,21 +326,7 @@ class TestLeavePolicyAssignment(unittest.TestCase):
self.assertEqual(effective_from, self.employee.date_of_joining) self.assertEqual(effective_from, self.employee.date_of_joining)
self.assertEqual(leaves_allocated, 3) self.assertEqual(leaves_allocated, 3)
# to ensure leave is not already allocated to avoid duplication
from erpnext.hr.utils import allocate_earned_leaves
frappe.flags.current_date = get_first_day(getdate())
allocate_earned_leaves()
leaves_allocated = frappe.db.get_value(
"Leave Allocation",
{"leave_policy_assignment": leave_policy_assignments[0]},
"total_leaves_allocated",
)
self.assertEqual(leaves_allocated, 3)
def tearDown(self): def tearDown(self):
frappe.db.rollback()
frappe.db.set_value("Employee", self.employee.name, "date_of_joining", self.original_doj) frappe.db.set_value("Employee", self.employee.name, "date_of_joining", self.original_doj)
frappe.flags.current_date = None frappe.flags.current_date = None

View File

@@ -428,7 +428,7 @@ def generate_leave_encashment():
create_leave_encashment(leave_allocation=leave_allocation) create_leave_encashment(leave_allocation=leave_allocation)
def allocate_earned_leaves(ignore_duplicates=False): def allocate_earned_leaves():
"""Allocate earned leaves to Employees""" """Allocate earned leaves to Employees"""
e_leave_types = get_earned_leaves() e_leave_types = get_earned_leaves()
today = getdate() today = getdate()
@@ -464,14 +464,10 @@ def allocate_earned_leaves(ignore_duplicates=False):
if check_effective_date( if check_effective_date(
from_date, today, e_leave_type.earned_leave_frequency, e_leave_type.based_on_date_of_joining from_date, today, e_leave_type.earned_leave_frequency, e_leave_type.based_on_date_of_joining
): ):
update_previous_leave_allocation( update_previous_leave_allocation(allocation, annual_allocation, e_leave_type)
allocation, annual_allocation, e_leave_type, ignore_duplicates
)
def update_previous_leave_allocation( def update_previous_leave_allocation(allocation, annual_allocation, e_leave_type):
allocation, annual_allocation, e_leave_type, ignore_duplicates=False
):
earned_leaves = get_monthly_earned_leave( earned_leaves = get_monthly_earned_leave(
annual_allocation, e_leave_type.earned_leave_frequency, e_leave_type.rounding annual_allocation, e_leave_type.earned_leave_frequency, e_leave_type.rounding
) )
@@ -485,7 +481,6 @@ def update_previous_leave_allocation(
if new_allocation != allocation.total_leaves_allocated: if new_allocation != allocation.total_leaves_allocated:
today_date = today() today_date = today()
if ignore_duplicates or not is_earned_leave_already_allocated(allocation, annual_allocation):
allocation.db_set("total_leaves_allocated", new_allocation, update_modified=False) allocation.db_set("total_leaves_allocated", new_allocation, update_modified=False)
create_additional_leave_ledger_entry(allocation, earned_leaves, today_date) create_additional_leave_ledger_entry(allocation, earned_leaves, today_date)