fix: consider expiry in leaves

This commit is contained in:
Mangesh-Khairnar 2019-06-07 11:30:27 +05:30
parent 368a974368
commit e4d03bf0d0
3 changed files with 26 additions and 34 deletions

View File

@ -23,10 +23,13 @@ frappe.ui.form.on("Leave Allocation", {
refresh: function(frm) { refresh: function(frm) {
if(frm.doc.docstatus === 1 && frm.doc.status === "Active") { if(frm.doc.docstatus === 1 && frm.doc.status === "Active") {
var valid_expiry = moment(frappe.datetime.get_today()).isBetween(frm.doc.from_date, frm.doc.to_date);
if(valid_expiry) {
// expire current allocation // expire current allocation
frm.add_custom_button(__('Expire Allocation'), function() { frm.add_custom_button(__('Expire Allocation'), function() {
frm.trigger("expire_allocation"); frm.trigger("expire_allocation");
}); });
}
// opens leave balance report for employee // opens leave balance report for employee
frm.add_custom_button(__('Leave Balance'), function() { frm.add_custom_button(__('Leave Balance'), function() {
@ -40,15 +43,17 @@ frappe.ui.form.on("Leave Allocation", {
expire_allocation: function(frm) { expire_allocation: function(frm) {
frappe.call({ frappe.call({
method: 'erpnext.hr.doctype.leave_application.leave_application.expire_previous_allocation', method: 'expire_allocation',
doc: frm.doc,
args: { args: {
ref_doc: frm.doc current: true
}, },
freeze: true, freeze: true,
callback: function(r){ callback: function(r){
if(!r.exc){ if(!r.exc){
frappe.msgprint(__("Allocation Expired!")); frappe.msgprint(__("Allocation Expired!"));
} }
frm.refresh();
} }
}); });
}, },
@ -81,7 +86,7 @@ frappe.ui.form.on("Leave Allocation", {
frappe.db.get_value("Leave Policy Detail", frappe.db.get_value("Leave Policy Detail",
{'parent': frm.doc.leave_policy, 'leave_type': frm.doc.leave_type}, {'parent': frm.doc.leave_policy, 'leave_type': frm.doc.leave_type},
'annual_allocation', (r) => { 'annual_allocation', (r) => {
if (!r.exc) { if (r && !r.exc) {
frm.set_value("new_leaves_allocated", flt(r.annual_allocation)); frm.set_value("new_leaves_allocated", flt(r.annual_allocation));
} }
}, "Leave Policy") }, "Leave Policy")

View File

@ -3,7 +3,7 @@
from __future__ import unicode_literals from __future__ import unicode_literals
import frappe 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 import _
from frappe.model.document import Document from frappe.model.document import Document
from erpnext.hr.utils import set_employee_name, get_leave_period from erpnext.hr.utils import set_employee_name, get_leave_period
@ -41,8 +41,8 @@ class LeaveAllocation(Document):
.format(self.leave_type, self.employee)) .format(self.leave_type, self.employee))
def on_submit(self): def on_submit(self):
self.expire_previous_allocation()
self.create_leave_ledger_entry() self.create_leave_ledger_entry()
self.expire_allocation()
def on_cancel(self): def on_cancel(self):
self.create_leave_ledger_entry(submit=False) self.create_leave_ledger_entry(submit=False)
@ -128,20 +128,25 @@ class LeaveAllocation(Document):
) )
create_leave_ledger_entry(self, args, submit) create_leave_ledger_entry(self, args, submit)
def expire_previous_allocation(self): def expire_allocation(self, current=False):
''' expire previous allocation leaves ''' ''' expires allocation '''
leaves = get_unused_leaves(self.employee, self.leave_type, self.from_date) date = self.to_date if current else self.from_date
leaves = get_unused_leaves(self.employee, self.leave_type, date)
if leaves: if leaves:
expiry_date = today() if current else add_days(self.from_date, -1)
args = dict( args = dict(
leaves=flt(leaves) * -1, leaves=flt(leaves) * -1,
from_date=self.from_date, from_date=expiry_date,
to_date=self.from_date, to_date=expiry_date,
is_carry_forward=0, is_carry_forward=0,
is_expired=1 is_expired=1
) )
create_leave_ledger_entry(self, args) 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): def get_leave_allocation_for_period(employee, leave_type, from_date, to_date):
leave_allocated = 0 leave_allocated = 0
leave_allocations = frappe.db.sql(""" 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 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): def get_unused_leaves(employee, leave_type, date):
return frappe.db.get_value("Leave Ledger Entry", filters={ return frappe.db.get_value("Leave Ledger Entry", filters={
"to_date": ("<=", date), "to_date": ("<=", date),

View File

@ -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 where employee=%(employee)s and leave_type=%(leave_type)s
and docstatus=1 and docstatus=1
and leaves<0 and leaves<0
and (is_expired=0 or is_carry_forward=1)
and (from_date between %(from_date)s and %(to_date)s and (from_date between %(from_date)s and %(to_date)s
or to_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)) 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 leave_days = 0
for leave_entry in leave_entries: for leave_entry in leave_entries:
if leave_entry.from_date >= getdate(from_date) and \ if leave_entry.from_date >= getdate(from_date) and leave_entry.to_date <= getdate(to_date) \
leave_entry.to_date <= getdate(to_date) and leave_entry.transaction_type=='Leave Encashment': and leave_entry.transaction_type in ('Leave Encashment', 'Leave Allocation'):
leave_days += leave_entry.leaves leave_days += leave_entry.leaves
else: else:
if leave_entry.from_date < getdate(from_date): if leave_entry.from_date < getdate(from_date):