fix: added validation to if account is being added to child company
diff --git a/erpnext/accounts/doctype/account/account.py b/erpnext/accounts/doctype/account/account.py
index 5d504b9..d699146 100644
--- a/erpnext/accounts/doctype/account/account.py
+++ b/erpnext/accounts/doctype/account/account.py
@@ -34,6 +34,7 @@
 			return
 		self.validate_parent()
 		self.validate_root_details()
+		self.validate_root_company()
 		validate_field_number("Account", self.name, self.account_number, self.company, "account_number")
 		self.validate_group_or_ledger()
 		self.set_root_and_report_type()
@@ -90,6 +91,13 @@
 		if not self.parent_account and not self.is_group:
 			frappe.throw(_("Root Account must be a group"))
 
+	def validate_root_company(self):
+		# fetch all ancestors in top-down hierarchy
+		if frappe.local.flags.ignore_root_company_validation: return
+		ancestors = get_root_company(self.company)
+		if ancestors:
+			frappe.throw(_("Please add the account to root level Company - %s" % ancestors[0]))
+
 	def validate_group_or_ledger(self):
 		if self.get("__islocal"):
 			return
@@ -250,3 +258,9 @@
 	frappe.rename_doc("Account", old, new, merge=1, ignore_permissions=1)
 
 	return new
+
+@frappe.whitelist()
+def get_root_company(company):
+	# return the topmost company in the hierarchy
+	ancestors = frappe.utils.nestedset.get_ancestors_of('Company', company, "lft asc")
+	return [ancestors[0]] if ancestors else []
diff --git a/erpnext/setup/doctype/company/company.py b/erpnext/setup/doctype/company/company.py
index c49c264..33361e8 100644
--- a/erpnext/setup/doctype/company/company.py
+++ b/erpnext/setup/doctype/company/company.py
@@ -141,6 +141,7 @@
 
 	def create_default_accounts(self):
 		from erpnext.accounts.doctype.account.chart_of_accounts.chart_of_accounts import create_charts
+		frappe.local.flags.ignore_root_company_validation = True
 		create_charts(self.name, self.chart_of_accounts, self.existing_company)
 
 		frappe.db.set(self, "default_receivable_account", frappe.db.get_value("Account",