Merge branch 'develop'
diff --git a/erpnext/__version__.py b/erpnext/__version__.py
index dd1a9e5..e5e5fc1 100644
--- a/erpnext/__version__.py
+++ b/erpnext/__version__.py
@@ -1,2 +1,2 @@
 from __future__ import unicode_literals
-__version__ = '5.8.0'
+__version__ = '5.8.1'
diff --git a/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.js b/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.js
index 6257865..70ebee1 100644
--- a/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.js
+++ b/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.js
@@ -148,12 +148,22 @@
 }
 
 cur_frm.fields_dict['credit_to'].get_query = function(doc) {
-	return{
-		filters:{
-			'account_type': 'Payable',
-			'root_type': 'Liability',
-			'is_group': 0,
-			'company': doc.company
+	// filter on Account
+	if (doc.supplier) {
+		return {
+			filters: {
+				'account_type': 'Payable',
+				'is_group': 0,
+				'company': doc.company
+			}
+		}
+	} else {
+		return {
+			filters: {
+				'report_type': 'Balance Sheet',
+				'is_group': 0,
+				'company': doc.company
+			}
 		}
 	}
 }
diff --git a/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.py b/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.py
index 2208937..a43e553 100644
--- a/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.py
+++ b/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.py
@@ -91,8 +91,12 @@
 			throw(_("Conversion rate cannot be 0 or 1"))
 
 	def validate_credit_to_acc(self):
-		account_type = frappe.db.get_value("Account", self.credit_to, "account_type")
-		if account_type != "Payable":
+		account = frappe.db.get_value("Account", self.credit_to, ["account_type", "report_type"], as_dict=True)
+
+		if account.report_type != "Balance Sheet":
+			frappe.throw(_("Credit To account must be a Balance Sheet account"))
+
+		if self.supplier and account.account_type != "Payable":
 			frappe.throw(_("Credit To account must be a Payable account"))
 
 	def check_for_stopped_status(self):
diff --git a/erpnext/accounts/doctype/sales_invoice/sales_invoice.js b/erpnext/accounts/doctype/sales_invoice/sales_invoice.js
index 5e59078..cb211c5 100644
--- a/erpnext/accounts/doctype/sales_invoice/sales_invoice.js
+++ b/erpnext/accounts/doctype/sales_invoice/sales_invoice.js
@@ -405,10 +405,22 @@
 }
 
 cur_frm.set_query("debit_to", function(doc) {
-	return{
-		filters: [
-			['Account', 'root_type', '=', 'Asset'],
-			['Account', 'account_type', '=', 'Receivable']
-		]
+	// filter on Account
+	if (doc.customer) {
+		return {
+			filters: {
+				'account_type': 'Receivable',
+				'is_group': 0,
+				'company': doc.company
+			}
+		}
+	} else {
+		return {
+			filters: {
+				'report_type': 'Balance Sheet',
+				'is_group': 0,
+				'company': doc.company
+			}
+		}
 	}
 });
diff --git a/erpnext/accounts/doctype/sales_invoice/sales_invoice.py b/erpnext/accounts/doctype/sales_invoice/sales_invoice.py
index 1274016..92e3c0d 100644
--- a/erpnext/accounts/doctype/sales_invoice/sales_invoice.py
+++ b/erpnext/accounts/doctype/sales_invoice/sales_invoice.py
@@ -271,8 +271,12 @@
 			reconcile_against_document(lst)
 
 	def validate_debit_to_acc(self):
-		account_type = frappe.db.get_value("Account", self.debit_to, "account_type")
-		if account_type != "Receivable":
+		account = frappe.db.get_value("Account", self.debit_to, ["account_type", "report_type"], as_dict=True)
+
+		if account.report_type != "Balance Sheet":
+			frappe.throw(_("Debit To account must be a Balance Sheet account"))
+
+		if self.customer and account.account_type != "Receivable":
 			frappe.throw(_("Debit To account must be a Receivable account"))
 
 	def validate_fixed_asset_account(self):
diff --git a/erpnext/hooks.py b/erpnext/hooks.py
index ff6e4e6..9ee5bc0 100644
--- a/erpnext/hooks.py
+++ b/erpnext/hooks.py
@@ -27,7 +27,7 @@
 """
 app_icon = "icon-th"
 app_color = "#e74c3c"
-app_version = "5.8.0"
+app_version = "5.8.1"
 github_link = "https://github.com/frappe/erpnext"
 
 error_report_email = "support@erpnext.com"
diff --git a/erpnext/patches/v5_8/update_order_reference_in_return_entries.py b/erpnext/patches/v5_8/update_order_reference_in_return_entries.py
index c6cfceb..a875aed 100644
--- a/erpnext/patches/v5_8/update_order_reference_in_return_entries.py
+++ b/erpnext/patches/v5_8/update_order_reference_in_return_entries.py
@@ -5,6 +5,9 @@
 import frappe
 
 def execute():
+	frappe.reload_doctype("Sales Order Item")
+	frappe.reload_doctype("Purchase Order Item")
+
 	# sales return
 	return_entries = list(frappe.db.sql("""
 		select dn.name as name, dn_item.name as row_id, dn.return_against,
diff --git a/setup.py b/setup.py
index 21b5940..0389555 100644
--- a/setup.py
+++ b/setup.py
@@ -1,6 +1,6 @@
 from setuptools import setup, find_packages
 
-version = "5.8.0"
+version = "5.8.1"
 
 with open("requirements.txt", "r") as f:
 	install_requires = f.readlines()