Skip to content

Commit 1875d69

Browse files
Merge pull request #46888 from frappe/mergify/bp/version-15/pr-46880
fix: slow query (backport #46845) (backport #46880)
2 parents 5dd99f8 + b2d71b4 commit 1875d69

2 files changed

Lines changed: 9 additions & 61 deletions

File tree

erpnext/stock/deprecated_serial_batch.py

Lines changed: 7 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,7 @@ def calculate_avg_rate_for_non_batchwise_valuation(self):
195195

196196
@deprecated
197197
def set_balance_value_for_non_batchwise_valuation_batches(self):
198+
self.last_sle = self.get_last_sle_for_non_batch()
198199
self.set_balance_value_from_sl_entries()
199200
self.set_balance_value_from_bundle()
200201

@@ -242,11 +243,10 @@ def set_balance_value_from_sl_entries(self) -> None:
242243
for d in batch_data:
243244
self.available_qty[d.batch_no] += flt(d.batch_qty)
244245

245-
last_sle = self.get_last_sle_for_non_batch()
246246
for d in batch_data:
247247
if self.available_qty.get(d.batch_no):
248-
self.non_batchwise_balance_value[d.batch_no] += flt(last_sle.stock_value)
249-
self.non_batchwise_balance_qty[d.batch_no] += flt(last_sle.qty_after_transaction)
248+
self.non_batchwise_balance_value[d.batch_no] += flt(self.last_sle.stock_value)
249+
self.non_batchwise_balance_qty[d.batch_no] += flt(self.last_sle.qty_after_transaction)
250250

251251
def get_last_sle_for_non_batch(self):
252252
from erpnext.stock.utils import get_combine_datetime
@@ -285,60 +285,8 @@ def get_last_sle_for_non_batch(self):
285285
query = query.where(sle.name != self.sle.name)
286286

287287
data = query.run(as_dict=True)
288-
return data[0] if data else {}
289288

290-
@deprecated
291-
def get_last_sle_for_sabb_no_batchwise_valuation(self):
292-
sabb = frappe.qb.DocType("Serial and Batch Bundle")
293-
sabb_entry = frappe.qb.DocType("Serial and Batch Entry")
294-
batch = frappe.qb.DocType("Batch")
295-
296-
posting_datetime = CombineDatetime(self.sle.posting_date, self.sle.posting_time)
297-
timestamp_condition = CombineDatetime(sabb.posting_date, sabb.posting_time) < posting_datetime
298-
299-
if self.sle.creation:
300-
timestamp_condition |= (
301-
CombineDatetime(sabb.posting_date, sabb.posting_time) == posting_datetime
302-
) & (sabb.creation < self.sle.creation)
303-
304-
query = (
305-
frappe.qb.from_(sabb)
306-
.inner_join(sabb_entry)
307-
.on(sabb.name == sabb_entry.parent)
308-
.inner_join(batch)
309-
.on(sabb_entry.batch_no == batch.name)
310-
.select(sabb.name)
311-
.where(
312-
(sabb.item_code == self.sle.item_code)
313-
& (sabb.warehouse == self.sle.warehouse)
314-
& (sabb_entry.batch_no.isnotnull())
315-
& (sabb.is_cancelled == 0)
316-
& (sabb.docstatus == 1)
317-
)
318-
.where(timestamp_condition)
319-
.orderby(sabb.posting_date, order=Order.desc)
320-
.orderby(sabb.posting_time, order=Order.desc)
321-
.orderby(sabb.creation, order=Order.desc)
322-
.limit(1)
323-
)
324-
325-
if self.sle.voucher_detail_no:
326-
query = query.where(sabb.voucher_detail_no != self.sle.voucher_detail_no)
327-
328-
query = query.where(sabb.voucher_type != "Pick List")
329-
330-
data = query.run(as_dict=True)
331-
if not data:
332-
return {}
333-
334-
sle = frappe.db.get_value(
335-
"Stock Ledger Entry",
336-
{"serial_and_batch_bundle": data[0].name},
337-
["stock_value", "qty_after_transaction"],
338-
as_dict=1,
339-
)
340-
341-
return sle if sle else {}
289+
return data[0] if data else frappe._dict()
342290

343291
@deprecated
344292
def set_balance_value_from_bundle(self) -> None:
@@ -389,10 +337,9 @@ def set_balance_value_from_bundle(self) -> None:
389337
for d in batch_data:
390338
self.available_qty[d.batch_no] += flt(d.batch_qty)
391339

392-
last_sle = self.get_last_sle_for_sabb_no_batchwise_valuation()
393-
if not last_sle:
340+
if not self.last_sle:
394341
return
395342

396343
for batch_no in self.available_qty:
397-
self.non_batchwise_balance_value[batch_no] = flt(last_sle.stock_value)
398-
self.non_batchwise_balance_qty[batch_no] = flt(last_sle.qty_after_transaction)
344+
self.non_batchwise_balance_value[batch_no] = flt(self.last_sle.stock_value)
345+
self.non_batchwise_balance_qty[batch_no] = flt(self.last_sle.qty_after_transaction)

erpnext/stock/doctype/serial_and_batch_bundle/serial_and_batch_bundle.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2103,7 +2103,8 @@ def get_auto_batch_nos(kwargs):
21032103
filter_zero_near_batches(available_batches, kwargs)
21042104

21052105
if not kwargs.consider_negative_batches:
2106-
available_batches = list(filter(lambda x: x.qty > 0, available_batches))
2106+
precision = frappe.get_precision("Stock Ledger Entry", "actual_qty")
2107+
available_batches = [d for d in available_batches if flt(d.qty, precision) > 0]
21072108

21082109
if not qty:
21092110
return available_batches

0 commit comments

Comments
 (0)