From ee7c9add397633c86ec79c4e3f2e29ec7af956e6 Mon Sep 17 00:00:00 2001 From: Rucha Mahabal Date: Mon, 20 Mar 2023 10:56:00 +0530 Subject: [PATCH] fix: earned leave exceeding annual allocation --- erpnext/hr/utils.py | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/erpnext/hr/utils.py b/erpnext/hr/utils.py index 181e3b12b98..c71336e26a8 100644 --- a/erpnext/hr/utils.py +++ b/erpnext/hr/utils.py @@ -496,18 +496,28 @@ def allocate_earned_leaves(): def update_previous_leave_allocation(allocation, annual_allocation, e_leave_type): + allocation = frappe.get_doc("Leave Allocation", allocation.name) + annual_allocation = flt(annual_allocation, allocation.precision("total_leaves_allocated")) + earned_leaves = get_monthly_earned_leave( annual_allocation, e_leave_type.earned_leave_frequency, e_leave_type.rounding ) - allocation = frappe.get_doc("Leave Allocation", allocation.name) new_allocation = flt(allocation.total_leaves_allocated) + flt(earned_leaves) + new_allocation_without_cf = flt( + flt(allocation.get_existing_leave_count()) + flt(earned_leaves), + allocation.precision("total_leaves_allocated"), + ) if new_allocation > e_leave_type.max_leaves_allowed and e_leave_type.max_leaves_allowed > 0: new_allocation = e_leave_type.max_leaves_allowed - if new_allocation != allocation.total_leaves_allocated: - today_date = today() + if ( + new_allocation != allocation.total_leaves_allocated + # annual allocation as per policy should not be exceeded + and new_allocation_without_cf <= annual_allocation + ): + today_date = frappe.flags.current_date or getdate() allocation.db_set("total_leaves_allocated", new_allocation, update_modified=False) create_additional_leave_ledger_entry(allocation, earned_leaves, today_date)