fix: project query controller logic

(cherry picked from commit 4eefb445a748100f3c36094188e38c127ad80051)
This commit is contained in:
ruthra kumar 2024-01-16 13:38:53 +05:30 committed by Mergify
parent bcf8053fd9
commit 07e2901e4b

View File

@ -6,8 +6,9 @@ import json
from collections import OrderedDict, defaultdict from collections import OrderedDict, defaultdict
import frappe import frappe
from frappe import scrub from frappe import qb, scrub
from frappe.desk.reportview import get_filters_cond, get_match_cond from frappe.desk.reportview import get_filters_cond, get_match_cond
from frappe.query_builder import Criterion
from frappe.query_builder.functions import Concat, Sum from frappe.query_builder.functions import Concat, Sum
from frappe.utils import nowdate, today, unique from frappe.utils import nowdate, today, unique
@ -339,37 +340,32 @@ def bom(doctype, txt, searchfield, start, page_len, filters):
@frappe.whitelist() @frappe.whitelist()
@frappe.validate_and_sanitize_search_inputs @frappe.validate_and_sanitize_search_inputs
def get_project_name(doctype, txt, searchfield, start, page_len, filters): def get_project_name(doctype, txt, searchfield, start, page_len, filters):
doctype = "Project" proj = qb.DocType("Project")
cond = "" qb_filter_and_conditions = []
qb_filter_or_conditions = []
if filters and filters.get("customer"): if filters and filters.get("customer"):
cond = """(`tabProject`.customer = %s or qb_filter_and_conditions.append(proj.customer == filters.get("customer"))
ifnull(`tabProject`.customer,"")="") and""" % (
frappe.db.escape(filters.get("customer"))
)
fields = get_fields(doctype, ["name", "project_name"]) qb_filter_and_conditions.append(proj.status.notin(["Completed", "Cancelled"]))
searchfields = frappe.get_meta(doctype).get_search_fields()
searchfields = " or ".join(["`tabProject`." + field + " like %(txt)s" for field in searchfields])
return frappe.db.sql( q = qb.from_(proj)
"""select {fields} from `tabProject`
where fields = get_fields("Project", ["name", "project_name"])
`tabProject`.status not in ('Completed', 'Cancelled') for x in fields:
and {cond} {scond} {match_cond} q = q.select(proj[x])
order by
(case when locate(%(_txt)s, `tabProject`.name) > 0 then locate(%(_txt)s, `tabProject`.name) else 99999 end), # ignore 'customer' and 'status' on searchfields as they must be exactly matched
`tabProject`.idx desc, searchfields = [
`tabProject`.name asc x for x in frappe.get_meta(doctype).get_search_fields() if x not in ["customer", "status"]
limit {page_len} offset {start}""".format( ]
fields=", ".join(["`tabProject`.{0}".format(f) for f in fields]), if txt:
cond=cond, for x in searchfields:
scond=searchfields, qb_filter_or_conditions.append(proj[x].like(f"%{txt}%"))
match_cond=get_match_cond(doctype),
start=start, q = q.where(Criterion.all(qb_filter_and_conditions)).where(Criterion.any(qb_filter_or_conditions))
page_len=page_len, if page_len:
), q = q.limit(page_len)
{"txt": "%{0}%".format(txt), "_txt": txt.replace("%", "")}, return q.run()
)
@frappe.whitelist() @frappe.whitelist()