Issue with Stock Entry Creation in Relation to Serial and Batch Bundle

Hello guys. I am having trouble with creating a Stock Entry of type Material Issuance which involves a previously created Batch. The goal is that on submission of a custom doctype which I’ve named tea_delivery_note, a related stock entry is created which allows me to remove items from the stock based on a previously created batch.The issue stems from the serial and batch bundle that is created as I cannot pass the batch no as a parameter and when I attempt to create the serial and batch bundle and then pass it as a stock entry item, I am still left stuck.

On my first attempt, the method for issuing a stock entry was as below:

@frappe.whitelist()
def issue_stock_entry(**args):

stock_entry = frappe.new_doc("Stock Entry")

items = args.get('items')

itemsTable = json.loads(items) 

for index, item in enumerate(itemsTable):   
	batchDoc = frappe.get_doc('Batch',item['pallet'])
	if item['delivery_qty'] < item['package_qty']:
		batchDoc.status = 'Partially-Delivered'
	else:
		batchDoc.status = 'Delivered'
	batchDoc.save(ignore_permissions=True)

stock_entry.stock_entry_type = 'Material Issue'

itemsTable = json.loads(items)

for item in itemsTable:  
	stock_entry.append('items', {	
	's_warehouse': item['warehouse'],
	'batch_no': item['pallet'],
	'item':'TEA',
	'item_code': 'TEA',
	'qty': item['delivery_qty'], 
	'conversion_factor': int(1)
	})

stock_entry.save(ignore_permissions=True)
stock_entry.submit()

task = frappe.get_last_doc('Stock Entry')

return task.name

and got the following error:

frappe.exceptions.ValidationError: At row 1: Serial and Batch Bundle SABB-00000016 has already created. Please remove the values from the serial no or batch no fields.

On my second attempt, I changed the method to as below:

def issue_stock_entry(**args):

stock_entry = frappe.new_doc("Stock Entry")

items = args.get('items')

itemsTable = json.loads(items) 

sb_bundle = frappe.new_doc("Serial and Batch Bundle")
sb_bundle.item_code = 'Tea'
# sb_bundle.warehouse = set_warehouse
sb_bundle.type_of_transaction = 'Outward'
sb_bundle.company = frappe.defaults.get_user_default("Company")
sb_bundle.voucher_type = 'Stock Entry'

for index, item in enumerate(itemsTable):   
	batchDoc = frappe.get_doc('Batch',item['pallet'])
	if item['delivery_qty'] < item['package_qty']:
		batchDoc.status = 'Partially-Delivered'
	else:
		batchDoc.status = 'Delivered'
	batchDoc.save(ignore_permissions=True)

	sb_bundle.append('entries', {
		'batch_no': batchDoc.name, 
		'qty': int(item['package_qty']),
		'warehouse': item['warehouse']
	})

sb_bundle.save(ignore_permissions=True) 
# sb_bundle.submit()
sb_bundle_last_doc = frappe.get_last_doc('Serial and Batch Bundle')

stock_entry.stock_entry_type = 'Material Issue'

itemsTable = json.loads(items)

for item in itemsTable:  
	stock_entry.append('items', {	
	's_warehouse': item['warehouse'],
	'serial_and_batch_bundle': sb_bundle_last_doc.name,
	'item':'Tea',
	'item_code': 'Tea',
	'qty': item['delivery_qty'], 
	'conversion_factor': int(1)
	})

stock_entry.save(ignore_permissions=True)
stock_entry.submit()

task = frappe.get_last_doc('Stock Entry')

return task.name

and got the following error:

frappe.exceptions.ValidationError:
The Serial and Batch Bundle
SABB-00000016
does not belong to Item Tea
or Warehouse Stores - T
or Stock Entry no MAT-STE-2024-00015

Any help on how to create this Material Issue stock entries would be of help because I can successfully accomplish Material Receipts using something similar to my second attempt.

I did not get a reply but I thought it was best I showed how I solved this. Instead of creating a Stock Entry of type ‘Material Issue’, I instead created a Delivery Note and attached the relevant batch numbers to the items in the items list. This affected both the stock ledger and the corresponding batches as well as automatically created a serial and batch bundle.

@frappe.whitelist()
def issue_delivery_note(**args):

items = args.get('items')
customer = args.get('client')
posting_date = args.get('posting_date')
posting_time = datetime.datetime.now().strftime("%H:%M:%S")

itemsTable = json.loads(items)

delivery_note = frappe.new_doc("Delivery Note")
delivery_note.customer = customer
delivery_note.posting_date = posting_date
delivery_note.posting_time = posting_time
delivery_note.selling_price_list = 'Standard Selling'

for index, item in enumerate(itemsTable):   
	batchDoc = frappe.get_doc('Batch',item['pallet'])
	if item['delivery_qty'] < item['package_qty']:
		batchDoc.status = 'Partially-Delivered'
	else:
		batchDoc.status = 'Delivered'
	batchDoc.save(ignore_permissions=True)

for item in itemsTable:
	delivery_note.append('items', {
		'item':'Tea',
		'item_code': 'Tea',
		'qty': item['delivery_qty'], 
		'uom': 'Bag',
		'batch_no': item['pallet'],
	})

delivery_note.save(ignore_permissions=True)
delivery_note.submit()

task = frappe.get_last_doc('Delivery Note')

return task.name