fix(stock): create opening stock via Stock Reconciliation with serial/batch bundle support

This commit is contained in:
khushi8112
2026-05-19 14:20:07 +05:30
parent 70086f92f5
commit 03be975f26
2 changed files with 29 additions and 25 deletions

View File

@@ -31,6 +31,7 @@ from erpnext.controllers.item_variant import (
validate_item_variant_attributes,
)
from erpnext.stock.doctype.item_default.item_default import ItemDefault
from erpnext.stock.serial_batch_bundle import SerialBatchCreation
from erpnext.stock.utils import get_valuation_method
@@ -302,7 +303,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 self.valuation_rate is None and not self.is_customer_provided_item:
@@ -332,35 +337,23 @@ class Item(Document):
if not opening_account:
frappe.throw(
_(
"Please set a Temporary Opening account for company {0} to create an Opening Stock entry."
"Please set a Temporary Opening account for company {0} to create an Opening Stock reconciliation."
).format(frappe.bold(default.company))
)
stock_reco = frappe.get_doc(
{
"doctype": "Stock Reconciliation",
"purpose": "Opening Stock",
"company": default.company,
"expense_account": opening_account,
"items": [
{
"item_code": self.name,
"warehouse": default_warehouse,
"qty": self.opening_stock,
"valuation_rate": self.valuation_rate,
"allow_zero_valuation_rate": 1 if flt(self.valuation_rate) == 0 else 0,
}
],
}
stock_reco = create_opening_stock_reconciliation(
item_code=self.name,
company=default.company,
qty=self.opening_stock,
valuation_rate=self.valuation_rate,
warehouse=default_warehouse,
expense_account=opening_account,
)
stock_reco.insert()
stock_reco.submit()
stock_reco.add_comment("Comment", _("Opening Stock"))
stock_reco_link = frappe.utils.get_link_to_form("Stock Reconciliation", stock_reco.name)
if self.valuation_rate == 0:
frappe.msgprint(
_("Opening Stock entry created with zero valuation rate: {0}").format(
_("Opening Stock reconciliation created with zero valuation rate: {0}").format(
stock_reco_link
),
indicator="orange",
@@ -368,7 +361,7 @@ class Item(Document):
)
else:
frappe.msgprint(
_("Opening Stock entry created: {0}").format(stock_reco_link),
_("Opening Stock reconciliation created: {0}").format(stock_reco_link),
indicator="green",
alert=True,
)

View File

@@ -997,13 +997,24 @@ class TestItem(ERPNextTestSuite):
for item_code, properties in items.items():
make_item(item_code, properties)
serial_and_batch_bundle = frappe.db.get_value(
stock_entry_bundle = frappe.db.get_value(
"Stock Entry Detail", {"docstatus": 1, "item_code": item_code}, "serial_and_batch_bundle"
)
self.assertFalse(stock_entry_bundle)
serial_and_batch_bundle = frappe.db.get_value(
"Stock Ledger Entry",
{
"voucher_type": "Stock Reconciliation",
"is_cancelled": 0,
"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"])
self.assertEqual(abs(sabb_qty), properties["opening_stock"])
def set_item_variant_settings(fields):