From d9876880586e5f98b5a991cc1327db370a30396e Mon Sep 17 00:00:00 2001 From: Shllokkk <140623894+Shllokkk@users.noreply.github.com> Date: Fri, 27 Mar 2026 10:49:34 +0530 Subject: [PATCH] fix: support translated search in get_party_type and refactor raw sql to qb --- .../setup/doctype/party_type/party_type.py | 36 +++++++++++-------- 1 file changed, 22 insertions(+), 14 deletions(-) diff --git a/erpnext/setup/doctype/party_type/party_type.py b/erpnext/setup/doctype/party_type/party_type.py index 334e45e865b..cf72eb3bbdc 100644 --- a/erpnext/setup/doctype/party_type/party_type.py +++ b/erpnext/setup/doctype/party_type/party_type.py @@ -4,6 +4,7 @@ import frappe from frappe.model.document import Document +from frappe.query_builder import DocType class PartyType(Document): @@ -25,28 +26,35 @@ class PartyType(Document): @frappe.whitelist() @frappe.validate_and_sanitize_search_inputs def get_party_type(doctype: str, txt: str, searchfield: str, start: int, page_len: int, filters: dict): - cond = "" - account_type = None + PartyType = DocType("Party Type") + get_party_type_query = frappe.qb.from_(PartyType).select(PartyType.name).orderby(PartyType.name) + + condition_list = [] if filters and filters.get("account"): account_type = frappe.db.get_value("Account", filters.get("account"), "account_type") if account_type: if account_type in ["Receivable", "Payable"]: # Include Employee regardless of its configured account_type, but still respect the text filter - cond = "and (account_type = %(account_type)s or name = 'Employee')" + condition_list.append( + (PartyType.account_type == account_type) | (PartyType.name == "Employee") + ) else: - cond = "and account_type = %(account_type)s" + condition_list.append(PartyType.account_type == account_type) - # Build parameters dictionary - params = {"txt": "%" + txt + "%", "start": start, "page_len": page_len} - if account_type: - params["account_type"] = account_type + for condition in condition_list: + get_party_type_query = get_party_type_query.where(condition) - result = frappe.db.sql( - f"""select name from `tabParty Type` - where `{searchfield}` LIKE %(txt)s {cond} - order by name limit %(page_len)s offset %(start)s""", - params, - ) + if frappe.local.lang == "en": + get_party_type_query = get_party_type_query.where(getattr(PartyType, searchfield).like(f"%{txt}%")) + get_party_type_query = get_party_type_query.limit(page_len) + get_party_type_query = get_party_type_query.offset(start) + + result = get_party_type_query.run() + else: + result = get_party_type_query.run() + test_str = txt.lower() + result = [row for row in result if test_str in frappe._(row[0]).lower()] + result = result[start : start + page_len] return result or []