Barecode issue in sales and purchase order

I have to write this extra code in every doctype if want to user barcode to add items. New rows wont add without using this code in JS.

scan_barcode: function(frm){
        var doc = cur_frm.doc;
        var is_empty = 0;
        for (var i in doc.items){
            if(doc.items[i].item_code==undefined){
                is_empty = 1;
            }
        }
        if (is_empty ==0){
            frm.add_child('items');
        }
    }