From fd5d2ed87f079a270894de6821e406fdc3d78d65 Mon Sep 17 00:00:00 2001 From: Rucha Mahabal Date: Thu, 9 Mar 2023 12:44:56 +0530 Subject: [PATCH] refactor: consider cases for partially consumed cf and new leaves - the above two cases weren't considering the split between cf leaves taken and new leaves taken and substracting all consumed leaves from cf leaves --- .../leave_application/leave_application.py | 26 ++++++++++++------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/erpnext/hr/doctype/leave_application/leave_application.py b/erpnext/hr/doctype/leave_application/leave_application.py index dedfcdbe21c..de88b3d807c 100755 --- a/erpnext/hr/doctype/leave_application/leave_application.py +++ b/erpnext/hr/doctype/leave_application/leave_application.py @@ -935,25 +935,33 @@ def get_remaining_leaves( return remaining_leaves - leave_balance = leave_balance_for_consumption = flt(allocation.total_leaves_allocated) + flt( - leaves_taken - ) - - # balance for carry forwarded leaves if cf_expiry and allocation.unused_leaves: + # allocation contains both carry forwarded and new leaves cf_leaves_taken = get_leaves_for_period( allocation.employee, allocation.leave_type, allocation.from_date, cf_expiry ) + new_leaves_taken = get_leaves_for_period( + allocation.employee, allocation.leave_type, add_days(cf_expiry, 1), allocation.to_date + ) if getdate(date) > getdate(cf_expiry): - # carry forwarded leave expiry date passed - cf_leaves = remaining_cf_leaves = flt(cf_leaves_taken) + # carry forwarded leaves have expired + cf_leaves = remaining_cf_leaves = 0 else: cf_leaves = flt(allocation.unused_leaves) + flt(cf_leaves_taken) remaining_cf_leaves = _get_remaining_leaves(cf_leaves, cf_expiry) - leave_balance = flt(allocation.new_leaves_allocated) + flt(cf_leaves) - leave_balance_for_consumption = flt(allocation.new_leaves_allocated) + flt(remaining_cf_leaves) + # new leaves allocated - new leaves taken + cf leave balance + # Note: `new_leaves_taken` is added here because its already a -ve number in the ledger + leave_balance = (flt(allocation.new_leaves_allocated) + flt(new_leaves_taken)) + flt(cf_leaves) + leave_balance_for_consumption = ( + flt(allocation.new_leaves_allocated) + flt(new_leaves_taken) + ) + flt(remaining_cf_leaves) + else: + # allocation only contains newly allocated leaves + leave_balance = leave_balance_for_consumption = flt(allocation.total_leaves_allocated) + flt( + leaves_taken + ) remaining_leaves = _get_remaining_leaves(leave_balance_for_consumption, allocation.to_date) return frappe._dict(leave_balance=leave_balance, leave_balance_for_consumption=remaining_leaves)