blob: f0aaf0294426dd9ba0ea092eff2b5c5cd4e45452 [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
6
Rushabh Mehta20038ad2016-07-21 16:01:59 +05307def get_domain(domain):
Anand Doshi0e4119b2016-06-28 21:37:28 +05308 '''Written as a function to prevent data mutation effects'''
Rushabh Mehta20038ad2016-07-21 16:01:59 +05309 data = {
Anand Doshi0e4119b2016-06-28 21:37:28 +053010 'Manufacturing': {
11 'desktop_icons': ['Item', 'BOM', 'Customer', 'Supplier', 'Sales Order',
12 'Production Order', 'Stock Entry', 'Purchase Order', 'Task', 'Buying', 'Selling',
13 'Accounts', 'HR', 'ToDo'],
mbauskare94c6e72017-04-29 15:41:34 +053014 'remove_roles': ['Academics User', 'Instructor'],
Anand Doshi0e4119b2016-06-28 21:37:28 +053015 'properties': [
16 {'doctype': 'Item', 'fieldname': 'manufacturing', 'property': 'collapsible_depends_on', 'value': 'is_stock_item'},
17 ],
18 'set_value': [
19 ['Stock Settings', None, 'show_barcode_field', 1]
Rushabh Mehta20038ad2016-07-21 16:01:59 +053020 ],
Rushabh Mehtafc800b32016-09-23 21:51:51 +053021 'default_portal_role': 'Customer'
Anand Doshi0e4119b2016-06-28 21:37:28 +053022 },
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +053023
Anand Doshi0e4119b2016-06-28 21:37:28 +053024 'Retail': {
Rushabh Mehta868bb262017-03-09 18:11:11 +053025 'desktop_icons': ['POS', 'Item', 'Customer', 'Sales Invoice', 'Purchase Order',
26 'Warranty Claim', 'Accounts', 'Task', 'Buying', 'ToDo'],
mbauskare94c6e72017-04-29 15:41:34 +053027 'remove_roles': ['Manufacturing User', 'Manufacturing Manager', 'Academics User', 'Instructor'],
Anand Doshi0e4119b2016-06-28 21:37:28 +053028 'properties': [
29 {'doctype': 'Item', 'fieldname': 'manufacturing', 'property': 'hidden', 'value': 1},
30 {'doctype': 'Customer', 'fieldname': 'credit_limit_section', 'property': 'hidden', 'value': 1},
31 ],
32 'set_value': [
33 ['Stock Settings', None, 'show_barcode_field', 1]
Rushabh Mehta20038ad2016-07-21 16:01:59 +053034 ],
Rushabh Mehtafc800b32016-09-23 21:51:51 +053035 'default_portal_role': 'Customer'
Anand Doshi0e4119b2016-06-28 21:37:28 +053036 },
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +053037
Anand Doshi0e4119b2016-06-28 21:37:28 +053038 'Distribution': {
Rushabh Mehta868bb262017-03-09 18:11:11 +053039 'desktop_icons': ['Item', 'Customer', 'Supplier', 'Lead', 'Sales Order', 'Task',
Anand Doshi0e4119b2016-06-28 21:37:28 +053040 'Sales Invoice', 'CRM', 'Selling', 'Buying', 'Stock', 'Accounts', 'HR', 'ToDo'],
mbauskare94c6e72017-04-29 15:41:34 +053041 'remove_roles': ['Manufacturing User', 'Manufacturing Manager', 'Academics User', 'Instructor'],
Anand Doshi0e4119b2016-06-28 21:37:28 +053042 'set_value': [
43 ['Stock Settings', None, 'show_barcode_field', 1]
Rushabh Mehta20038ad2016-07-21 16:01:59 +053044 ],
Rushabh Mehtafc800b32016-09-23 21:51:51 +053045 'default_portal_role': 'Customer'
Anand Doshi0e4119b2016-06-28 21:37:28 +053046 },
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +053047
Anand Doshi0e4119b2016-06-28 21:37:28 +053048 'Services': {
Rushabh Mehta868bb262017-03-09 18:11:11 +053049 'desktop_icons': ['Project', 'Timesheet', 'Customer', 'Sales Order', 'Sales Invoice',
50 'Lead', 'Opportunity', 'Task', 'Expense Claim', 'Employee', 'HR', 'ToDo'],
mbauskare94c6e72017-04-29 15:41:34 +053051 'remove_roles': ['Manufacturing User', 'Manufacturing Manager', 'Academics User', 'Instructor'],
Anand Doshi0e4119b2016-06-28 21:37:28 +053052 'properties': [
53 {'doctype': 'Item', 'fieldname': 'is_stock_item', 'property': 'default', 'value': 0},
54 ],
55 'set_value': [
56 ['Stock Settings', None, 'show_barcode_field', 0]
Rushabh Mehta20038ad2016-07-21 16:01:59 +053057 ],
Rushabh Mehtafc800b32016-09-23 21:51:51 +053058 'default_portal_role': 'Customer'
Rushabh Mehta20038ad2016-07-21 16:01:59 +053059 },
60 'Education': {
61 'desktop_icons': ['Student', 'Program', 'Course', 'Student Group', 'Instructor',
Rushabh Mehta868bb262017-03-09 18:11:11 +053062 'Fees', 'Task', 'ToDo', 'Schools'],
Rushabh Mehta57a19052016-09-20 17:22:54 +053063 'allow_roles': ['Academics User', 'Accounts User', 'Accounts Manager', 'Item Manager',
Rushabh Mehtafc800b32016-09-23 21:51:51 +053064 'Website Manager', 'HR User', 'HR Manager', 'Purchase User', 'Purchase Manager',
Nabin Hait11171252017-05-01 11:39:36 +053065 'Student', 'Projects User', 'Instructor'],
Rushabh Mehtafc800b32016-09-23 21:51:51 +053066 'default_portal_role': 'Student'
Rushabh Mehta20038ad2016-07-21 16:01:59 +053067 },
Rushabh Mehta2dd05812016-05-11 15:13:44 +053068 }
Rushabh Mehta20038ad2016-07-21 16:01:59 +053069 if not domain in data:
70 raise 'Invalid Domain {0}'.format(domain)
71 return frappe._dict(data[domain])
Rushabh Mehta2dd05812016-05-11 15:13:44 +053072
73def setup_domain(domain):
Rushabh Mehta20038ad2016-07-21 16:01:59 +053074 '''Setup roles, desktop icons, properties, values, portal sidebar menu based on domain'''
75 data = get_domain(domain)
76 setup_roles(data)
77 setup_desktop_icons(data)
78 setup_properties(data)
79 set_values(data)
80 setup_sidebar_items(data)
Rushabh Mehtafc800b32016-09-23 21:51:51 +053081 if data.get('default_portal_role'):
82 frappe.db.set_value('Portal Settings', None, 'default_role', data.get('default_portal_role'))
Rushabh Mehta20038ad2016-07-21 16:01:59 +053083 frappe.clear_cache()
Anand Doshi0e4119b2016-06-28 21:37:28 +053084
Rushabh Mehta20038ad2016-07-21 16:01:59 +053085def setup_desktop_icons(data):
86 '''set desktop icons form `data.desktop_icons`'''
Rushabh Mehta2dd05812016-05-11 15:13:44 +053087 from frappe.desk.doctype.desktop_icon.desktop_icon import set_desktop_icons
Rushabh Mehta2dd05812016-05-11 15:13:44 +053088 if data.desktop_icons:
89 set_desktop_icons(data.desktop_icons)
90
Rushabh Mehta20038ad2016-07-21 16:01:59 +053091def setup_properties(data):
Rushabh Mehta2dd05812016-05-11 15:13:44 +053092 if data.properties:
93 for args in data.properties:
94 frappe.make_property_setter(args)
95
Rushabh Mehta20038ad2016-07-21 16:01:59 +053096def setup_roles(data):
97 '''Add, remove roles from `data.allow_roles` or `data.remove_roles`'''
98 def remove_role(role):
Rohit Waghchaure31249802017-02-20 15:11:56 +053099 frappe.db.sql('delete from `tabHas Role` where role=%s', role)
Rushabh Mehta1aac9562016-10-25 15:01:30 +0530100 frappe.set_value('Role', role, 'disabled', 1)
Rushabh Mehta20038ad2016-07-21 16:01:59 +0530101
102 if data.remove_roles:
103 for role in data.remove_roles:
104 remove_role(role)
105
106 if data.allow_roles:
107 # remove all roles other than allowed roles
Makarand Bauskardd986f62017-06-16 13:02:45 +0530108 active_domains = frappe.get_active_domains()
pratu16x7a43639e2016-11-05 10:09:53 +0530109 data.allow_roles += ['Administrator', 'Guest', 'System Manager', 'All']
Makarand Bauskardd986f62017-06-16 13:02:45 +0530110 for role in frappe.get_all('Role', filters = {"restrict_to_domain": ("not in", active_domains)}):
Rushabh Mehta20038ad2016-07-21 16:01:59 +0530111 if not (role.name in data.allow_roles):
112 remove_role(role.name)
113
114def set_values(data):
115 '''set values based on `data.set_value`'''
Rushabh Mehta48c76582016-05-12 16:26:01 +0530116 if data.set_value:
117 for args in data.set_value:
118 doc = frappe.get_doc(args[0], args[1] or args[0])
119 doc.set(args[2], args[3])
120 doc.save()
121
Rushabh Mehta20038ad2016-07-21 16:01:59 +0530122def setup_sidebar_items(data):
123 '''Enable / disable sidebar items'''
124 if data.allow_sidebar_items:
125 # disable all
126 frappe.db.sql('update `tabPortal Menu Item` set enabled=0')
127
128 # enable
129 frappe.db.sql('''update `tabPortal Menu Item` set enabled=1
130 where route in ({0})'''.format(', '.join(['"{0}"'.format(d) for d in data.allow_sidebar_items])))
131
132 if data.remove_sidebar_items:
133 # disable all
134 frappe.db.sql('update `tabPortal Menu Item` set enabled=1')
135
136 # enable
137 frappe.db.sql('''update `tabPortal Menu Item` set enabled=0
138 where route in ({0})'''.format(', '.join(['"{0}"'.format(d) for d in data.remove_sidebar_items])))
139
Rushabh Mehta2dd05812016-05-11 15:13:44 +0530140
141def reset():
142 from frappe.desk.page.setup_wizard.setup_wizard import add_all_roles_to
143 add_all_roles_to('Administrator')
144
Anand Doshi0e4119b2016-06-28 21:37:28 +0530145 frappe.db.sql('delete from `tabProperty Setter`')