@@ -367,58 +367,63 @@ def calculate_mean(self, reading):
367367@frappe .whitelist ()
368368@frappe .validate_and_sanitize_search_inputs
369369def item_query (doctype : Any , txt : str | None , searchfield : Any , start : int , page_len : int , filters : dict ):
370- from frappe . desk . reportview import get_match_cond
370+ reference_doctype = filters . get ( "reference_doctype" )
371371
372- from_doctype = cstr (filters .get ("from" ))
373- parent_doctype = cstr (filters .get ("parent_doctype" ))
374- if not from_doctype or not frappe .db .exists ("DocType" , from_doctype ):
372+ if not reference_doctype :
375373 return []
376-
377- mcond = get_match_cond (parent_doctype or from_doctype )
378- cond , qi_condition = "" , "and (quality_inspection is null or quality_inspection = '')"
379-
380- if filters .get ("parent" ):
381- if (
382- from_doctype in ["Purchase Invoice Item" , "Purchase Receipt Item" ]
383- and filters .get ("inspection_type" ) != "In Process"
384- ):
385- cond = """and item_code in (select name from `tabItem` where
386- inspection_required_before_purchase = 1)"""
387- elif (
388- from_doctype in ["Sales Invoice Item" , "Delivery Note Item" ]
389- and filters .get ("inspection_type" ) != "In Process"
390- ):
391- cond = """and item_code in (select name from `tabItem` where
392- inspection_required_before_delivery = 1)"""
393- elif from_doctype == "Stock Entry Detail" :
394- cond = """and s_warehouse is null"""
395-
396- if from_doctype in ["Supplier Quotation Item" ]:
397- qi_condition = ""
398-
399- return frappe .db .sql (
400- f"""
401- SELECT distinct item_code, item_name
402- FROM `tab{ from_doctype } `
403- WHERE parent=%(parent)s and docstatus < 2 and item_code like %(txt)s
404- { qi_condition } { cond } { mcond }
405- ORDER BY item_code limit { cint (page_len )} offset { cint (start )}
406- """ ,
407- {"parent" : filters .get ("parent" ), "txt" : "%%%s%%" % txt },
374+ elif reference_doctype == "Job Card" :
375+ production_item , item_name = frappe .get_value (
376+ "Job Card" , filters .get ("reference_name" ), ["production_item" , "item_name" ]
408377 )
378+ return ((production_item , item_name ),)
379+ else :
380+ my_filters = [
381+ ["items.parent" , "=" , filters .get ("reference_name" )],
382+ "and" ,
383+ ["items.item_code" , "like" , f"%{ txt } %" ],
384+ "and" ,
385+ ["docstatus" , "<" , 2 ],
386+ "and" ,
387+ ["items.quality_inspection" , "is" , "not set" ],
388+ ]
409389
410- elif filters .get ("reference_name" ):
411- return frappe .db .sql (
412- f"""
413- SELECT production_item
414- FROM `tab{ from_doctype } `
415- WHERE name = %(reference_name)s and docstatus < 2 and production_item like %(txt)s
416- { qi_condition } { cond } { mcond }
417- ORDER BY production_item
418- limit { cint (page_len )} offset { cint (start )}
419- """ ,
420- {"reference_name" : filters .get ("reference_name" ), "txt" : "%%%s%%" % txt },
421- )
390+ if reference_doctype == "Stock Entry" :
391+ my_filters .extend (
392+ [
393+ "and" ,
394+ ["items.t_warehouse" , "is" , "not set" ],
395+ ]
396+ )
397+ elif filters .get ("inspection_type" ) != "In Process" :
398+ my_filters .extend (
399+ [
400+ "and" ,
401+ [
402+ "items.item_code" ,
403+ "in" ,
404+ frappe .get_list (
405+ "Item" ,
406+ filters = {
407+ "inspection_required_before_purchase"
408+ if filters .get ("inspection_type" ) == "Incoming"
409+ else "inspection_required_before_delivery" : 1
410+ },
411+ pluck = "name" ,
412+ ),
413+ ],
414+ ]
415+ )
416+
417+ return frappe .get_query (
418+ reference_doctype ,
419+ fields = ["items.item_code, items.item_name" ],
420+ filters = my_filters ,
421+ offset = start ,
422+ limit = page_len ,
423+ order_by = "items.item_code" ,
424+ ignore_permissions = False ,
425+ distinct = True ,
426+ ).run ()
422427
423428
424429@frappe .whitelist ()
0 commit comments