Merge branch 'pratu16x7-domainify' into domainify (#9324)

* bootstrap domains and set active_domain on setup

* [minor] Patch for domain docs

* [minor]

* Update create_domain_docs.py
diff --git a/erpnext/patches.txt b/erpnext/patches.txt
index 738ed9e..6ff4c31 100644
--- a/erpnext/patches.txt
+++ b/erpnext/patches.txt
@@ -402,4 +402,5 @@
 erpnext.patches.v8_0.rename_items_in_status_field_of_material_request
 erpnext.patches.v8_0.delete_bin_indexes
 erpnext.patches.v8_0.move_account_head_from_account_to_warehouse_for_inventory
-erpnext.patches.v8_0.change_in_words_varchar_length
\ No newline at end of file
+erpnext.patches.v8_0.change_in_words_varchar_length
+erpnext.patches.v8_0.create_domain_docs
\ No newline at end of file
diff --git a/erpnext/patches/v8_0/create_domain_docs.py b/erpnext/patches/v8_0/create_domain_docs.py
new file mode 100644
index 0000000..1fa3b29
--- /dev/null
+++ b/erpnext/patches/v8_0/create_domain_docs.py
@@ -0,0 +1,41 @@
+# Copyright (c) 2013, Web Notes Technologies Pvt. Ltd. and Contributors
+# License: GNU General Public License v3. See license.txt
+
+from __future__ import unicode_literals
+import frappe
+import erpnext
+
+def execute():
+	"""Create domain documents"""
+
+	for domain in ("Distribution", "Manufacturing", "Retail", "Services", "Education"):
+		if not frappe.db.exists({'doctype': 'Domain', 'domain': domain}):
+			doc = frappe.new_doc("Domain")
+			doc.domain = domain
+			doc.save()
+
+
+	# set domain in domain settings based on company domain
+
+	domains = []
+	condition = ""
+	company = erpnext.get_default_company()
+	if company:
+		condition = " where name='{0}'".format(company)
+
+	domains = frappe.db.sql_list("select distinct domain from `tabCompany` {0}".format(condition))
+
+	if not domains:
+		return
+
+	domain_settings = frappe.get_doc("Domain Settings", "Domain Settings")
+	checked_domains = [row.domain for row in domain_settings.active_domains]
+
+	for domain in domains:
+		# check and ignore if the domains is already checked in domain settings
+		if domain in checked_domains:
+			continue
+
+		row = domain_settings.append("active_domains", dict(domain=args.domain))
+
+	domain_settings.save(ignore_permissions=True)
\ No newline at end of file
diff --git a/erpnext/setup/setup_wizard/domainify.py b/erpnext/setup/setup_wizard/domainify.py
index 0069e88..f0aaf02 100644
--- a/erpnext/setup/setup_wizard/domainify.py
+++ b/erpnext/setup/setup_wizard/domainify.py
@@ -105,8 +105,9 @@
 
 	if data.allow_roles:
 		# remove all roles other than allowed roles
+		active_domains = frappe.get_active_domains()
 		data.allow_roles += ['Administrator', 'Guest', 'System Manager', 'All']
-		for role in frappe.get_all('Role'):
+		for role in frappe.get_all('Role', filters = {"restrict_to_domain": ("not in", active_domains)}):
 			if not (role.name in data.allow_roles):
 				remove_role(role.name)
 
diff --git a/erpnext/setup/setup_wizard/install_fixtures.py b/erpnext/setup/setup_wizard/install_fixtures.py
index ef276a0..43baf2f 100644
--- a/erpnext/setup/setup_wizard/install_fixtures.py
+++ b/erpnext/setup/setup_wizard/install_fixtures.py
@@ -13,6 +13,13 @@
 
 def install(country=None):
 	records = [
+		# domains
+		{ 'doctype': 'Domain', 'domain': _('Distribution')},
+		{ 'doctype': 'Domain', 'domain': _('Manufacturing')},
+		{ 'doctype': 'Domain', 'domain': _('Retail')},
+		{ 'doctype': 'Domain', 'domain': _('Services')},
+		{ 'doctype': 'Domain', 'domain': _('Education')},
+
 		# address template
 		{'doctype':"Address Template", "country": country},
 
@@ -35,7 +42,7 @@
 		{'doctype': 'Salary Component', 'salary_component': _('Basic'), 'description': _('Basic'), 'type': 'Earning'},
 		{'doctype': 'Salary Component', 'salary_component': _('Arrear'), 'description': _('Arrear'), 'type': 'Earning'},
 		{'doctype': 'Salary Component', 'salary_component': _('Leave Encashment'), 'description': _('Leave Encashment'), 'type': 'Earning'},
-		
+
 
 		# expense claim type
 		{'doctype': 'Expense Claim Type', 'name': _('Calls'), 'expense_type': _('Calls')},
@@ -197,7 +204,7 @@
 		# Assessment Group
 		{'doctype': 'Assessment Group', 'assessment_group_name': _('All Assessment Groups'),
 			'is_group': 1, 'parent_assessment_group': ''},
-	
+
 	]
 
 	from erpnext.setup.setup_wizard.industry_type import get_industry_types
diff --git a/erpnext/setup/setup_wizard/setup_wizard.py b/erpnext/setup/setup_wizard/setup_wizard.py
index 940d35c..93e879d 100644
--- a/erpnext/setup/setup_wizard/setup_wizard.py
+++ b/erpnext/setup/setup_wizard/setup_wizard.py
@@ -198,6 +198,10 @@
 	hr_settings.emp_created_by = "Naming Series"
 	hr_settings.save()
 
+	domain_settings = frappe.get_doc("Domain Settings")
+	domain_settings.append('active_domain', dict(domain=args.domain))
+	domain_settings.save()
+
 def create_feed_and_todo():
 	"""update Activity feed and create todo for creation of item, customer, vendor"""
 	add_info_comment(**{