fix: check for old unexpired allocation
diff --git a/erpnext/hr/doctype/leave_encashment/leave_encashment.py b/erpnext/hr/doctype/leave_encashment/leave_encashment.py
index d256c7f..30529b6 100644
--- a/erpnext/hr/doctype/leave_encashment/leave_encashment.py
+++ b/erpnext/hr/doctype/leave_encashment/leave_encashment.py
@@ -64,7 +64,7 @@
allocation = self.get_leave_allocation()
- self.leave_balance = allocation.total_leaves_allocated - get_unused_leaves(self.employee, self.leave_type, self.encashment_date)
+ self.leave_balance = allocation.total_leaves_allocated - get_unused_leaves(self.employee, self.leave_type, allocation.from_date, self.encashment_date)
encashable_days = self.leave_balance - frappe.db.get_value('Leave Type', self.leave_type, 'encashment_threshold_days')
self.encashable_days = encashable_days if encashable_days > 0 else 0
diff --git a/erpnext/hr/doctype/leave_ledger_entry/leave_ledger_entry.py b/erpnext/hr/doctype/leave_ledger_entry/leave_ledger_entry.py
index d19e15c..8fca241 100644
--- a/erpnext/hr/doctype/leave_ledger_entry/leave_ledger_entry.py
+++ b/erpnext/hr/doctype/leave_ledger_entry/leave_ledger_entry.py
@@ -72,8 +72,11 @@
''' Returns the expiry ledger entry having same creation date as the ledger entry to be cancelled '''
creation_date = frappe.db.get_value("Leave Ledger Entry", filters={
'transaction_name': ledger.transaction_name,
- 'is_expired': 0
- }, fieldname=['creation']).strftime(DATE_FORMAT)
+ 'is_expired': 0,
+ 'transaction_type': 'Leave Allocation'
+ }, fieldname=['creation'])
+
+ creation_date = creation_date.strftime(DATE_FORMAT) if creation_date else ''
return frappe.db.get_value("Leave Ledger Entry", filters={
'creation': ('like', creation_date+"%"),
@@ -94,24 +97,31 @@
if leave_type_records:
leave_type = [record[0] for record in leave_type_records]
- expired_allocation = frappe.get_all("Leave Ledger Entry",
+
+ expired_allocation = frappe.db.sql_list("""SELECT name
+ FROM `tabLeave Ledger Entry`
+ WHERE
+ `transaction_type`='Leave Allocation'
+ AND `is_expired`=1""")
+
+ expire_allocation = frappe.get_all("Leave Ledger Entry",
fields=['leaves', 'to_date', 'employee', 'leave_type', 'is_carry_forward', 'transaction_name as name', 'transaction_type'],
filters={
- 'to_date': add_days(today(), -1),
+ 'to_date': ("<", today()),
'transaction_type': 'Leave Allocation',
+ 'transaction_name': ('not in', expired_allocation)
},
or_filters={
'is_carry_forward': 0,
'leave_type': ('in', leave_type)
})
- if expired_allocation:
- create_expiry_ledger_entry(expired_allocation)
+ if expire_allocation:
+ create_expiry_ledger_entry(expire_allocation)
-def create_expiry_ledger_entry(expired_allocation):
+def create_expiry_ledger_entry(expire_allocation):
''' Create ledger entry for expired allocation '''
- for allocation in expired_allocation:
-
+ for allocation in expire_allocation:
if allocation.is_carry_forward:
expire_carried_forward_allocation(allocation)
else: