fix: item price not fetching when customer is unset in item price (#21488)

* fix: item price not fetching when customer is unset in item price

* fix: item price of selling type has hidden supplier value

* fix: remove test variable

* fix: test

* patch: invalid customer/supplier based on item price type

* fix: invalid query

* fix: patch

Co-authored-by: Marica <maricadsouza221197@gmail.com>
diff --git a/erpnext/patches.txt b/erpnext/patches.txt
index f721724..0edadcc 100644
--- a/erpnext/patches.txt
+++ b/erpnext/patches.txt
@@ -681,3 +681,4 @@
 erpnext.patches.v13_0.patch_to_fix_reverse_linking_in_additional_salary_encashment_and_incentive
 execute:frappe.delete_doc_if_exists("Page", "appointment-analytic")
 execute:frappe.rename_doc("Desk Page", "Getting Started", "Home", force=True)
+erpnext.patches.v12_0.unset_customer_supplier_based_on_type_of_item_price
diff --git a/erpnext/patches/v12_0/unset_customer_supplier_based_on_type_of_item_price.py b/erpnext/patches/v12_0/unset_customer_supplier_based_on_type_of_item_price.py
new file mode 100644
index 0000000..60aec05
--- /dev/null
+++ b/erpnext/patches/v12_0/unset_customer_supplier_based_on_type_of_item_price.py
@@ -0,0 +1,15 @@
+from __future__ import unicode_literals
+import frappe
+
+def execute():
+    invalid_selling_item_price = frappe.db.sql(
+        """SELECT name FROM `tabItem Price` WHERE selling = 1 and buying = 0 and (supplier IS NOT NULL or supplier = '')"""
+    )
+    invalid_buying_item_price = frappe.db.sql(
+        """SELECT name FROM `tabItem Price` WHERE selling = 0 and buying = 1 and (customer IS NOT NULL or customer = '')"""
+    )
+    docs_to_modify = invalid_buying_item_price + invalid_selling_item_price
+    for d in docs_to_modify:
+        # saving the doc will auto reset invalid customer/supplier field
+        doc = frappe.get_doc("Item Price", d[0])
+        doc.save()
\ No newline at end of file
diff --git a/erpnext/stock/doctype/item_price/item_price.py b/erpnext/stock/doctype/item_price/item_price.py
index 957c415..8e39eb5 100644
--- a/erpnext/stock/doctype/item_price/item_price.py
+++ b/erpnext/stock/doctype/item_price/item_price.py
@@ -69,3 +69,10 @@
 			self.reference = self.customer
 		if self.buying:
 			self.reference = self.supplier
+		
+		if self.selling and not self.buying:
+			# if only selling then remove supplier
+			self.supplier = None
+		if self.buying and not self.selling:
+			# if only buying then remove customer
+			self.customer = None
diff --git a/erpnext/stock/get_item_details.py b/erpnext/stock/get_item_details.py
index d50712a..11b6403 100644
--- a/erpnext/stock/get_item_details.py
+++ b/erpnext/stock/get_item_details.py
@@ -630,7 +630,7 @@
 		elif args.get("supplier"):
 			conditions += " and supplier=%(supplier)s"
 		else:
-			conditions += " and (customer is null or customer = '') and (supplier is null or supplier = '')"
+			conditions += "and (customer is null or customer = '') and (supplier is null or supplier = '')"
 
 	if args.get('transaction_date'):
 		conditions += """ and %(transaction_date)s between