diff --git a/erpnext/stock/doctype/item/item.py b/erpnext/stock/doctype/item/item.py index 5c92fecbfdf..feedb6a0f8a 100644 --- a/erpnext/stock/doctype/item/item.py +++ b/erpnext/stock/doctype/item/item.py @@ -183,7 +183,23 @@ class Item(Document): self.add_price(default.default_price_list) if self.opening_stock: - self.set_opening_stock() + if self.opening_stock > 10000 and self.has_serial_no: + frappe.enqueue( + self.set_opening_stock, + queue="long", + timeout=600, + job_name=f"set_opening_stock_for_{self.name}", + ) + frappe.msgprint( + _( + "Opening stock creation has been queued and will be created in the background. Please check the stock entry after some time." + ), + indicator="orange", + alert=True, + ) + + else: + self.set_opening_stock() def validate(self): if not self.item_name: @@ -264,7 +280,11 @@ class Item(Document): def set_opening_stock(self): """set opening stock""" - if not self.is_stock_item or self.has_serial_no or self.has_batch_no: + if ( + not self.is_stock_item + or (self.has_serial_no and not self.serial_no_series) + or (self.has_batch_no and (not self.create_new_batch or not self.batch_number_series)) + ): return if not self.valuation_rate and not self.standard_rate and not self.is_customer_provided_item: diff --git a/erpnext/stock/doctype/item/test_item.py b/erpnext/stock/doctype/item/test_item.py index 7814efb1c0b..b259f6592c1 100644 --- a/erpnext/stock/doctype/item/test_item.py +++ b/erpnext/stock/doctype/item/test_item.py @@ -957,6 +957,43 @@ class TestItem(IntegrationTestCase): msg="Different Variant UOM should not be allowed when `allow_different_uom` is disabled.", ) + def test_opening_stock_for_serial_batch(self): + items = { + "Test Opening Stock for Serial No": { + "has_serial_no": 1, + "opening_stock": 5, + "serial_no_series": "SN-TOPN-.####", + "valuation_rate": 100, + }, + "Test Opening Stock for Batch No": { + "has_batch_no": 1, + "opening_stock": 5, + "batch_number_series": "BCH-TOPN-.####", + "valuation_rate": 100, + "create_new_batch": 1, + }, + "Test Opening Stock for Serial and Batch No": { + "has_serial_no": 1, + "has_batch_no": 1, + "opening_stock": 5, + "batch_number_series": "SN-BCH-TOPN-.####", + "serial_no_series": "BCH-SN-TOPN-.####", + "valuation_rate": 100, + "create_new_batch": 1, + }, + } + + for item_code, properties in items.items(): + make_item(item_code, properties) + + serial_and_batch_bundle = frappe.db.get_value( + "Stock Entry Detail", {"docstatus": 1, "item_code": item_code}, "serial_and_batch_bundle" + ) + self.assertTrue(serial_and_batch_bundle) + + sabb_qty = frappe.db.get_value("Serial and Batch Bundle", serial_and_batch_bundle, "total_qty") + self.assertEqual(sabb_qty, properties["opening_stock"]) + def set_item_variant_settings(fields): doc = frappe.get_doc("Item Variant Settings")