fix: consider expiry in leaves
diff --git a/erpnext/hr/doctype/leave_allocation/leave_allocation.js b/erpnext/hr/doctype/leave_allocation/leave_allocation.js
index 7ecd3d1..1b3349a 100755
--- a/erpnext/hr/doctype/leave_allocation/leave_allocation.js
+++ b/erpnext/hr/doctype/leave_allocation/leave_allocation.js
@@ -23,10 +23,13 @@
 
 	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 @@
 
 	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.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 843d305..72ea273 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 @@
 				.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 @@
 		)
 		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 @@
 
 	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 02539a4..a9e4f05 100755
--- a/erpnext/hr/doctype/leave_application/leave_application.py
+++ b/erpnext/hr/doctype/leave_application/leave_application.py
@@ -523,7 +523,6 @@
 		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 @@
 	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):