[cleanup] move contact, address to frappe :boom:
diff --git a/erpnext/utilities/address_and_contact.py b/erpnext/utilities/address_and_contact.py
index 36879d2..fddb1f7 100644
--- a/erpnext/utilities/address_and_contact.py
+++ b/erpnext/utilities/address_and_contact.py
@@ -6,17 +6,33 @@
 
 def load_address_and_contact(doc, key):
 	"""Loads address list and contact list in `__onload`"""
-	from erpnext.utilities.doctype.address.address import get_address_display
+	from frappe.geo.doctype.address.address import get_address_display
 
-	doc.get("__onload")["addr_list"] = [a.update({"display": get_address_display(a)}) \
-		for a in frappe.get_all("Address",
-			fields="*", filters={key: doc.name},
-			order_by="is_primary_address desc, modified desc")]
+	address_list = [frappe.get_value('Address', a.parent, '*')
+		for a in frappe.get_all('Dynamic Link', fields='parent',
+			filters=dict(parenttype='Address', link_doctype=doc.doctype, link_name=doc.name))]
+
+	address_list = [a.update({"display": get_address_display(a)})
+		for a in address_list]
+
+	address_list = sorted(address_list,
+		lambda a, b:
+			(int(a.is_primary_address - b.is_primary_address)) or
+			(1 if a.modified - b.modified else 0))
+
+	doc.set_onload('addr_list', address_list)
 
 	if doc.doctype != "Lead":
-		doc.get("__onload")["contact_list"] = frappe.get_all("Contact",
-			fields="*", filters={key: doc.name},
-			order_by="is_primary_contact desc, modified desc")
+		contact_list = [frappe.get_value('Contact', a.parent, '*')
+			for a in frappe.get_all('Dynamic Link', fields='parent',
+				filters=dict(parenttype='Contact', link_doctype=doc.doctype, link_name=doc.name))]
+
+		contact_list = sorted(contact_list,
+			lambda a, b:
+				(int(a.is_primary_contact - b.is_primary_contact)) or
+				(1 if a.modified - b.modified else 0))
+
+		doc.set_onload('contact_list', contact_list)
 
 def has_permission(doc, ptype, user):
 	links = get_permitted_and_not_permitted_links(doc.doctype)
@@ -50,15 +66,15 @@
 	if not links.get("not_permitted_links"):
 		# when everything is permitted, don't add additional condition
 		return ""
-		
+
 	elif not links.get("permitted_links"):
 		conditions = []
-		
+
 		# when everything is not permitted
 		for df in links.get("not_permitted_links"):
 			# like ifnull(customer, '')='' and ifnull(supplier, '')=''
 			conditions.append("ifnull(`tab{doctype}`.`{fieldname}`, '')=''".format(doctype=doctype, fieldname=df.fieldname))
-			
+
 		return "( " + " and ".join(conditions) + " )"
 
 	else:
@@ -66,7 +82,7 @@
 
 		for df in links.get("permitted_links"):
 			# like ifnull(customer, '')!='' or ifnull(supplier, '')!=''
-			conditions.append("ifnull(`tab{doctype}`.`{fieldname}`, '')!=''".format(doctype=doctype, fieldname=df.fieldname))			
+			conditions.append("ifnull(`tab{doctype}`.`{fieldname}`, '')!=''".format(doctype=doctype, fieldname=df.fieldname))
 
 		return "( " + " or ".join(conditions) + " )"