From ac97489a32deb33442645bd32a92f821eaa9fb75 Mon Sep 17 00:00:00 2001 From: Rohit Waghchaure Date: Tue, 11 Mar 2025 10:13:34 +0530 Subject: [PATCH] fix: filter batches that going to be zero (cherry picked from commit aba512c1c669c3b108b1d60c521499a2f11575e2) --- .../serial_and_batch_bundle.py | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) 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 c37051e1e2b..d30cf84d93e 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 @@ -2094,6 +2094,9 @@ def get_auto_batch_nos(kwargs): picked_batches, ) + if available_batches and kwargs.get("posting_date"): + filter_zero_near_batches(available_batches, kwargs) + if not kwargs.consider_negative_batches: available_batches = list(filter(lambda x: x.qty > 0, available_batches)) @@ -2103,6 +2106,26 @@ def get_auto_batch_nos(kwargs): return get_qty_based_available_batches(available_batches, qty) +def filter_zero_near_batches(available_batches, kwargs): + kwargs.batch_no = [d.batch_no for d in available_batches] + + del kwargs["posting_date"] + del kwargs["posting_time"] + + available_batches_in_future = get_available_batches(kwargs) + for batch in available_batches: + if batch.qty <= 0: + continue + + for future_batch in available_batches_in_future: + if ( + batch.batch_no == future_batch.batch_no + and batch.warehouse == future_batch.warehouse + and future_batch.qty <= 0 + ): + batch.qty = 0 + + def get_qty_based_available_batches(available_batches, qty): batches = [] for batch in available_batches: