blob: 91c96973096a75e201aaf17c3843298c7622f880 [file] [log] [blame]
Rushabh Mehta2dd05812016-05-11 15:13:44 +05301# Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors
2# See license.txt
3
4from __future__ import unicode_literals
5import frappe
Makarand Bauskar6e30f042017-07-18 12:30:57 +05306from frappe import _
Rushabh Mehta2dd05812016-05-11 15:13:44 +05307
Rushabh Mehta20038ad2016-07-21 16:01:59 +05308def get_domain(domain):
Anand Doshi0e4119b2016-06-28 21:37:28 +05309 '''Written as a function to prevent data mutation effects'''
Rushabh Mehta20038ad2016-07-21 16:01:59 +053010 data = {
Anand Doshi0e4119b2016-06-28 21:37:28 +053011 'Manufacturing': {
12 'desktop_icons': ['Item', 'BOM', 'Customer', 'Supplier', 'Sales Order',
13 'Production Order', 'Stock Entry', 'Purchase Order', 'Task', 'Buying', 'Selling',
14 'Accounts', 'HR', 'ToDo'],
mbauskare94c6e72017-04-29 15:41:34 +053015 'remove_roles': ['Academics User', 'Instructor'],
Anand Doshi0e4119b2016-06-28 21:37:28 +053016 'properties': [
17 {'doctype': 'Item', 'fieldname': 'manufacturing', 'property': 'collapsible_depends_on', 'value': 'is_stock_item'},
18 ],
19 'set_value': [
20 ['Stock Settings', None, 'show_barcode_field', 1]
Rushabh Mehta20038ad2016-07-21 16:01:59 +053021 ],
Rushabh Mehtafc800b32016-09-23 21:51:51 +053022 'default_portal_role': 'Customer'
Anand Doshi0e4119b2016-06-28 21:37:28 +053023 },
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +053024
Anand Doshi0e4119b2016-06-28 21:37:28 +053025 'Retail': {
Rushabh Mehta868bb262017-03-09 18:11:11 +053026 'desktop_icons': ['POS', 'Item', 'Customer', 'Sales Invoice', 'Purchase Order',
27 'Warranty Claim', 'Accounts', 'Task', 'Buying', 'ToDo'],
mbauskare94c6e72017-04-29 15:41:34 +053028 'remove_roles': ['Manufacturing User', 'Manufacturing Manager', 'Academics User', 'Instructor'],
Anand Doshi0e4119b2016-06-28 21:37:28 +053029 'properties': [
30 {'doctype': 'Item', 'fieldname': 'manufacturing', 'property': 'hidden', 'value': 1},
31 {'doctype': 'Customer', 'fieldname': 'credit_limit_section', 'property': 'hidden', 'value': 1},
32 ],
33 'set_value': [
34 ['Stock Settings', None, 'show_barcode_field', 1]
Rushabh Mehta20038ad2016-07-21 16:01:59 +053035 ],
Rushabh Mehtafc800b32016-09-23 21:51:51 +053036 'default_portal_role': 'Customer'
Anand Doshi0e4119b2016-06-28 21:37:28 +053037 },
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +053038
Anand Doshi0e4119b2016-06-28 21:37:28 +053039 'Distribution': {
Rushabh Mehta868bb262017-03-09 18:11:11 +053040 'desktop_icons': ['Item', 'Customer', 'Supplier', 'Lead', 'Sales Order', 'Task',
Anand Doshi0e4119b2016-06-28 21:37:28 +053041 'Sales Invoice', 'CRM', 'Selling', 'Buying', 'Stock', 'Accounts', 'HR', 'ToDo'],
mbauskare94c6e72017-04-29 15:41:34 +053042 'remove_roles': ['Manufacturing User', 'Manufacturing Manager', 'Academics User', 'Instructor'],
Anand Doshi0e4119b2016-06-28 21:37:28 +053043 'set_value': [
44 ['Stock Settings', None, 'show_barcode_field', 1]
Rushabh Mehta20038ad2016-07-21 16:01:59 +053045 ],
Rushabh Mehtafc800b32016-09-23 21:51:51 +053046 'default_portal_role': 'Customer'
Anand Doshi0e4119b2016-06-28 21:37:28 +053047 },
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +053048
Anand Doshi0e4119b2016-06-28 21:37:28 +053049 'Services': {
Rushabh Mehta868bb262017-03-09 18:11:11 +053050 'desktop_icons': ['Project', 'Timesheet', 'Customer', 'Sales Order', 'Sales Invoice',
51 'Lead', 'Opportunity', 'Task', 'Expense Claim', 'Employee', 'HR', 'ToDo'],
mbauskare94c6e72017-04-29 15:41:34 +053052 'remove_roles': ['Manufacturing User', 'Manufacturing Manager', 'Academics User', 'Instructor'],
Anand Doshi0e4119b2016-06-28 21:37:28 +053053 'properties': [
54 {'doctype': 'Item', 'fieldname': 'is_stock_item', 'property': 'default', 'value': 0},
55 ],
56 'set_value': [
57 ['Stock Settings', None, 'show_barcode_field', 0]
Rushabh Mehta20038ad2016-07-21 16:01:59 +053058 ],
Rushabh Mehtafc800b32016-09-23 21:51:51 +053059 'default_portal_role': 'Customer'
Rushabh Mehta20038ad2016-07-21 16:01:59 +053060 },
61 'Education': {
62 'desktop_icons': ['Student', 'Program', 'Course', 'Student Group', 'Instructor',
Rushabh Mehta868bb262017-03-09 18:11:11 +053063 'Fees', 'Task', 'ToDo', 'Schools'],
Rushabh Mehta57a19052016-09-20 17:22:54 +053064 'allow_roles': ['Academics User', 'Accounts User', 'Accounts Manager', 'Item Manager',
Rushabh Mehtafc800b32016-09-23 21:51:51 +053065 'Website Manager', 'HR User', 'HR Manager', 'Purchase User', 'Purchase Manager',
Nabin Hait11171252017-05-01 11:39:36 +053066 'Student', 'Projects User', 'Instructor'],
Rushabh Mehtafc800b32016-09-23 21:51:51 +053067 'default_portal_role': 'Student'
Rushabh Mehta20038ad2016-07-21 16:01:59 +053068 },
Rushabh Mehta2dd05812016-05-11 15:13:44 +053069 }
Rushabh Mehta20038ad2016-07-21 16:01:59 +053070 if not domain in data:
71 raise 'Invalid Domain {0}'.format(domain)
72 return frappe._dict(data[domain])
Rushabh Mehta2dd05812016-05-11 15:13:44 +053073
74def setup_domain(domain):
Rushabh Mehta20038ad2016-07-21 16:01:59 +053075 '''Setup roles, desktop icons, properties, values, portal sidebar menu based on domain'''
76 data = get_domain(domain)
77 setup_roles(data)
78 setup_desktop_icons(data)
79 setup_properties(data)
80 set_values(data)
81 setup_sidebar_items(data)
Makarand Bauskar6e30f042017-07-18 12:30:57 +053082 update_module_def_restrict_to_domain()
83
Rushabh Mehtafc800b32016-09-23 21:51:51 +053084 if data.get('default_portal_role'):
85 frappe.db.set_value('Portal Settings', None, 'default_role', data.get('default_portal_role'))
Makarand Bauskar6e30f042017-07-18 12:30:57 +053086
Rushabh Mehta20038ad2016-07-21 16:01:59 +053087 frappe.clear_cache()
Anand Doshi0e4119b2016-06-28 21:37:28 +053088
Rushabh Mehta20038ad2016-07-21 16:01:59 +053089def setup_desktop_icons(data):
90 '''set desktop icons form `data.desktop_icons`'''
Rushabh Mehta2dd05812016-05-11 15:13:44 +053091 from frappe.desk.doctype.desktop_icon.desktop_icon import set_desktop_icons
Rushabh Mehta2dd05812016-05-11 15:13:44 +053092 if data.desktop_icons:
93 set_desktop_icons(data.desktop_icons)
94
Rushabh Mehta20038ad2016-07-21 16:01:59 +053095def setup_properties(data):
Rushabh Mehta2dd05812016-05-11 15:13:44 +053096 if data.properties:
97 for args in data.properties:
98 frappe.make_property_setter(args)
99
Rushabh Mehta20038ad2016-07-21 16:01:59 +0530100def setup_roles(data):
101 '''Add, remove roles from `data.allow_roles` or `data.remove_roles`'''
102 def remove_role(role):
Rohit Waghchaure31249802017-02-20 15:11:56 +0530103 frappe.db.sql('delete from `tabHas Role` where role=%s', role)
Rushabh Mehta1aac9562016-10-25 15:01:30 +0530104 frappe.set_value('Role', role, 'disabled', 1)
Rushabh Mehta20038ad2016-07-21 16:01:59 +0530105
106 if data.remove_roles:
107 for role in data.remove_roles:
108 remove_role(role)
109
110 if data.allow_roles:
111 # remove all roles other than allowed roles
Makarand Bauskardd986f62017-06-16 13:02:45 +0530112 active_domains = frappe.get_active_domains()
pratu16x7a43639e2016-11-05 10:09:53 +0530113 data.allow_roles += ['Administrator', 'Guest', 'System Manager', 'All']
Makarand Bauskardd986f62017-06-16 13:02:45 +0530114 for role in frappe.get_all('Role', filters = {"restrict_to_domain": ("not in", active_domains)}):
Rushabh Mehta20038ad2016-07-21 16:01:59 +0530115 if not (role.name in data.allow_roles):
116 remove_role(role.name)
117
118def set_values(data):
119 '''set values based on `data.set_value`'''
Rushabh Mehta48c76582016-05-12 16:26:01 +0530120 if data.set_value:
121 for args in data.set_value:
122 doc = frappe.get_doc(args[0], args[1] or args[0])
123 doc.set(args[2], args[3])
124 doc.save()
125
Rushabh Mehta20038ad2016-07-21 16:01:59 +0530126def setup_sidebar_items(data):
127 '''Enable / disable sidebar items'''
128 if data.allow_sidebar_items:
129 # disable all
130 frappe.db.sql('update `tabPortal Menu Item` set enabled=0')
131
132 # enable
133 frappe.db.sql('''update `tabPortal Menu Item` set enabled=1
134 where route in ({0})'''.format(', '.join(['"{0}"'.format(d) for d in data.allow_sidebar_items])))
135
136 if data.remove_sidebar_items:
137 # disable all
138 frappe.db.sql('update `tabPortal Menu Item` set enabled=1')
139
140 # enable
141 frappe.db.sql('''update `tabPortal Menu Item` set enabled=0
142 where route in ({0})'''.format(', '.join(['"{0}"'.format(d) for d in data.remove_sidebar_items])))
143
Rushabh Mehta2dd05812016-05-11 15:13:44 +0530144def reset():
145 from frappe.desk.page.setup_wizard.setup_wizard import add_all_roles_to
146 add_all_roles_to('Administrator')
147
Anand Doshi0e4119b2016-06-28 21:37:28 +0530148 frappe.db.sql('delete from `tabProperty Setter`')
Makarand Bauskar6e30f042017-07-18 12:30:57 +0530149
150def update_module_def_restrict_to_domain():
151 """ set the restrict to domain for the module def """
152
153 module_def_restrict_to_domain_mapper = {
154 "Schools": _('Education')
155 }
156
157 for module, domain in module_def_restrict_to_domain_mapper.iteritems():
158 frappe.set_value("Module Def", module, "restrict_to_domain", domain)