From e4d03bf0d0d9d4a3ca8fb548630ebb7ce30018c1 Mon Sep 17 00:00:00 2001 From: Mangesh-Khairnar Date: Fri, 7 Jun 2019 11:30:27 +0530 Subject: [PATCH] fix: consider expiry in leaves --- .../leave_allocation/leave_allocation.js | 19 ++++++---- .../leave_allocation/leave_allocation.py | 36 +++++++------------ .../leave_application/leave_application.py | 5 ++- 3 files changed, 26 insertions(+), 34 deletions(-) diff --git a/erpnext/hr/doctype/leave_allocation/leave_allocation.js b/erpnext/hr/doctype/leave_allocation/leave_allocation.js index 7ecd3d1c91..1b3349a1ce 100755 --- a/erpnext/hr/doctype/leave_allocation/leave_allocation.js +++ b/erpnext/hr/doctype/leave_allocation/leave_allocation.js @@ -23,10 +23,13 @@ frappe.ui.form.on("Leave Allocation", { refresh: function(frm) { if(frm.doc.docstatus === 1 && frm.doc.status === "Active") { - // expire current allocation - frm.add_custom_button(__('Expire Allocation'), function() { - frm.trigger("expire_allocation"); - }); + var valid_expiry = moment(frappe.datetime.get_today()).isBetween(frm.doc.from_date, frm.doc.to_date); + if(valid_expiry) { + // expire current allocation + frm.add_custom_button(__('Expire Allocation'), function() { + frm.trigger("expire_allocation"); + }); + } // opens leave balance report for employee frm.add_custom_button(__('Leave Balance'), function() { @@ -40,15 +43,17 @@ frappe.ui.form.on("Leave Allocation", { expire_allocation: function(frm) { frappe.call({ - method: 'erpnext.hr.doctype.leave_application.leave_application.expire_previous_allocation', + method: 'expire_allocation', + doc: frm.doc, args: { - ref_doc: frm.doc + current: true }, freeze: true, callback: function(r){ if(!r.exc){ frappe.msgprint(__("Allocation Expired!")); } + frm.refresh(); } }); }, @@ -81,7 +86,7 @@ frappe.ui.form.on("Leave Allocation", { frappe.db.get_value("Leave Policy Detail", {'parent': frm.doc.leave_policy, 'leave_type': frm.doc.leave_type}, 'annual_allocation', (r) => { - if (!r.exc) { + if (r && !r.exc) { frm.set_value("new_leaves_allocated", flt(r.annual_allocation)); } }, "Leave Policy") diff --git a/erpnext/hr/doctype/leave_allocation/leave_allocation.py b/erpnext/hr/doctype/leave_allocation/leave_allocation.py index 843d3055d5..72ea2733b0 100755 --- a/erpnext/hr/doctype/leave_allocation/leave_allocation.py +++ b/erpnext/hr/doctype/leave_allocation/leave_allocation.py @@ -3,7 +3,7 @@ from __future__ import unicode_literals import frappe -from frappe.utils import flt, date_diff, formatdate, add_days +from frappe.utils import flt, date_diff, formatdate, add_days, today from frappe import _ from frappe.model.document import Document from erpnext.hr.utils import set_employee_name, get_leave_period @@ -41,8 +41,8 @@ class LeaveAllocation(Document): .format(self.leave_type, self.employee)) def on_submit(self): - self.expire_previous_allocation() self.create_leave_ledger_entry() + self.expire_allocation() def on_cancel(self): self.create_leave_ledger_entry(submit=False) @@ -128,20 +128,25 @@ class LeaveAllocation(Document): ) create_leave_ledger_entry(self, args, submit) - def expire_previous_allocation(self): - ''' expire previous allocation leaves ''' - leaves = get_unused_leaves(self.employee, self.leave_type, self.from_date) + def expire_allocation(self, current=False): + ''' expires allocation ''' + date = self.to_date if current else self.from_date + leaves = get_unused_leaves(self.employee, self.leave_type, date) if leaves: + expiry_date = today() if current else add_days(self.from_date, -1) args = dict( leaves=flt(leaves) * -1, - from_date=self.from_date, - to_date=self.from_date, + from_date=expiry_date, + to_date=expiry_date, is_carry_forward=0, is_expired=1 ) create_leave_ledger_entry(self, args) + if current: + frappe.db.set_value("Leave Allocation", self.name, "status", "Expired") + def get_leave_allocation_for_period(employee, leave_type, from_date, to_date): leave_allocated = 0 leave_allocations = frappe.db.sql(""" @@ -174,23 +179,6 @@ def get_carry_forwarded_leaves(employee, leave_type, date, carry_forward=None): return carry_forwarded_leaves -@frappe.whitelist() -def expire_current_allocation(ref_doc): - ''' expire previous allocation leaves ''' - leaves = get_unused_leaves(ref_doc.employee, ref_doc.leave_type, ref_doc.to_date) - - if flt(leaves) > 0: - args = dict( - leaves=leaves * -1, - from_date=ref_doc.to_date, - to_date=ref_doc.to_date, - is_carry_forward=0, - is_expired=1 - ) - create_leave_ledger_entry(ref_doc, args) - - frappe.db.set_value("Leave Allocation", ref_doc.name, "status", "Expired") - def get_unused_leaves(employee, leave_type, date): return frappe.db.get_value("Leave Ledger Entry", filters={ "to_date": ("<=", date), diff --git a/erpnext/hr/doctype/leave_application/leave_application.py b/erpnext/hr/doctype/leave_application/leave_application.py index 02539a4c3b..a9e4f05ca2 100755 --- a/erpnext/hr/doctype/leave_application/leave_application.py +++ b/erpnext/hr/doctype/leave_application/leave_application.py @@ -523,7 +523,6 @@ def get_leaves_for_period(employee, leave_type, from_date, to_date): where employee=%(employee)s and leave_type=%(leave_type)s and docstatus=1 and leaves<0 - and (is_expired=0 or is_carry_forward=1) and (from_date between %(from_date)s and %(to_date)s or to_date between %(from_date)s and %(to_date)s or (from_date < %(from_date)s and to_date > %(to_date)s)) @@ -536,8 +535,8 @@ def get_leaves_for_period(employee, leave_type, from_date, to_date): leave_days = 0 for leave_entry in leave_entries: - if leave_entry.from_date >= getdate(from_date) and \ - leave_entry.to_date <= getdate(to_date) and leave_entry.transaction_type=='Leave Encashment': + if leave_entry.from_date >= getdate(from_date) and leave_entry.to_date <= getdate(to_date) \ + and leave_entry.transaction_type in ('Leave Encashment', 'Leave Allocation'): leave_days += leave_entry.leaves else: if leave_entry.from_date < getdate(from_date):