From 552c5b5911756bbbd6d7c32d7af191f251ed1fe7 Mon Sep 17 00:00:00 2001 From: "mergify[bot]" <37929162+mergify[bot]@users.noreply.github.com> Date: Wed, 17 Dec 2025 13:46:31 +0530 Subject: [PATCH] fix: incorrect current qty in stock reco (backport #51152) (#51158) * fix: incorrect current qty in stock reco (#51152) (cherry picked from commit dec474ef3a48ea1e0daa4dd6d14257bbf30b81bc) * chore: fix conflicts --------- Co-authored-by: rohitwaghchaure (cherry picked from commit 89d6a8f02ec7b4ec9886b4b44621da3cb7d5bf70) --- erpnext/stock/doctype/batch/batch.py | 4 +- .../stock_reconciliation.py | 37 +++++++------------ 2 files changed, 17 insertions(+), 24 deletions(-) diff --git a/erpnext/stock/doctype/batch/batch.py b/erpnext/stock/doctype/batch/batch.py index 699b7a9562d..20dd94da906 100644 --- a/erpnext/stock/doctype/batch/batch.py +++ b/erpnext/stock/doctype/batch/batch.py @@ -166,7 +166,9 @@ class Batch(Document): for row in batches: batch_qty += row.get("qty") - self.db_set("batch_qty", batch_qty) + if self.batch_qty != batch_qty: + self.db_set("batch_qty", batch_qty) + frappe.msgprint(_("Batch Qty updated to {0}").format(batch_qty), alert=True) def set_batchwise_valuation(self): diff --git a/erpnext/stock/doctype/stock_reconciliation/stock_reconciliation.py b/erpnext/stock/doctype/stock_reconciliation/stock_reconciliation.py index cc4b08b50f4..2bf4333b14e 100644 --- a/erpnext/stock/doctype/stock_reconciliation/stock_reconciliation.py +++ b/erpnext/stock/doctype/stock_reconciliation/stock_reconciliation.py @@ -1227,32 +1227,23 @@ class StockReconciliation(StockController): def get_batch_qty_for_stock_reco( item_code, warehouse, batch_no, posting_date, posting_time, voucher_no, sle_creation ): - ledger = frappe.qb.DocType("Stock Ledger Entry") - posting_datetime = get_combine_datetime(posting_date, posting_time) - - query = ( - frappe.qb.from_(ledger) - .select( - Sum(ledger.actual_qty).as_("batch_qty"), + qty = ( + get_batch_qty( + batch_no, + warehouse, + item_code, + creation=sle_creation, + posting_date=posting_date, + posting_time=posting_time, + ignore_voucher_nos=[voucher_no], + for_stock_levels=True, + consider_negative_batches=True, + do_not_check_future_batches=True, ) - .where( - (ledger.item_code == item_code) - & (ledger.warehouse == warehouse) - & (ledger.docstatus == 1) - & (ledger.is_cancelled == 0) - & (ledger.batch_no == batch_no) - & (ledger.voucher_no != voucher_no) - & ( - (ledger.posting_datetime < posting_datetime) - | ((ledger.posting_datetime == posting_datetime) & (ledger.creation < sle_creation)) - ) - ) - .groupby(ledger.batch_no) + or 0 ) - sle = query.run(as_dict=True) - - return flt(sle[0].batch_qty) if sle else 0 + return flt(qty) @frappe.whitelist()