fix(customer): Improve performance by reducing queries

This commit is contained in:
Rohan Bansal 2019-06-17 12:11:04 +05:30
parent e3a3306b30
commit 0426636a32

View File

@ -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
return items