fix: validation to prevent submission if the SABB is not linked to a stock transaction

This commit is contained in:
Rohit Waghchaure
2025-01-31 10:26:35 +05:30
parent 968e235a3f
commit f976115a2b
4 changed files with 122 additions and 5 deletions

View File

@@ -330,7 +330,7 @@ class SellingController(StockController):
"batch_no": p.batch_no if self.docstatus == 2 else None,
"uom": p.uom,
"serial_and_batch_bundle": p.serial_and_batch_bundle
or get_serial_and_batch_bundle(p, self),
or get_serial_and_batch_bundle(p, self, d),
"name": d.name,
"target_warehouse": p.target_warehouse,
"company": self.company,
@@ -796,7 +796,7 @@ def set_default_income_account_for_item(obj):
set_item_default(d.item_code, obj.company, "income_account", d.income_account)
def get_serial_and_batch_bundle(child, parent):
def get_serial_and_batch_bundle(child, parent, delivery_note_child=None):
from erpnext.stock.serial_batch_bundle import SerialBatchCreation
if child.get("use_serial_batch_fields"):
@@ -816,7 +816,7 @@ def get_serial_and_batch_bundle(child, parent):
"warehouse": child.warehouse,
"voucher_type": parent.doctype,
"voucher_no": parent.name if parent.docstatus < 2 else None,
"voucher_detail_no": child.name,
"voucher_detail_no": delivery_note_child.name if delivery_note_child else child.name,
"posting_date": parent.posting_date,
"posting_time": parent.posting_time,
"qty": child.qty,

View File

@@ -216,6 +216,10 @@ class StockController(AccountsController):
if self.doctype == "Asset Capitalization":
table_name = "stock_items"
parent_details = frappe._dict()
if table_name == "packed_items":
parent_details = self.get_parent_details_for_packed_items()
for row in self.get(table_name):
if row.serial_and_batch_bundle and (row.serial_no or row.batch_no):
self.validate_serial_nos_and_batches_with_bundle(row)
@@ -246,13 +250,20 @@ class StockController(AccountsController):
}
if row.get("qty") or row.get("consumed_qty") or row.get("stock_qty"):
self.update_bundle_details(bundle_details, table_name, row)
self.update_bundle_details(bundle_details, table_name, row, parent_details=parent_details)
self.create_serial_batch_bundle(bundle_details, row)
if row.get("rejected_qty"):
self.update_bundle_details(bundle_details, table_name, row, is_rejected=True)
self.create_serial_batch_bundle(bundle_details, row)
def get_parent_details_for_packed_items(self):
parent_details = frappe._dict()
for row in self.get("items"):
parent_details[row.name] = row
return parent_details
def make_bundle_for_sales_purchase_return(self, table_name=None):
if not self.get("is_return"):
return
@@ -387,7 +398,7 @@ class StockController(AccountsController):
return False
def update_bundle_details(self, bundle_details, table_name, row, is_rejected=False):
def update_bundle_details(self, bundle_details, table_name, row, is_rejected=False, parent_details=None):
from erpnext.stock.doctype.serial_no.serial_no import get_serial_nos
# Since qty field is different for different doctypes
@@ -429,6 +440,11 @@ class StockController(AccountsController):
warehouse = row.get("target_warehouse") or row.get("warehouse")
type_of_transaction = "Outward"
if table_name == "packed_items":
if not warehouse:
warehouse = parent_details[row.parent_detail_docname].warehouse
bundle_details["voucher_detail_no"] = parent_details[row.parent_detail_docname].name
bundle_details.update(
{
"qty": qty,

View File

@@ -84,6 +84,9 @@ class SerialandBatchBundle(Document):
# end: auto-generated types
def validate(self):
if self.docstatus == 1 and self.voucher_detail_no:
self.validate_voucher_detail_no()
self.reset_serial_batch_bundle()
self.set_batch_no()
self.validate_serial_and_batch_no()
@@ -107,6 +110,30 @@ class SerialandBatchBundle(Document):
self.calculate_qty_and_amount()
def validate_voucher_detail_no(self):
if self.type_of_transaction not in ["Inward", "Outward"] or self.voucher_type in [
"Installation Note",
"Job Card",
"Maintenance Schedule",
"Pick List",
]:
return
if self.voucher_type == "POS Invoice":
if not frappe.db.exists("POS Invoice Item", self.voucher_detail_no):
frappe.throw(
_("The serial and batch bundle {0} not linked to {1} {2}").format(
bold(self.name), self.voucher_type, bold(self.voucher_no)
)
)
elif not frappe.db.exists("Stock Ledger Entry", {"voucher_detail_no": self.voucher_detail_no}):
frappe.throw(
_("The serial and batch bundle {0} not linked to {1} {2}").format(
bold(self.name), self.voucher_type, bold(self.voucher_no)
)
)
def allow_existing_serial_nos(self):
if self.type_of_transaction == "Outward" or not self.has_serial_no:
return

View File

@@ -766,6 +766,80 @@ class TestSerialandBatchBundle(IntegrationTestCase):
"Stock Settings", "auto_create_serial_and_batch_bundle_for_outward", original_value
)
def test_voucher_detail_no(self):
item_code = make_item(
"Test Voucher Detail No 1",
properties={
"is_stock_item": 1,
"has_batch_no": 1,
"create_new_batch": 1,
"batch_number_series": "TST-VDN-.#####",
},
).name
se = make_stock_entry(
item_code=item_code,
qty=10,
target="_Test Warehouse - _TC",
rate=500,
use_serial_batch_fields=True,
do_not_submit=True,
)
if not frappe.db.exists("Batch", "TST-ACSBBO-TACSB-00001"):
frappe.get_doc(
{
"doctype": "Batch",
"batch_id": "TST-ACSBBO-TACSB-00001",
"item": item_code,
"company": "_Test Company",
}
).insert(ignore_permissions=True)
bundle_doc = make_serial_batch_bundle(
{
"item_code": item_code,
"warehouse": "_Test Warehouse - _TC",
"voucher_type": "Stock Entry",
"posting_date": today(),
"posting_time": nowtime(),
"qty": 10,
"batches": frappe._dict({"TST-ACSBBO-TACSB-00001": 10}),
"type_of_transaction": "Inward",
"do_not_submit": True,
}
)
se.append(
"items",
{
"item_code": item_code,
"t_warehouse": "_Test Warehouse - _TC",
"stock_uom": "Nos",
"stock_qty": 10,
"conversion_factor": 1,
"uom": "Nos",
"basic_rate": 500,
"qty": 10,
"use_serial_batch_fields": 0,
"serial_and_batch_bundle": bundle_doc.name,
},
)
se.save()
bundle_doc = frappe.get_doc("Serial and Batch Bundle", bundle_doc.name)
self.assertEqual(bundle_doc.voucher_detail_no, se.items[1].name)
se.remove(se.items[1])
se.save()
self.assertTrue(len(se.items) == 1)
se.submit()
bundle_doc.reload()
self.assertTrue(bundle_doc.docstatus == 0)
self.assertRaises(frappe.ValidationError, bundle_doc.submit)
def get_batch_from_bundle(bundle):
from erpnext.stock.serial_batch_bundle import get_batch_nos