Merge pull request #5572 from saurabh6790/warehouse_tree_fix

[fix] warehouse tree patch fix
diff --git a/erpnext/accounts/doctype/account/account_tree.js b/erpnext/accounts/doctype/account/account_tree.js
index 3252788..b24a17d 100644
--- a/erpnext/accounts/doctype/account/account_tree.js
+++ b/erpnext/accounts/doctype/account/account_tree.js
@@ -27,13 +27,13 @@
 		{fieldtype:'Select', fieldname:'root_type', label:__('Root Type'),
 			options: ['Asset', 'Liability', 'Equity', 'Income', 'Expense'].join('\n')},
 		{fieldtype:'Select', fieldname:'account_type', label:__('Account Type'),
-			options: ['', 'Bank', 'Cash', 'Warehouse', 'Tax', 'Chargeable'].join('\n'),
+			options: ['', 'Bank', 'Cash', 'Stock', 'Tax', 'Chargeable'].join('\n'),
 			description: __("Optional. This setting will be used to filter in various transactions."),
 			depends_on: 'eval:doc.is_group==1'},
 		{fieldtype:'Float', fieldname:'tax_rate', label:__('Tax Rate'),
 			depends_on: 'eval:doc.is_group==1&&doc.account_type=="Tax"'},
 		{fieldtype:'Link', fieldname:'warehouse', label:__('Warehouse'), options:"Warehouse",
-			depends_on: 'eval:(doc.is_group==1&&doc.account_type=="Warehouse")'},
+			depends_on: 'eval:(!doc.is_group&&doc.account_type=="Warehouse")'},
 		{fieldtype:'Link', fieldname:'account_currency', label:__('Currency'), options:"Currency",
 			description: __("Optional. Sets company's default currency, if not specified.")}
 	],
diff --git a/erpnext/patches/v7_0/create_warehouse_nestedset.py b/erpnext/patches/v7_0/create_warehouse_nestedset.py
index aaccfb9..71a22fd 100644
--- a/erpnext/patches/v7_0/create_warehouse_nestedset.py
+++ b/erpnext/patches/v7_0/create_warehouse_nestedset.py
@@ -1,42 +1,35 @@
 import frappe
 from frappe import _
+from frappe.utils import cint
+from frappe.utils.nestedset import rebuild_tree
 
 def execute():
 	frappe.reload_doc("stock", "doctype", "warehouse")
-	
+
 	for company in frappe.get_all("Company", fields=["name", "abbr"]):
+		validate_parent_account_for_warehouse(company)
+		
 		if not frappe.db.get_value("Warehouse", "{0} - {1}".format(_("All Warehouses"), company.abbr)):
 			create_default_warehouse_group(company)
 		
-		for warehouse in frappe.get_all("Warehouse", filters={"company": company.name}, fields=["name", "create_account_under",
-			"parent_warehouse", "is_group"]):
-			set_parent_to_warehouses(warehouse, company)
-			set_parent_to_warehouse_acounts(warehouse, company)
+		set_parent_to_warehouse(company)
+		if cint(frappe.defaults.get_global_default("auto_accounting_for_stock")):
+			set_parent_to_warehouse_acount(company)
 
-def set_parent_to_warehouses(warehouse, company):
-	warehouse = frappe.get_doc("Warehouse", warehouse.name)
-	warehouse.is_group = warehouse.is_group
+def set_parent_to_warehouse(company):
+	frappe.db.sql(""" update tabWarehouse set parent_warehouse = %s
+		where (is_group = 0 or is_group is null or is_group = '') and company = %s
+		""",("{0} - {1}".format(_("All Warehouses"), company.abbr), company.name))
 	
-	if not warehouse.parent_warehouse and warehouse.name != "{0} - {1}".format(_("All Warehouses"), company.abbr):
-		warehouse.parent_warehouse = "{0} - {1}".format(_("All Warehouses"), company.abbr)
+	rebuild_tree("Warehouse", "parent_warehouse")
+
+def set_parent_to_warehouse_acount(company):
+	frappe.db.sql(""" update tabAccount set parent_account = %s
+		where is_group = 0 and account_type = "Warehouse"
+		and (warehouse is not null or warehouse != '') and company = %s
+		""",("{0} - {1}".format(_("All Warehouses"), company.abbr), company.name))
 	
-	warehouse.save(ignore_permissions=True)
-
-def set_parent_to_warehouse_acounts(warehouse, company):
-	account = frappe.db.get_value("Account", {"warehouse": warehouse.name})
-	stock_group = frappe.db.get_value("Account", {"account_type": "Stock",
-		"is_group": 1, "company": company.name})
-
-	if account and account != "{0} - {1}".format(_("All Warehouses"), company.abbr):
-		account = frappe.get_doc("Account", account)
-		
-		if warehouse.create_account_under == stock_group or not warehouse.create_account_under:
-			if not warehouse.parent_warehouse:
-				account.parent_account = "{0} - {1}".format(_("All Warehouses"), company.abbr)
-			else:
-				account.parent_account = frappe.db.get_value("Account", warehouse.parent_warehouse)
-
-		account.save(ignore_permissions=True)
+	rebuild_tree("Account", "parent_account")
 
 def create_default_warehouse_group(company):
 	frappe.get_doc({
@@ -45,4 +38,18 @@
 		"is_group": 1,
 		"company": company.name,
 		"parent_warehouse": ""
-	}).insert(ignore_permissions=True)
\ No newline at end of file
+	}).insert(ignore_permissions=True)
+	
+def validate_parent_account_for_warehouse(company):
+	if cint(frappe.defaults.get_global_default("auto_accounting_for_stock")):
+
+		parent_account = frappe.db.sql("""select name from tabAccount
+			where account_type='Stock' and company=%s and is_group=1
+			and (warehouse is null or warehouse = '')""", company.name)
+
+		if not parent_account:
+			current_parent_accounts_for_warehouse = frappe.db.sql("""select parent_account from tabAccount
+				where account_type='Warehouse' and (warehouse is not null or warehouse != '') """)
+
+			if current_parent_accounts_for_warehouse:
+				frappe.db.set_value("Account", current_parent_accounts_for_warehouse[0][0], "account_type", "Stock")
diff --git a/erpnext/stock/doctype/warehouse/warehouse.py b/erpnext/stock/doctype/warehouse/warehouse.py
index 3b50f30..515d260 100644
--- a/erpnext/stock/doctype/warehouse/warehouse.py
+++ b/erpnext/stock/doctype/warehouse/warehouse.py
@@ -113,7 +113,7 @@
 		if warehouse_account:
 			frappe.delete_doc("Account", warehouse_account)
 
-		if self.check_sle_exists():
+		if self.check_if_sle_exists():
 			throw(_("Warehouse can not be deleted as stock ledger entry exists for this warehouse."))
 
 		if self.check_if_child_exists():