Skip to content

Commit

Permalink
perf: use iterator for stock ageing report (frappe#39346)
Browse files Browse the repository at this point in the history
  • Loading branch information
ankush authored Jan 15, 2024
1 parent 749c735 commit 2e03af7
Showing 1 changed file with 11 additions and 6 deletions.
17 changes: 11 additions & 6 deletions erpnext/stock/report/stock_ageing/stock_ageing.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@


from operator import itemgetter
from typing import Dict, List, Tuple, Union
from typing import Dict, Iterator, List, Tuple, Union

import frappe
from frappe import _
Expand Down Expand Up @@ -231,10 +231,12 @@ def generate(self) -> Dict:
consumed/updated and maintained via FIFO. **
}
"""
if self.sle is None:
self.sle = self.__get_stock_ledger_entries()

for d in self.sle:
stock_ledger_entries = self.sle
if stock_ledger_entries is None:
stock_ledger_entries = self.__get_stock_ledger_entries()

for d in stock_ledger_entries:
key, fifo_queue, transferred_item_key = self.__init_key_stores(d)

if d.voucher_type == "Stock Reconciliation":
Expand All @@ -251,6 +253,9 @@ def generate(self) -> Dict:

self.__update_balances(d, key)

# Note that stock_ledger_entries is an iterator, you can not reuse it like a list
del stock_ledger_entries

if not self.filters.get("show_warehouse_wise_stock"):
# (Item 1, WH 1), (Item 1, WH 2) => (Item 1)
self.item_details = self.__aggregate_details_by_item(self.item_details)
Expand Down Expand Up @@ -381,7 +386,7 @@ def __aggregate_details_by_item(self, wh_wise_data: Dict) -> Dict:

return item_aggregated_data

def __get_stock_ledger_entries(self) -> List[Dict]:
def __get_stock_ledger_entries(self) -> Iterator[Dict]:
sle = frappe.qb.DocType("Stock Ledger Entry")
item = self.__get_item_query() # used as derived table in sle query

Expand Down Expand Up @@ -418,7 +423,7 @@ def __get_stock_ledger_entries(self) -> List[Dict]:

sle_query = sle_query.orderby(sle.posting_date, sle.posting_time, sle.creation, sle.actual_qty)

return sle_query.run(as_dict=True)
return sle_query.run(as_dict=True, as_iterator=True)

def __get_item_query(self) -> str:
item_table = frappe.qb.DocType("Item")
Expand Down

0 comments on commit 2e03af7

Please sign in to comment.