From 3165682d7a6efb73b1123998417ef0356c276bd2 Mon Sep 17 00:00:00 2001 From: Rohit Waghchaure Date: Mon, 8 Apr 2024 19:03:36 +0530 Subject: [PATCH] perf: memory consumption for the Batch-Wise Balance History report --- .../batch_wise_balance_history.py | 43 ++++++++++--------- 1 file changed, 23 insertions(+), 20 deletions(-) diff --git a/erpnext/stock/report/batch_wise_balance_history/batch_wise_balance_history.py b/erpnext/stock/report/batch_wise_balance_history/batch_wise_balance_history.py index 31ea576ec1d..e6490f1717a 100644 --- a/erpnext/stock/report/batch_wise_balance_history/batch_wise_balance_history.py +++ b/erpnext/stock/report/batch_wise_balance_history/batch_wise_balance_history.py @@ -117,34 +117,37 @@ def get_stock_ledger_entries(filters): if filters.get(field): query = query.where(sle[field] == filters.get(field)) - return query.run(as_dict=True) + return query def get_item_warehouse_batch_map(filters, float_precision): - sle = get_stock_ledger_entries(filters) - iwb_map = {} + with frappe.db.unbuffered_cursor(): + sle = get_stock_ledger_entries(filters) + sle = sle.run(as_dict=True, as_iterator=True) - from_date = getdate(filters["from_date"]) - to_date = getdate(filters["to_date"]) + iwb_map = {} - for d in sle: - iwb_map.setdefault(d.item_code, {}).setdefault(d.warehouse, {}).setdefault( - d.batch_no, frappe._dict({"opening_qty": 0.0, "in_qty": 0.0, "out_qty": 0.0, "bal_qty": 0.0}) - ) - qty_dict = iwb_map[d.item_code][d.warehouse][d.batch_no] - if d.posting_date < from_date: - qty_dict.opening_qty = flt(qty_dict.opening_qty, float_precision) + flt( - d.actual_qty, float_precision + from_date = getdate(filters["from_date"]) + to_date = getdate(filters["to_date"]) + + for d in sle: + iwb_map.setdefault(d.item_code, {}).setdefault(d.warehouse, {}).setdefault( + d.batch_no, frappe._dict({"opening_qty": 0.0, "in_qty": 0.0, "out_qty": 0.0, "bal_qty": 0.0}) ) - elif d.posting_date >= from_date and d.posting_date <= to_date: - if flt(d.actual_qty) > 0: - qty_dict.in_qty = flt(qty_dict.in_qty, float_precision) + flt(d.actual_qty, float_precision) - else: - qty_dict.out_qty = flt(qty_dict.out_qty, float_precision) + abs( - flt(d.actual_qty, float_precision) + qty_dict = iwb_map[d.item_code][d.warehouse][d.batch_no] + if d.posting_date < from_date: + qty_dict.opening_qty = flt(qty_dict.opening_qty, float_precision) + flt( + d.actual_qty, float_precision ) + elif d.posting_date >= from_date and d.posting_date <= to_date: + if flt(d.actual_qty) > 0: + qty_dict.in_qty = flt(qty_dict.in_qty, float_precision) + flt(d.actual_qty, float_precision) + else: + qty_dict.out_qty = flt(qty_dict.out_qty, float_precision) + abs( + flt(d.actual_qty, float_precision) + ) - qty_dict.bal_qty = flt(qty_dict.bal_qty, float_precision) + flt(d.actual_qty, float_precision) + qty_dict.bal_qty = flt(qty_dict.bal_qty, float_precision) + flt(d.actual_qty, float_precision) return iwb_map