diff --git a/erpnext/stock/doctype/material_request/material_request.py b/erpnext/stock/doctype/material_request/material_request.py index b0a0158abf4c..8d38fdcd8df0 100644 --- a/erpnext/stock/doctype/material_request/material_request.py +++ b/erpnext/stock/doctype/material_request/material_request.py @@ -550,17 +550,28 @@ def get_default_supplier_query(doctype, txt, searchfield, start, page_len, filte for d in doc.items: item_list.append(d.item_code) - return frappe.db.sql( - """select default_supplier - from `tabItem Default` - where parent in ({0}) and - default_supplier IS NOT NULL - """.format( - ", ".join(["%s"] * len(item_list)) - ), - tuple(item_list), + supplier = frappe.qb.DocType("Supplier") + item_default = frappe.qb.DocType("Item Default") + query = ( + frappe.qb.from_(supplier) + .left_join(item_default) + .on(supplier.name == item_default.default_supplier) + .select(item_default.default_supplier) + .where( + (item_default.parent.isin(item_list)) + & (item_default.default_supplier.notnull()) + & (supplier[searchfield].like(f"%{txt}%")) + ) + .offset(start) + .limit(page_len) ) + meta = frappe.get_meta("Supplier") + if meta.show_title_field_in_link and meta.title_field: + query = query.select(supplier[meta.title_field]) + + return query.run(as_dict=False) + @frappe.whitelist() def make_supplier_quotation(source_name, target_doc=None):