Skip to content

Commit 675b94b

Browse files
fix: support translated search in get_party_type and refactor raw sql to qb (backport #53191) (#53832)
* fix: support translated search in get_party_type and refactor raw sql to qb (cherry picked from commit d987688) # Conflicts: # erpnext/setup/doctype/party_type/party_type.py * fix: resolve merge conflicts in party_type.py --------- Co-authored-by: Shllokkk <140623894+Shllokkk@users.noreply.github.com>
1 parent bec83c1 commit 675b94b

1 file changed

Lines changed: 25 additions & 17 deletions

File tree

erpnext/setup/doctype/party_type/party_type.py

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import frappe
66
from frappe.model.document import Document
7+
from frappe.query_builder import DocType
78

89

910
class PartyType(Document):
@@ -24,29 +25,36 @@ class PartyType(Document):
2425

2526
@frappe.whitelist()
2627
@frappe.validate_and_sanitize_search_inputs
27-
def get_party_type(doctype, txt, searchfield, start, page_len, filters):
28-
cond = ""
29-
account_type = None
28+
def get_party_type(doctype: str, txt: str, searchfield: str, start: int, page_len: int, filters: dict):
29+
PartyType = DocType("Party Type")
30+
get_party_type_query = frappe.qb.from_(PartyType).select(PartyType.name).orderby(PartyType.name)
31+
32+
condition_list = []
3033

3134
if filters and filters.get("account"):
3235
account_type = frappe.db.get_value("Account", filters.get("account"), "account_type")
3336
if account_type:
3437
if account_type in ["Receivable", "Payable"]:
3538
# Include Employee regardless of its configured account_type, but still respect the text filter
36-
cond = "and (account_type = %(account_type)s or name = 'Employee')"
39+
condition_list.append(
40+
(PartyType.account_type == account_type) | (PartyType.name == "Employee")
41+
)
3742
else:
38-
cond = "and account_type = %(account_type)s"
39-
40-
# Build parameters dictionary
41-
params = {"txt": "%" + txt + "%", "start": start, "page_len": page_len}
42-
if account_type:
43-
params["account_type"] = account_type
44-
45-
result = frappe.db.sql(
46-
f"""select name from `tabParty Type`
47-
where `{searchfield}` LIKE %(txt)s {cond}
48-
order by name limit %(page_len)s offset %(start)s""",
49-
params,
50-
)
43+
condition_list.append(PartyType.account_type == account_type)
44+
45+
for condition in condition_list:
46+
get_party_type_query = get_party_type_query.where(condition)
47+
48+
if frappe.local.lang == "en":
49+
get_party_type_query = get_party_type_query.where(getattr(PartyType, searchfield).like(f"%{txt}%"))
50+
get_party_type_query = get_party_type_query.limit(page_len)
51+
get_party_type_query = get_party_type_query.offset(start)
52+
53+
result = get_party_type_query.run()
54+
else:
55+
result = get_party_type_query.run()
56+
test_str = txt.lower()
57+
result = [row for row in result if test_str in frappe._(row[0]).lower()]
58+
result = result[start : start + page_len]
5159

5260
return result or []

0 commit comments

Comments
 (0)