Merge pull request #3446 from anandpdoshi/anand-june-9

POS - search by Item Group
diff --git a/erpnext/accounts/doctype/sales_invoice/pos.py b/erpnext/accounts/doctype/sales_invoice/pos.py
index 6047378..7d5613d 100644
--- a/erpnext/accounts/doctype/sales_invoice/pos.py
+++ b/erpnext/accounts/doctype/sales_invoice/pos.py
@@ -7,6 +7,7 @@
 @frappe.whitelist()
 def get_items(price_list, sales_or_purchase, item=None):
 	condition = ""
+	order_by = ""
 	args = {"price_list": price_list}
 
 	if sales_or_purchase == "Sales":
@@ -30,16 +31,25 @@
 			item_code[0]["barcode"] = item
 			return item_code
 
-		condition += " and (CONCAT(i.name, i.item_name) like %(name)s or (i.variant_of like %(name)s))"
+		condition += " and ((CONCAT(i.name, i.item_name) like %(name)s) or (i.variant_of like %(name)s) or (i.item_group like %(name)s))"
+		order_by = """if(locate(%(_name)s, i.name), locate(%(_name)s, i.name), 99999),
+			if(locate(%(_name)s, i.item_name), locate(%(_name)s, i.item_name), 99999),
+			if(locate(%(_name)s, i.variant_of), locate(%(_name)s, i.variant_of), 99999),
+			if(locate(%(_name)s, i.item_group), locate(%(_name)s, i.item_group), 99999),"""
 		args["name"] = "%%%s%%" % item
+		args["_name"] = item.replace("%", "")
 
+	# locate function is used to sort by closest match from the beginning of the value
 	return frappe.db.sql("""select i.name, i.item_name, i.image,
 		item_det.price_list_rate, item_det.currency
 		from `tabItem` i LEFT JOIN
 			(select item_code, price_list_rate, currency from
-				`tabItem Price`	where price_list=%s) item_det
+				`tabItem Price`	where price_list=%(price_list)s) item_det
 		ON
 			(item_det.item_code=i.name or item_det.item_code=i.variant_of)
 		where
 			ifnull(i.has_variants, 0) = 0 and
-			%s""" % ('%(price_list)s', condition), args, as_dict=1)
+			{condition}
+		order by
+			{order_by}
+			i.name""".format(condition=condition, order_by=order_by), args, as_dict=1)