diff --git a/frappe/contacts/doctype/contact/contact.js b/frappe/contacts/doctype/contact/contact.js index 9e9b0248d9..5f13a55f5a 100644 --- a/frappe/contacts/doctype/contact/contact.js +++ b/frappe/contacts/doctype/contact/contact.js @@ -41,6 +41,25 @@ frappe.ui.form.on("Contact", { } }); frm.refresh_field("links"); + + if (frm.doc.links.length > 0) { + frappe.call({ + method: "frappe.contacts.doctype.contact.contact.address_query", + args: {links: frm.doc.links}, + callback: function(r) { + if (r && r.message) { + console.log(r.message); + frm.set_query("address", function () { + return { + filters: { + name: ["in", r.message], + } + } + }); + } + } + }); + } }, validate: function(frm) { // clear linked customer / supplier / sales partner on saving... diff --git a/frappe/contacts/doctype/contact/contact.py b/frappe/contacts/doctype/contact/contact.py index 6857d013a1..588e80def3 100644 --- a/frappe/contacts/doctype/contact/contact.py +++ b/frappe/contacts/doctype/contact/contact.py @@ -198,6 +198,29 @@ def contact_query(doctype, txt, searchfield, start, page_len, filters): 'link_doctype': link_doctype }) +@frappe.whitelist() +def address_query(links): + import json + + links = [{"link_doctype": d.get("link_doctype"), "link_name": d.get("link_name")} for d in json.loads(links)] + result = [] + + for link in links: + res = frappe.db.sql(""" + SELECT `tabAddress`.name + FROM `tabAddress`, `tabDynamic Link` + WHERE `tabDynamic Link`.parenttype='Address' + AND `tabDynamic Link`.parent=`tabAddress`.name + AND `tabDynamic Link`.link_doctype = %(link_doctype)s + AND `tabDynamic Link`.link_name = %(link_name)s + """, { + "link_doctype": link.get("link_doctype"), + "link_name": link.get("link_name"), + }, as_dict=True) + + result.extend([l.name for l in res]) + + return result def get_contact_with_phone_number(number): if not number: return