fix: Remove duplicate leave ledger entry (#21871)
* fix: Remove duplicate leave ledger entry * fix: Remove duplicate leave ledger entry
This commit is contained in:
parent
bd24a07459
commit
32635ef193
@ -6,6 +6,7 @@ import frappe
|
|||||||
|
|
||||||
def execute():
|
def execute():
|
||||||
"""Delete duplicate leave ledger entries of type allocation created."""
|
"""Delete duplicate leave ledger entries of type allocation created."""
|
||||||
|
frappe.reload_doc('hr', 'doctype', 'leave_ledger_entry')
|
||||||
if not frappe.db.a_row_exists("Leave Ledger Entry"):
|
if not frappe.db.a_row_exists("Leave Ledger Entry"):
|
||||||
return
|
return
|
||||||
|
|
||||||
@ -14,31 +15,32 @@ def execute():
|
|||||||
|
|
||||||
def get_duplicate_records():
|
def get_duplicate_records():
|
||||||
"""Fetch all but one duplicate records from the list of expired leave allocation."""
|
"""Fetch all but one duplicate records from the list of expired leave allocation."""
|
||||||
return frappe.db.sql_list("""
|
return frappe.db.sql("""
|
||||||
WITH duplicate_records AS
|
SELECT name, employee, transaction_name, leave_type, is_carry_forward, from_date, to_date
|
||||||
(SELECT
|
FROM `tabLeave Ledger Entry`
|
||||||
name, transaction_name, is_carry_forward,
|
WHERE
|
||||||
ROW_NUMBER() over(partition by transaction_name order by creation)as row
|
transaction_type = 'Leave Allocation'
|
||||||
FROM `tabLeave Ledger Entry` l
|
AND docstatus = 1
|
||||||
WHERE (EXISTS
|
AND is_expired = 1
|
||||||
(SELECT name
|
GROUP BY
|
||||||
FROM `tabLeave Ledger Entry`
|
employee, transaction_name, leave_type, is_carry_forward, from_date, to_date
|
||||||
WHERE
|
HAVING
|
||||||
transaction_name = l.transaction_name
|
count(name) > 1
|
||||||
AND transaction_type = 'Leave Allocation'
|
ORDER BY
|
||||||
AND name <> l.name
|
creation
|
||||||
AND employee = l.employee
|
|
||||||
AND docstatus = 1
|
|
||||||
AND leave_type = l.leave_type
|
|
||||||
AND is_carry_forward=l.is_carry_forward
|
|
||||||
AND to_date = l.to_date
|
|
||||||
AND from_date = l.from_date
|
|
||||||
AND is_expired = 1
|
|
||||||
)))
|
|
||||||
SELECT name FROM duplicate_records WHERE row > 1
|
|
||||||
""")
|
""")
|
||||||
|
|
||||||
def delete_duplicate_ledger_entries(duplicate_records_list):
|
def delete_duplicate_ledger_entries(duplicate_records_list):
|
||||||
"""Delete duplicate leave ledger entries."""
|
"""Delete duplicate leave ledger entries."""
|
||||||
if not duplicate_records_list: return
|
if not duplicate_records_list: return
|
||||||
frappe.db.sql('''DELETE FROM `tabLeave Ledger Entry` WHERE name in %s''', ((tuple(duplicate_records_list)), ))
|
for d in duplicate_records_list:
|
||||||
|
frappe.db.sql('''
|
||||||
|
DELETE FROM `tabLeave Ledger Entry`
|
||||||
|
WHERE name != %s
|
||||||
|
AND employee = %s
|
||||||
|
AND transaction_name = %s
|
||||||
|
AND leave_type = %s
|
||||||
|
AND is_carry_forward = %s
|
||||||
|
AND from_date = %s
|
||||||
|
AND to_date = %s
|
||||||
|
''', tuple(d))
|
Loading…
x
Reference in New Issue
Block a user