blob: 1d7bad2686a5241a5588f7ed9db05eeae68d195d [file] [log] [blame]
Anand Doshi885e0742015-03-03 14:55:30 +05301# Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors
Rushabh Mehtae67d1fb2013-08-05 14:59:54 +05302# License: GNU General Public License v3. See license.txt
3
Rushabh Mehta6f9915c2013-01-16 17:48:17 +05304
Rushabh Mehta793ba6b2014-02-14 15:47:51 +05305import frappe
Rushabh Mehtad42167e2016-05-11 16:47:14 +05306from frappe import _
Nabin Hait45dce892017-09-14 15:17:38 +05307from frappe.custom.doctype.custom_field.custom_field import create_custom_field
Chillar Anand915b3432021-09-02 16:44:59 +05308from frappe.desk.page.setup_wizard.setup_wizard import add_all_roles_to
9from frappe.installer import update_site_config
10from frappe.utils import cint
Rushabh Mehta6f9915c2013-01-16 17:48:17 +053011
Chillar Anand915b3432021-09-02 16:44:59 +053012from erpnext.accounts.doctype.cash_flow_mapper.default_cash_flow_mapper import DEFAULT_MAPPERS
13from erpnext.setup.default_energy_point_rules import get_default_energy_point_rules
14
15from .default_success_action import get_default_success_action
16
Anand Doshi56198f42014-06-26 12:47:45 +053017default_mail_footer = """<div style="padding: 7px; text-align: right; color: #888"><small>Sent via
Anand Doshi7f41ff22014-06-26 12:02:55 +053018 <a style="color: #888" href="http://erpnext.org">ERPNext</a></div>"""
19
tundebabzycad22db2018-02-22 06:38:36 +010020
Rushabh Mehta1f847992013-12-12 19:12:19 +053021def after_install():
Rushabh Mehta40b2b032014-04-21 13:43:11 +053022 frappe.get_doc({'doctype': "Role", "role_name": "Analytics"}).insert()
Rushabh Mehta33003c62014-04-30 11:16:02 +053023 set_single_defaults()
Rushabh Mehtad42167e2016-05-11 16:47:14 +053024 create_compact_item_print_custom_field()
Frappe ERPnextf28bef82020-09-02 21:42:52 +020025 create_print_uom_after_qty_custom_field()
Nabin Hait45dce892017-09-14 15:17:38 +053026 create_print_zero_amount_taxes_custom_field()
Rushabh Mehtadb7139a2013-01-17 18:22:22 +053027 add_all_roles_to("Administrator")
tundebabzycad22db2018-02-22 06:38:36 +010028 create_default_cash_flow_mapper_templates()
Suraj Shetty00cced12018-05-03 19:06:32 +053029 create_default_success_action()
Suraj Shetty627a3dc2019-09-17 15:54:41 +053030 create_default_energy_point_rules()
Rucha Mahabal4865eab2019-07-24 13:37:54 +053031 add_company_to_session_defaults()
Deepesh Garga306af82020-08-06 20:52:02 +053032 add_standard_navbar_items()
Shivam Mishra1323a9a2020-09-30 16:33:14 +053033 add_app_name()
Rohit Waghchaure03635fb2021-01-18 15:06:35 +053034 add_non_standard_user_types()
Anand Doshie9baaa62014-02-26 12:35:33 +053035 frappe.db.commit()
Rushabh Mehtadb7139a2013-01-17 18:22:22 +053036
tundebabzycad22db2018-02-22 06:38:36 +010037
Rushabh Mehtad55bdcf2015-12-31 11:12:48 +053038def check_setup_wizard_not_completed():
Saurabh58d05ac2020-08-10 19:36:45 +053039 if cint(frappe.db.get_single_value('System Settings', 'setup_complete') or 0):
Deepesh Garga306af82020-08-06 20:52:02 +053040 message = """ERPNext can only be installed on a fresh site where the setup wizard is not completed.
Faris Ansarif3a6e302020-07-08 10:45:55 +053041You can reinstall this site (after saving your data) using: bench --site [sitename] reinstall"""
Ankush Menatff96bdf2021-05-25 19:54:40 +053042 frappe.throw(message) # nosemgrep
Rushabh Mehtad55bdcf2015-12-31 11:12:48 +053043
tundebabzycad22db2018-02-22 06:38:36 +010044
Pratik Vyas28da7522014-02-19 20:53:45 +053045def set_single_defaults():
Rushabh Mehtab62ed7a2016-10-13 11:00:00 +053046 for dt in ('Accounts Settings', 'Print Settings', 'HR Settings', 'Buying Settings',
Suraj Shettyd3069fe2018-02-21 15:15:43 +053047 'Selling Settings', 'Stock Settings'):
Anand Doshieb7fea62014-03-19 17:10:01 +053048 default_values = frappe.db.sql("""select fieldname, `default` from `tabDocField`
Rushabh Mehtaf14b8092014-04-03 14:30:42 +053049 where parent=%s""", dt)
Anand Doshieb7fea62014-03-19 17:10:01 +053050 if default_values:
51 try:
Rushabh Mehtab385ecf2014-03-28 16:44:37 +053052 b = frappe.get_doc(dt, dt)
Anand Doshieb7fea62014-03-19 17:10:01 +053053 for fieldname, value in default_values:
Anand Doshif78d1ae2014-03-28 13:55:00 +053054 b.set(fieldname, value)
Anand Doshieb7fea62014-03-19 17:10:01 +053055 b.save()
56 except frappe.MandatoryError:
57 pass
Rushabh Mehtab62ed7a2016-10-13 11:00:00 +053058 except frappe.ValidationError:
59 pass
Rushabh Mehtad42167e2016-05-11 16:47:14 +053060
rtdany10227466c2021-09-04 14:04:56 +053061 frappe.db.set_default("date_format", "dd-mm-yyyy")
Deepesh Garge2dab6f2022-01-10 17:31:38 +053062
63 setup_currency_exchange()
64
65def setup_currency_exchange():
rtdany10227466c2021-09-04 14:04:56 +053066 ces = frappe.get_single('Currency Exchange Settings')
67 try:
Deepesh Garge2dab6f2022-01-10 17:31:38 +053068 ces.set('result_key', [])
69 ces.set('req_params', [])
70
71 ces.api_endpoint = "https://frankfurter.app/{transaction_date}"
72 ces.append('result_key', {'key': 'rates'})
73 ces.append('result_key', {'key': '{to_currency}'})
74 ces.append('req_params', {'key': 'base', 'value': '{from_currency}'})
75 ces.append('req_params', {'key': 'symbols', 'value': '{to_currency}'})
rtdany10227466c2021-09-04 14:04:56 +053076 ces.save()
77 except frappe.ValidationError:
78 pass
tundebabzycad22db2018-02-22 06:38:36 +010079
Rushabh Mehtad42167e2016-05-11 16:47:14 +053080def create_compact_item_print_custom_field():
Rushabh Mehtad42167e2016-05-11 16:47:14 +053081 create_custom_field('Print Settings', {
82 'label': _('Compact Item Print'),
83 'fieldname': 'compact_item_print',
84 'fieldtype': 'Check',
85 'default': 1,
86 'insert_after': 'with_letterhead'
Nabin Hait45dce892017-09-14 15:17:38 +053087 })
88
tundebabzycad22db2018-02-22 06:38:36 +010089
Frappe ERPnextf28bef82020-09-02 21:42:52 +020090def create_print_uom_after_qty_custom_field():
91 create_custom_field('Print Settings', {
92 'label': _('Print UOM after Quantity'),
93 'fieldname': 'print_uom_after_quantity',
94 'fieldtype': 'Check',
95 'default': 0,
96 'insert_after': 'compact_item_print'
97 })
98
99
Nabin Hait45dce892017-09-14 15:17:38 +0530100def create_print_zero_amount_taxes_custom_field():
101 create_custom_field('Print Settings', {
102 'label': _('Print taxes with zero amount'),
103 'fieldname': 'print_taxes_with_zero_amount',
104 'fieldtype': 'Check',
105 'default': 0,
106 'insert_after': 'allow_print_for_cancelled'
tundebabzycad22db2018-02-22 06:38:36 +0100107 })
108
109
110def create_default_cash_flow_mapper_templates():
Suraj Shetty00cced12018-05-03 19:06:32 +0530111 for mapper in DEFAULT_MAPPERS:
tundebabzycad22db2018-02-22 06:38:36 +0100112 if not frappe.db.exists('Cash Flow Mapper', mapper['section_name']):
113 doc = frappe.get_doc(mapper)
114 doc.insert(ignore_permissions=True)
Suraj Shetty00cced12018-05-03 19:06:32 +0530115
116def create_default_success_action():
117 for success_action in get_default_success_action():
118 if not frappe.db.exists('Success Action', success_action.get("ref_doctype")):
119 doc = frappe.get_doc(success_action)
120 doc.insert(ignore_permissions=True)
Rucha Mahabal4865eab2019-07-24 13:37:54 +0530121
Suraj Shetty627a3dc2019-09-17 15:54:41 +0530122def create_default_energy_point_rules():
123
124 for rule in get_default_energy_point_rules():
125 # check if any rule for ref. doctype exists
126 rule_exists = frappe.db.exists('Energy Point Rule', {
127 'reference_doctype': rule.get('reference_doctype')
128 })
129 if rule_exists: continue
130 doc = frappe.get_doc(rule)
131 doc.insert(ignore_permissions=True)
132
Rucha Mahabal4865eab2019-07-24 13:37:54 +0530133def add_company_to_session_defaults():
134 settings = frappe.get_single("Session Default Settings")
135 settings.append("session_defaults", {
136 "ref_doctype": "Company"
137 })
138 settings.save()
Deepesh Garga306af82020-08-06 20:52:02 +0530139
140def add_standard_navbar_items():
141 navbar_settings = frappe.get_single("Navbar Settings")
142
143 erpnext_navbar_items = [
144 {
145 'item_label': 'Documentation',
146 'item_type': 'Route',
147 'route': 'https://erpnext.com/docs/user/manual',
148 'is_standard': 1
149 },
150 {
151 'item_label': 'User Forum',
152 'item_type': 'Route',
153 'route': 'https://discuss.erpnext.com',
154 'is_standard': 1
155 },
156 {
157 'item_label': 'Report an Issue',
158 'item_type': 'Route',
159 'route': 'https://github.com/frappe/erpnext/issues',
160 'is_standard': 1
161 }
162 ]
163
prssanna194dd122021-03-24 16:45:32 +0530164 current_navbar_items = navbar_settings.help_dropdown
Deepesh Garga306af82020-08-06 20:52:02 +0530165 navbar_settings.set('help_dropdown', [])
166
167 for item in erpnext_navbar_items:
prssanna194dd122021-03-24 16:45:32 +0530168 current_labels = [item.get('item_label') for item in current_navbar_items]
169 if not item.get('item_label') in current_labels:
170 navbar_settings.append('help_dropdown', item)
Deepesh Garga306af82020-08-06 20:52:02 +0530171
prssanna194dd122021-03-24 16:45:32 +0530172 for item in current_navbar_items:
Deepesh Garga306af82020-08-06 20:52:02 +0530173 navbar_settings.append('help_dropdown', {
174 'item_label': item.item_label,
175 'item_type': item.item_type,
176 'route': item.route,
177 'action': item.action,
178 'is_standard': item.is_standard,
179 'hidden': item.hidden
180 })
181
182 navbar_settings.save()
Shivam Mishra1323a9a2020-09-30 16:33:14 +0530183
184def add_app_name():
Rushabh Mehta45735b32021-03-16 12:22:31 +0530185 frappe.db.set_value('System Settings', None, 'app_name', 'ERPNext')
Rohit Waghchaure03635fb2021-01-18 15:06:35 +0530186
187def add_non_standard_user_types():
188 user_types = get_user_types_data()
189
190 user_type_limit = {}
Ankush Menat8fe5feb2021-11-04 19:48:32 +0530191 for user_type, data in user_types.items():
Rucha Mahabal6c1c85b2022-01-27 00:51:29 +0530192 user_type_limit.setdefault(frappe.scrub(user_type), 20)
Rohit Waghchaure03635fb2021-01-18 15:06:35 +0530193
194 update_site_config('user_type_doctype_limit', user_type_limit)
195
Ankush Menat8fe5feb2021-11-04 19:48:32 +0530196 for user_type, data in user_types.items():
Rohit Waghchaure03635fb2021-01-18 15:06:35 +0530197 create_custom_role(data)
198 create_user_type(user_type, data)
199
200def get_user_types_data():
201 return {
Rohit Waghchaure610ccd42021-03-16 00:39:10 +0530202 'Employee Self Service': {
203 'role': 'Employee Self Service',
Rohit Waghchaure03635fb2021-01-18 15:06:35 +0530204 'apply_user_permission_on': 'Employee',
205 'user_id_field': 'user_id',
206 'doctypes': {
Rucha Mahabal6c1c85b2022-01-27 00:51:29 +0530207 # masters
208 'Holiday List': ['read'],
Rohit Waghchaure03635fb2021-01-18 15:06:35 +0530209 'Employee': ['read', 'write'],
Rucha Mahabal6c1c85b2022-01-27 00:51:29 +0530210 # payroll
211 'Salary Slip': ['read'],
212 'Employee Benefit Application': ['read', 'write', 'create', 'delete'],
213 # expenses
Rohit Waghchaure610ccd42021-03-16 00:39:10 +0530214 'Expense Claim': ['read', 'write', 'create', 'delete'],
Rucha Mahabal6c1c85b2022-01-27 00:51:29 +0530215 'Employee Advance': ['read', 'write', 'create', 'delete'],
216 # leave and attendance
Rohit Waghchaure610ccd42021-03-16 00:39:10 +0530217 'Leave Application': ['read', 'write', 'create', 'delete'],
218 'Attendance Request': ['read', 'write', 'create', 'delete'],
219 'Compensatory Leave Request': ['read', 'write', 'create', 'delete'],
Rucha Mahabal6c1c85b2022-01-27 00:51:29 +0530220 # tax
Rohit Waghchaure610ccd42021-03-16 00:39:10 +0530221 'Employee Tax Exemption Declaration': ['read', 'write', 'create', 'delete'],
222 'Employee Tax Exemption Proof Submission': ['read', 'write', 'create', 'delete'],
Rucha Mahabal6c1c85b2022-01-27 00:51:29 +0530223 # projects
224 'Timesheet': ['read', 'write', 'create', 'delete', 'submit', 'cancel', 'amend'],
225 # trainings
226 'Training Program': ['read'],
227 'Training Feedback': ['read', 'write', 'create', 'delete', 'submit', 'cancel', 'amend'],
228 # shifts
229 'Shift Request': ['read', 'write', 'create', 'delete', 'submit', 'cancel', 'amend'],
230 # misc
231 'Employee Grievance': ['read', 'write', 'create', 'delete'],
232 'Employee Referral': ['read', 'write', 'create', 'delete'],
233 'Travel Request': ['read', 'write', 'create', 'delete']
Rohit Waghchaure03635fb2021-01-18 15:06:35 +0530234 }
235 }
236 }
237
238def create_custom_role(data):
239 if data.get('role') and not frappe.db.exists('Role', data.get('role')):
240 frappe.get_doc({
241 'doctype': 'Role',
242 'role_name': data.get('role'),
243 'desk_access': 1,
244 'is_custom': 1
245 }).insert(ignore_permissions=True)
246
247def create_user_type(user_type, data):
248 if frappe.db.exists('User Type', user_type):
249 doc = frappe.get_cached_doc('User Type', user_type)
250 doc.user_doctypes = []
251 else:
252 doc = frappe.new_doc('User Type')
253 doc.update({
254 'name': user_type,
255 'role': data.get('role'),
256 'user_id_field': data.get('user_id_field'),
257 'apply_user_permission_on': data.get('apply_user_permission_on')
258 })
259
260 create_role_permissions_for_doctype(doc, data)
261 doc.save(ignore_permissions=True)
262
263def create_role_permissions_for_doctype(doc, data):
Ankush Menat8fe5feb2021-11-04 19:48:32 +0530264 for doctype, perms in data.get('doctypes').items():
Rohit Waghchaure03635fb2021-01-18 15:06:35 +0530265 args = {'document_type': doctype}
266 for perm in perms:
267 args[perm] = 1
268
269 doc.append('user_doctypes', args)
Rohit Waghchaurea8f78fa2021-04-13 18:43:57 +0530270
271def update_select_perm_after_install():
272 if not frappe.flags.update_select_perm_after_migrate:
273 return
274
275 frappe.flags.ignore_select_perm = False
276 for row in frappe.get_all('User Type', filters= {'is_standard': 0}):
277 print('Updating user type :- ', row.name)
278 doc = frappe.get_doc('User Type', row.name)
279 doc.save()
280
281 frappe.flags.update_select_perm_after_migrate = False