blob: b106cfcc1a02d5775330c2e8415a8de9edc62295 [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
Ankush Menat1e8ee932023-06-12 19:20:52 +05305import click
Rushabh Mehta793ba6b2014-02-14 15:47:51 +05306import frappe
Rushabh Mehtad42167e2016-05-11 16:47:14 +05307from frappe import _
Sagar Voraaafb7352022-08-18 15:31:20 +00008from frappe.custom.doctype.custom_field.custom_field import create_custom_fields
Chillar Anand915b3432021-09-02 16:44:59 +05309from frappe.desk.page.setup_wizard.setup_wizard import add_all_roles_to
Chillar Anand915b3432021-09-02 16:44:59 +053010from frappe.utils import cint
Rushabh Mehta6f9915c2013-01-16 17:48:17 +053011
Ankush Menat1e8ee932023-06-12 19:20:52 +053012import erpnext
Chillar Anand915b3432021-09-02 16:44:59 +053013from erpnext.setup.default_energy_point_rules import get_default_energy_point_rules
barredterrace83f022022-11-17 22:17:19 +010014from erpnext.setup.doctype.incoterm.incoterm import create_incoterms
Chillar Anand915b3432021-09-02 16:44:59 +053015
16from .default_success_action import get_default_success_action
17
Anand Doshi56198f42014-06-26 12:47:45 +053018default_mail_footer = """<div style="padding: 7px; text-align: right; color: #888"><small>Sent via
Anand Doshi7f41ff22014-06-26 12:02:55 +053019 <a style="color: #888" href="http://erpnext.org">ERPNext</a></div>"""
20
tundebabzycad22db2018-02-22 06:38:36 +010021
Rushabh Mehta1f847992013-12-12 19:12:19 +053022def after_install():
Ankush Menat494bd9e2022-03-28 18:52:46 +053023 frappe.get_doc({"doctype": "Role", "role_name": "Analytics"}).insert()
Rushabh Mehta33003c62014-04-30 11:16:02 +053024 set_single_defaults()
Ankush Menat9a99f992022-01-23 17:38:04 +053025 create_print_setting_custom_fields()
Rushabh Mehtadb7139a2013-01-17 18:22:22 +053026 add_all_roles_to("Administrator")
Suraj Shetty00cced12018-05-03 19:06:32 +053027 create_default_success_action()
Suraj Shetty627a3dc2019-09-17 15:54:41 +053028 create_default_energy_point_rules()
barredterrace83f022022-11-17 22:17:19 +010029 create_incoterms()
Deepesh Garg76197cc2023-06-07 10:11:32 +053030 create_default_role_profiles()
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 Waghchaure5949a7e2022-12-01 15:59:42 +053034 setup_log_settings()
Nabin Hait243c49c2023-05-24 16:49:27 +053035 hide_workspaces()
Ankush Menatd2096cf2023-10-16 16:53:43 +053036 update_roles()
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():
Ankush Menat494bd9e2022-03-28 18:52:46 +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 Menat494bd9e2022-03-28 18:52:46 +053044 frappe.throw(message) # nosemgrep
Rushabh Mehtad55bdcf2015-12-31 11:12:48 +053045
tundebabzycad22db2018-02-22 06:38:36 +010046
Ankush Menat1e8ee932023-06-12 19:20:52 +053047def check_frappe_version():
48 def major_version(v: str) -> str:
49 return v.split(".")[0]
50
51 frappe_version = major_version(frappe.__version__)
52 erpnext_version = major_version(erpnext.__version__)
53
54 if frappe_version == erpnext_version:
55 return
56
57 click.secho(
58 f"You're attempting to install ERPNext version {erpnext_version} with Frappe version {frappe_version}. "
59 "This is not supported and will result in broken install. Switch to correct branch before installing.",
60 fg="red",
61 )
62
63 raise SystemExit(1)
64
65
Pratik Vyas28da7522014-02-19 20:53:45 +053066def set_single_defaults():
Ankush Menat494bd9e2022-03-28 18:52:46 +053067 for dt in (
68 "Accounts Settings",
69 "Print Settings",
Ankush Menat494bd9e2022-03-28 18:52:46 +053070 "Buying Settings",
71 "Selling Settings",
72 "Stock Settings",
73 ):
74 default_values = frappe.db.sql(
75 """select fieldname, `default` from `tabDocField`
76 where parent=%s""",
77 dt,
78 )
Anand Doshieb7fea62014-03-19 17:10:01 +053079 if default_values:
80 try:
Ankush Menatc3e1f0e2022-03-27 11:52:31 +053081 doc = frappe.get_doc(dt, dt)
Anand Doshieb7fea62014-03-19 17:10:01 +053082 for fieldname, value in default_values:
Ankush Menatc3e1f0e2022-03-27 11:52:31 +053083 doc.set(fieldname, value)
84 doc.flags.ignore_mandatory = True
85 doc.save()
Rushabh Mehtab62ed7a2016-10-13 11:00:00 +053086 except frappe.ValidationError:
87 pass
Rushabh Mehtad42167e2016-05-11 16:47:14 +053088
rtdany10227466c2021-09-04 14:04:56 +053089 frappe.db.set_default("date_format", "dd-mm-yyyy")
Deepesh Garge2dab6f2022-01-10 17:31:38 +053090
91 setup_currency_exchange()
92
Ankush Menat494bd9e2022-03-28 18:52:46 +053093
Deepesh Garge2dab6f2022-01-10 17:31:38 +053094def setup_currency_exchange():
Ankush Menat494bd9e2022-03-28 18:52:46 +053095 ces = frappe.get_single("Currency Exchange Settings")
rtdany10227466c2021-09-04 14:04:56 +053096 try:
Ankush Menat494bd9e2022-03-28 18:52:46 +053097 ces.set("result_key", [])
98 ces.set("req_params", [])
Deepesh Garge2dab6f2022-01-10 17:31:38 +053099
100 ces.api_endpoint = "https://frankfurter.app/{transaction_date}"
Ankush Menat494bd9e2022-03-28 18:52:46 +0530101 ces.append("result_key", {"key": "rates"})
102 ces.append("result_key", {"key": "{to_currency}"})
103 ces.append("req_params", {"key": "base", "value": "{from_currency}"})
104 ces.append("req_params", {"key": "symbols", "value": "{to_currency}"})
rtdany10227466c2021-09-04 14:04:56 +0530105 ces.save()
106 except frappe.ValidationError:
107 pass
tundebabzycad22db2018-02-22 06:38:36 +0100108
Ankush Menat494bd9e2022-03-28 18:52:46 +0530109
Ankush Menat9a99f992022-01-23 17:38:04 +0530110def create_print_setting_custom_fields():
Sagar Voraaafb7352022-08-18 15:31:20 +0000111 create_custom_fields(
Ankush Menat494bd9e2022-03-28 18:52:46 +0530112 {
Sagar Voraaafb7352022-08-18 15:31:20 +0000113 "Print Settings": [
114 {
115 "label": _("Compact Item Print"),
116 "fieldname": "compact_item_print",
117 "fieldtype": "Check",
118 "default": "1",
119 "insert_after": "with_letterhead",
120 },
121 {
122 "label": _("Print UOM after Quantity"),
123 "fieldname": "print_uom_after_quantity",
124 "fieldtype": "Check",
125 "default": "0",
126 "insert_after": "compact_item_print",
127 },
128 {
129 "label": _("Print taxes with zero amount"),
130 "fieldname": "print_taxes_with_zero_amount",
131 "fieldtype": "Check",
132 "default": "0",
133 "insert_after": "allow_print_for_cancelled",
134 },
135 ]
136 }
Ankush Menat494bd9e2022-03-28 18:52:46 +0530137 )
tundebabzycad22db2018-02-22 06:38:36 +0100138
139
Suraj Shetty00cced12018-05-03 19:06:32 +0530140def create_default_success_action():
141 for success_action in get_default_success_action():
Ankush Menat494bd9e2022-03-28 18:52:46 +0530142 if not frappe.db.exists("Success Action", success_action.get("ref_doctype")):
Suraj Shetty00cced12018-05-03 19:06:32 +0530143 doc = frappe.get_doc(success_action)
144 doc.insert(ignore_permissions=True)
Rucha Mahabal4865eab2019-07-24 13:37:54 +0530145
Ankush Menat494bd9e2022-03-28 18:52:46 +0530146
Suraj Shetty627a3dc2019-09-17 15:54:41 +0530147def create_default_energy_point_rules():
148
149 for rule in get_default_energy_point_rules():
150 # check if any rule for ref. doctype exists
Ankush Menat494bd9e2022-03-28 18:52:46 +0530151 rule_exists = frappe.db.exists(
152 "Energy Point Rule", {"reference_doctype": rule.get("reference_doctype")}
153 )
154 if rule_exists:
155 continue
Suraj Shetty627a3dc2019-09-17 15:54:41 +0530156 doc = frappe.get_doc(rule)
157 doc.insert(ignore_permissions=True)
158
Ankush Menat494bd9e2022-03-28 18:52:46 +0530159
Rucha Mahabal4865eab2019-07-24 13:37:54 +0530160def add_company_to_session_defaults():
161 settings = frappe.get_single("Session Default Settings")
Ankush Menat494bd9e2022-03-28 18:52:46 +0530162 settings.append("session_defaults", {"ref_doctype": "Company"})
Rucha Mahabal4865eab2019-07-24 13:37:54 +0530163 settings.save()
Deepesh Garga306af82020-08-06 20:52:02 +0530164
Ankush Menat494bd9e2022-03-28 18:52:46 +0530165
Deepesh Garga306af82020-08-06 20:52:02 +0530166def add_standard_navbar_items():
167 navbar_settings = frappe.get_single("Navbar Settings")
168
169 erpnext_navbar_items = [
170 {
Ankush Menat494bd9e2022-03-28 18:52:46 +0530171 "item_label": "Documentation",
172 "item_type": "Route",
Ankush Menata449a4b2023-07-04 14:58:45 +0530173 "route": "https://docs.erpnext.com/",
Ankush Menat494bd9e2022-03-28 18:52:46 +0530174 "is_standard": 1,
Deepesh Garga306af82020-08-06 20:52:02 +0530175 },
176 {
Ankush Menat494bd9e2022-03-28 18:52:46 +0530177 "item_label": "User Forum",
178 "item_type": "Route",
MohsinAlidd93ea02023-04-17 17:00:52 +0530179 "route": "https://discuss.frappe.io",
Ankush Menat494bd9e2022-03-28 18:52:46 +0530180 "is_standard": 1,
Deepesh Garga306af82020-08-06 20:52:02 +0530181 },
182 {
Ankush Menata449a4b2023-07-04 14:58:45 +0530183 "item_label": "Frappe School",
184 "item_type": "Route",
185 "route": "https://frappe.school?utm_source=in_app",
186 "is_standard": 1,
187 },
188 {
Ankush Menat494bd9e2022-03-28 18:52:46 +0530189 "item_label": "Report an Issue",
190 "item_type": "Route",
191 "route": "https://github.com/frappe/erpnext/issues",
192 "is_standard": 1,
193 },
Deepesh Garga306af82020-08-06 20:52:02 +0530194 ]
195
prssanna194dd122021-03-24 16:45:32 +0530196 current_navbar_items = navbar_settings.help_dropdown
Ankush Menat494bd9e2022-03-28 18:52:46 +0530197 navbar_settings.set("help_dropdown", [])
Deepesh Garga306af82020-08-06 20:52:02 +0530198
199 for item in erpnext_navbar_items:
Ankush Menat494bd9e2022-03-28 18:52:46 +0530200 current_labels = [item.get("item_label") for item in current_navbar_items]
201 if not item.get("item_label") in current_labels:
202 navbar_settings.append("help_dropdown", item)
Deepesh Garga306af82020-08-06 20:52:02 +0530203
prssanna194dd122021-03-24 16:45:32 +0530204 for item in current_navbar_items:
Ankush Menat494bd9e2022-03-28 18:52:46 +0530205 navbar_settings.append(
206 "help_dropdown",
207 {
208 "item_label": item.item_label,
209 "item_type": item.item_type,
210 "route": item.route,
211 "action": item.action,
212 "is_standard": item.is_standard,
213 "hidden": item.hidden,
214 },
215 )
Deepesh Garga306af82020-08-06 20:52:02 +0530216
217 navbar_settings.save()
Shivam Mishra1323a9a2020-09-30 16:33:14 +0530218
Ankush Menat494bd9e2022-03-28 18:52:46 +0530219
Shivam Mishra1323a9a2020-09-30 16:33:14 +0530220def add_app_name():
Ankush Menata3ea9852023-06-13 17:30:38 +0530221 frappe.db.set_single_value("System Settings", "app_name", "ERPNext")
Rohit Waghchaure5949a7e2022-12-01 15:59:42 +0530222
223
224def setup_log_settings():
225 log_settings = frappe.get_single("Log Settings")
226 log_settings.append("logs_to_clear", {"ref_doctype": "Repost Item Valuation", "days": 60})
227
228 log_settings.save(ignore_permissions=True)
Nabin Hait243c49c2023-05-24 16:49:27 +0530229
230
231def hide_workspaces():
Nabin Hait86f88812023-05-26 17:25:20 +0530232 for ws in ["Integration", "Settings"]:
233 frappe.db.set_value("Workspace", ws, "public", 0)
Deepesh Garg76197cc2023-06-07 10:11:32 +0530234
235
Ankush Menatd2096cf2023-10-16 16:53:43 +0530236def update_roles():
237 website_user_roles = ("Customer", "Supplier")
238 for role in website_user_roles:
239 frappe.db.set_value("Role", role, "desk_access", 0)
240
241
Deepesh Garg76197cc2023-06-07 10:11:32 +0530242def create_default_role_profiles():
Ankush Menat0166f692023-06-07 15:09:49 +0530243 for role_profile_name, roles in DEFAULT_ROLE_PROFILES.items():
244 role_profile = frappe.new_doc("Role Profile")
245 role_profile.role_profile = role_profile_name
246 for role in roles:
247 role_profile.append("roles", {"role": role})
248
249 role_profile.insert(ignore_permissions=True)
Deepesh Garg76197cc2023-06-07 10:11:32 +0530250
251
Ankush Menat0166f692023-06-07 15:09:49 +0530252DEFAULT_ROLE_PROFILES = {
253 "Inventory": [
254 "Stock User",
255 "Stock Manager",
256 "Item Manager",
257 ],
258 "Manufacturing": [
259 "Stock User",
260 "Manufacturing User",
261 "Manufacturing Manager",
262 ],
263 "Accounts": [
264 "Accounts User",
265 "Accounts Manager",
266 ],
267 "Sales": [
268 "Sales User",
269 "Stock User",
270 "Sales Manager",
271 ],
272 "Purchase": [
273 "Item Manager",
274 "Stock User",
275 "Purchase User",
276 "Purchase Manager",
277 ],
278}