Skip to content

Commit

Permalink
perf: fetch SLE's on demand and memoize
Browse files Browse the repository at this point in the history
(cherry picked from commit 3e56910)
  • Loading branch information
ruthra-kumar authored and mergify[bot] committed Feb 23, 2023
1 parent b0103b8 commit db1f17e
Showing 1 changed file with 32 additions and 20 deletions.
52 changes: 32 additions & 20 deletions erpnext/accounts/report/gross_profit/gross_profit.py
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,7 @@ def get_column_names():

class GrossProfitGenerator(object):
def __init__(self, filters=None):
self.sle = {}
self.data = []
self.average_buying_rate = {}
self.filters = frappe._dict(filters)
Expand All @@ -404,7 +405,6 @@ def __init__(self, filters=None):
if filters.group_by == "Invoice":
self.group_items_by_invoice()

self.load_stock_ledger_entries()
self.load_product_bundle()
self.load_non_stock_items()
self.get_returned_invoice_items()
Expand Down Expand Up @@ -633,7 +633,7 @@ def get_buying_amount(self, row, item_code):
return flt(row.qty) * item_rate

else:
my_sle = self.sle.get((item_code, row.warehouse))
my_sle = self.get_stock_ledger_entries(item_code, row.warehouse)
if (row.update_stock or row.dn_detail) and my_sle:
parenttype, parent = row.parenttype, row.parent
if row.dn_detail:
Expand All @@ -651,7 +651,7 @@ def get_buying_amount(self, row, item_code):
dn["item_row"],
dn["warehouse"],
)
my_sle = self.sle.get((item_code, warehouse))
my_sle = self.get_stock_ledger_entries(item_code, row.warehouse)
return self.calculate_buying_amount_from_sle(
row, my_sle, parenttype, parent, item_row, item_code
)
Expand Down Expand Up @@ -940,24 +940,36 @@ def get_bundle_item_details(self, item_code):
"Item", item_code, ["item_name", "description", "item_group", "brand"]
)

def load_stock_ledger_entries(self):
res = frappe.db.sql(
"""select item_code, voucher_type, voucher_no,
voucher_detail_no, stock_value, warehouse, actual_qty as qty
from `tabStock Ledger Entry`
where company=%(company)s and is_cancelled = 0
order by
item_code desc, warehouse desc, posting_date desc,
posting_time desc, creation desc""",
self.filters,
as_dict=True,
)
self.sle = {}
for r in res:
if (r.item_code, r.warehouse) not in self.sle:
self.sle[(r.item_code, r.warehouse)] = []
def get_stock_ledger_entries(self, item_code, warehouse):
if item_code and warehouse:
if (item_code, warehouse) not in self.sle:
sle = qb.DocType("Stock Ledger Entry")
res = (
qb.from_(sle)
.select(
sle.item_code,
sle.voucher_type,
sle.voucher_no,
sle.voucher_detail_no,
sle.stock_value,
sle.warehouse,
sle.actual_qty.as_("qty"),
)
.where(
(sle.company == self.filters.company)
& (sle.item_code == item_code)
& (sle.warehouse == warehouse)
& (sle.is_cancelled == 0)
)
.orderby(sle.item_code)
.orderby(sle.warehouse, sle.posting_date, sle.posting_time, sle.creation, order=Order.desc)
.run(as_dict=True)
)

self.sle[(item_code, warehouse)] = res

self.sle[(r.item_code, r.warehouse)].append(r)
return self.sle[(item_code, warehouse)]
return []

def load_product_bundle(self):
self.product_bundles = {}
Expand Down

0 comments on commit db1f17e

Please sign in to comment.