Merge pull request #4190 from nabinhait/income_expense_account_query

[fix] Allowed accounts with account type='Income Account' for default income account in item
diff --git a/erpnext/accounts/doctype/sales_invoice/sales_invoice.js b/erpnext/accounts/doctype/sales_invoice/sales_invoice.js
index 7ff0a36..9d620c2 100644
--- a/erpnext/accounts/doctype/sales_invoice/sales_invoice.js
+++ b/erpnext/accounts/doctype/sales_invoice/sales_invoice.js
@@ -376,7 +376,7 @@
 // --------------------------------
 cur_frm.set_query("income_account", "items", function(doc) {
 	return{
-		query: "erpnext.accounts.doctype.sales_invoice.sales_invoice.get_income_account",
+		query: "erpnext.controllers.queries.get_income_account",
 		filters: {'company': doc.company}
 	}
 });
diff --git a/erpnext/accounts/doctype/sales_invoice/sales_invoice.py b/erpnext/accounts/doctype/sales_invoice/sales_invoice.py
index a349e2f..a0b0c4e 100644
--- a/erpnext/accounts/doctype/sales_invoice/sales_invoice.py
+++ b/erpnext/accounts/doctype/sales_invoice/sales_invoice.py
@@ -637,24 +637,6 @@
 		"account": account
 	}
 
-
-@frappe.whitelist()
-def get_income_account(doctype, txt, searchfield, start, page_len, filters):
-	from erpnext.controllers.queries import get_match_cond
-
-	# income account can be any Credit account,
-	# but can also be a Asset account with account_type='Income Account' in special circumstances.
-	# Hence the first condition is an "OR"
-	return frappe.db.sql("""select tabAccount.name from `tabAccount`
-			where (tabAccount.report_type = "Profit and Loss"
-					or tabAccount.account_type in ("Income Account", "Temporary"))
-				and tabAccount.is_group=0
-				and tabAccount.docstatus!=2
-				and tabAccount.company = '%(company)s'
-				and tabAccount.%(key)s LIKE '%(txt)s'
-				%(mcond)s""" % {'company': filters['company'], 'key': searchfield,
-			'txt': "%%%s%%" % frappe.db.escape(txt), 'mcond':get_match_cond(doctype)})
-
 @frappe.whitelist()
 def make_delivery_note(source_name, target_doc=None):
 	def set_missing_values(source, target):
diff --git a/erpnext/controllers/queries.py b/erpnext/controllers/queries.py
index a6bfa53..e2de6c3 100644
--- a/erpnext/controllers/queries.py
+++ b/erpnext/controllers/queries.py
@@ -293,3 +293,27 @@
 		fields = ["name", "parent_account"],
 		limit_start=start, limit_page_length=page_len, as_list=True)
 
+
+@frappe.whitelist()
+def get_income_account(doctype, txt, searchfield, start, page_len, filters):
+	from erpnext.controllers.queries import get_match_cond
+
+	# income account can be any Credit account,
+	# but can also be a Asset account with account_type='Income Account' in special circumstances.
+	# Hence the first condition is an "OR"
+	if not filters: filters = {}
+
+	condition = ""	
+	if filters.get("company"):
+		condition += "and tabAccount.company = %(company)s"
+	
+	return frappe.db.sql("""select tabAccount.name from `tabAccount`
+			where (tabAccount.report_type = "Profit and Loss"
+					or tabAccount.account_type in ("Income Account", "Temporary"))
+				and tabAccount.is_group=0
+				and tabAccount.`{key}` LIKE %(txt)s
+				{condition} {match_condition}"""
+			.format(condition=condition, match_condition=get_match_cond(doctype), key=searchfield), {
+				'txt': "%%%s%%" % frappe.db.escape(txt), 
+				'company': filters.get("company", "")
+			})
\ No newline at end of file
diff --git a/erpnext/stock/doctype/item/item.js b/erpnext/stock/doctype/item/item.js
index 097dd04..93f9266 100644
--- a/erpnext/stock/doctype/item/item.js
+++ b/erpnext/stock/doctype/item/item.js
@@ -116,11 +116,7 @@
 		// --------------------------------
 		frm.fields_dict['income_account'].get_query = function(doc) {
 			return {
-				filters: {
-					"report_type": "Profit and Loss",
-					"is_group": 0,
-					'account_type': "Income Account"
-				}
+				query: "erpnext.controllers.queries.get_income_account"
 			}
 		}