mirror of
https://github.com/frappe/erpnext.git
synced 2026-05-16 19:49:18 +00:00
fix: slow query
This commit is contained in:
@@ -224,6 +224,7 @@ class DeprecatedBatchNoValuation:
|
|||||||
"No known instructions.",
|
"No known instructions.",
|
||||||
)
|
)
|
||||||
def set_balance_value_for_non_batchwise_valuation_batches(self):
|
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_sl_entries()
|
||||||
self.set_balance_value_from_bundle()
|
self.set_balance_value_from_bundle()
|
||||||
|
|
||||||
@@ -276,11 +277,10 @@ class DeprecatedBatchNoValuation:
|
|||||||
for d in batch_data:
|
for d in batch_data:
|
||||||
self.available_qty[d.batch_no] += flt(d.batch_qty)
|
self.available_qty[d.batch_no] += flt(d.batch_qty)
|
||||||
|
|
||||||
last_sle = self.get_last_sle_for_non_batch()
|
|
||||||
for d in batch_data:
|
for d in batch_data:
|
||||||
if self.available_qty.get(d.batch_no):
|
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_value[d.batch_no] += flt(self.last_sle.stock_value)
|
||||||
self.non_batchwise_balance_qty[d.batch_no] += flt(last_sle.qty_after_transaction)
|
self.non_batchwise_balance_qty[d.batch_no] += flt(self.last_sle.qty_after_transaction)
|
||||||
|
|
||||||
def get_last_sle_for_non_batch(self):
|
def get_last_sle_for_non_batch(self):
|
||||||
from erpnext.stock.utils import get_combine_datetime
|
from erpnext.stock.utils import get_combine_datetime
|
||||||
@@ -319,59 +319,8 @@ class DeprecatedBatchNoValuation:
|
|||||||
query = query.where(sle.name != self.sle.name)
|
query = query.where(sle.name != self.sle.name)
|
||||||
|
|
||||||
data = query.run(as_dict=True)
|
data = query.run(as_dict=True)
|
||||||
return data[0] if data else {}
|
|
||||||
|
|
||||||
def get_last_sle_for_sabb_no_batchwise_valuation(self):
|
return data[0] if data else frappe._dict()
|
||||||
sabb = frappe.qb.DocType("Serial and Batch Bundle")
|
|
||||||
sabb_entry = frappe.qb.DocType("Serial and Batch Entry")
|
|
||||||
batch = frappe.qb.DocType("Batch")
|
|
||||||
|
|
||||||
posting_datetime = CombineDatetime(self.sle.posting_date, self.sle.posting_time)
|
|
||||||
timestamp_condition = CombineDatetime(sabb.posting_date, sabb.posting_time) < posting_datetime
|
|
||||||
|
|
||||||
if self.sle.creation:
|
|
||||||
timestamp_condition |= (
|
|
||||||
CombineDatetime(sabb.posting_date, sabb.posting_time) == posting_datetime
|
|
||||||
) & (sabb.creation < self.sle.creation)
|
|
||||||
|
|
||||||
query = (
|
|
||||||
frappe.qb.from_(sabb)
|
|
||||||
.inner_join(sabb_entry)
|
|
||||||
.on(sabb.name == sabb_entry.parent)
|
|
||||||
.inner_join(batch)
|
|
||||||
.on(sabb_entry.batch_no == batch.name)
|
|
||||||
.select(sabb.name)
|
|
||||||
.where(
|
|
||||||
(sabb.item_code == self.sle.item_code)
|
|
||||||
& (sabb.warehouse == self.sle.warehouse)
|
|
||||||
& (sabb_entry.batch_no.isnotnull())
|
|
||||||
& (sabb.is_cancelled == 0)
|
|
||||||
& (sabb.docstatus == 1)
|
|
||||||
)
|
|
||||||
.where(timestamp_condition)
|
|
||||||
.orderby(sabb.posting_date, order=Order.desc)
|
|
||||||
.orderby(sabb.posting_time, order=Order.desc)
|
|
||||||
.orderby(sabb.creation, order=Order.desc)
|
|
||||||
.limit(1)
|
|
||||||
)
|
|
||||||
|
|
||||||
if self.sle.voucher_detail_no:
|
|
||||||
query = query.where(sabb.voucher_detail_no != self.sle.voucher_detail_no)
|
|
||||||
|
|
||||||
query = query.where(sabb.voucher_type != "Pick List")
|
|
||||||
|
|
||||||
data = query.run(as_dict=True)
|
|
||||||
if not data:
|
|
||||||
return {}
|
|
||||||
|
|
||||||
sle = frappe.db.get_value(
|
|
||||||
"Stock Ledger Entry",
|
|
||||||
{"serial_and_batch_bundle": data[0].name},
|
|
||||||
["stock_value", "qty_after_transaction"],
|
|
||||||
as_dict=1,
|
|
||||||
)
|
|
||||||
|
|
||||||
return sle if sle else {}
|
|
||||||
|
|
||||||
@deprecated(
|
@deprecated(
|
||||||
"erpnext.stock.serial_batch_bundle.BatchNoValuation.set_balance_value_from_bundle",
|
"erpnext.stock.serial_batch_bundle.BatchNoValuation.set_balance_value_from_bundle",
|
||||||
@@ -427,10 +376,9 @@ class DeprecatedBatchNoValuation:
|
|||||||
for d in batch_data:
|
for d in batch_data:
|
||||||
self.available_qty[d.batch_no] += flt(d.batch_qty)
|
self.available_qty[d.batch_no] += flt(d.batch_qty)
|
||||||
|
|
||||||
last_sle = self.get_last_sle_for_sabb_no_batchwise_valuation()
|
if not self.last_sle:
|
||||||
if not last_sle:
|
|
||||||
return
|
return
|
||||||
|
|
||||||
for batch_no in self.available_qty:
|
for batch_no in self.available_qty:
|
||||||
self.non_batchwise_balance_value[batch_no] = flt(last_sle.stock_value)
|
self.non_batchwise_balance_value[batch_no] = flt(self.last_sle.stock_value)
|
||||||
self.non_batchwise_balance_qty[batch_no] = flt(last_sle.qty_after_transaction)
|
self.non_batchwise_balance_qty[batch_no] = flt(self.last_sle.qty_after_transaction)
|
||||||
|
|||||||
@@ -2121,7 +2121,8 @@ def get_auto_batch_nos(kwargs):
|
|||||||
filter_zero_near_batches(available_batches, kwargs)
|
filter_zero_near_batches(available_batches, kwargs)
|
||||||
|
|
||||||
if not kwargs.consider_negative_batches:
|
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:
|
if not qty:
|
||||||
return available_batches
|
return available_batches
|
||||||
|
|||||||
Reference in New Issue
Block a user