commified item_code query
diff --git a/accounts/doctype/purchase_invoice/purchase_invoice.js b/accounts/doctype/purchase_invoice/purchase_invoice.js
index 1ab1bce..bb56bd1 100644
--- a/accounts/doctype/purchase_invoice/purchase_invoice.js
+++ b/accounts/doctype/purchase_invoice/purchase_invoice.js
@@ -193,7 +193,9 @@
 }
 
 cur_frm.fields_dict['entries'].grid.get_field("item_code").get_query = function(doc, cdt, cdn) {
-	return 'SELECT tabItem.name, tabItem.description FROM tabItem WHERE tabItem.is_purchase_item="Yes" AND (IFNULL(`tabItem`.`end_of_life`,"") = "" OR `tabItem`.`end_of_life` ="0000-00-00" OR `tabItem`.`end_of_life` > NOW()) AND tabItem.docstatus != 2 AND tabItem.%(key)s LIKE "%s" LIMIT 50'
+	return return erpnext.queries.item({
+		'ifnull(tabItem.is_purchase_item, "No")': 'Yes'
+	})
 }
 
 cur_frm.fields_dict['credit_to'].get_query = function(doc) {
diff --git a/buying/doctype/purchase_common/purchase_common.js b/buying/doctype/purchase_common/purchase_common.js
index 519905a..fa7c91f 100644
--- a/buying/doctype/purchase_common/purchase_common.js
+++ b/buying/doctype/purchase_common/purchase_common.js
@@ -167,9 +167,13 @@
 // Only Is Purchase Item = 'Yes' and Items not moved to trash are allowed.
 cur_frm.fields_dict[fname].grid.get_field("item_code").get_query = function(doc, cdt, cdn) {
 	if (doc.is_subcontracted =="Yes") {
-		return 'SELECT tabItem.name, tabItem.description FROM tabItem WHERE ifnull(tabItem.is_sub_contracted_item, "No")="Yes" AND (IFNULL(`tabItem`.`end_of_life`,"") = "" OR `tabItem`.`end_of_life` ="0000-00-00" OR `tabItem`.`end_of_life` > NOW()) AND tabItem.docstatus != 2 AND tabItem.%(key)s LIKE "%s" LIMIT 50'
+		return erpnext.queries.item({
+			'ifnull(tabItem.is_sub_contracted_item, "No")': 'Yes'
+		})
 	} else {
-		return 'SELECT tabItem.name, tabItem.description FROM tabItem WHERE ifnull(tabItem.is_purchase_item, "No")="Yes" AND (IFNULL(`tabItem`.`end_of_life`,"") = "" OR `tabItem`.`end_of_life` ="0000-00-00" OR `tabItem`.`end_of_life` > NOW()) AND tabItem.docstatus != 2 AND tabItem.%(key)s LIKE "%s" LIMIT 50'
+		return erpnext.queries.item({
+			'ifnull(tabItem.is_purchase_item, "No")': 'Yes'
+		})
 	}
 }
 
diff --git a/manufacturing/doctype/bom/bom.js b/manufacturing/doctype/bom/bom.js
index 179e63f..f2746de 100644
--- a/manufacturing/doctype/bom/bom.js
+++ b/manufacturing/doctype/bom/bom.js
@@ -174,10 +174,9 @@
 
 
 cur_frm.fields_dict['item'].get_query = function(doc) {
-	return 'SELECT DISTINCT `tabItem`.`name`, `tabItem`.description FROM `tabItem` \
-		WHERE is_manufactured_item = "Yes" and (IFNULL(`tabItem`.`end_of_life`,"") = "" OR \
-		 	`tabItem`.`end_of_life` = "0000-00-00" OR `tabItem`.`end_of_life` > NOW()) AND \
-		 	`tabItem`.`%(key)s` like "%s" ORDER BY `tabItem`.`name` LIMIT 50';
+	return return erpnext.queries.item({
+		'ifnull(tabItem.is_manufactured_item, "No")': 'Yes'
+	})
 }
 
 cur_frm.fields_dict['project_name'].get_query = function(doc, dt, dn) {
diff --git a/manufacturing/doctype/production_planning_tool/production_planning_tool.js b/manufacturing/doctype/production_planning_tool/production_planning_tool.js
index 5d9a936..95b84b2 100644
--- a/manufacturing/doctype/production_planning_tool/production_planning_tool.js
+++ b/manufacturing/doctype/production_planning_tool/production_planning_tool.js
@@ -45,12 +45,9 @@
 }
 
 cur_frm.fields_dict['pp_details'].grid.get_field('item_code').get_query = function(doc) {
-	return 'SELECT DISTINCT `tabItem`.`name`,`tabItem`.`item_name` \
-		FROM `tabItem` WHERE `tabItem`.is_pro_applicable = "Yes" \
-		AND (IFNULL(`tabItem`.`end_of_life`,"") = "" \
-			OR `tabItem`.`end_of_life`="0000-00-00" OR `tabItem`.`end_of_life` > NOW()) \
-		AND tabItem.%(key)s like "%s" \
-		ORDER BY `tabItem`.`name` LIMIT 50';
+ 	return erpnext.queries.item({
+		'ifnull(tabItem.is_pro_applicable, "No")': 'Yes'
+	});
 }
 
 cur_frm.fields_dict['pp_details'].grid.get_field('bom_no').get_query = function(doc) {
diff --git a/public/js/utils.js b/public/js/utils.js
index 3eaad08..aa65d1a 100644
--- a/public/js/utils.js
+++ b/public/js/utils.js
@@ -114,16 +114,33 @@
 
 wn.provide("erpnext.queries");
 
+erpnext.queries.get_conditions = function(doctype, opts) {
+	conditions = [];
+	if (opts) {
+		$.each(opts, function(key, val) {
+			var lhs = "`tab" + doctype + "`.`" + key + "`";
+			
+			if(key.indexOf(doctype)!=-1) {
+				// with function
+				lhs = key;
+			}
+			
+			if (esc_quotes(val).charAt(0) != "!")
+				conditions.push(lhs + "='"+esc_quotes(val)+"'");
+			else
+				conditions.push(lhs + "!='"+esc_quotes(val).substr(1)+"'");
+		});
+	}
+	return conditions;
+}
+
 erpnext.queries.account = function(opts) {
 	if(!opts) 
 		opts = {};
 	if(!opts.group_or_ledger) 
 		opts.group_or_ledger = "Ledger";
 		
-	conditions = [];
-	$.each(opts, function(key, val) {
-		conditions.push("tabAccount.`" + key + "`='"+esc_quotes(val)+"'");
-	});
+	var conditions = erpnext.queries.get_conditions("Account", opts);
 	
 	return 'SELECT tabAccount.name, tabAccount.parent_account, tabAccount.debit_or_credit \
 		FROM tabAccount \
@@ -131,18 +148,29 @@
 		AND tabAccount.%(key)s LIKE "%s" ' + (conditions 
 			? (" AND " + conditions.join(" AND "))
 			: "")
+		+ " LIMIT 50"
+}
+
+erpnext.queries.item = function(opts) {
+	var conditions = erpnext.queries.get_conditions("Item", opts);
+	
+	return 'SELECT tabItem.name, \
+		if(length(tabItem.item_name) > 40, \
+			concat(substr(tabItem.item_name, 1, 40), "..."), item_name) as item_name, \
+		if(length(tabItem.description) > 40, \
+			concat(substr(tabItem.description, 1, 40), "..."), description) as decription \
+		FROM tabItem \
+		WHERE tabItem.docstatus!=2 \
+		AND (ifnull(`tabItem`.`end_of_life`,"") in ("", "0000-00-00") \
+			OR `tabItem`.`end_of_life` > NOW()) \
+		AND tabItem.%(key)s LIKE "%s" ' + (conditions 
+			? (" AND " + conditions.join(" AND "))
+			: "")
+		+ " LIMIT 50"
 }
 
 erpnext.queries.bom = function(opts) {
-	conditions = [];
-	if (opts) {
-		$.each(opts, function(key, val) {
-			if (esc_quotes(val).charAt(0) != "!")
-				conditions.push("tabBOM.`" + key + "`='"+esc_quotes(val)+"'");
-			else
-				conditions.push("tabBOM.`" + key + "`!='"+esc_quotes(val).substr(1)+"'");
-		});
-	}
+	var conditions = erpnext.queries.get_conditions("BOM", opts);
 	
 	return 'SELECT tabBOM.name, tabBOM.item \
 		FROM tabBOM \
@@ -151,4 +179,6 @@
 		AND tabBOM.%(key)s LIKE "%s" ' + (conditions.length 
 			? (" AND " + conditions.join(" AND "))
 			: "")
+		+ " LIMIT 50"
+
 }
