From c2b8b97d7dcbdfd3cd8a070ce842f339c67ebd43 Mon Sep 17 00:00:00 2001 From: Mihir Kandoi Date: Tue, 25 Nov 2025 10:18:04 +0530 Subject: [PATCH] fix: incorrect query filter when selecting primary customer adr (#50727) --- erpnext/buying/doctype/supplier/supplier.js | 8 +++--- erpnext/buying/doctype/supplier/supplier.py | 22 ++++++++++------ erpnext/selling/doctype/customer/customer.js | 9 ++++--- .../selling/doctype/customer/customer.json | 2 +- erpnext/selling/doctype/customer/customer.py | 26 ++++++++++++------- 5 files changed, 43 insertions(+), 24 deletions(-) diff --git a/erpnext/buying/doctype/supplier/supplier.js b/erpnext/buying/doctype/supplier/supplier.js index 0621503c100..fb4ef867ade 100644 --- a/erpnext/buying/doctype/supplier/supplier.js +++ b/erpnext/buying/doctype/supplier/supplier.js @@ -41,18 +41,20 @@ frappe.ui.form.on("Supplier", { frm.set_query("supplier_primary_contact", function (doc) { return { - query: "erpnext.buying.doctype.supplier.supplier.get_supplier_primary_contact", + query: "erpnext.buying.doctype.supplier.supplier.get_supplier_primary", filters: { supplier: doc.name, + type: "Contact", }, }; }); frm.set_query("supplier_primary_address", function (doc) { return { + query: "erpnext.buying.doctype.supplier.supplier.get_supplier_primary", filters: { - link_doctype: "Supplier", - link_name: doc.name, + supplier: doc.name, + type: "Address", }, }; }); diff --git a/erpnext/buying/doctype/supplier/supplier.py b/erpnext/buying/doctype/supplier/supplier.py index e3aac1b407a..be85f887047 100644 --- a/erpnext/buying/doctype/supplier/supplier.py +++ b/erpnext/buying/doctype/supplier/supplier.py @@ -219,19 +219,25 @@ class Supplier(TransactionBase): @frappe.whitelist() @frappe.validate_and_sanitize_search_inputs -def get_supplier_primary_contact(doctype, txt, searchfield, start, page_len, filters): +def get_supplier_primary(doctype, txt, searchfield, start, page_len, filters): supplier = filters.get("supplier") - contact = frappe.qb.DocType("Contact") + type = filters.get("type") + type_doctype = frappe.qb.DocType(type) dynamic_link = frappe.qb.DocType("Dynamic Link") - return ( - frappe.qb.from_(contact) + query = ( + frappe.qb.from_(type_doctype) .join(dynamic_link) - .on(contact.name == dynamic_link.parent) - .select(contact.name, contact.email_id) + .on(type_doctype.name == dynamic_link.parent) + .select(type_doctype.name) .where( (dynamic_link.link_name == supplier) & (dynamic_link.link_doctype == "Supplier") - & (contact.name.like(f"%{txt}%")) + & (type_doctype.name.like(f"%{txt}%")) ) - ).run(as_dict=False) + ) + + if type == "Contact": + query = query.select(type_doctype.email_id) + + return query.run() diff --git a/erpnext/selling/doctype/customer/customer.js b/erpnext/selling/doctype/customer/customer.js index 230472a6c29..9864beeffa5 100644 --- a/erpnext/selling/doctype/customer/customer.js +++ b/erpnext/selling/doctype/customer/customer.js @@ -74,17 +74,20 @@ frappe.ui.form.on("Customer", { frm.set_query("customer_primary_contact", function (doc) { return { - query: "erpnext.selling.doctype.customer.customer.get_customer_primary_contact", + query: "erpnext.selling.doctype.customer.customer.get_customer_primary", filters: { customer: doc.name, + type: "Contact", }, }; }); + frm.set_query("customer_primary_address", function (doc) { return { + query: "erpnext.selling.doctype.customer.customer.get_customer_primary", filters: { - link_doctype: "Customer", - link_name: doc.name, + customer: doc.name, + type: "Address", }, }; }); diff --git a/erpnext/selling/doctype/customer/customer.json b/erpnext/selling/doctype/customer/customer.json index 33ddc6d316d..73994d3dd0b 100644 --- a/erpnext/selling/doctype/customer/customer.json +++ b/erpnext/selling/doctype/customer/customer.json @@ -618,7 +618,7 @@ "link_fieldname": "party" } ], - "modified": "2025-04-27 12:01:29.549008", + "modified": "2025-11-25 09:35:56.772949", "modified_by": "Administrator", "module": "Selling", "name": "Customer", diff --git a/erpnext/selling/doctype/customer/customer.py b/erpnext/selling/doctype/customer/customer.py index 8b5d6a21604..fab36058bc9 100644 --- a/erpnext/selling/doctype/customer/customer.py +++ b/erpnext/selling/doctype/customer/customer.py @@ -815,21 +815,29 @@ def make_address(args, is_primary_address=1, is_shipping_address=1): @frappe.whitelist() @frappe.validate_and_sanitize_search_inputs -def get_customer_primary_contact(doctype, txt, searchfield, start, page_len, filters): +def get_customer_primary(doctype, txt, searchfield, start, page_len, filters): customer = filters.get("customer") - - con = qb.DocType("Contact") + type = filters.get("type") + type_doctype = qb.DocType(type) dlink = qb.DocType("Dynamic Link") - return ( - qb.from_(con) + query = ( + qb.from_(type_doctype) .join(dlink) - .on(con.name == dlink.parent) - .select(con.name, con.email_id) - .where((dlink.link_name == customer) & (con.name.like(f"%{txt}%"))) - .run() + .on(type_doctype.name == dlink.parent) + .select(type_doctype.name) + .where( + (dlink.link_name == customer) + & (type_doctype.name.like(f"%{txt}%")) + & (dlink.link_doctype == "Customer") + ) ) + if type == "Contact": + query = query.select(type_doctype.email_id) + + return query.run() + def parse_full_name(full_name: str) -> tuple[str, str | None, str | None]: """Parse full name into first name, middle name and last name"""