From fc3b924d4dd727431c82925bce6b2f9afd90d698 Mon Sep 17 00:00:00 2001 From: Mangesh-Khairnar Date: Thu, 14 Nov 2019 12:02:10 +0530 Subject: [PATCH] fix: skip leave ledger entry creation for denied leaves (#19556) * fix: skip leave ledger entry creation for denied leave application * patch: remove incorrect leave ledger entries * fix: create reverse ledger entry before setting status to cancel --- .../leave_application/leave_application.py | 5 +++- erpnext/patches.txt | 4 +-- .../remove_denied_leaves_from_leave_ledger.py | 26 +++++++++++++++++++ 3 files changed, 32 insertions(+), 3 deletions(-) create mode 100644 erpnext/patches/v12_0/remove_denied_leaves_from_leave_ledger.py diff --git a/erpnext/hr/doctype/leave_application/leave_application.py b/erpnext/hr/doctype/leave_application/leave_application.py index e1e5e8001d..0e6630541c 100755 --- a/erpnext/hr/doctype/leave_application/leave_application.py +++ b/erpnext/hr/doctype/leave_application/leave_application.py @@ -55,11 +55,11 @@ class LeaveApplication(Document): self.reload() def on_cancel(self): + self.create_leave_ledger_entry(submit=False) self.status = "Cancelled" # notify leave applier about cancellation self.notify_employee() self.cancel_attendance() - self.create_leave_ledger_entry(submit=False) def validate_applicable_after(self): if self.leave_type: @@ -351,6 +351,9 @@ class LeaveApplication(Document): pass def create_leave_ledger_entry(self, submit=True): + if self.status != 'Approved': + return + expiry_date = get_allocation_expiry(self.employee, self.leave_type, self.to_date, self.from_date) diff --git a/erpnext/patches.txt b/erpnext/patches.txt index 0155b27820..9e4dc12e65 100644 --- a/erpnext/patches.txt +++ b/erpnext/patches.txt @@ -638,11 +638,11 @@ erpnext.patches.v12_0.add_variant_of_in_item_attribute_table erpnext.patches.v12_0.rename_bank_account_field_in_journal_entry_account erpnext.patches.v12_0.create_default_energy_point_rules erpnext.patches.v12_0.set_produced_qty_field_in_sales_order_for_work_order -erpnext.patches.v12_0.generate_leave_ledger_entries erpnext.patches.v12_0.set_default_shopify_app_type erpnext.patches.v12_0.set_cwip_and_delete_asset_settings erpnext.patches.v12_0.set_expense_account_in_landed_cost_voucher_taxes erpnext.patches.v12_0.replace_accounting_with_accounts_in_home_settings erpnext.patches.v12_0.set_payment_entry_status erpnext.patches.v12_0.update_owner_fields_in_acc_dimension_custom_fields -erpnext.patches.v12_0.set_default_for_add_taxes_from_item_tax_template \ No newline at end of file +erpnext.patches.v12_0.set_default_for_add_taxes_from_item_tax_template +erpnext.patches.v12_0.remove_denied_leaves_from_leave_ledger \ No newline at end of file diff --git a/erpnext/patches/v12_0/remove_denied_leaves_from_leave_ledger.py b/erpnext/patches/v12_0/remove_denied_leaves_from_leave_ledger.py new file mode 100644 index 0000000000..24f0e7cb21 --- /dev/null +++ b/erpnext/patches/v12_0/remove_denied_leaves_from_leave_ledger.py @@ -0,0 +1,26 @@ +# Copyright (c) 2018, Frappe and Contributors +# License: GNU General Public License v3. See license.txt + +from __future__ import unicode_literals +import frappe +from frappe.utils import getdate, today + +def execute(): + ''' Delete leave ledger entry created + via leave applications with status != Approved ''' + if not frappe.db.a_row_exists("Leave Ledger Entry"): + return + + leave_application_list = get_denied_leave_application_list() + if leave_application_list: + delete_denied_leaves_from_leave_ledger_entry(leave_application_list) + +def get_denied_leave_application_list(): + return frappe.db.sql_list(''' Select name from `tabLeave Application` where status <> 'Approved' ''') + +def delete_denied_leaves_from_leave_ledger_entry(leave_application_list): + frappe.db.sql(''' Delete + FROM `tabLeave Ledger Entry` + WHERE + transaction_type = 'Leave Application' + AND transaction_name in {0} '''.format(tuple(leave_application_list))) #nosec \ No newline at end of file