\ No newline at end of file
diff --git a/selling/doctype/opportunity/opportunity.js b/selling/doctype/opportunity/opportunity.js
index cfc546c..d5c665a 100644
--- a/selling/doctype/opportunity/opportunity.js
+++ b/selling/doctype/opportunity/opportunity.js
@@ -149,9 +149,13 @@
 //=======================================
 cur_frm.fields_dict['enquiry_details'].grid.get_field('item_code').get_query = function(doc, cdt, cdn) {
 	if (doc.enquiry_type == 'Maintenance')
-		return 'SELECT tabItem.name,tabItem.item_name,tabItem.description FROM tabItem WHERE tabItem.is_service_item="Yes" AND (ifnull(`tabItem`.`end_of_life`,"") = "" OR `tabItem`.`end_of_life` > NOW() OR `tabItem`.`end_of_life`="0000-00-00") AND tabItem.%(key)s LIKE "%s" LIMIT 50';
+	 	return erpnext.queries.item({
+			'ifnull(tabItem.is_service_item, "No")': 'Yes'
+		});
 	else 
-		return 'SELECT tabItem.name,tabItem.item_name,tabItem.description FROM tabItem WHERE tabItem.is_sales_item="Yes" AND (ifnull(`tabItem`.`end_of_life`,"") = "" OR `tabItem`.`end_of_life` > NOW() OR `tabItem`.`end_of_life`="0000-00-00") AND tabItem.%(key)s LIKE "%s" LIMIT 50';
+ 		return erpnext.queries.item({
+			'ifnull(tabItem.is_sales_item, "No")': 'Yes'
+		});
 }
 
 // Create New Quotation
