fix: slow query

(cherry picked from commit d44f574581)
This commit is contained in:
Rohit Waghchaure
2026-05-22 11:31:00 +05:30
committed by Mergify
parent 123b4ad563
commit 66c9170465

View File

@@ -40,7 +40,7 @@ import erpnext
from erpnext.accounts.doctype.account.account import get_account_currency from erpnext.accounts.doctype.account.account import get_account_currency
from erpnext.accounts.doctype.accounting_dimension.accounting_dimension import get_dimensions from erpnext.accounts.doctype.accounting_dimension.accounting_dimension import get_dimensions
from erpnext.stock import get_warehouse_account_map from erpnext.stock import get_warehouse_account_map
from erpnext.stock.utils import get_stock_value_on from erpnext.stock.utils import get_combine_datetime, get_stock_value_on
if TYPE_CHECKING: if TYPE_CHECKING:
from erpnext.stock.doctype.repost_item_valuation.repost_item_valuation import RepostItemValuation from erpnext.stock.doctype.repost_item_valuation.repost_item_valuation import RepostItemValuation
@@ -1752,31 +1752,31 @@ def sort_stock_vouchers_by_posting_date(
def get_future_stock_vouchers(posting_date, posting_time, for_warehouses=None, for_items=None, company=None): def get_future_stock_vouchers(posting_date, posting_time, for_warehouses=None, for_items=None, company=None):
values = [] posting_datetime = get_combine_datetime(posting_date, posting_time)
condition = ""
SLE = DocType("Stock Ledger Entry")
query = (
frappe.qb.from_(SLE)
.select(SLE.voucher_type, SLE.voucher_no)
.distinct()
.where(SLE.posting_datetime >= posting_datetime)
.where(SLE.is_cancelled == 0)
.orderby(SLE.posting_datetime)
.orderby(SLE.creation)
.for_update()
)
if for_items: if for_items:
condition += " and item_code in ({})".format(", ".join(["%s"] * len(for_items))) query = query.where(SLE.item_code.isin(for_items))
values += for_items
if for_warehouses: if for_warehouses:
condition += " and warehouse in ({})".format(", ".join(["%s"] * len(for_warehouses))) query = query.where(SLE.warehouse.isin(for_warehouses))
values += for_warehouses
if company: if company:
condition += " and company = %s" query = query.where(SLE.company == company)
values.append(company)
future_stock_vouchers = frappe.db.sql( future_stock_vouchers = query.run(as_dict=True)
f"""select distinct sle.voucher_type, sle.voucher_no
from `tabStock Ledger Entry` sle
where
timestamp(sle.posting_date, sle.posting_time) >= timestamp(%s, %s)
and is_cancelled = 0
{condition}
order by timestamp(sle.posting_date, sle.posting_time) asc, creation asc for update""",
tuple([posting_date, posting_time, *values]),
as_dict=True,
)
return [(d.voucher_type, d.voucher_no) for d in future_stock_vouchers] return [(d.voucher_type, d.voucher_no) for d in future_stock_vouchers]