Merge pull request #31601 from pps190/pref-wh-wise-stock-value
perf: reduce db calls for warehouse wise stock value chart
This commit is contained in:
commit
1a2ffc7c6e
@ -6,8 +6,6 @@ import frappe
|
||||
from frappe import _
|
||||
from frappe.utils.dashboard import cache_source
|
||||
|
||||
from erpnext.stock.utils import get_stock_value_from_bin
|
||||
|
||||
|
||||
@frappe.whitelist()
|
||||
@cache_source
|
||||
@ -30,26 +28,24 @@ def get(
|
||||
warehouse_filters.append(["company", "=", filters.get("company")])
|
||||
|
||||
warehouses = frappe.get_list(
|
||||
"Warehouse", fields=["name"], filters=warehouse_filters, order_by="name"
|
||||
"Warehouse", pluck="name", filters=warehouse_filters, order_by="name"
|
||||
)
|
||||
|
||||
for wh in warehouses:
|
||||
balance = get_stock_value_from_bin(warehouse=wh.name)
|
||||
wh["balance"] = balance[0][0]
|
||||
|
||||
warehouses = [x for x in warehouses if not (x.get("balance") == None)]
|
||||
warehouses = frappe.get_list(
|
||||
"Bin",
|
||||
fields=["warehouse", "sum(stock_value) stock_value"],
|
||||
filters={"warehouse": ["IN", warehouses], "stock_value": [">", 0]},
|
||||
group_by="warehouse",
|
||||
order_by="stock_value DESC",
|
||||
limit_page_length=10,
|
||||
)
|
||||
|
||||
if not warehouses:
|
||||
return []
|
||||
|
||||
sorted_warehouse_map = sorted(warehouses, key=lambda i: i["balance"], reverse=True)
|
||||
|
||||
if len(sorted_warehouse_map) > 10:
|
||||
sorted_warehouse_map = sorted_warehouse_map[:10]
|
||||
|
||||
for warehouse in sorted_warehouse_map:
|
||||
labels.append(_(warehouse.get("name")))
|
||||
datapoints.append(warehouse.get("balance"))
|
||||
for warehouse in warehouses:
|
||||
labels.append(_(warehouse.get("warehouse")))
|
||||
datapoints.append(warehouse.get("stock_value"))
|
||||
|
||||
return {
|
||||
"labels": labels,
|
||||
|
Loading…
x
Reference in New Issue
Block a user