diff --git a/selling/doctype/sales_common/sales_common.js b/selling/doctype/sales_common/sales_common.js
index de56a6c..c1435bd 100644
--- a/selling/doctype/sales_common/sales_common.js
+++ b/selling/doctype/sales_common/sales_common.js
@@ -249,21 +249,15 @@
 
 // ******************** ITEM CODE ******************************** 
 cur_frm.fields_dict[cur_frm.cscript.fname].grid.get_field("item_code").get_query = function(doc, cdt, cdn) {
-	if (inList(['Maintenance', 'Service'], doc.order_type))
-		return 'SELECT tabItem.name,tabItem.item_name,tabItem.description \
-			FROM tabItem WHERE tabItem.is_service_item="Yes" \
-			AND tabItem.docstatus != 2 \
-			AND (ifnull(`tabItem`.`end_of_life`,"") = "" \
-				OR `tabItem`.`end_of_life` > NOW() \
-				OR `tabItem`.`end_of_life`="0000-00-00") \
-			AND tabItem.%(key)s LIKE "%s" LIMIT 50';
-	else 
-		return 'SELECT tabItem.name,tabItem.item_name,tabItem.description FROM tabItem \
-			WHERE tabItem.is_sales_item="Yes" AND tabItem.docstatus != 2 \
-			AND (ifnull(`tabItem`.`end_of_life`,"") = "" \
-				OR `tabItem`.`end_of_life` > NOW() \
-				OR `tabItem`.`end_of_life`="0000-00-00") \
-			AND tabItem.%(key)s LIKE "%s" LIMIT 50';
+	if (inList(['Maintenance', 'Service'], doc.order_type)) {
+	 	return erpnext.queries.item({
+			'ifnull(tabItem.is_service_item, "No")': 'Yes'
+		});
+	} else {
+		return erpnext.queries.item({
+			'ifnull(tabItem.is_sales_item, "No")': 'Yes'
+		});
+	}
 }
 
 
