Fixed issues in Expired Batches while making Stock Entry
diff --git a/erpnext/controllers/queries.py b/erpnext/controllers/queries.py
index 4f35fea..801f6f2 100644
--- a/erpnext/controllers/queries.py
+++ b/erpnext/controllers/queries.py
@@ -229,9 +229,10 @@
 			}, { "start": start, "page_len": page_len, "txt": ("%%%s%%" % txt) })
 
 def get_batch_no(doctype, txt, searchfield, start, page_len, filters):
-	if not filters.get("posting_date"):
-		filters["posting_date"] = nowdate()
-
+	cond = ""
+	if filters.get("posting_date"):
+		cond = "and (ifnull(batch.expiry_date, '')='' or batch.expiry_date >= %(posting_date)s)"
+	
 	batch_nos = None
 	args = {
 		'item_code': filters.get("item_code"),
@@ -251,23 +252,23 @@
 					and sle.warehouse = %(warehouse)s
 					and sle.batch_no like %(txt)s
 					and batch.docstatus < 2
-					and (ifnull(batch.expiry_date, '')='' or batch.expiry_date >= %(posting_date)s)
+					{0}
 					{match_conditions}
 				group by batch_no having sum(sle.actual_qty) > 0
 				order by batch.expiry_date, sle.batch_no desc
-				limit %(start)s, %(page_len)s""".format(match_conditions=get_match_cond(doctype)), args)
+				limit %(start)s, %(page_len)s""".format(cond, match_conditions=get_match_cond(doctype)), args)
 
 	if batch_nos:
 		return batch_nos
 	else:
-		return frappe.db.sql("""select name, expiry_date from `tabBatch`
+		return frappe.db.sql("""select name, expiry_date from `tabBatch` batch
 			where item = %(item_code)s
 			and name like %(txt)s
 			and docstatus < 2
-			and (ifnull(expiry_date, '')='' or expiry_date >= %(posting_date)s)
+			{0}
 			{match_conditions}
 			order by expiry_date, name desc
-			limit %(start)s, %(page_len)s""".format(match_conditions=get_match_cond(doctype)), args)
+			limit %(start)s, %(page_len)s""".format(cond, match_conditions=get_match_cond(doctype)), args, debug=1)
 
 def get_account_list(doctype, txt, searchfield, start, page_len, filters):
 	filter_list = []
diff --git a/erpnext/selling/sales_common.js b/erpnext/selling/sales_common.js
index 775eb41..cd65d18 100644
--- a/erpnext/selling/sales_common.js
+++ b/erpnext/selling/sales_common.js
@@ -71,7 +71,7 @@
 				} else {
 					filters = {
 						'item_code': item.item_code,
-						'posting_date': me.frm.doc.posting_date,
+						'posting_date': me.frm.doc.posting_date || nowdate(),
 					}
 					if(item.warehouse) filters["warehouse"] = item.warehouse
 
diff --git a/erpnext/stock/doctype/stock_entry/stock_entry.js b/erpnext/stock/doctype/stock_entry/stock_entry.js
index 1135bc7..c123957 100644
--- a/erpnext/stock/doctype/stock_entry/stock_entry.js
+++ b/erpnext/stock/doctype/stock_entry/stock_entry.js
@@ -411,14 +411,21 @@
 	var item = locals[cdt][cdn];
 	if(!item.item_code) {
 		frappe.throw(__("Please enter Item Code to get batch no"));
-	} else {
-		var filters = {
-			'item_code': item.item_code,
-			'posting_date': me.frm.doc.posting_date,
+	}
+	else {
+		if (in_list(["Material Transfer for Manufacture", "Manufacture", "Repack", "Subcontract"], doc.purpose)) {
+			var filters = {
+				'item_code': item.item_code,
+				'posting_date': me.frm.doc.posting_date || nowdate()
+			}	
+		} else {
+			var filters = {
+				'item_code': item.item_code
+			}
 		}
+		
 
 		if(item.s_warehouse) filters["warehouse"] = item.s_warehouse
-
 		return {
 			query : "erpnext.controllers.queries.get_batch_no",
 			filters: filters
diff --git a/erpnext/stock/doctype/stock_entry/stock_entry.py b/erpnext/stock/doctype/stock_entry/stock_entry.py
index 813a61b..e5e3bb3 100644
--- a/erpnext/stock/doctype/stock_entry/stock_entry.py
+++ b/erpnext/stock/doctype/stock_entry/stock_entry.py
@@ -727,7 +727,7 @@
 						frappe.MappingMismatchError)
 						
 	def validate_batch(self):
-		if self.purpose == "Material Transfer for Manufacture":
+		if self.purpose in ["Material Transfer for Manufacture", "Manufacture", "Repack", "Subcontract"]:
 			for item in self.get("items"):
 				if item.batch_no:
 					if getdate(self.posting_date) > getdate(frappe.db.get_value("Batch", item.batch_no, "expiry_date")):