From 0426636a3270f23c6cccaa9ea519c850798c95fc Mon Sep 17 00:00:00 2001 From: Rohan Bansal Date: Mon, 17 Jun 2019 12:11:04 +0530 Subject: [PATCH] fix(customer): Improve performance by reducing queries --- .../customer_wise_item_price/customer_wise_item_price.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) 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 bdd39229dfd..eb9273a5626 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_columns(filters=None): 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 @@ def get_selling_items(filters): 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