diff --git a/stock/doctype/packing_slip/packing_slip.js b/stock/doctype/packing_slip/packing_slip.js
index 74f800d..a332ca8 100644
--- a/stock/doctype/packing_slip/packing_slip.js
+++ b/stock/doctype/packing_slip/packing_slip.js
@@ -20,7 +20,7 @@
 
 
 cur_frm.fields_dict['item_details'].grid.get_field('item_code').get_query = function(doc, cdt, cdn) {
-	var query = 'SELECT name, description FROM `tabItem` WHERE name IN ( \
+	var query = 'SELECT name, item_name, description FROM `tabItem` WHERE name IN ( \
 		SELECT item_code FROM `tabDelivery Note Item` dnd \
 		WHERE parent="'	+ doc.delivery_note + '" AND IFNULL(qty, 0) > IFNULL(packed_qty, 0)) AND %(key)s LIKE "%s" LIMIT 50';
 	return query;
diff --git a/stock/doctype/serial_no/serial_no.js b/stock/doctype/serial_no/serial_no.js
index 49e88e2..dad50d3 100644
--- a/stock/doctype/serial_no/serial_no.js
+++ b/stock/doctype/serial_no/serial_no.js
@@ -14,14 +14,12 @@
 // You should have received a copy of the GNU General Public License
 // along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
-// ************************************** onload ****************************************************
 cur_frm.cscript.onload = function(doc, cdt, cdn) {
   if(!doc.status) set_multiple(cdt, cdn, {status:'In Store'});
   if(doc.__islocal) hide_field(['supplier_name','address_display'])
 }
 
 
-// ************************************** refresh ***************************************************
 cur_frm.cscript.refresh = function(doc, cdt, cdn) {
   if(!doc.__islocal) {
     flds = ['item_code', 'warehouse', 'purchase_document_type', 'purchase_document_no', 'purchase_date', 'purchase_time', 'purchase_rate', 'supplier']
@@ -31,7 +29,6 @@
 }
 
 
-// ************************************** triggers **************************************************
 
 // item details
 // -------------
@@ -63,10 +60,9 @@
 //item code
 //----------
 cur_frm.fields_dict['item_code'].get_query = function(doc,cdt,cdn) {
-  return 'SELECT `tabItem`.`name`,`tabItem`.`description` FROM `tabItem` \
-	WHERE `tabItem`.`docstatus`!= 2 AND ifnull(`tabItem`.`has_serial_no`, "No") = "Yes" \
-	AND (ifnull(`tabItem`.`end_of_life`,"") = "" OR `tabItem`.`end_of_life` > NOW() OR `tabItem`.`end_of_life`="0000-00-00") \
-	AND `tabItem`.%(key)s LIKE "%s"  ORDER BY  `tabItem`.`name` ASC LIMIT 50';
+ 	return erpnext.queries.item({
+		'ifnull(tabItem.has_serial_no, "No")': 'Yes'
+	});
 }
 
 cur_frm.fields_dict.customer.get_query = erpnext.utils.customer_query;
diff --git a/stock/doctype/stock_uom_replace_utility/stock_uom_replace_utility.js b/stock/doctype/stock_uom_replace_utility/stock_uom_replace_utility.js
index b71f94b..cb88311 100644
--- a/stock/doctype/stock_uom_replace_utility/stock_uom_replace_utility.js
+++ b/stock/doctype/stock_uom_replace_utility/stock_uom_replace_utility.js
@@ -15,7 +15,7 @@
 // along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
 cur_frm.fields_dict['item_code'].get_query = function(doc) {
-  return 'SELECT DISTINCT `tabItem`.`name`, `tabItem`.description FROM `tabItem` WHERE (IFNULL(`tabItem`.`end_of_life`,"") = "" OR `tabItem`.`end_of_life` ="0000-00-00" OR `tabItem`.`end_of_life` > NOW()) AND `tabItem`.`%(key)s` like "%s" ORDER BY `tabItem`.`name` LIMIT 50';
+ 	return erpnext.queries.item();	
 }
 
 //==================== Get Items Stock UOM =====================================================