fix(customer): Improve performance by reducing queries
diff --git a/erpnext/selling/report/customer_wise_item_price/customer_wise_item_price.py b/erpnext/selling/report/customer_wise_item_price/customer_wise_item_price.py
index bdd3922..eb9273a 100644
--- a/erpnext/selling/report/customer_wise_item_price/customer_wise_item_price.py
+++ b/erpnext/selling/report/customer_wise_item_price/customer_wise_item_price.py
@@ -62,12 +62,14 @@
def get_data(filters=None):
data = []
customer_details = get_customer_details(filters)
+
items = get_selling_items(filters)
+ item_stock_map = frappe.get_all("Bin", fields=["item_code", "sum(actual_qty) AS available"], group_by="item_code")
+ item_stock_map = {item.item_code: item.available for item in item_stock_map}
for item in items:
price_list_rate = get_price_list_rate_for(customer_details, item.item_code) or 0.0
- available_stock = frappe.db.sql("SELECT sum(actual_qty) FROM `tabBin` WHERE item_code = %s", item.item_code)
- available_stock = available_stock[0][0] if available_stock else None
+ available_stock = item_stock_map.get(item.item_code)
data.append({
"item_code": item.item_code,
@@ -98,4 +100,4 @@
items = frappe.get_all("Item", filters=item_filters, fields=["item_code", "item_name"], order_by="item_name")
- return items
\ No newline at end of file
+ return items