Merge branch 'develop' into fix/scr-return-rejected-qty
diff --git a/erpnext/buying/doctype/purchase_order/purchase_order.json b/erpnext/buying/doctype/purchase_order/purchase_order.json
index 2193985..ded45b8 100644
--- a/erpnext/buying/doctype/purchase_order/purchase_order.json
+++ b/erpnext/buying/doctype/purchase_order/purchase_order.json
@@ -1108,7 +1108,8 @@
    "fetch_from": "supplier.is_internal_supplier",
    "fieldname": "is_internal_supplier",
    "fieldtype": "Check",
-   "label": "Is Internal Supplier"
+   "label": "Is Internal Supplier",
+   "read_only": 1
   },
   {
    "fetch_from": "supplier.represents_company",
@@ -1232,7 +1233,7 @@
  "idx": 105,
  "is_submittable": 1,
  "links": [],
- "modified": "2022-10-11 13:01:41.674352",
+ "modified": "2022-11-17 12:34:36.033363",
  "modified_by": "Administrator",
  "module": "Buying",
  "name": "Purchase Order",
diff --git a/erpnext/buying/doctype/request_for_quotation/request_for_quotation.py b/erpnext/buying/doctype/request_for_quotation/request_for_quotation.py
index a560bda..bdbc9ce 100644
--- a/erpnext/buying/doctype/request_for_quotation/request_for_quotation.py
+++ b/erpnext/buying/doctype/request_for_quotation/request_for_quotation.py
@@ -478,7 +478,7 @@
 		conditions += "and rfq.transaction_date = '{0}'".format(filters.get("transaction_date"))
 
 	rfq_data = frappe.db.sql(
-		"""
+		f"""
 		select
 			distinct rfq.name, rfq.transaction_date,
 			rfq.company
@@ -486,15 +486,18 @@
 			`tabRequest for Quotation` rfq, `tabRequest for Quotation Supplier` rfq_supplier
 		where
 			rfq.name = rfq_supplier.parent
-			and rfq_supplier.supplier = '{0}'
+			and rfq_supplier.supplier = %(supplier)s
 			and rfq.docstatus = 1
-			and rfq.company = '{1}'
-			{2}
+			and rfq.company = %(company)s
+			{conditions}
 		order by rfq.transaction_date ASC
-		limit %(page_len)s offset %(start)s """.format(
-			filters.get("supplier"), filters.get("company"), conditions
-		),
-		{"page_len": page_len, "start": start},
+		limit %(page_len)s offset %(start)s """,
+		{
+			"page_len": page_len,
+			"start": start,
+			"company": filters.get("company"),
+			"supplier": filters.get("supplier"),
+		},
 		as_dict=1,
 	)
 
diff --git a/erpnext/stock/doctype/material_request/material_request.py b/erpnext/stock/doctype/material_request/material_request.py
index 817248e..04aee42 100644
--- a/erpnext/stock/doctype/material_request/material_request.py
+++ b/erpnext/stock/doctype/material_request/material_request.py
@@ -10,7 +10,7 @@
 import frappe
 from frappe import _, msgprint
 from frappe.model.mapper import get_mapped_doc
-from frappe.utils import cstr, flt, get_link_to_form, getdate, new_line_sep, nowdate
+from frappe.utils import cint, cstr, flt, get_link_to_form, getdate, new_line_sep, nowdate
 
 from erpnext.buying.utils import check_on_hold_or_closed_status, validate_for_items
 from erpnext.controllers.buying_controller import BuyingController
@@ -500,13 +500,13 @@
 			and mr.per_ordered < 99.99
 			and mr.docstatus = 1
 			and mr.status != 'Stopped'
-			and mr.company = '{1}'
-			{2}
+			and mr.company = %s
+			{1}
 		order by mr_item.item_code ASC
-		limit {3} offset {4} """.format(
-			", ".join(["%s"] * len(supplier_items)), filters.get("company"), conditions, page_len, start
+		limit {2} offset {3} """.format(
+			", ".join(["%s"] * len(supplier_items)), conditions, cint(page_len), cint(start)
 		),
-		tuple(supplier_items),
+		tuple(supplier_items) + (filters.get("company"),),
 		as_dict=1,
 	)
 
diff --git a/erpnext/stock/doctype/quality_inspection/quality_inspection.py b/erpnext/stock/doctype/quality_inspection/quality_inspection.py
index 8ffd3f2..e44e1b7 100644
--- a/erpnext/stock/doctype/quality_inspection/quality_inspection.py
+++ b/erpnext/stock/doctype/quality_inspection/quality_inspection.py
@@ -6,7 +6,7 @@
 from frappe import _
 from frappe.model.document import Document
 from frappe.model.mapper import get_mapped_doc
