Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 
 
 

143 linhas
5.1 KiB

  1. # Copyright (c) 2012 Web Notes Technologies Pvt Ltd (http://erpnext.com)
  2. #
  3. # MIT License (MIT)
  4. #
  5. # Permission is hereby granted, free of charge, to any person obtaining a
  6. # copy of this software and associated documentation files (the "Software"),
  7. # to deal in the Software without restriction, including without limitation
  8. # the rights to use, copy, modify, merge, publish, distribute, sublicense,
  9. # and/or sell copies of the Software, and to permit persons to whom the
  10. # Software is furnished to do so, subject to the following conditions:
  11. #
  12. # The above copyright notice and this permission notice shall be included in
  13. # all copies or substantial portions of the Software.
  14. #
  15. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
  16. # INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  17. # PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  18. # HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
  19. # CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
  20. # OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  21. #
  22. from __future__ import unicode_literals
  23. import webnotes, json
  24. from webnotes import _
  25. from webnotes.utils import cstr
  26. from webnotes.model import default_fields
  27. def get_mapped_doclist(from_doctype, from_docname, table_maps, target_doclist=[],
  28. postprocess=None, ignore_permissions=False):
  29. if isinstance(target_doclist, basestring):
  30. target_doclist = json.loads(target_doclist)
  31. source = webnotes.bean(from_doctype, from_docname)
  32. if not ignore_permissions and not webnotes.has_permission(from_doctype, "read", source.doc):
  33. webnotes.msgprint("No Permission", raise_exception=webnotes.PermissionError)
  34. source_meta = webnotes.get_doctype(from_doctype)
  35. target_meta = webnotes.get_doctype(table_maps[from_doctype]["doctype"])
  36. # main
  37. if target_doclist:
  38. if isinstance(target_doclist[0], dict):
  39. target_doc = webnotes.doc(fielddata=target_doclist[0])
  40. else:
  41. target_doc = target_doclist[0]
  42. else:
  43. target_doc = webnotes.new_doc(table_maps[from_doctype]["doctype"])
  44. map_doc(source.doc, target_doc, table_maps[source.doc.doctype], source_meta, target_meta)
  45. if target_doclist:
  46. target_doclist[0] = target_doc
  47. else:
  48. target_doclist = [target_doc]
  49. # children
  50. for source_d in source.doclist[1:]:
  51. table_map = table_maps.get(source_d.doctype)
  52. if table_map:
  53. if "condition" in table_map:
  54. if not table_map["condition"](source_d):
  55. continue
  56. target_doctype = table_map["doctype"]
  57. parentfield = target_meta.get({
  58. "parent": target_doc.doctype,
  59. "doctype": "DocField",
  60. "fieldtype": "Table",
  61. "options": target_doctype
  62. })[0].fieldname
  63. if table_map.get("add_if_empty") and row_exists_in_target(parentfield, target_doclist):
  64. continue
  65. target_d = webnotes.new_doc(target_doctype, target_doc, parentfield)
  66. map_doc(source_d, target_d, table_map, source_meta, target_meta, source.doclist[0])
  67. target_doclist.append(target_d)
  68. target_doclist = webnotes.doclist(target_doclist)
  69. if postprocess:
  70. new_target_doclist = postprocess(source, target_doclist)
  71. if new_target_doclist:
  72. target_doclist = new_target_doclist
  73. return target_doclist
  74. def map_doc(source_doc, target_doc, table_map, source_meta, target_meta, source_parent=None):
  75. no_copy_fields = set(\
  76. [d.fieldname for d in source_meta.get({"no_copy": 1,
  77. "parent": source_doc.doctype})] \
  78. + [d.fieldname for d in target_meta.get({"no_copy": 1,
  79. "parent": target_doc.doctype})] \
  80. + default_fields)
  81. if table_map.get("validation"):
  82. for key, condition in table_map["validation"].items():
  83. if condition[0]=="=":
  84. if source_doc.fields.get(key) != condition[1]:
  85. webnotes.msgprint(_("Cannot map because following condition fails: ")
  86. + key + "=" + cstr(condition[1]), raise_exception=webnotes.ValidationError)
  87. # map same fields
  88. target_fields = target_meta.get({"doctype": "DocField", "parent": target_doc.doctype})
  89. for key in [d.fieldname for d in target_fields]:
  90. if key not in no_copy_fields:
  91. val = source_doc.fields.get(key)
  92. if val not in (None, ""):
  93. target_doc.fields[key] = val
  94. # map other fields
  95. field_map = table_map.get("field_map")
  96. if field_map:
  97. if isinstance(field_map, dict):
  98. for source_key, target_key in field_map.items():
  99. val = source_doc.fields.get(source_key)
  100. if val not in (None, ""):
  101. target_doc.fields[target_key] = val
  102. else:
  103. for fmap in field_map:
  104. val = source_doc.fields.get(fmap[0])
  105. if val not in (None, ""):
  106. target_doc.fields[fmap[1]] = val
  107. # map idx
  108. if source_doc.idx:
  109. target_doc.idx = source_doc.idx
  110. if "postprocess" in table_map:
  111. table_map["postprocess"](source_doc, target_doc, source_parent)
  112. row_exists_for_parentfield = {}
  113. def row_exists_in_target(parentfield, target_doclist):
  114. global row_exists_for_parentfield
  115. if parentfield not in row_exists_for_parentfield:
  116. row_exists_for_parentfield[parentfield] = True if \
  117. webnotes.doclist(target_doclist).get({"parentfield": parentfield}) else False
  118. return row_exists_for_parentfield[parentfield]