blob: da397763331dd8c4b8a86f71c82c402762da168f [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")
62 ces = frappe.get_single('Currency Exchange Settings')
63 try:
64 ces.api_endpoint = "https://api.exchangerate.host/convert"
65 ces.append('result_key', {'key': 'result'})
66 ces.append('req_params', {'key': 'date', 'value': '{transaction_date}'})
67 ces.append('req_params', {'key': 'from', 'value': '{from_currency}'})
68 ces.append('req_params', {'key': 'to', 'value': '{to_currency}'})
69 ces.save()
70 except frappe.ValidationError:
71 pass
tundebabzycad22db2018-02-22 06:38:36 +010072
Rushabh Mehtad42167e2016-05-11 16:47:14 +053073def create_compact_item_print_custom_field():
Rushabh Mehtad42167e2016-05-11 16:47:14 +053074 create_custom_field('Print Settings', {
75 'label': _('Compact Item Print'),
76 'fieldname': 'compact_item_print',
77 'fieldtype': 'Check',
78 'default': 1,
79 'insert_after': 'with_letterhead'
Nabin Hait45dce892017-09-14 15:17:38 +053080 })
81
tundebabzycad22db2018-02-22 06:38:36 +010082
Frappe ERPnextf28bef82020-09-02 21:42:52 +020083def create_print_uom_after_qty_custom_field():
84 create_custom_field('Print Settings', {
85 'label': _('Print UOM after Quantity'),
86 'fieldname': 'print_uom_after_quantity',
87 'fieldtype': 'Check',
88 'default': 0,
89 'insert_after': 'compact_item_print'
90 })
91
92
Nabin Hait45dce892017-09-14 15:17:38 +053093def create_print_zero_amount_taxes_custom_field():
94 create_custom_field('Print Settings', {
95 'label': _('Print taxes with zero amount'),
96 'fieldname': 'print_taxes_with_zero_amount',
97 'fieldtype': 'Check',
98 'default': 0,
99 'insert_after': 'allow_print_for_cancelled'
tundebabzycad22db2018-02-22 06:38:36 +0100100 })
101
102
103def create_default_cash_flow_mapper_templates():
Suraj Shetty00cced12018-05-03 19:06:32 +0530104 for mapper in DEFAULT_MAPPERS:
tundebabzycad22db2018-02-22 06:38:36 +0100105 if not frappe.db.exists('Cash Flow Mapper', mapper['section_name']):
106 doc = frappe.get_doc(mapper)
107 doc.insert(ignore_permissions=True)
Suraj Shetty00cced12018-05-03 19:06:32 +0530108
109def create_default_success_action():
110 for success_action in get_default_success_action():
111 if not frappe.db.exists('Success Action', success_action.get("ref_doctype")):
112 doc = frappe.get_doc(success_action)
113 doc.insert(ignore_permissions=True)
Rucha Mahabal4865eab2019-07-24 13:37:54 +0530114
Suraj Shetty627a3dc2019-09-17 15:54:41 +0530115def create_default_energy_point_rules():
116
117 for rule in get_default_energy_point_rules():
118 # check if any rule for ref. doctype exists
119 rule_exists = frappe.db.exists('Energy Point Rule', {
120 'reference_doctype': rule.get('reference_doctype')
121 })
122 if rule_exists: continue
123 doc = frappe.get_doc(rule)
124 doc.insert(ignore_permissions=True)
125
Rucha Mahabal4865eab2019-07-24 13:37:54 +0530126def add_company_to_session_defaults():
127 settings = frappe.get_single("Session Default Settings")
128 settings.append("session_defaults", {
129 "ref_doctype": "Company"
130 })
131 settings.save()
Deepesh Garga306af82020-08-06 20:52:02 +0530132
133def add_standard_navbar_items():
134 navbar_settings = frappe.get_single("Navbar Settings")
135
136 erpnext_navbar_items = [
137 {
138 'item_label': 'Documentation',
139 'item_type': 'Route',
140 'route': 'https://erpnext.com/docs/user/manual',
141 'is_standard': 1
142 },
143 {
144 'item_label': 'User Forum',
145 'item_type': 'Route',
146 'route': 'https://discuss.erpnext.com',
147 'is_standard': 1
148 },
149 {
150 'item_label': 'Report an Issue',
151 'item_type': 'Route',
152 'route': 'https://github.com/frappe/erpnext/issues',
153 'is_standard': 1
154 }
155 ]
156
prssanna194dd122021-03-24 16:45:32 +0530157 current_navbar_items = navbar_settings.help_dropdown
Deepesh Garga306af82020-08-06 20:52:02 +0530158 navbar_settings.set('help_dropdown', [])
159
160 for item in erpnext_navbar_items:
prssanna194dd122021-03-24 16:45:32 +0530161 current_labels = [item.get('item_label') for item in current_navbar_items]
162 if not item.get('item_label') in current_labels:
163 navbar_settings.append('help_dropdown', item)
Deepesh Garga306af82020-08-06 20:52:02 +0530164
prssanna194dd122021-03-24 16:45:32 +0530165 for item in current_navbar_items:
Deepesh Garga306af82020-08-06 20:52:02 +0530166 navbar_settings.append('help_dropdown', {
167 'item_label': item.item_label,
168 'item_type': item.item_type,
169 'route': item.route,
170 'action': item.action,
171 'is_standard': item.is_standard,
172 'hidden': item.hidden
173 })
174
175 navbar_settings.save()
Shivam Mishra1323a9a2020-09-30 16:33:14 +0530176
177def add_app_name():
Rushabh Mehta45735b32021-03-16 12:22:31 +0530178 frappe.db.set_value('System Settings', None, 'app_name', 'ERPNext')
Rohit Waghchaure03635fb2021-01-18 15:06:35 +0530179
180def add_non_standard_user_types():
181 user_types = get_user_types_data()
182
183 user_type_limit = {}
Ankush Menat8fe5feb2021-11-04 19:48:32 +0530184 for user_type, data in user_types.items():
Rohit Waghchaure03635fb2021-01-18 15:06:35 +0530185 user_type_limit.setdefault(frappe.scrub(user_type), 10)
186
187 update_site_config('user_type_doctype_limit', user_type_limit)
188
Ankush Menat8fe5feb2021-11-04 19:48:32 +0530189 for user_type, data in user_types.items():
Rohit Waghchaure03635fb2021-01-18 15:06:35 +0530190 create_custom_role(data)
191 create_user_type(user_type, data)
192
193def get_user_types_data():
194 return {
Rohit Waghchaure610ccd42021-03-16 00:39:10 +0530195 'Employee Self Service': {
196 'role': 'Employee Self Service',
Rohit Waghchaure03635fb2021-01-18 15:06:35 +0530197 'apply_user_permission_on': 'Employee',
198 'user_id_field': 'user_id',
199 'doctypes': {
200 'Salary Slip': ['read'],
201 'Employee': ['read', 'write'],
Rohit Waghchaure610ccd42021-03-16 00:39:10 +0530202 'Expense Claim': ['read', 'write', 'create', 'delete'],
203 'Leave Application': ['read', 'write', 'create', 'delete'],
204 'Attendance Request': ['read', 'write', 'create', 'delete'],
205 'Compensatory Leave Request': ['read', 'write', 'create', 'delete'],
206 'Employee Tax Exemption Declaration': ['read', 'write', 'create', 'delete'],
207 'Employee Tax Exemption Proof Submission': ['read', 'write', 'create', 'delete'],
208 'Timesheet': ['read', 'write', 'create', 'delete', 'submit', 'cancel', 'amend']
Rohit Waghchaure03635fb2021-01-18 15:06:35 +0530209 }
210 }
211 }
212
213def create_custom_role(data):
214 if data.get('role') and not frappe.db.exists('Role', data.get('role')):
215 frappe.get_doc({
216 'doctype': 'Role',
217 'role_name': data.get('role'),
218 'desk_access': 1,
219 'is_custom': 1
220 }).insert(ignore_permissions=True)
221
222def create_user_type(user_type, data):
223 if frappe.db.exists('User Type', user_type):
224 doc = frappe.get_cached_doc('User Type', user_type)
225 doc.user_doctypes = []
226 else:
227 doc = frappe.new_doc('User Type')
228 doc.update({
229 'name': user_type,
230 'role': data.get('role'),
231 'user_id_field': data.get('user_id_field'),
232 'apply_user_permission_on': data.get('apply_user_permission_on')
233 })
234
235 create_role_permissions_for_doctype(doc, data)
236 doc.save(ignore_permissions=True)
237
238def create_role_permissions_for_doctype(doc, data):
Ankush Menat8fe5feb2021-11-04 19:48:32 +0530239 for doctype, perms in data.get('doctypes').items():
Rohit Waghchaure03635fb2021-01-18 15:06:35 +0530240 args = {'document_type': doctype}
241 for perm in perms:
242 args[perm] = 1
243
244 doc.append('user_doctypes', args)
Rohit Waghchaurea8f78fa2021-04-13 18:43:57 +0530245
246def update_select_perm_after_install():
247 if not frappe.flags.update_select_perm_after_migrate:
248 return
249
250 frappe.flags.ignore_select_perm = False
251 for row in frappe.get_all('User Type', filters= {'is_standard': 0}):
252 print('Updating user type :- ', row.name)
253 doc = frappe.get_doc('User Type', row.name)
254 doc.save()
255
256 frappe.flags.update_select_perm_after_migrate = False