Anand Doshi | 885e074 | 2015-03-03 14:55:30 +0530 | [diff] [blame] | 1 | # Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors |
Rushabh Mehta | e67d1fb | 2013-08-05 14:59:54 +0530 | [diff] [blame] | 2 | # License: GNU General Public License v3. See license.txt |
| 3 | |
Rushabh Mehta | 6f9915c | 2013-01-16 17:48:17 +0530 | [diff] [blame] | 4 | |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 5 | import frappe |
Rushabh Mehta | d42167e | 2016-05-11 16:47:14 +0530 | [diff] [blame] | 6 | from frappe import _ |
Nabin Hait | 45dce89 | 2017-09-14 15:17:38 +0530 | [diff] [blame] | 7 | from frappe.custom.doctype.custom_field.custom_field import create_custom_field |
Chillar Anand | 915b343 | 2021-09-02 16:44:59 +0530 | [diff] [blame] | 8 | from frappe.desk.page.setup_wizard.setup_wizard import add_all_roles_to |
| 9 | from frappe.installer import update_site_config |
| 10 | from frappe.utils import cint |
Rushabh Mehta | 6f9915c | 2013-01-16 17:48:17 +0530 | [diff] [blame] | 11 | |
Chillar Anand | 915b343 | 2021-09-02 16:44:59 +0530 | [diff] [blame] | 12 | from erpnext.accounts.doctype.cash_flow_mapper.default_cash_flow_mapper import DEFAULT_MAPPERS |
| 13 | from erpnext.setup.default_energy_point_rules import get_default_energy_point_rules |
| 14 | |
| 15 | from .default_success_action import get_default_success_action |
| 16 | |
Anand Doshi | 56198f4 | 2014-06-26 12:47:45 +0530 | [diff] [blame] | 17 | default_mail_footer = """<div style="padding: 7px; text-align: right; color: #888"><small>Sent via |
Anand Doshi | 7f41ff2 | 2014-06-26 12:02:55 +0530 | [diff] [blame] | 18 | <a style="color: #888" href="http://erpnext.org">ERPNext</a></div>""" |
| 19 | |
tundebabzy | cad22db | 2018-02-22 06:38:36 +0100 | [diff] [blame] | 20 | |
Rushabh Mehta | 1f84799 | 2013-12-12 19:12:19 +0530 | [diff] [blame] | 21 | def after_install(): |
Rushabh Mehta | 40b2b03 | 2014-04-21 13:43:11 +0530 | [diff] [blame] | 22 | frappe.get_doc({'doctype': "Role", "role_name": "Analytics"}).insert() |
Rushabh Mehta | 33003c6 | 2014-04-30 11:16:02 +0530 | [diff] [blame] | 23 | set_single_defaults() |
Rushabh Mehta | d42167e | 2016-05-11 16:47:14 +0530 | [diff] [blame] | 24 | create_compact_item_print_custom_field() |
Frappe ERPnext | f28bef8 | 2020-09-02 21:42:52 +0200 | [diff] [blame] | 25 | create_print_uom_after_qty_custom_field() |
Nabin Hait | 45dce89 | 2017-09-14 15:17:38 +0530 | [diff] [blame] | 26 | create_print_zero_amount_taxes_custom_field() |
Rushabh Mehta | db7139a | 2013-01-17 18:22:22 +0530 | [diff] [blame] | 27 | add_all_roles_to("Administrator") |
tundebabzy | cad22db | 2018-02-22 06:38:36 +0100 | [diff] [blame] | 28 | create_default_cash_flow_mapper_templates() |
Suraj Shetty | 00cced1 | 2018-05-03 19:06:32 +0530 | [diff] [blame] | 29 | create_default_success_action() |
Suraj Shetty | 627a3dc | 2019-09-17 15:54:41 +0530 | [diff] [blame] | 30 | create_default_energy_point_rules() |
Rucha Mahabal | 4865eab | 2019-07-24 13:37:54 +0530 | [diff] [blame] | 31 | add_company_to_session_defaults() |
Deepesh Garg | a306af8 | 2020-08-06 20:52:02 +0530 | [diff] [blame] | 32 | add_standard_navbar_items() |
Shivam Mishra | 1323a9a | 2020-09-30 16:33:14 +0530 | [diff] [blame] | 33 | add_app_name() |
Rohit Waghchaure | 03635fb | 2021-01-18 15:06:35 +0530 | [diff] [blame] | 34 | add_non_standard_user_types() |
Anand Doshi | e9baaa6 | 2014-02-26 12:35:33 +0530 | [diff] [blame] | 35 | frappe.db.commit() |
Rushabh Mehta | db7139a | 2013-01-17 18:22:22 +0530 | [diff] [blame] | 36 | |
tundebabzy | cad22db | 2018-02-22 06:38:36 +0100 | [diff] [blame] | 37 | |
Rushabh Mehta | d55bdcf | 2015-12-31 11:12:48 +0530 | [diff] [blame] | 38 | def check_setup_wizard_not_completed(): |
Saurabh | 58d05ac | 2020-08-10 19:36:45 +0530 | [diff] [blame] | 39 | if cint(frappe.db.get_single_value('System Settings', 'setup_complete') or 0): |
Deepesh Garg | a306af8 | 2020-08-06 20:52:02 +0530 | [diff] [blame] | 40 | message = """ERPNext can only be installed on a fresh site where the setup wizard is not completed. |
Faris Ansari | f3a6e30 | 2020-07-08 10:45:55 +0530 | [diff] [blame] | 41 | You can reinstall this site (after saving your data) using: bench --site [sitename] reinstall""" |
Ankush Menat | ff96bdf | 2021-05-25 19:54:40 +0530 | [diff] [blame] | 42 | frappe.throw(message) # nosemgrep |
Rushabh Mehta | d55bdcf | 2015-12-31 11:12:48 +0530 | [diff] [blame] | 43 | |
tundebabzy | cad22db | 2018-02-22 06:38:36 +0100 | [diff] [blame] | 44 | |
Pratik Vyas | 28da752 | 2014-02-19 20:53:45 +0530 | [diff] [blame] | 45 | def set_single_defaults(): |
Rushabh Mehta | b62ed7a | 2016-10-13 11:00:00 +0530 | [diff] [blame] | 46 | for dt in ('Accounts Settings', 'Print Settings', 'HR Settings', 'Buying Settings', |
Suraj Shetty | d3069fe | 2018-02-21 15:15:43 +0530 | [diff] [blame] | 47 | 'Selling Settings', 'Stock Settings'): |
Anand Doshi | eb7fea6 | 2014-03-19 17:10:01 +0530 | [diff] [blame] | 48 | default_values = frappe.db.sql("""select fieldname, `default` from `tabDocField` |
Rushabh Mehta | f14b809 | 2014-04-03 14:30:42 +0530 | [diff] [blame] | 49 | where parent=%s""", dt) |
Anand Doshi | eb7fea6 | 2014-03-19 17:10:01 +0530 | [diff] [blame] | 50 | if default_values: |
| 51 | try: |
Rushabh Mehta | b385ecf | 2014-03-28 16:44:37 +0530 | [diff] [blame] | 52 | b = frappe.get_doc(dt, dt) |
Anand Doshi | eb7fea6 | 2014-03-19 17:10:01 +0530 | [diff] [blame] | 53 | for fieldname, value in default_values: |
Anand Doshi | f78d1ae | 2014-03-28 13:55:00 +0530 | [diff] [blame] | 54 | b.set(fieldname, value) |
Anand Doshi | eb7fea6 | 2014-03-19 17:10:01 +0530 | [diff] [blame] | 55 | b.save() |
| 56 | except frappe.MandatoryError: |
| 57 | pass |
Rushabh Mehta | b62ed7a | 2016-10-13 11:00:00 +0530 | [diff] [blame] | 58 | except frappe.ValidationError: |
| 59 | pass |
Anand Doshi | eb7fea6 | 2014-03-19 17:10:01 +0530 | [diff] [blame] | 60 | |
Anand Doshi | e9baaa6 | 2014-02-26 12:35:33 +0530 | [diff] [blame] | 61 | frappe.db.set_default("date_format", "dd-mm-yyyy") |
Rushabh Mehta | d42167e | 2016-05-11 16:47:14 +0530 | [diff] [blame] | 62 | |
tundebabzy | cad22db | 2018-02-22 06:38:36 +0100 | [diff] [blame] | 63 | |
Rushabh Mehta | d42167e | 2016-05-11 16:47:14 +0530 | [diff] [blame] | 64 | def create_compact_item_print_custom_field(): |
Rushabh Mehta | d42167e | 2016-05-11 16:47:14 +0530 | [diff] [blame] | 65 | create_custom_field('Print Settings', { |
| 66 | 'label': _('Compact Item Print'), |
| 67 | 'fieldname': 'compact_item_print', |
| 68 | 'fieldtype': 'Check', |
| 69 | 'default': 1, |
| 70 | 'insert_after': 'with_letterhead' |
Nabin Hait | 45dce89 | 2017-09-14 15:17:38 +0530 | [diff] [blame] | 71 | }) |
| 72 | |
tundebabzy | cad22db | 2018-02-22 06:38:36 +0100 | [diff] [blame] | 73 | |
Frappe ERPnext | f28bef8 | 2020-09-02 21:42:52 +0200 | [diff] [blame] | 74 | def create_print_uom_after_qty_custom_field(): |
| 75 | create_custom_field('Print Settings', { |
| 76 | 'label': _('Print UOM after Quantity'), |
| 77 | 'fieldname': 'print_uom_after_quantity', |
| 78 | 'fieldtype': 'Check', |
| 79 | 'default': 0, |
| 80 | 'insert_after': 'compact_item_print' |
| 81 | }) |
| 82 | |
| 83 | |
Nabin Hait | 45dce89 | 2017-09-14 15:17:38 +0530 | [diff] [blame] | 84 | def create_print_zero_amount_taxes_custom_field(): |
| 85 | create_custom_field('Print Settings', { |
| 86 | 'label': _('Print taxes with zero amount'), |
| 87 | 'fieldname': 'print_taxes_with_zero_amount', |
| 88 | 'fieldtype': 'Check', |
| 89 | 'default': 0, |
| 90 | 'insert_after': 'allow_print_for_cancelled' |
tundebabzy | cad22db | 2018-02-22 06:38:36 +0100 | [diff] [blame] | 91 | }) |
| 92 | |
| 93 | |
| 94 | def create_default_cash_flow_mapper_templates(): |
Suraj Shetty | 00cced1 | 2018-05-03 19:06:32 +0530 | [diff] [blame] | 95 | for mapper in DEFAULT_MAPPERS: |
tundebabzy | cad22db | 2018-02-22 06:38:36 +0100 | [diff] [blame] | 96 | if not frappe.db.exists('Cash Flow Mapper', mapper['section_name']): |
| 97 | doc = frappe.get_doc(mapper) |
| 98 | doc.insert(ignore_permissions=True) |
Suraj Shetty | 00cced1 | 2018-05-03 19:06:32 +0530 | [diff] [blame] | 99 | |
| 100 | def create_default_success_action(): |
| 101 | for success_action in get_default_success_action(): |
| 102 | if not frappe.db.exists('Success Action', success_action.get("ref_doctype")): |
| 103 | doc = frappe.get_doc(success_action) |
| 104 | doc.insert(ignore_permissions=True) |
Rucha Mahabal | 4865eab | 2019-07-24 13:37:54 +0530 | [diff] [blame] | 105 | |
Suraj Shetty | 627a3dc | 2019-09-17 15:54:41 +0530 | [diff] [blame] | 106 | def create_default_energy_point_rules(): |
| 107 | |
| 108 | for rule in get_default_energy_point_rules(): |
| 109 | # check if any rule for ref. doctype exists |
| 110 | rule_exists = frappe.db.exists('Energy Point Rule', { |
| 111 | 'reference_doctype': rule.get('reference_doctype') |
| 112 | }) |
| 113 | if rule_exists: continue |
| 114 | doc = frappe.get_doc(rule) |
| 115 | doc.insert(ignore_permissions=True) |
| 116 | |
Rucha Mahabal | 4865eab | 2019-07-24 13:37:54 +0530 | [diff] [blame] | 117 | def add_company_to_session_defaults(): |
| 118 | settings = frappe.get_single("Session Default Settings") |
| 119 | settings.append("session_defaults", { |
| 120 | "ref_doctype": "Company" |
| 121 | }) |
| 122 | settings.save() |
Deepesh Garg | a306af8 | 2020-08-06 20:52:02 +0530 | [diff] [blame] | 123 | |
| 124 | def add_standard_navbar_items(): |
| 125 | navbar_settings = frappe.get_single("Navbar Settings") |
| 126 | |
| 127 | erpnext_navbar_items = [ |
| 128 | { |
| 129 | 'item_label': 'Documentation', |
| 130 | 'item_type': 'Route', |
| 131 | 'route': 'https://erpnext.com/docs/user/manual', |
| 132 | 'is_standard': 1 |
| 133 | }, |
| 134 | { |
| 135 | 'item_label': 'User Forum', |
| 136 | 'item_type': 'Route', |
| 137 | 'route': 'https://discuss.erpnext.com', |
| 138 | 'is_standard': 1 |
| 139 | }, |
| 140 | { |
| 141 | 'item_label': 'Report an Issue', |
| 142 | 'item_type': 'Route', |
| 143 | 'route': 'https://github.com/frappe/erpnext/issues', |
| 144 | 'is_standard': 1 |
| 145 | } |
| 146 | ] |
| 147 | |
prssanna | 194dd12 | 2021-03-24 16:45:32 +0530 | [diff] [blame] | 148 | current_navbar_items = navbar_settings.help_dropdown |
Deepesh Garg | a306af8 | 2020-08-06 20:52:02 +0530 | [diff] [blame] | 149 | navbar_settings.set('help_dropdown', []) |
| 150 | |
| 151 | for item in erpnext_navbar_items: |
prssanna | 194dd12 | 2021-03-24 16:45:32 +0530 | [diff] [blame] | 152 | current_labels = [item.get('item_label') for item in current_navbar_items] |
| 153 | if not item.get('item_label') in current_labels: |
| 154 | navbar_settings.append('help_dropdown', item) |
Deepesh Garg | a306af8 | 2020-08-06 20:52:02 +0530 | [diff] [blame] | 155 | |
prssanna | 194dd12 | 2021-03-24 16:45:32 +0530 | [diff] [blame] | 156 | for item in current_navbar_items: |
Deepesh Garg | a306af8 | 2020-08-06 20:52:02 +0530 | [diff] [blame] | 157 | navbar_settings.append('help_dropdown', { |
| 158 | 'item_label': item.item_label, |
| 159 | 'item_type': item.item_type, |
| 160 | 'route': item.route, |
| 161 | 'action': item.action, |
| 162 | 'is_standard': item.is_standard, |
| 163 | 'hidden': item.hidden |
| 164 | }) |
| 165 | |
| 166 | navbar_settings.save() |
Shivam Mishra | 1323a9a | 2020-09-30 16:33:14 +0530 | [diff] [blame] | 167 | |
| 168 | def add_app_name(): |
Rushabh Mehta | 45735b3 | 2021-03-16 12:22:31 +0530 | [diff] [blame] | 169 | frappe.db.set_value('System Settings', None, 'app_name', 'ERPNext') |
Rohit Waghchaure | 03635fb | 2021-01-18 15:06:35 +0530 | [diff] [blame] | 170 | |
| 171 | def add_non_standard_user_types(): |
| 172 | user_types = get_user_types_data() |
| 173 | |
| 174 | user_type_limit = {} |
Ankush Menat | 8fe5feb | 2021-11-04 19:48:32 +0530 | [diff] [blame] | 175 | for user_type, data in user_types.items(): |
Rohit Waghchaure | 03635fb | 2021-01-18 15:06:35 +0530 | [diff] [blame] | 176 | user_type_limit.setdefault(frappe.scrub(user_type), 10) |
| 177 | |
| 178 | update_site_config('user_type_doctype_limit', user_type_limit) |
| 179 | |
Ankush Menat | 8fe5feb | 2021-11-04 19:48:32 +0530 | [diff] [blame] | 180 | for user_type, data in user_types.items(): |
Rohit Waghchaure | 03635fb | 2021-01-18 15:06:35 +0530 | [diff] [blame] | 181 | create_custom_role(data) |
| 182 | create_user_type(user_type, data) |
| 183 | |
| 184 | def get_user_types_data(): |
| 185 | return { |
Rohit Waghchaure | 610ccd4 | 2021-03-16 00:39:10 +0530 | [diff] [blame] | 186 | 'Employee Self Service': { |
| 187 | 'role': 'Employee Self Service', |
Rohit Waghchaure | 03635fb | 2021-01-18 15:06:35 +0530 | [diff] [blame] | 188 | 'apply_user_permission_on': 'Employee', |
| 189 | 'user_id_field': 'user_id', |
| 190 | 'doctypes': { |
| 191 | 'Salary Slip': ['read'], |
| 192 | 'Employee': ['read', 'write'], |
Rohit Waghchaure | 610ccd4 | 2021-03-16 00:39:10 +0530 | [diff] [blame] | 193 | 'Expense Claim': ['read', 'write', 'create', 'delete'], |
| 194 | 'Leave Application': ['read', 'write', 'create', 'delete'], |
| 195 | 'Attendance Request': ['read', 'write', 'create', 'delete'], |
| 196 | 'Compensatory Leave Request': ['read', 'write', 'create', 'delete'], |
| 197 | 'Employee Tax Exemption Declaration': ['read', 'write', 'create', 'delete'], |
| 198 | 'Employee Tax Exemption Proof Submission': ['read', 'write', 'create', 'delete'], |
| 199 | 'Timesheet': ['read', 'write', 'create', 'delete', 'submit', 'cancel', 'amend'] |
Rohit Waghchaure | 03635fb | 2021-01-18 15:06:35 +0530 | [diff] [blame] | 200 | } |
| 201 | } |
| 202 | } |
| 203 | |
| 204 | def create_custom_role(data): |
| 205 | if data.get('role') and not frappe.db.exists('Role', data.get('role')): |
| 206 | frappe.get_doc({ |
| 207 | 'doctype': 'Role', |
| 208 | 'role_name': data.get('role'), |
| 209 | 'desk_access': 1, |
| 210 | 'is_custom': 1 |
| 211 | }).insert(ignore_permissions=True) |
| 212 | |
| 213 | def create_user_type(user_type, data): |
| 214 | if frappe.db.exists('User Type', user_type): |
| 215 | doc = frappe.get_cached_doc('User Type', user_type) |
| 216 | doc.user_doctypes = [] |
| 217 | else: |
| 218 | doc = frappe.new_doc('User Type') |
| 219 | doc.update({ |
| 220 | 'name': user_type, |
| 221 | 'role': data.get('role'), |
| 222 | 'user_id_field': data.get('user_id_field'), |
| 223 | 'apply_user_permission_on': data.get('apply_user_permission_on') |
| 224 | }) |
| 225 | |
| 226 | create_role_permissions_for_doctype(doc, data) |
| 227 | doc.save(ignore_permissions=True) |
| 228 | |
| 229 | def create_role_permissions_for_doctype(doc, data): |
Ankush Menat | 8fe5feb | 2021-11-04 19:48:32 +0530 | [diff] [blame] | 230 | for doctype, perms in data.get('doctypes').items(): |
Rohit Waghchaure | 03635fb | 2021-01-18 15:06:35 +0530 | [diff] [blame] | 231 | args = {'document_type': doctype} |
| 232 | for perm in perms: |
| 233 | args[perm] = 1 |
| 234 | |
| 235 | doc.append('user_doctypes', args) |
Rohit Waghchaure | a8f78fa | 2021-04-13 18:43:57 +0530 | [diff] [blame] | 236 | |
| 237 | def update_select_perm_after_install(): |
| 238 | if not frappe.flags.update_select_perm_after_migrate: |
| 239 | return |
| 240 | |
| 241 | frappe.flags.ignore_select_perm = False |
| 242 | for row in frappe.get_all('User Type', filters= {'is_standard': 0}): |
| 243 | print('Updating user type :- ', row.name) |
| 244 | doc = frappe.get_doc('User Type', row.name) |
| 245 | doc.save() |
| 246 | |
| 247 | frappe.flags.update_select_perm_after_migrate = False |