From d53e780fb88f0b0f179ca0e34421e317e6036e7b Mon Sep 17 00:00:00 2001 From: Nabin Hait Date: Sat, 5 Dec 2015 12:15:49 +0530 Subject: [PATCH] [fix] Don't validate balance for lwp and in balance consider all leaves in the allocation period --- .../leave_allocation/leave_allocation.py | 2 +- .../leave_application/leave_application.js | 3 ++- .../leave_application/leave_application.py | 20 ++++++++++++------- 3 files changed, 16 insertions(+), 9 deletions(-) diff --git a/erpnext/hr/doctype/leave_allocation/leave_allocation.py b/erpnext/hr/doctype/leave_allocation/leave_allocation.py index 57eb146654..6740c6e9a4 100755 --- a/erpnext/hr/doctype/leave_allocation/leave_allocation.py +++ b/erpnext/hr/doctype/leave_allocation/leave_allocation.py @@ -51,7 +51,7 @@ class LeaveAllocation(Document): (self.employee, self.leave_type, self.from_date, self.to_date)) if leave_allocation: - frappe.msgprint(_("{0} already allocated for Employee {1} for period {2} - {3}") + frappe.msgprint(_("{0} already allocated for Employee {1} for period {2} to {3}") .format(self.leave_type, self.employee, formatdate(self.from_date), formatdate(self.to_date))) frappe.throw(_('Reference') + ': {0}' diff --git a/erpnext/hr/doctype/leave_application/leave_application.js b/erpnext/hr/doctype/leave_application/leave_application.js index e708b77911..9d440179d6 100755 --- a/erpnext/hr/doctype/leave_application/leave_application.js +++ b/erpnext/hr/doctype/leave_application/leave_application.js @@ -72,7 +72,8 @@ frappe.ui.form.on("Leave Application", { args: { employee: frm.doc.employee, date: frm.doc.from_date, - leave_type: frm.doc.leave_type + leave_type: frm.doc.leave_type, + consider_all_leaves_in_the_allocation_period: true }, callback: function(r) { if (!r.exc && r.message) { diff --git a/erpnext/hr/doctype/leave_application/leave_application.py b/erpnext/hr/doctype/leave_application/leave_application.py index 60bc531be0..fe1291f88f 100755 --- a/erpnext/hr/doctype/leave_application/leave_application.py +++ b/erpnext/hr/doctype/leave_application/leave_application.py @@ -62,9 +62,10 @@ class LeaveApplication(Document): def validate_dates(self): if self.from_date and self.to_date and (getdate(self.to_date) < getdate(self.from_date)): frappe.throw(_("To date cannot be before from date")) - - self.validate_dates_acorss_allocation() - self.validate_back_dated_application() + + if not is_lwp(self.leave_type): + self.validate_dates_acorss_allocation() + self.validate_back_dated_application() def validate_dates_acorss_allocation(self): def _get_leave_alloction_record(date): @@ -117,7 +118,8 @@ class LeaveApplication(Document): frappe.throw(_("The day(s) on which you are applying for leave are holidays. You need not apply for leave.")) if not is_lwp(self.leave_type): - self.leave_balance = get_leave_balance_on(self.employee, self.leave_type, self.from_date) + self.leave_balance = get_leave_balance_on(self.employee, self.leave_type, self.from_date, + consider_all_leaves_in_the_allocation_period=True) if self.status != "Rejected" and self.leave_balance < self.total_leave_days: if frappe.db.get_value("Leave Type", self.leave_type, "allow_negative"): @@ -240,12 +242,15 @@ def get_number_of_leave_days(employee, leave_type, from_date, to_date, half_day= return number_of_days @frappe.whitelist() -def get_leave_balance_on(employee, leave_type, date, allocation_records=None): +def get_leave_balance_on(employee, leave_type, date, allocation_records=None, + consider_all_leaves_in_the_allocation_period=False): if allocation_records == None: allocation_records = get_leave_allocation_records(date, employee).get(employee, frappe._dict()) allocation = allocation_records.get(leave_type, frappe._dict()) - + + if consider_all_leaves_in_the_allocation_period: + date = allocation.to_date leaves_taken = get_approved_leaves_for_period(employee, leave_type, allocation.from_date, date) return flt(allocation.total_leaves_allocated) - flt(leaves_taken) @@ -285,7 +290,7 @@ def get_leave_allocation_records(date, employee=None): conditions = (" and employee='%s'" % employee) if employee else "" leave_allocation_records = frappe.db.sql(""" - select employee, leave_type, total_leaves_allocated, from_date + select employee, leave_type, total_leaves_allocated, from_date, to_date from `tabLeave Allocation` where %s between from_date and to_date and docstatus=1 {0}""".format(conditions), (date), as_dict=1) @@ -293,6 +298,7 @@ def get_leave_allocation_records(date, employee=None): for d in leave_allocation_records: allocated_leaves.setdefault(d.employee, frappe._dict()).setdefault(d.leave_type, frappe._dict({ "from_date": d.from_date, + "to_date": d.to_date, "total_leaves_allocated": d.total_leaves_allocated }))