Replace renamed fields in custom script and custom print formats
diff --git a/erpnext/patches/v5_0/replace_renamed_fields_in_custom_script_and_print_formats.py b/erpnext/patches/v5_0/replace_renamed_fields_in_custom_script_and_print_formats.py
new file mode 100644
index 0000000..46bb221
--- /dev/null
+++ b/erpnext/patches/v5_0/replace_renamed_fields_in_custom_script_and_print_formats.py
@@ -0,0 +1,54 @@
+# Copyright (c) 2013, Web Notes Technologies Pvt. Ltd. and Contributors
+# License: GNU General Public License v3. See license.txt
+
+from __future__ import unicode_literals
+import frappe
+import re
+
+def execute():
+	# NOTE: sequence is important
+	renamed_fields = get_all_renamed_fields()
+
+	for dt, script_field in (("Custom Script", "script"), ("Print Format", "html")):
+
+		cond1 = " or ".join("""{0} like "%%{1}%%" """.format(script_field, d[0].replace("_", "\\_")) for d in renamed_fields)
+		cond2 = " and standard = 'No'" if dt == "Print Format" else ""
+
+		for name, script in frappe.db.sql("select name, {0} as script from `tab{1}` where ({2}) {3}".format(script_field, dt, cond1, cond2)):
+			update_script(dt, name, script_field, script, renamed_fields)
+
+def get_all_renamed_fields():
+	from erpnext.patches.v5_0.rename_table_fieldnames import rename_map
+
+	renamed_fields = (
+		("net_total", "base_net_total"),
+		("net_total_export", "net_total"),
+		("net_total_import", "net_total"),
+		("other_charges_total", "base_total_taxes_and_charges"),
+		("other_charges_total_export", "total_taxes_and_charges"),
+		("other_charges_added", "base_taxes_and_charges_added"),
+		("other_charges_added_import", "taxes_and_charges_added"),
+		("other_charges_deducted", "base_taxes_and_charges_deducted"),
+		("other_charges_deducted_import", "taxes_and_charges_deducted"),
+		("total_tax", "base_total_taxes_and_charges"),
+		("grand_total", "base_grand_total"),
+		("grand_total_export", "grand_total"),
+		("grand_total_import", "grand_total"),
+		("rounded_total", "base_rounded_total"),
+		("rounded_total_export", "rounded_total"),
+		("rounded_total_import", "rounded_total"),
+		("in_words", "base_in_words"),
+		("in_words_export", "in_words"),
+		("in_words_import", "in_words")
+	)
+
+	for fields in rename_map.values():
+		renamed_fields += fields
+
+	return renamed_fields
+
+def update_script(dt, name, script_field, script, renamed_fields):
+	for from_field, to_field in renamed_fields:
+		script = re.sub(r"\b{}\b".format(from_field), to_field, script)
+
+	frappe.db.set_value(dt, name, script_field, script)