About update of enable "has batch no." items with transaction exists

Hi, i’ve search related topic in how to enable “has batch no” items that with transactions but it seems that there still no any formal solution to solve this problem with coming updates…

I believe that it’s a quite common problem in the midway when a new user trying to use ERPNEXT after a while. It’s painful to the user that to wipe out their history or even existing stock record for just update a batch no. to trace/locate the items.

What we’ve found is this:

But need bear your own risk before apply…

Just want to know is there any new moves/update to this…? Thanks

Hi @Cody_Ng,
i have an update for the python code , to ensure covering all docs related to batch no.
the new code will cover (Delivery Note, Purchase Receipt, Purchase Invoice, Sales Invoice, Stock Entry, Stock Reconciliation).

If i am missing something , i appreciate telling me and i will update.

@frappe.whitelist()
def get_items_without_has_batch_in_item_group(group, series):
    if not group or not series:
        return 0
    counter = 0
    for item in frappe.db.get_list('Item', filters={'item_group': group, 'has_batch_no': 0}, fields=['item_code', 'item_name'], order_by='item_code', as_list=False):
        counter+=1
    return counter

def set_item_batch_properities(item_code, series):
    frappe.db.set_value('Item', item_code, {
        'has_batch_no': 1,
        'create_new_batch': 1,
        'batch_number_series': series
    })

def create_new_batch(item_code):
    batch_no = frappe.get_doc(dict(
	    doctype='Batch',
		item=item_code)).insert().name
    
    return batch_no

def update_transaction_doc(item_code, batch_no, doctype):
    for entry in frappe.db.get_list(doctype, filters={'item_code': item_code}, fields=['name'], order_by='name', as_list=False):
            frappe.db.set_value(doctype , entry.name, {
                'batch_no': batch_no
            })


def update_transactions(item_code, batch_no):
    update_transaction_doc(item_code, batch_no, 'Delivery Note Item')
    update_transaction_doc(item_code, batch_no, 'Purchase Receipt Item')
    update_transaction_doc(item_code, batch_no, 'Purchase Invoice Item')
    update_transaction_doc(item_code, batch_no, 'Sales Invoice Item')
    update_transaction_doc(item_code, batch_no, 'Stock Entry Detail')
    update_transaction_doc(item_code, batch_no, 'Stock Reconciliation Item')


def update_stock_ledger(item_code):
    batch_no =''
    size = len(frappe.db.get_list('Stock Ledger Entry', filters={'item_code': item_code}, fields=['name', 'actual_qty', 'qty_after_transaction', 'voucher_type', 'voucher_detail_no', 'voucher_no'], order_by='name', as_list=False))
    if size > 0:
        batch_no = create_new_batch(item_code)
        for entry in frappe.db.get_list('Stock Ledger Entry', filters={'item_code': item_code}, fields=['name', 'actual_qty', 'qty_after_transaction', 'voucher_type', 'voucher_detail_no', 'voucher_no'], order_by='name', as_list=False):
            frappe.db.set_value('Stock Ledger Entry', entry.name, {
                'batch_no': batch_no
            })
            if entry.voucher_type == 'Stock Reconciliation':
                srd = frappe.get_doc('Stock Reconciliation', entry.voucher_no)
                if srd.purpose == 'Opening Stock' and entry.actual_qty == 0:
                    frappe.db.set_value('Stock Ledger Entry', entry.name, {
                        'actual_qty': entry.qty_after_transaction
                })   

        update_transactions(item_code, batch_no)   

@frappe.whitelist()
def handle_convert_non_batch_to_has_batch(group, series):
    if not group or not series:
        return 0
    counter = 0
    size = len(frappe.db.get_list('Item', filters={'item_group': group, 'has_batch_no': 0}, fields=['item_code', 'item_name'], order_by='item_code', as_list=False))
    for item in frappe.db.get_list('Item', filters={'item_group': group, 'has_batch_no': 0}, fields=['item_code', 'item_name'], order_by='item_code', as_list=False):
        if counter > -1:
            set_item_batch_properities(item.item_code, series)
            update_stock_ledger(item.item_code)
            counter+=1
            
            progress = counter / size * 100
            frappe.publish_progress(float(counter*100)/size, title = "Processing Items...")
    return counter
1 Like