From 031303d719ae9cddb78fad887192dc6ecad8326b Mon Sep 17 00:00:00 2001 From: Nabin Hait Date: Thu, 12 Feb 2015 11:48:50 +0530 Subject: [PATCH] Replace renamed fields in custom script and custom print formats --- ...elds_in_custom_script_and_print_formats.py | 54 +++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 erpnext/patches/v5_0/replace_renamed_fields_in_custom_script_and_print_formats.py 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 0000000000..46bb221ac6 --- /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)