From 89e2462dddc16c582d45a19e9ff8d5d9e6333052 Mon Sep 17 00:00:00 2001 From: Mihir Kandoi Date: Tue, 8 Jul 2025 17:52:19 +0530 Subject: [PATCH 1/2] fix: sort available batches based on expiry --- .../serial_and_batch_bundle/serial_and_batch_bundle.py | 5 +++++ 1 file changed, 5 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 650e1f69888..d9364a54a50 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 @@ -2231,6 +2231,9 @@ def get_auto_batch_nos(kwargs): picked_batches, ) + if kwargs.based_on == "Expiry": + available_batches = sorted(available_batches, key=lambda x: (x.expiry_date or "9999-12-31")) + if not kwargs.get("do_not_check_future_batches") and available_batches and kwargs.get("posting_date"): filter_zero_near_batches(available_batches, kwargs) @@ -2330,6 +2333,7 @@ def get_available_batches(kwargs): batch_ledger.batch_no, batch_ledger.warehouse, Sum(batch_ledger.qty).as_("qty"), + batch_table.expiry_date, ) .where(batch_table.disabled == 0) .where(stock_ledger_entry.is_cancelled == 0) @@ -2620,6 +2624,7 @@ def get_stock_ledgers_batches(kwargs): stock_ledger_entry.item_code, Sum(stock_ledger_entry.actual_qty).as_("qty"), stock_ledger_entry.batch_no, + batch_table.expiry_date, ) .where((stock_ledger_entry.is_cancelled == 0) & (stock_ledger_entry.batch_no.isnotnull())) .groupby(stock_ledger_entry.batch_no, stock_ledger_entry.warehouse) -- GitLab From c4349644ddabc798d7ed0c05d79ad2e872587327 Mon Sep 17 00:00:00 2001 From: Mihir Kandoi Date: Tue, 8 Jul 2025 19:31:03 +0530 Subject: [PATCH 2/2] fix: test case --- erpnext/stock/doctype/batch/test_batch.py | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/erpnext/stock/doctype/batch/test_batch.py b/erpnext/stock/doctype/batch/test_batch.py index 3655fc6057c..10a924b48af 100644 --- a/erpnext/stock/doctype/batch/test_batch.py +++ b/erpnext/stock/doctype/batch/test_batch.py @@ -305,8 +305,18 @@ class TestBatch(IntegrationTestCase): self.assertEqual( get_batch_qty(item_code="ITEM-BATCH-2", warehouse="_Test Warehouse - _TC"), [ - {"batch_no": "batch a", "qty": 90.0, "warehouse": "_Test Warehouse - _TC"}, - {"batch_no": "batch b", "qty": 90.0, "warehouse": "_Test Warehouse - _TC"}, + { + "batch_no": "batch a", + "qty": 90.0, + "warehouse": "_Test Warehouse - _TC", + "expiry_date": None, + }, + { + "batch_no": "batch b", + "qty": 90.0, + "warehouse": "_Test Warehouse - _TC", + "expiry_date": None, + }, ], ) -- GitLab