From e5824fc3f1f862ab625eea19f27f61ab74e0708d Mon Sep 17 00:00:00 2001 From: rohitwaghchaure Date: Sun, 11 Feb 2024 11:10:21 +0530 Subject: [PATCH] fix: stock entry for use serial batch fields (#39843) --- erpnext/controllers/stock_controller.py | 8 +++- .../stock/doctype/stock_entry/stock_entry.py | 3 ++ .../doctype/stock_entry/stock_entry_utils.py | 11 +++-- .../doctype/stock_entry/test_stock_entry.py | 46 +++++++++++++++++++ 4 files changed, 61 insertions(+), 7 deletions(-) diff --git a/erpnext/controllers/stock_controller.py b/erpnext/controllers/stock_controller.py index 74c835c745..61eb0f63d3 100644 --- a/erpnext/controllers/stock_controller.py +++ b/erpnext/controllers/stock_controller.py @@ -156,14 +156,18 @@ class StockController(AccountsController): if self.doctype == "Stock Reconciliation": qty = row.qty type_of_transaction = "Inward" + warehouse = row.warehouse else: - qty = row.stock_qty + qty = row.stock_qty if self.doctype != "Stock Entry" else row.transfer_qty type_of_transaction = get_type_of_transaction(self, row) + warehouse = ( + row.warehouse if self.doctype != "Stock Entry" else row.s_warehouse or row.t_warehouse + ) sn_doc = SerialBatchCreation( { "item_code": row.item_code, - "warehouse": row.warehouse, + "warehouse": warehouse, "posting_date": self.posting_date, "posting_time": self.posting_time, "voucher_type": self.doctype, diff --git a/erpnext/stock/doctype/stock_entry/stock_entry.py b/erpnext/stock/doctype/stock_entry/stock_entry.py index 10e3522579..3419155f39 100644 --- a/erpnext/stock/doctype/stock_entry/stock_entry.py +++ b/erpnext/stock/doctype/stock_entry/stock_entry.py @@ -982,6 +982,9 @@ class StockEntry(StockController): already_picked_serial_nos = [] for row in self.items: + if row.use_serial_batch_fields and (row.serial_no or row.batch_no): + continue + if not row.s_warehouse: continue diff --git a/erpnext/stock/doctype/stock_entry/stock_entry_utils.py b/erpnext/stock/doctype/stock_entry/stock_entry_utils.py index 0f67e47ad9..271cbbc007 100644 --- a/erpnext/stock/doctype/stock_entry/stock_entry_utils.py +++ b/erpnext/stock/doctype/stock_entry/stock_entry_utils.py @@ -92,9 +92,6 @@ def make_stock_entry(**args): else: args.qty = cint(args.qty) - if args.serial_no or args.batch_no: - args.use_serial_batch_fields = True - # purpose if not args.purpose: if args.source and args.target: @@ -136,7 +133,7 @@ def make_stock_entry(**args): serial_number = args.serial_no bundle_id = None - if args.serial_no or args.batch_no or args.batches: + if not args.use_serial_batch_fields and (args.serial_no or args.batch_no or args.batches): batches = frappe._dict({}) if args.batch_no: batches = frappe._dict({args.batch_no: args.qty}) @@ -164,7 +161,11 @@ def make_stock_entry(**args): .name ) - args.serial_no = serial_number + args["serial_no"] = "" + args["batch_no"] = "" + + else: + args.serial_no = serial_number s.append( "items", diff --git a/erpnext/stock/doctype/stock_entry/test_stock_entry.py b/erpnext/stock/doctype/stock_entry/test_stock_entry.py index af91536acc..6c3faa6499 100644 --- a/erpnext/stock/doctype/stock_entry/test_stock_entry.py +++ b/erpnext/stock/doctype/stock_entry/test_stock_entry.py @@ -1596,6 +1596,7 @@ class TestStockEntry(FrappeTestCase): qty=4, to_warehouse="_Test Warehouse - _TC", batch_no=batch.name, + use_serial_batch_fields=1, do_not_save=True, ) @@ -1754,6 +1755,51 @@ class TestStockEntry(FrappeTestCase): mr.cancel() mr.delete() + def test_use_serial_and_batch_fields(self): + item = make_item( + "Test Use Serial and Batch Item SN Item", + {"has_serial_no": 1, "is_stock_item": 1}, + ) + + serial_nos = [ + "Test Use Serial and Batch Item SN Item - SN 001", + "Test Use Serial and Batch Item SN Item - SN 002", + ] + + se = make_stock_entry( + item_code=item.name, + qty=2, + to_warehouse="_Test Warehouse - _TC", + use_serial_batch_fields=1, + serial_no="\n".join(serial_nos), + ) + + self.assertTrue(se.items[0].use_serial_batch_fields) + self.assertFalse(se.items[0].serial_no) + self.assertTrue(se.items[0].serial_and_batch_bundle) + + for serial_no in serial_nos: + self.assertTrue(frappe.db.exists("Serial No", serial_no)) + self.assertEqual(frappe.db.get_value("Serial No", serial_no, "status"), "Active") + + se1 = make_stock_entry( + item_code=item.name, + qty=2, + from_warehouse="_Test Warehouse - _TC", + use_serial_batch_fields=1, + serial_no="\n".join(serial_nos), + ) + + se1.reload() + + self.assertTrue(se1.items[0].use_serial_batch_fields) + self.assertFalse(se1.items[0].serial_no) + self.assertTrue(se1.items[0].serial_and_batch_bundle) + + for serial_no in serial_nos: + self.assertTrue(frappe.db.exists("Serial No", serial_no)) + self.assertEqual(frappe.db.get_value("Serial No", serial_no, "status"), "Delivered") + def make_serialized_item(**args): args = frappe._dict(args)