From 2ab31ab2d3aed3dd686b579620418fea4b6a5d93 Mon Sep 17 00:00:00 2001 From: Anand Doshi Date: Mon, 14 Jan 2013 11:17:34 +0530 Subject: [PATCH] updated stock reconciliation patch --- .../january_2013/file_list_rename_returns.py | 66 +++++++++++++++++++ .../stock_reconciliation_patch.py | 20 ++++-- patches/patch_list.py | 4 ++ 3 files changed, 85 insertions(+), 5 deletions(-) create mode 100644 patches/january_2013/file_list_rename_returns.py diff --git a/patches/january_2013/file_list_rename_returns.py b/patches/january_2013/file_list_rename_returns.py new file mode 100644 index 0000000000..cca7a15725 --- /dev/null +++ b/patches/january_2013/file_list_rename_returns.py @@ -0,0 +1,66 @@ +import webnotes +from webnotes.utils import get_base_path +import os + +def execute(): + # find out when was the file list patch run + res = webnotes.conn.sql("""select applied_on from `__PatchLog` + where patch='patches.december_2012.file_list_rename' order by applied_on desc limit 1""") + if res: + patch_date = res[0][0].date() + files_path = os.path.join(get_base_path(), "public", "files") + + change_map = {} + + file_data_list = webnotes.conn.sql("""select name, file_name from `tabFile Data` + where date(modified) <= %s and ifnull(file_url, '')='' and name like "%%-%%" """, + patch_date) + + # print patch_date + # print file_data_list + # print files_path + + for fid, file_name in file_data_list: + if os.path.exists(os.path.join(files_path, fid)): + new_fid, new_file_name = fid.replace("-", ""), file_name.replace("-", "") + + try: + webnotes.conn.sql("""update `tabFile Data` + set name=%s, file_name=%s where name=%s""", (new_fid, new_file_name, fid)) + + os.rename(os.path.join(files_path, fid), os.path.join(files_path, new_fid)) + + change_map[",".join((file_name, fid))] = ",".join((new_file_name, new_fid)) + except Exception, e: + # if duplicate entry, then dont update + if e[0]!=1062: + print webnotes.getTraceback() + raise e + + print change_map + + changed_keys = change_map.keys() + + for dt in webnotes.conn.sql("""select distinct parent from tabDocField + where fieldname='file_list'"""): + try: + data = webnotes.conn.sql("""select name, file_list from `tab%s` + where ifnull(file_list, '')!=''""" % dt[0]) + for name, file_list in data: + new_file_list = [] + file_list = file_list.split("\n") + for f in file_list: + if f in changed_keys: + new_file_list.append(change_map[f]) + else: + new_file_list.append(f) + if new_file_list != file_list: + webnotes.conn.sql("""update `tab%s` set file_list=%s + where name=%s""" % (dt[0], "%s", "%s"), + ("\n".join(new_file_list), name)) + + except Exception, e: + if e[0]!=1146: + print webnotes.getTraceback() + raise e + \ No newline at end of file diff --git a/patches/january_2013/stock_reconciliation_patch.py b/patches/january_2013/stock_reconciliation_patch.py index 75dab765e2..9d72f58c09 100644 --- a/patches/january_2013/stock_reconciliation_patch.py +++ b/patches/january_2013/stock_reconciliation_patch.py @@ -15,19 +15,29 @@ def rename_fields(): def store_stock_reco_json(): import os - import conf import json from webnotes.utils.datautils import read_csv_content - base_path = os.path.dirname(os.path.abspath(conf.__file__)) + from webnotes.utils import get_base_path + files_path = os.path.join(get_base_path(), "public", "files") + + list_of_files = os.listdir(files_path) + replaced_list_of_files = [f.replace("-", "") for f in list_of_files] for reco, file_list in webnotes.conn.sql("""select name, file_list from `tabStock Reconciliation`"""): if file_list: file_list = file_list.split("\n") stock_reco_file = file_list[0].split(",")[1] - stock_reco_file = os.path.join(base_path, "public", "files", stock_reco_file) - if os.path.exists(stock_reco_file): - with open(stock_reco_file, "r") as open_reco_file: + stock_reco_file_path = os.path.join(files_path, stock_reco_file) + if not os.path.exists(stock_reco_file_path): + if stock_reco_file in replaced_list_of_files: + stock_reco_file_path = os.path.join(files_path, + list_of_files[replaced_list_of_files.index(stock_reco_file)]) + else: + stock_reco_file_path = "" + + if stock_reco_file_path: + with open(stock_reco_file_path, "r") as open_reco_file: content = open_reco_file.read() content = read_csv_content(content) webnotes.conn.set_value("Stock Reconciliation", reco, "reconciliation_json", diff --git a/patches/patch_list.py b/patches/patch_list.py index f9bb97cf8a..e842e163b8 100644 --- a/patches/patch_list.py +++ b/patches/patch_list.py @@ -582,4 +582,8 @@ patch_list = [ 'patch_module': 'patches.january_2013', 'patch_file': 'holiday_list_patch', }, + { + 'patch_module': 'patches.january_2013', + 'patch_file': 'stock_reconciliation_patch', + }, ] \ No newline at end of file