-from frappe.utils import cint, flt
+from frappe.utils import cint, cstr, flt
 
 from erpnext.stock.doctype.quality_inspection_template.quality_inspection_template import (
 	get_template_details,
@@ -219,68 +219,71 @@
 @frappe.whitelist()
 @frappe.validate_and_sanitize_search_inputs
 def item_query(doctype, txt, searchfield, start, page_len, filters):
-	if filters.get("from"):
-		from frappe.desk.reportview import get_match_cond
+	from frappe.desk.reportview import get_match_cond
 
-		mcond = get_match_cond(filters["from"])
-		cond, qi_condition = "", "and (quality_inspection is null or quality_inspection = '')"
+	from_doctype = cstr(filters.get("doctype"))
+	if not from_doctype or not frappe.db.exist("DocType", from_doctype):
+		return []
 
-		if filters.get("parent"):
-			if (
-				filters.get("from") in ["Purchase Invoice Item", "Purchase Receipt Item"]
-				and filters.get("inspection_type") != "In Process"
-			):
-				cond = """and item_code in (select name from `tabItem` where
-					inspection_required_before_purchase = 1)"""
-			elif (
-				filters.get("from") in ["Sales Invoice Item", "Delivery Note Item"]
-				and filters.get("inspection_type") != "In Process"
-			):
-				cond = """and item_code in (select name from `tabItem` where
-					inspection_required_before_delivery = 1)"""
-			elif filters.get("from") == "Stock Entry Detail":
-				cond = """and s_warehouse is null"""
+	mcond = get_match_cond(from_doctype)
+	cond, qi_condition = "", "and (quality_inspection is null or quality_inspection = '')"
 
-			if filters.get("from") in ["Supplier Quotation Item"]:
-				qi_condition = ""
+	if filters.get("parent"):
+		if (
+			from_doctype in ["Purchase Invoice Item", "Purchase Receipt Item"]
+			and filters.get("inspection_type") != "In Process"
+		):
+			cond = """and item_code in (select name from `tabItem` where
+				inspection_required_before_purchase = 1)"""
+		elif (
+			from_doctype in ["Sales Invoice Item", "Delivery Note Item"]
+			and filters.get("inspection_type") != "In Process"
+		):
+			cond = """and item_code in (select name from `tabItem` where
+				inspection_required_before_delivery = 1)"""
+		elif from_doctype == "Stock Entry Detail":
+			cond = """and s_warehouse is null"""
 
-			return frappe.db.sql(
-				"""
-					SELECT item_code
-					FROM `tab{doc}`
-					WHERE parent=%(parent)s and docstatus < 2 and item_code like %(txt)s
-					{qi_condition} {cond} {mcond}
-					ORDER BY item_code limit {page_len} offset {start}
-				""".format(
-					doc=filters.get("from"),
-					cond=cond,
-					mcond=mcond,
-					start=start,
-					page_len=page_len,
-					qi_condition=qi_condition,
-				),
-				{"parent": filters.get("parent"), "txt": "%%%s%%" % txt},
-			)
+		if from_doctype in ["Supplier Quotation Item"]:
+			qi_condition = ""
 
-		elif filters.get("reference_name"):
-			return frappe.db.sql(
-				"""
-					SELECT production_item
-					FROM `tab{doc}`
-					WHERE name = %(reference_name)s and docstatus < 2 and production_item like %(txt)s
-					{qi_condition} {cond} {mcond}
-					ORDER BY production_item
-					limit {page_len} offset {start}
-				""".format(
-					doc=filters.get("from"),
-					cond=cond,
-					mcond=mcond,
-					start=start,
-					page_len=page_len,
-					qi_condition=qi_condition,
-				),
-				{"reference_name": filters.get("reference_name"), "txt": "%%%s%%" % txt},
-			)
+		return frappe.db.sql(
+			"""
+				SELECT item_code
+				FROM `tab{doc}`
+				WHERE parent=%(parent)s and docstatus < 2 and item_code like %(txt)s
+				{qi_condition} {cond} {mcond}
+				ORDER BY item_code limit {page_len} offset {start}
+			""".format(
+				doc=from_doctype,
+				cond=cond,
+				mcond=mcond,
+				start=cint(start),
+				page_len=cint(page_len),
+				qi_condition=qi_condition,
+			),
+			{"parent": filters.get("parent"), "txt": "%%%s%%" % txt},
+		)
+
+	elif filters.get("reference_name"):
+		return frappe.db.sql(
+			"""
+				SELECT production_item
+				FROM `tab{doc}`
+				WHERE name = %(reference_name)s and docstatus < 2 and production_item like %(txt)s
+				{qi_condition} {cond} {mcond}
+				ORDER BY production_item
+				limit {page_len} offset {start}
+			""".format(
+				doc=from_doctype,
+				cond=cond,
+				mcond=mcond,
+				start=cint(start),
+				page_len=cint(page_len),
+				qi_condition=qi_condition,
+			),
+			{"reference_name": filters.get("reference_name"), "txt": "%%%s%%" % txt},
+		)
 
 
 @frappe.whitelist()