From 23dc9d58720330d5de17b737883d6d3d5ae064dd Mon Sep 17 00:00:00 2001 From: Rohit Waghchaure Date: Sun, 6 Apr 2025 09:52:41 +0530 Subject: [PATCH] fix: slow query (cherry picked from commit f82c8ea5eb3551a92ce0f2d54c57404a3cb5139d) # Conflicts: # erpnext/stock/deprecated_serial_batch.py --- erpnext/stock/deprecated_serial_batch.py | 18 ++++++++++-------- .../serial_and_batch_bundle.py | 3 ++- 2 files changed, 12 insertions(+), 9 deletions(-) diff --git a/erpnext/stock/deprecated_serial_batch.py b/erpnext/stock/deprecated_serial_batch.py index f084c324745..d6ce85ef7cc 100644 --- a/erpnext/stock/deprecated_serial_batch.py +++ b/erpnext/stock/deprecated_serial_batch.py @@ -195,6 +195,7 @@ class DeprecatedBatchNoValuation: @deprecated def set_balance_value_for_non_batchwise_valuation_batches(self): + self.last_sle = self.get_last_sle_for_non_batch() self.set_balance_value_from_sl_entries() self.set_balance_value_from_bundle() @@ -242,11 +243,10 @@ class DeprecatedBatchNoValuation: for d in batch_data: self.available_qty[d.batch_no] += flt(d.batch_qty) - last_sle = self.get_last_sle_for_non_batch() for d in batch_data: if self.available_qty.get(d.batch_no): - self.non_batchwise_balance_value[d.batch_no] += flt(last_sle.stock_value) - self.non_batchwise_balance_qty[d.batch_no] += flt(last_sle.qty_after_transaction) + self.non_batchwise_balance_value[d.batch_no] += flt(self.last_sle.stock_value) + self.non_batchwise_balance_qty[d.batch_no] += flt(self.last_sle.qty_after_transaction) def get_last_sle_for_non_batch(self): from erpnext.stock.utils import get_combine_datetime @@ -285,8 +285,8 @@ class DeprecatedBatchNoValuation: query = query.where(sle.name != self.sle.name) data = query.run(as_dict=True) - return data[0] if data else {} +<<<<<<< HEAD @deprecated def get_last_sle_for_sabb_no_batchwise_valuation(self): sabb = frappe.qb.DocType("Serial and Batch Bundle") @@ -339,6 +339,9 @@ class DeprecatedBatchNoValuation: ) return sle if sle else {} +======= + return data[0] if data else frappe._dict() +>>>>>>> f82c8ea5eb (fix: slow query) @deprecated def set_balance_value_from_bundle(self) -> None: @@ -389,10 +392,9 @@ class DeprecatedBatchNoValuation: for d in batch_data: self.available_qty[d.batch_no] += flt(d.batch_qty) - last_sle = self.get_last_sle_for_sabb_no_batchwise_valuation() - if not last_sle: + if not self.last_sle: return for batch_no in self.available_qty: - self.non_batchwise_balance_value[batch_no] = flt(last_sle.stock_value) - self.non_batchwise_balance_qty[batch_no] = flt(last_sle.qty_after_transaction) + self.non_batchwise_balance_value[batch_no] = flt(self.last_sle.stock_value) + self.non_batchwise_balance_qty[batch_no] = flt(self.last_sle.qty_after_transaction) diff --git a/erpnext/stock/doctype/serial_and_batch_bundle/serial_and_batch_bundle.py b/erpnext/stock/doctype/serial_and_batch_bundle/serial_and_batch_bundle.py index 8502c0cc3cc..8abe5b7e082 100644 --- a/erpnext/stock/doctype/serial_and_batch_bundle/serial_and_batch_bundle.py +++ b/erpnext/stock/doctype/serial_and_batch_bundle/serial_and_batch_bundle.py @@ -2103,7 +2103,8 @@ def get_auto_batch_nos(kwargs): filter_zero_near_batches(available_batches, kwargs) if not kwargs.consider_negative_batches: - available_batches = list(filter(lambda x: x.qty > 0, available_batches)) + precision = frappe.get_precision("Stock Ledger Entry", "actual_qty") + available_batches = [d for d in available_batches if flt(d.qty, precision) > 0] if not qty: return available_batches