From 295dec24dbec82cb4232357a5ae8d86cd56b9c06 Mon Sep 17 00:00:00 2001 From: Mihir Kandoi Date: Tue, 23 Jun 2026 08:58:29 +0530 Subject: [PATCH] fix(stock): group get_picked_batches by batch and warehouse get_picked_batches summed Serial-and-Batch-Entry qty while selecting bare batch_no and warehouse with no GROUP BY. PostgreSQL rejects this outright: column "tabSerial and Batch Entry.batch_no" must appear in the GROUP BY clause MariaDB does not error but collapses every picked row into a single result - the grand-total qty pinned to one arbitrary batch - so the caller, which keys the result by (batch_no, warehouse) to subtract already-picked stock, under-counts whenever more than one batch is picked. Add GROUP BY batch_no, warehouse so the query returns one correct row per batch on both engines (this corrects the MariaDB result, not just Postgres validity). --- .../serial_and_batch_bundle/serial_and_batch_bundle.py | 1 + .../test_serial_and_batch_bundle.py | 9 +++++++++ 2 files changed, 10 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 951a696019e..1d9ed5a2af7 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 @@ -3198,6 +3198,7 @@ def get_picked_batches(kwargs) -> dict[str, dict]: & (table.voucher_type == "Pick List") & (table.voucher_no.isnotnull()) ) + .groupby(child_table.batch_no, child_table.warehouse) ) if kwargs.get("company"): diff --git a/erpnext/stock/doctype/serial_and_batch_bundle/test_serial_and_batch_bundle.py b/erpnext/stock/doctype/serial_and_batch_bundle/test_serial_and_batch_bundle.py index 93f071a0b1c..2c6aac7d4ce 100644 --- a/erpnext/stock/doctype/serial_and_batch_bundle/test_serial_and_batch_bundle.py +++ b/erpnext/stock/doctype/serial_and_batch_bundle/test_serial_and_batch_bundle.py @@ -1355,6 +1355,15 @@ class TestSerialandBatchBundle(ERPNextTestSuite): # Stock queue should have the returned stock: [[5, 100]] self.assertEqual(json.loads(return_sle.stock_queue), [[5, 100]]) + def test_get_picked_batches_runs(self): + from erpnext.stock.doctype.serial_and_batch_bundle.serial_and_batch_bundle import get_picked_batches + + # Sum(qty) is selected with bare batch_no/warehouse; without a GROUP BY this + # raises a GroupingError on Postgres (and collapses to one arbitrary row on + # MariaDB). It must run and return a per-(batch, warehouse) mapping on both. + result = get_picked_batches(frappe._dict()) + self.assertIsInstance(result, dict) + def get_batch_from_bundle(bundle): from erpnext.stock.serial_batch_bundle import get_batch_nos