From dabf212940f8bceef6b4fbe8a661de5c98edd9c6 Mon Sep 17 00:00:00 2001 From: Nabin Hait Date: Mon, 7 May 2012 15:57:50 +0530 Subject: [PATCH] mapper fix for custom fields --- .../doctype/doctype_mapper/doctype_mapper.py | 71 ++++++++----------- 1 file changed, 30 insertions(+), 41 deletions(-) diff --git a/py/core/doctype/doctype_mapper/doctype_mapper.py b/py/core/doctype/doctype_mapper/doctype_mapper.py index 19abf14182..7b863e8165 100644 --- a/py/core/doctype/doctype_mapper/doctype_mapper.py +++ b/py/core/doctype/doctype_mapper/doctype_mapper.py @@ -29,6 +29,7 @@ from webnotes.model.doc import Document, addchild, removechild, getchildren, mak from webnotes.model.doclist import getlist, copy_doclist from webnotes.model.code import get_obj, get_server_obj, run_server_obj, updatedb, check_syntax from webnotes import session, form, is_testing, msgprint, errprint +from webnotes.model.doctype import get set = webnotes.conn.set sql = webnotes.conn.sql @@ -140,20 +141,12 @@ class DocType: exception_flds = copy.copy(default_fields) exception_flds += [f[1] for f in flds] - similar_flds = [ - [d[0], d[0], 'Yes'] for d in sql(""" - select t1.fieldname - from `tabDocField` t1, `tabDocField` t2 - where t1.parent = %s and t2.parent = %s - and t1.fieldname = t2.fieldname - and t1.docstatus != 2 and t2.docstatus != 2 - and ifnull(t1.no_copy, 0) = 0 - and ifnull(t1.fieldname, '') != '' - and t1.fieldtype not in ('Table', 'Section Break', 'Column Break', 'HTML') - """,(t['from_table'], t['to_table'])) if d[0] not in exception_flds - ] + from_flds = [d.fieldname for d in get(t['from_table']) if cint(d.no_copy) == 0 and d.docstatus != 2 and d.fieldname and d.fieldtype not in ('Table', 'Section Break', 'Column Break', 'HTML')] + to_flds = [d.fieldname for d in get(t['to_table']) if cint(d.no_copy) == 0 and d.docstatus != 2 and d.fieldname and d.fieldtype not in ('Table', 'Section Break', 'Column Break', 'HTML')] - return similar_flds + similar_flds = [[d, d, 'Yes'] for d in from_flds if d in to_flds and d not in exception_flds] + + return similar_flds #--------------------------------------------------------------------------- def set_value(self, fld_list, obj, to_doc): @@ -199,18 +192,18 @@ class DocType: """ Check if any wrong fieldname entered in mapper """ + flds = {} + for t in getlist(self.doclist, 'table_mapper_details'): + from_flds = [cstr(d.fieldname) for d in get(t.from_table)] + to_flds = [cstr(d.fieldname) for d in get(t.to_table)] + flds[cstr(t.match_id)] = [cstr(t.from_table), from_flds, cstr(t.to_table), to_flds] + for d in getlist(self.doclist, 'field_mapper_details'): - table_name = sql("select from_table, to_table from `tabTable Mapper Detail` where parent ='%s' and match_id = '%s'" % (self.doc.name, d.match_id)) - - if table_name: - exists1 = sql("select name from tabDocField where parent = '%s' and fieldname = '%s'" % (table_name[0][0], d.from_field)) - exists2 = sql("select name from tabDocField where parent = '%s' and fieldname = '%s'" % (table_name[0][1], d.to_field)) - - # Default fields like name, parent, owner does not exists in DocField - if not exists1 and d.from_field not in default_fields: - msgprint('"' + cstr(d.from_field) + '" does not exists in DocType "' + cstr(table_name[0][0]) + '"') - if not exists2 and d.to_field not in default_fields: - msgprint('"' + cstr(d.to_field) + '" does not exists in DocType "' + cstr(table_name[0][1]) + '"') + # Default fields like name, parent, owner does not exists in DocField + if d.from_field not in flds[cstr(d.match_id)][1] and d.from_field not in default_fields: + msgprint('"' + cstr(d.from_field) + '" does not exists in DocType "' + cstr(flds[cstr(d.match_id)][0]) + '"') + if d.to_field not in flds[cstr(d.match_id)][3] and d.to_field not in default_fields: + msgprint('"' + cstr(d.to_field) + '" does not exists in DocType "' + cstr(flds[cstr(d.match_id)][2]) + '"') # Check consistency of value with reference document @@ -235,12 +228,6 @@ class DocType: checklist.append([f.from_field, f.to_field, f.checking_operator, f.match_id]) return checklist - def check_fld_type(self, tbl, fld, cur_val): - ft = sql("select fieldtype from tabDocField where fieldname = '%s' and parent = '%s'" % (fld,tbl)) - ft = ft and ft[0][0] or '' - if ft == 'Currency' or ft == 'Float': - cur_val = '%.2f' % flt(cur_val) - return cur_val, ft # Check consistency #------------------- @@ -249,6 +236,12 @@ class DocType: self.ref_doc = '' for t in getlist(self.doclist, 'table_mapper_details'): if t.reference_key and child_obj.fields[t.reference_key]: + from_flds, to_flds = {}, {} + for d in get(d.from_table): + flds[d.fieldname] = [d.label, d.fieldtype] + for d in get(d.to_table): + flds[d.fieldname] = [d.label, d.fieldtype] + for cl in checklist: if cl[3] == t.match_id: if t.to_field: @@ -256,30 +249,26 @@ class DocType: else: cur_val = par_obj.fields[cl[1]] - ft = self.check_fld_type(t.to_table, cl[1], cur_val) - cur_val = ft[0] + if to_flds[fld][0] in ['Currency', 'Float']: + cur_val = '%.2f' % flt(cur_val) - if cl[2] == '=' and (ft[1] == 'Currency' or ft[1] == 'Float'): + if cl[2] == '=' and to_flds[fld][0] in ['Currency', 'Float']: consistent = sql("select name, %s from `tab%s` where name = '%s' and '%s' - %s <= 0.5" % (cl[0], t.from_table, child_obj.fields[t.reference_key], flt(cur_val), cl[0])) else: - consistent = sql("select name, %s from `tab%s` where name = '%s' and '%s' %s ifnull(%s, '')" % (cl[0], t.from_table, child_obj.fields[t.reference_key], ft[1] in ('Currency', 'Float', 'Int') and flt(cur_val) or cstr(cur_val), cl[2], cl[0])) + consistent = sql("select name, %s from `tab%s` where name = '%s' and '%s' %s ifnull(%s, '')" % (cl[0], t.from_table, child_obj.fields[t.reference_key], to_flds[fld][0] in ('Currency', 'Float', 'Int') and flt(cur_val) or cstr(cur_val), cl[2], cl[0])) if not self.ref_doc: det = sql("select name, parent from `tab%s` where name = '%s'" % (t.from_table, child_obj.fields[t.reference_key])) self.ref_doc = det[0][1] and det[0][1] or det[0][0] if not consistent: - self.give_message(t.from_table, t.to_table, cl[0], cl[1], child_obj.fields[t.reference_key], cl[2]) + self.give_message(from_flds[cl[0]][1], to_flds[cl[1]][1], cl[2]) # Gives message and raise exception #----------------------------------- - def give_message(self, from_table, to_table, from_field, to_field, ref_value, operator): - # Select label of the field - to_fld_label = sql("select label from tabDocField where parent = '%s' and fieldname = '%s'" % (to_table, to_field)) - from_fld_label = sql("select label from tabDocField where parent = '%s' and fieldname = '%s'" % (from_table, from_field)) - + def give_message(self, from_label, to_label, operator): op_in_words = {'=':'equal to ', '>=':'greater than equal to ', '>':'greater than ', '<=':'less than equal to ', '<':'less than '} - msgprint(to_fld_label[0][0] + " should be " + op_in_words[operator] + from_fld_label[0][0] + " of " + self.doc.from_doctype + ": " + self.ref_doc, raise_exception=1) + msgprint(to_label + " should be " + op_in_words[operator] + from_label + " of " + self.doc.from_doctype + ": " + self.ref_doc, raise_exception=1) def check_ref_docstatus(self): if self.ref_doc: