[fix] address queries + tests
diff --git a/erpnext/buying/doctype/purchase_common/purchase_common.js b/erpnext/buying/doctype/purchase_common/purchase_common.js
index 921c3b5..720a1dc 100644
--- a/erpnext/buying/doctype/purchase_common/purchase_common.js
+++ b/erpnext/buying/doctype/purchase_common/purchase_common.js
@@ -75,7 +75,7 @@
 	},
 
 	refresh: function(doc) {
-		frappe.contact_link = {doc: this.frm.doc, fieldname: 'supplier', doctype: 'Supplier'};
+		frappe.dynamic_link = {doc: this.frm.doc, fieldname: 'supplier', doctype: 'Supplier'};
 
 		this.frm.toggle_display("supplier_name",
 			(this.frm.doc.supplier_name && this.frm.doc.supplier_name!==this.frm.doc.supplier));
diff --git a/erpnext/buying/doctype/supplier/supplier.js b/erpnext/buying/doctype/supplier/supplier.js
index 9808718..ce6f451 100644
--- a/erpnext/buying/doctype/supplier/supplier.js
+++ b/erpnext/buying/doctype/supplier/supplier.js
@@ -16,7 +16,7 @@
 		});
 	},
 	refresh: function(frm) {
-		frappe.contact_link = {doc: frm.doc, fieldname: 'name', doctype: 'Supplier'}
+		frappe.dynamic_link = {doc: frm.doc, fieldname: 'name', doctype: 'Supplier'}
 
 		if(frappe.defaults.get_default("supp_master_name")!="Naming Series") {
 			frm.toggle_display("naming_series", false);
diff --git a/erpnext/crm/doctype/opportunity/opportunity.js b/erpnext/crm/doctype/opportunity/opportunity.js
index d7d412a..32171b0 100644
--- a/erpnext/crm/doctype/opportunity/opportunity.js
+++ b/erpnext/crm/doctype/opportunity/opportunity.js
@@ -52,9 +52,9 @@
 
 	set_contact_link: function(frm) {
 		if(frm.doc.customer) {
-			frappe.contact_link = {doc: frm.doc, fieldname: 'customer', doctype: 'Customer'}
+			frappe.dynamic_link = {doc: frm.doc, fieldname: 'customer', doctype: 'Customer'}
 		} else if(frm.doc.lead) {
-			frappe.contact_link = {doc: frm.doc, fieldname: 'lead', doctype: 'Lead'}
+			frappe.dynamic_link = {doc: frm.doc, fieldname: 'lead', doctype: 'Lead'}
 		}
 	},
 
diff --git a/erpnext/maintenance/doctype/maintenance_schedule/maintenance_schedule.js b/erpnext/maintenance/doctype/maintenance_schedule/maintenance_schedule.js
index b5d1ad3..9639e7f 100644
--- a/erpnext/maintenance/doctype/maintenance_schedule/maintenance_schedule.js
+++ b/erpnext/maintenance/doctype/maintenance_schedule/maintenance_schedule.js
@@ -23,7 +23,7 @@
 // TODO commonify this code
 erpnext.maintenance.MaintenanceSchedule = frappe.ui.form.Controller.extend({
 	refresh: function() {
-		frappe.contact_link = {doc: this.frm.doc, fieldname: 'customer', doctype: 'Customer'}
+		frappe.dynamic_link = {doc: this.frm.doc, fieldname: 'customer', doctype: 'Customer'}
 
 		var me = this;
 
diff --git a/erpnext/maintenance/doctype/maintenance_visit/maintenance_visit.js b/erpnext/maintenance/doctype/maintenance_visit/maintenance_visit.js
index 1a3c35c..62cdf86 100644
--- a/erpnext/maintenance/doctype/maintenance_visit/maintenance_visit.js
+++ b/erpnext/maintenance/doctype/maintenance_visit/maintenance_visit.js
@@ -25,7 +25,7 @@
 // TODO commonify this code
 erpnext.maintenance.MaintenanceVisit = frappe.ui.form.Controller.extend({
 	refresh: function() {
-		frappe.contact_link = {doc: this.frm.doc, fieldname: 'customer', doctype: 'Customer'}
+		frappe.dynamic_link = {doc: this.frm.doc, fieldname: 'customer', doctype: 'Customer'}
 
 		if (this.frm.doc.docstatus===0) {
 			cur_frm.add_custom_button(__('Maintenance Schedule'),
diff --git a/erpnext/public/js/queries.js b/erpnext/public/js/queries.js
index 8236ce7..1141f99 100644
--- a/erpnext/public/js/queries.js
+++ b/erpnext/public/js/queries.js
@@ -43,28 +43,28 @@
 	},
 
 	contact_query: function(doc) {
-		if(frappe.contact_link) {
-			if(!doc[frappe.contact_link.fieldname]) {
+		if(frappe.dynamic_link) {
+			if(!doc[frappe.dynamic_link.fieldname]) {
 				frappe.throw(__("Please set {0}", __(frappe.meta.get_label(doc.doctype,
-					frappe.contact_link.fieldname, doc.name))));
+					frappe.dynamic_link.fieldname, doc.name))));
 			}
 
 			return {
 				query: 'frappe.email.doctype.contact.contact.contact_query',
-				filters: { link_doctype: frappe.contact_link.doctype, link_name: doc[frappe.contact_link.fieldname] } };
+				filters: { link_doctype: frappe.dynamic_link.doctype, link_name: doc[frappe.dynamic_link.fieldname] } };
 		}
 	},
 
 	address_query: function(doc) {
-		if(frappe.contact_link) {
-			if(!doc[frappe.contact_link.fieldname]) {
+		if(frappe.dynamic_link) {
+			if(!doc[frappe.dynamic_link.fieldname]) {
 				frappe.throw(__("Please set {0}", __(frappe.meta.get_label(doc.doctype,
-					frappe.contact_link.fieldname, doc.name))));
+					frappe.dynamic_link.fieldname, doc.name))));
 			}
 
 			return {
-				query: 'frappe.email.doctype.address.address_query',
-				filters: { link_doctype: frappe.contact_link.doctype, link_name: doc[frappe.contact_link.fieldname] } };
+				query: 'frappe.geo.doctype.address.address.address_query',
+				filters: { link_doctype: frappe.dynamic_link.doctype, link_name: doc[frappe.dynamic_link.fieldname] } };
 		}
 	},
 
diff --git a/erpnext/selling/doctype/customer/customer.js b/erpnext/selling/doctype/customer/customer.js
index 975604d..747b31e 100644
--- a/erpnext/selling/doctype/customer/customer.js
+++ b/erpnext/selling/doctype/customer/customer.js
@@ -32,7 +32,7 @@
 			erpnext.toggle_naming_series();
 		}
 
-		frappe.contact_link = {doc: frm.doc, fieldname: 'name', doctype: 'Customer'}
+		frappe.dynamic_link = {doc: frm.doc, fieldname: 'name', doctype: 'Customer'}
 
 		frm.toggle_display(['address_html','contact_html'], !frm.doc.__islocal);
 
diff --git a/erpnext/selling/doctype/quotation/quotation.js b/erpnext/selling/doctype/quotation/quotation.js
index ab1a24a..e06f963 100644
--- a/erpnext/selling/doctype/quotation/quotation.js
+++ b/erpnext/selling/doctype/quotation/quotation.js
@@ -67,12 +67,8 @@
 		this.frm.toggle_reqd("customer", this.frm.doc.quotation_to == "Customer");
 
 		// to overwrite the customer_filter trigger from queries.js
-		$.each(["customer_address", "shipping_address_name"],
-			function(i, opts) {
-				me.frm.set_query(opts, me.frm.doc.quotation_to==="Lead"
-					? erpnext.queries["lead_filter"] : erpnext.queries["customer_filter"]);
-			}
-		);
+		this.frm.set_query('customer_address', erpnext.queries.address_query);
+		this.frm.set_query('shipping_address_name', erpnext.queries.address_query);
 	},
 
 	tc_name: function() {
diff --git a/erpnext/selling/sales_common.js b/erpnext/selling/sales_common.js
index 96ce79a..9d9511e 100644
--- a/erpnext/selling/sales_common.js
+++ b/erpnext/selling/sales_common.js
@@ -106,7 +106,7 @@
 	refresh: function() {
 		this._super();
 
-		frappe.contact_link = {doc: this.frm.doc, fieldname: 'customer', doctype: 'Customer'}
+		frappe.dynamic_link = {doc: this.frm.doc, fieldname: 'customer', doctype: 'Customer'}
 
 		this.frm.toggle_display("customer_name",
 			(this.frm.doc.customer_name && this.frm.doc.customer_name!==this.frm.doc.customer));
diff --git a/erpnext/setup/doctype/sales_partner/sales_partner.js b/erpnext/setup/doctype/sales_partner/sales_partner.js
index 6531630..df8d23f 100644
--- a/erpnext/setup/doctype/sales_partner/sales_partner.js
+++ b/erpnext/setup/doctype/sales_partner/sales_partner.js
@@ -3,7 +3,7 @@
 
 frappe.ui.form.on('Sales Partner', {
 	refresh: function(frm) {
-		frappe.contact_link = {doc: frm.doc, fieldname: 'name', doctype: 'Sales Person'}
+		frappe.dynamic_link = {doc: frm.doc, fieldname: 'name', doctype: 'Sales Person'}
 
 		if(doc.__islocal){
 			hide_field(['address_html', 'contact_html']);
diff --git a/erpnext/shopping_cart/cart.py b/erpnext/shopping_cart/cart.py
index e52ef81..5ca0315 100644
--- a/erpnext/shopping_cart/cart.py
+++ b/erpnext/shopping_cart/cart.py
@@ -375,15 +375,17 @@
 	if not party:
 		return []
 
-	address_docs = frappe.db.sql("""select * from `tabAddress`
-		where `{0}`=%s order by name limit {1}, {2}""".format(party.doctype.lower(),
-			limit_start, limit_page_length), party.name,
-		as_dict=True, update={"doctype": "Address"})
+	address_names = frappe.db.get_all('Dyanamic Link', fields=('parent'),
+		filters=dict(parenttype='Address', link_doctype=party.doctype, link_name=party.name))
 
-	for address in address_docs:
-		address.display = get_address_display(address)
+	out = []
 
-	return address_docs
+	for address_name in address_names:
+		address = frappe.get_doc('Address', address_name)
+		address.display = get_address_display(address.as_dict())
+		out.append(address)
+
+	return out
 
 @frappe.whitelist()
 def apply_shipping_rule(shipping_rule):
diff --git a/erpnext/support/doctype/warranty_claim/warranty_claim.js b/erpnext/support/doctype/warranty_claim/warranty_claim.js
index abd207b..2369a8a 100644
--- a/erpnext/support/doctype/warranty_claim/warranty_claim.js
+++ b/erpnext/support/doctype/warranty_claim/warranty_claim.js
@@ -21,7 +21,7 @@
 
 erpnext.support.WarrantyClaim = frappe.ui.form.Controller.extend({
 	refresh: function() {
-		frappe.contact_link = {doc: this.frm.doc, fieldname: 'customer', doctype: 'Customer'}
+		frappe.dynamic_link = {doc: this.frm.doc, fieldname: 'customer', doctype: 'Customer'}
 
 		if(!cur_frm.doc.__islocal &&
 			(cur_frm.doc.status=='Open' || cur_frm.doc.status == 'Work In Progress')) {
diff --git a/erpnext/utilities/address_and_contact.py b/erpnext/utilities/address_and_contact.py
index eaa71f9..f3435ae 100644
--- a/erpnext/utilities/address_and_contact.py
+++ b/erpnext/utilities/address_and_contact.py
@@ -122,7 +122,7 @@
 def delete_contact_and_address(doctype, name):
 	for parenttype in ('Contact', 'Address'):
 		items = frappe.db.sql("""select parent from `tabDynamic Link`
-			where parenttype=%s and link_type=%s and link_name=%s""",
+			where parenttype=%s and link_doctype=%s and link_name=%s""",
 			(parenttype, doctype, name))
 
 		for name in items: