Skip to content

Commit

Permalink
Merge pull request #32693 from rohitwaghchaure/searchfield-not-workin…
Browse files Browse the repository at this point in the history
…g-for-customer-supplier

fix: Search field not working for customer, supplier
  • Loading branch information
rohitwaghchaure authored Oct 24, 2022
2 parents 48808ae + 5f84993 commit 183662c
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 11 deletions.
35 changes: 35 additions & 0 deletions erpnext/buying/doctype/supplier/test_supplier.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@


import frappe
from frappe.custom.doctype.property_setter.property_setter import make_property_setter
from frappe.test_runner import make_test_records

from erpnext.accounts.party import get_due_date
Expand Down Expand Up @@ -152,6 +153,40 @@ def test_party_details_tax_category(self):
# Rollback
address.delete()

def test_serach_fields_for_supplier(self):
from erpnext.controllers.queries import supplier_query

supplier_name = create_supplier(supplier_name="Test Supplier 1").name

make_property_setter(
"Supplier", None, "search_fields", "supplier_group", "Data", for_doctype="Doctype"
)

data = supplier_query(
"Supplier", supplier_name, "name", 0, 20, filters={"name": supplier_name}, as_dict=True
)

self.assertEqual(data[0].name, supplier_name)
self.assertEqual(data[0].supplier_group, "Services")
self.assertTrue("supplier_type" not in data[0])

make_property_setter(
"Supplier",
None,
"search_fields",
"supplier_group, supplier_type",
"Data",
for_doctype="Doctype",
)
data = supplier_query(
"Supplier", supplier_name, "name", 0, 20, filters={"name": supplier_name}, as_dict=True
)

self.assertEqual(data[0].name, supplier_name)
self.assertEqual(data[0].supplier_group, "Services")
self.assertEqual(data[0].supplier_type, "Company")
self.assertTrue("supplier_type" in data[0])


def create_supplier(**args):
args = frappe._dict(args)
Expand Down
21 changes: 10 additions & 11 deletions erpnext/controllers/queries.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,18 +78,16 @@ def lead_query(doctype, txt, searchfield, start, page_len, filters):

@frappe.whitelist()
@frappe.validate_and_sanitize_search_inputs
def customer_query(doctype, txt, searchfield, start, page_len, filters):
def customer_query(doctype, txt, searchfield, start, page_len, filters, as_dict=False):
doctype = "Customer"
conditions = []
cust_master_name = frappe.defaults.get_user_default("cust_master_name")

if cust_master_name == "Customer Name":
fields = ["name", "customer_group", "territory"]
else:
fields = ["name", "customer_name", "customer_group", "territory"]
fields = ["name"]
if cust_master_name != "Customer Name":
fields = ["customer_name"]

fields = get_fields(doctype, fields)

searchfields = frappe.get_meta(doctype).get_search_fields()
searchfields = " or ".join(field + " like %(txt)s" for field in searchfields)

Expand All @@ -112,20 +110,20 @@ def customer_query(doctype, txt, searchfield, start, page_len, filters):
}
),
{"txt": "%%%s%%" % txt, "_txt": txt.replace("%", ""), "start": start, "page_len": page_len},
as_dict=as_dict,
)


# searches for supplier
@frappe.whitelist()
@frappe.validate_and_sanitize_search_inputs
def supplier_query(doctype, txt, searchfield, start, page_len, filters):
def supplier_query(doctype, txt, searchfield, start, page_len, filters, as_dict=False):
doctype = "Supplier"
supp_master_name = frappe.defaults.get_user_default("supp_master_name")

if supp_master_name == "Supplier Name":
fields = ["name", "supplier_group"]
else:
fields = ["name", "supplier_name", "supplier_group"]
fields = ["name"]
if supp_master_name != "Supplier Name":
fields = ["supplier_name"]

fields = get_fields(doctype, fields)

Expand All @@ -145,6 +143,7 @@ def supplier_query(doctype, txt, searchfield, start, page_len, filters):
**{"field": ", ".join(fields), "key": searchfield, "mcond": get_match_cond(doctype)}
),
{"txt": "%%%s%%" % txt, "_txt": txt.replace("%", ""), "start": start, "page_len": page_len},
as_dict=as_dict,
)


Expand Down
28 changes: 28 additions & 0 deletions erpnext/selling/doctype/customer/test_customer.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@


import frappe
from frappe.custom.doctype.property_setter.property_setter import make_property_setter
from frappe.test_runner import make_test_records
from frappe.tests.utils import FrappeTestCase
from frappe.utils import flt
Expand Down Expand Up @@ -341,6 +342,33 @@ def test_customer_payment_terms(self):
due_date = get_due_date("2017-01-22", "Customer", "_Test Customer")
self.assertEqual(due_date, "2017-01-22")

def test_serach_fields_for_customer(self):
from erpnext.controllers.queries import customer_query

make_property_setter(
"Customer", None, "search_fields", "customer_group", "Data", for_doctype="Doctype"
)

data = customer_query(
"Customer", "_Test Customer", "", 0, 20, filters={"name": "_Test Customer"}, as_dict=True
)

self.assertEqual(data[0].name, "_Test Customer")
self.assertEqual(data[0].customer_group, "_Test Customer Group")
self.assertTrue("territory" not in data[0])

make_property_setter(
"Customer", None, "search_fields", "customer_group, territory", "Data", for_doctype="Doctype"
)
data = customer_query(
"Customer", "_Test Customer", "", 0, 20, filters={"name": "_Test Customer"}, as_dict=True
)

self.assertEqual(data[0].name, "_Test Customer")
self.assertEqual(data[0].customer_group, "_Test Customer Group")
self.assertEqual(data[0].territory, "_Test Territory")
self.assertTrue("territory" in data[0])


def get_customer_dict(customer_name):
return {
Expand Down

0 comments on commit 183662c

Please sign in to comment.