blob: 55be9ee32a19c56cc361bd73247fbcb6da271bc4 [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
Aditya Hase6ccb6562017-08-28 18:17:36 +05304from __future__ import print_function, unicode_literals
Rushabh Mehta6f9915c2013-01-16 17:48:17 +05305
Rushabh Mehta793ba6b2014-02-14 15:47:51 +05306import frappe
Rushabh Mehtad42167e2016-05-11 16:47:14 +05307from frappe import _
Nabin Hait45dce892017-09-14 15:17:38 +05308from frappe.custom.doctype.custom_field.custom_field import create_custom_field
Chillar Anand915b3432021-09-02 16:44:59 +05309from frappe.desk.page.setup_wizard.setup_wizard import add_all_roles_to
10from frappe.installer import update_site_config
11from frappe.utils import cint
Rohit Waghchaure03635fb2021-01-18 15:06:35 +053012from six import iteritems
Rushabh Mehta6f9915c2013-01-16 17:48:17 +053013
Chillar Anand915b3432021-09-02 16:44:59 +053014from erpnext.accounts.doctype.cash_flow_mapper.default_cash_flow_mapper import DEFAULT_MAPPERS
15from erpnext.setup.default_energy_point_rules import get_default_energy_point_rules
16
17from .default_success_action import get_default_success_action
18
Anand Doshi56198f42014-06-26 12:47:45 +053019default_mail_footer = """<div style="padding: 7px; text-align: right; color: #888"><small>Sent via
Anand Doshi7f41ff22014-06-26 12:02:55 +053020 <a style="color: #888" href="http://erpnext.org">ERPNext</a></div>"""
21
tundebabzycad22db2018-02-22 06:38:36 +010022
Rushabh Mehta1f847992013-12-12 19:12:19 +053023def after_install():
Rushabh Mehta40b2b032014-04-21 13:43:11 +053024 frappe.get_doc({'doctype': "Role", "role_name": "Analytics"}).insert()
Rushabh Mehta33003c62014-04-30 11:16:02 +053025 set_single_defaults()
Rushabh Mehtad42167e2016-05-11 16:47:14 +053026 create_compact_item_print_custom_field()
Frappe ERPnextf28bef82020-09-02 21:42:52 +020027 create_print_uom_after_qty_custom_field()
Nabin Hait45dce892017-09-14 15:17:38 +053028 create_print_zero_amount_taxes_custom_field()
Rushabh Mehtadb7139a2013-01-17 18:22:22 +053029 add_all_roles_to("Administrator")
tundebabzycad22db2018-02-22 06:38:36 +010030 create_default_cash_flow_mapper_templates()
Suraj Shetty00cced12018-05-03 19:06:32 +053031 create_default_success_action()
Suraj Shetty627a3dc2019-09-17 15:54:41 +053032 create_default_energy_point_rules()
Rucha Mahabal4865eab2019-07-24 13:37:54 +053033 add_company_to_session_defaults()
Deepesh Garga306af82020-08-06 20:52:02 +053034 add_standard_navbar_items()
Shivam Mishra1323a9a2020-09-30 16:33:14 +053035 add_app_name()
Rohit Waghchaure03635fb2021-01-18 15:06:35 +053036 add_non_standard_user_types()
Anand Doshie9baaa62014-02-26 12:35:33 +053037 frappe.db.commit()
Rushabh Mehtadb7139a2013-01-17 18:22:22 +053038
tundebabzycad22db2018-02-22 06:38:36 +010039
Rushabh Mehtad55bdcf2015-12-31 11:12:48 +053040def check_setup_wizard_not_completed():
Saurabh58d05ac2020-08-10 19:36:45 +053041 if cint(frappe.db.get_single_value('System Settings', 'setup_complete') or 0):
Deepesh Garga306af82020-08-06 20:52:02 +053042 message = """ERPNext can only be installed on a fresh site where the setup wizard is not completed.
Faris Ansarif3a6e302020-07-08 10:45:55 +053043You can reinstall this site (after saving your data) using: bench --site [sitename] reinstall"""
Ankush Menatff96bdf2021-05-25 19:54:40 +053044 frappe.throw(message) # nosemgrep
Rushabh Mehtad55bdcf2015-12-31 11:12:48 +053045
tundebabzycad22db2018-02-22 06:38:36 +010046
Pratik Vyas28da7522014-02-19 20:53:45 +053047def set_single_defaults():
Rushabh Mehtab62ed7a2016-10-13 11:00:00 +053048 for dt in ('Accounts Settings', 'Print Settings', 'HR Settings', 'Buying Settings',
Suraj Shettyd3069fe2018-02-21 15:15:43 +053049 'Selling Settings', 'Stock Settings'):
Anand Doshieb7fea62014-03-19 17:10:01 +053050 default_values = frappe.db.sql("""select fieldname, `default` from `tabDocField`
Rushabh Mehtaf14b8092014-04-03 14:30:42 +053051 where parent=%s""", dt)
Anand Doshieb7fea62014-03-19 17:10:01 +053052 if default_values:
53 try:
Rushabh Mehtab385ecf2014-03-28 16:44:37 +053054 b = frappe.get_doc(dt, dt)
Anand Doshieb7fea62014-03-19 17:10:01 +053055 for fieldname, value in default_values:
Anand Doshif78d1ae2014-03-28 13:55:00 +053056 b.set(fieldname, value)
Anand Doshieb7fea62014-03-19 17:10:01 +053057 b.save()
58 except frappe.MandatoryError:
59 pass
Rushabh Mehtab62ed7a2016-10-13 11:00:00 +053060 except frappe.ValidationError:
61 pass
Rushabh Mehtad42167e2016-05-11 16:47:14 +053062
rtdany10227466c2021-09-04 14:04:56 +053063 frappe.db.set_default("date_format", "dd-mm-yyyy")
64 ces = frappe.get_single('Currency Exchange Settings')
65 try:
66 ces.api_endpoint = "https://api.exchangerate.host/convert"
67 ces.append('result_key', {'key': 'result'})
68 ces.append('req_params', {'key': 'date', 'value': '{transaction_date}'})
69 ces.append('req_params', {'key': 'from', 'value': '{from_currency}'})
70 ces.append('req_params', {'key': 'to', 'value': '{to_currency}'})
71 ces.save()
72 except frappe.ValidationError:
73 pass
tundebabzycad22db2018-02-22 06:38:36 +010074
Rushabh Mehtad42167e2016-05-11 16:47:14 +053075def create_compact_item_print_custom_field():
Rushabh Mehtad42167e2016-05-11 16:47:14 +053076 create_custom_field('Print Settings', {
77 'label': _('Compact Item Print'),
78 'fieldname': 'compact_item_print',
79 'fieldtype': 'Check',
80 'default': 1,
81 'insert_after': 'with_letterhead'
Nabin Hait45dce892017-09-14 15:17:38 +053082 })
83
tundebabzycad22db2018-02-22 06:38:36 +010084
Frappe ERPnextf28bef82020-09-02 21:42:52 +020085def create_print_uom_after_qty_custom_field():
86 create_custom_field('Print Settings', {
87 'label': _('Print UOM after Quantity'),
88 'fieldname': 'print_uom_after_quantity',
89 'fieldtype': 'Check',
90 'default': 0,
91 'insert_after': 'compact_item_print'
92 })
93
94
Nabin Hait45dce892017-09-14 15:17:38 +053095def create_print_zero_amount_taxes_custom_field():
96 create_custom_field('Print Settings', {
97 'label': _('Print taxes with zero amount'),
98 'fieldname': 'print_taxes_with_zero_amount',
99 'fieldtype': 'Check',
100 'default': 0,
101 'insert_after': 'allow_print_for_cancelled'
tundebabzycad22db2018-02-22 06:38:36 +0100102 })
103
104
105def create_default_cash_flow_mapper_templates():
Suraj Shetty00cced12018-05-03 19:06:32 +0530106 for mapper in DEFAULT_MAPPERS:
tundebabzycad22db2018-02-22 06:38:36 +0100107 if not frappe.db.exists('Cash Flow Mapper', mapper['section_name']):
108 doc = frappe.get_doc(mapper)
109 doc.insert(ignore_permissions=True)
Suraj Shetty00cced12018-05-03 19:06:32 +0530110
111def create_default_success_action():
112 for success_action in get_default_success_action():
113 if not frappe.db.exists('Success Action', success_action.get("ref_doctype")):
114 doc = frappe.get_doc(success_action)
115 doc.insert(ignore_permissions=True)
Rucha Mahabal4865eab2019-07-24 13:37:54 +0530116
Suraj Shetty627a3dc2019-09-17 15:54:41 +0530117def create_default_energy_point_rules():
118
119 for rule in get_default_energy_point_rules():
120 # check if any rule for ref. doctype exists
121 rule_exists = frappe.db.exists('Energy Point Rule', {
122 'reference_doctype': rule.get('reference_doctype')
123 })
124 if rule_exists: continue
125 doc = frappe.get_doc(rule)
126 doc.insert(ignore_permissions=True)
127
Rucha Mahabal4865eab2019-07-24 13:37:54 +0530128def add_company_to_session_defaults():
129 settings = frappe.get_single("Session Default Settings")
130 settings.append("session_defaults", {
131 "ref_doctype": "Company"
132 })
133 settings.save()
Deepesh Garga306af82020-08-06 20:52:02 +0530134
135def add_standard_navbar_items():
136 navbar_settings = frappe.get_single("Navbar Settings")
137
138 erpnext_navbar_items = [
139 {
140 'item_label': 'Documentation',
141 'item_type': 'Route',
142 'route': 'https://erpnext.com/docs/user/manual',
143 'is_standard': 1
144 },
145 {
146 'item_label': 'User Forum',
147 'item_type': 'Route',
148 'route': 'https://discuss.erpnext.com',
149 'is_standard': 1
150 },
151 {
152 'item_label': 'Report an Issue',
153 'item_type': 'Route',
154 'route': 'https://github.com/frappe/erpnext/issues',
155 'is_standard': 1
156 }
157 ]
158
prssanna194dd122021-03-24 16:45:32 +0530159 current_navbar_items = navbar_settings.help_dropdown
Deepesh Garga306af82020-08-06 20:52:02 +0530160 navbar_settings.set('help_dropdown', [])
161
162 for item in erpnext_navbar_items:
prssanna194dd122021-03-24 16:45:32 +0530163 current_labels = [item.get('item_label') for item in current_navbar_items]
164 if not item.get('item_label') in current_labels:
165 navbar_settings.append('help_dropdown', item)
Deepesh Garga306af82020-08-06 20:52:02 +0530166
prssanna194dd122021-03-24 16:45:32 +0530167 for item in current_navbar_items:
Deepesh Garga306af82020-08-06 20:52:02 +0530168 navbar_settings.append('help_dropdown', {
169 'item_label': item.item_label,
170 'item_type': item.item_type,
171 'route': item.route,
172 'action': item.action,
173 'is_standard': item.is_standard,
174 'hidden': item.hidden
175 })
176
177 navbar_settings.save()
Shivam Mishra1323a9a2020-09-30 16:33:14 +0530178
179def add_app_name():
Rushabh Mehta45735b32021-03-16 12:22:31 +0530180 frappe.db.set_value('System Settings', None, 'app_name', 'ERPNext')
Rohit Waghchaure03635fb2021-01-18 15:06:35 +0530181
182def add_non_standard_user_types():
183 user_types = get_user_types_data()
184
185 user_type_limit = {}
186 for user_type, data in iteritems(user_types):
187 user_type_limit.setdefault(frappe.scrub(user_type), 10)
188
189 update_site_config('user_type_doctype_limit', user_type_limit)
190
191 for user_type, data in iteritems(user_types):
192 create_custom_role(data)
193 create_user_type(user_type, data)
194
195def get_user_types_data():
196 return {
Rohit Waghchaure610ccd42021-03-16 00:39:10 +0530197 'Employee Self Service': {
198 'role': 'Employee Self Service',
Rohit Waghchaure03635fb2021-01-18 15:06:35 +0530199 'apply_user_permission_on': 'Employee',
200 'user_id_field': 'user_id',
201 'doctypes': {
202 'Salary Slip': ['read'],
203 'Employee': ['read', 'write'],
Rohit Waghchaure610ccd42021-03-16 00:39:10 +0530204 'Expense Claim': ['read', 'write', 'create', 'delete'],
205 'Leave Application': ['read', 'write', 'create', 'delete'],
206 'Attendance Request': ['read', 'write', 'create', 'delete'],
207 'Compensatory Leave Request': ['read', 'write', 'create', 'delete'],
208 'Employee Tax Exemption Declaration': ['read', 'write', 'create', 'delete'],
209 'Employee Tax Exemption Proof Submission': ['read', 'write', 'create', 'delete'],
210 'Timesheet': ['read', 'write', 'create', 'delete', 'submit', 'cancel', 'amend']
Rohit Waghchaure03635fb2021-01-18 15:06:35 +0530211 }
212 }
213 }
214
215def create_custom_role(data):
216 if data.get('role') and not frappe.db.exists('Role', data.get('role')):
217 frappe.get_doc({
218 'doctype': 'Role',
219 'role_name': data.get('role'),
220 'desk_access': 1,
221 'is_custom': 1
222 }).insert(ignore_permissions=True)
223
224def create_user_type(user_type, data):
225 if frappe.db.exists('User Type', user_type):
226 doc = frappe.get_cached_doc('User Type', user_type)
227 doc.user_doctypes = []
228 else:
229 doc = frappe.new_doc('User Type')
230 doc.update({
231 'name': user_type,
232 'role': data.get('role'),
233 'user_id_field': data.get('user_id_field'),
234 'apply_user_permission_on': data.get('apply_user_permission_on')
235 })
236
237 create_role_permissions_for_doctype(doc, data)
238 doc.save(ignore_permissions=True)
239
240def create_role_permissions_for_doctype(doc, data):
241 for doctype, perms in iteritems(data.get('doctypes')):
242 args = {'document_type': doctype}
243 for perm in perms:
244 args[perm] = 1
245
246 doc.append('user_doctypes', args)
Rohit Waghchaurea8f78fa2021-04-13 18:43:57 +0530247
248def update_select_perm_after_install():
249 if not frappe.flags.update_select_perm_after_migrate:
250 return
251
252 frappe.flags.ignore_select_perm = False
253 for row in frappe.get_all('User Type', filters= {'is_standard': 0}):
254 print('Updating user type :- ', row.name)
255 doc = frappe.get_doc('User Type', row.name)
256 doc.save()
257
258 frappe.flags.update_select_perm_after_migrate = False