Browse Source

[mapper] quotation-sales order for mapping in parent + child items

version-14
Rushabh Mehta 12 years ago
parent
commit
f5880f0cad
4 changed files with 71 additions and 26 deletions
  1. +10
    -2
      public/js/wn/model/sync.js
  2. +3
    -3
      webnotes/__init__.py
  3. +8
    -3
      webnotes/model/create_new.py
  4. +50
    -18
      webnotes/model/mapper.py

+ 10
- 2
public/js/wn/model/sync.js View File

@@ -35,12 +35,20 @@ $.extend(wn.model, {
if(doclist && doclist.length)
wn.model.clear_doclist(doclist[0].doctype, doclist[0].name)

var last_parent_name = null;
$.each(doclist, function(i, d) {
if(!d.name) { // get name (local if required)
if(!d.name && d.__islocal) { // get name (local if required)
d.name = wn.model.get_new_name(d.doctype);
wn.provide("wn.model.docinfo." + d.doctype + "." + d.name);
if(!d.parenttype)
last_parent_name = d.name;
}

// set parent for subsequent orphans
if(d.parenttype && !d.parent && d.__islocal) {
d.parent = last_parent_name;
}

if(!locals[d.doctype])
locals[d.doctype] = {};



+ 3
- 3
webnotes/__init__.py View File

@@ -160,7 +160,7 @@ def connect(db_name=None, password=None):
import webnotes.profile
global user
user = webnotes.profile.Profile('Administrator')
def get_env_vars(env_var):
import os
return os.environ.get(env_var,'None')
@@ -300,9 +300,9 @@ def doc(doctype=None, name=None, fielddata=None):
from webnotes.model.doc import Document
return Document(doctype, name, fielddata)

def new_doc(doctype):
def new_doc(doctype, parent_doc=None, parentfield=None):
from webnotes.model.create_new import get_new_doc
return get_new_doc(doctype)
return get_new_doc(doctype, parent_doc, parentfield)

def doclist(lst=None):
from webnotes.model.doclist import DocList


+ 8
- 3
webnotes/model/create_new.py View File

@@ -6,15 +6,20 @@ Create a new document with defaults set
import webnotes
from webnotes.utils import nowdate, nowtime, cint, flt

def get_new_doc(doctype, parent_doc = None):
def get_new_doc(doctype, parent_doc = None, parentfield = None):
doc = webnotes.doc({
"doctype": doctype,
"__islocal": 1
})
meta = webnotes.get_doctype(doctype)
for d in meta.get({"doctype":"DocField"}):
if parent_doc:
doc.parent = parent_doc.name
doc.parenttype = parent_doc.doctype
doc.parentfield = parentfield
for d in meta.get({"doctype":"DocField", "parent": doctype}):
default = webnotes.conn.get_default(d.fieldname)
if default:
doc.fields[d.fieldname] = default


+ 50
- 18
webnotes/model/mapper.py View File

@@ -21,44 +21,76 @@
#

from __future__ import unicode_literals
import webnotes
import webnotes, json
from webnotes import _
from webnotes.utils import cstr
from webnotes.model import default_fields

def get_mapped_doclist(from_doctype, from_docname, table_maps, target_doclist=[]):
if not webnotes.has_permission(from_doctype, from_docname):
webnotes.msgprint("No Permission", raise_exception=webnotes.PermissionError)

from webnotes.model import default_fields
source = webnotes.bean(from_doctype, from_docname)

source_meta = webnotes.get_doctype(from_doctype)
target_meta = webnotes.get_doctype(table_maps[from_doctype]["doctype"])
# main
if target_doclist:
target_doc = webnotes.doc(target_doclist[0])
else:
target_doc = webnotes.new_doc(table_maps[from_doctype]["doctype"])
no_copy_fields = set(get_no_copy_fields(from_doctype) \
+ get_no_copy_fields(table_maps[from_doctype]["doctype"]) \
map_doc(source.doc, target_doc, table_maps[source.doc.doctype], source_meta, target_meta)
doclist = [target_doc]

# children
for source_d in source.doclist[1:]:
target_doctype = table_maps[source_d.doctype]["doctype"]
parentfield = target_meta.get({
"parent": target_doc.doctype,
"doctype": "DocField",
"fieldtype": "Table",
"options": target_doctype
})[0].fieldname
target_d = webnotes.new_doc(target_doctype, target_doc, parentfield)
map_doc(source_d, target_d, table_maps[source_d.doctype], source_meta, target_meta)
doclist.append(target_d)
return doclist

def map_doc(source_doc, target_doc, table_map, source_meta, target_meta):
no_copy_fields = set(\
[d.fieldname for d in source_meta.get({"no_copy": 1,
"parent": source_doc.doctype})] \
+ [d.fieldname for d in target_meta.get({"no_copy": 1,
"parent": target_doc.doctype})] \
+ default_fields)

if table_map.get("validation"):
for key, condition in table_map["validation"].items():
if condition[0]=="=":
if source_doc.fields.get(key) != condition[1]:
webnotes.msgprint(_("Cannot map because following condition fails: ")
+ key + "=" + cstr(condition[1]), raise_exception=webnotes.ValidationError)

# map same fields
for key in target_doc.fields:
target_fields = target_meta.get({"doctype": "DocField", "parent": target_doc.doctype})
for key in [d.fieldname for d in target_fields]:
if key not in no_copy_fields:
val = source.doc.fields.get(key)
val = source_doc.fields.get(key)
if val not in (None, ""):
target_doc.fields[key] = val

# map other fields
for source_key, target_key in table_maps[from_doctype].get("field_map", {}).items():
val = source.doc.fields.get(source_key)
for source_key, target_key in table_map.get("field_map", {}).items():
val = source_doc.fields.get(source_key)
if val not in (None, ""):
target_doc.fields[target_key] = val

return [target_doc]

def get_no_copy_fields(doctype):
meta = webnotes.get_doctype(doctype)
no_copy_fields = []
for d in meta.get({"doctype":"DocField", "no_copy": 1}):
no_copy_fields.append(d.fieldname)
return no_copy_fields
# map idx
if source_doc.idx:
target_doc.idx = source_doc.idx

Loading…
Cancel
Save