old_contents: # -*- coding: utf-8 -*-
  # Copyright (c) 2019, Frappe Technologies and contributors
  # For license information, please see license.txt
  
  from __future__ import unicode_literals
  import frappe
  import json
  from frappe.model.document import Document
  
  
  class DocumentTypeMapping(Document):
  	def get_mapped_doc(self, update):
  		doc = frappe._dict(json.loads(update))
  		for mapping in self.field_mapping:
  			if doc.get(mapping.remote_fieldname):
  
  				if mapping.is_child_table:
  					doc[mapping.local_fieldname] = self.get_mapped_child_table_docs(mapping.child_table_mapping, doc[mapping.remote_fieldname])
  				else:
  					# copy value into local fieldname key and remove remote fieldname key
  					doc[mapping.local_fieldname] = doc[mapping.remote_fieldname]
  				doc.pop(mapping.remote_fieldname, None)
  
  			elif mapping.has_default_value:
  				doc[mapping.local_fieldname] = mapping.default_value
  
  		doc['doctype'] = self.local_doctype
  		return frappe.as_json(doc)
  
  
  def get_mapped_child_table_docs(child_map, table_entries):
  	"""Get mapping for child doctypes"""
  	child_map = frappe.get_doc('Document Type Mapping', child_map)
  	mapped_entries = []
  	for child_doc in table_entries:
  		for mapping in child_map.field_mapping:
  			if child_doc.get(mapping.remote_fieldname):
  				child_doc[mapping.local_fieldname] = child_doc[mapping.remote_fieldname]
  				child_doc.pop(mapping.remote_fieldname, None)
  		child_doc['doctype'] = child_map.local_doctype
  		mapped_entries.append(child_doc)
  	return mapped_entries
  
new_contents: # -*- coding: utf-8 -*-
  # Copyright (c) 2019, Frappe Technologies and contributors
  # For license information, please see license.txt
  
  from __future__ import unicode_literals
  import frappe
  import json
  from frappe.model.document import Document
  
  
  class DocumentTypeMapping(Document):
  	def get_mapped_doc(self, update):
  		doc = frappe._dict(json.loads(update))
  		remote_fields = []
  		for mapping in self.field_mapping:
  			if doc.get(mapping.remote_fieldname):
  				if mapping.is_child_table:
  					doc[mapping.local_fieldname] = self.get_mapped_child_table_docs(mapping.child_table_mapping, doc[mapping.remote_fieldname])
  				else:
  					# copy value into local fieldname key and remove remote fieldname key
  					doc[mapping.local_fieldname] = doc[mapping.remote_fieldname]
  				remote_fields.append(mapping.remote_fieldname)
  
  			elif mapping.has_default_value:
  				doc[mapping.local_fieldname] = mapping.default_value
  
  		#remove the remote fieldnames
  		for field in remote_fields:
  			doc.pop(field, None)
  
  		doc['doctype'] = self.local_doctype
  		return frappe.as_json(doc)
  
  
  def get_mapped_child_table_docs(child_map, table_entries):
  	"""Get mapping for child doctypes"""
  	child_map = frappe.get_doc('Document Type Mapping', child_map)
  	mapped_entries = []
  	for child_doc in table_entries:
  		for mapping in child_map.field_mapping:
  			if child_doc.get(mapping.remote_fieldname):
  				child_doc[mapping.local_fieldname] = child_doc[mapping.remote_fieldname]
  				child_doc.pop(mapping.remote_fieldname, None)
  		child_doc['doctype'] = child_map.local_doctype
  		mapped_entries.append(child_doc)
  	return mapped_entries
  
subject: handle case for different local fields mapping to same remote field