'Create batches if required. Called before submit'

def make_batches(self, warehouse_field):

    for d in self.items:
        if (d.get(warehouse_field) and (not d.batch_no)):
            (has_batch_no, create_new_batch) = frappe.db.get_value(u'Item', d.item_code, [u'has_batch_no', u'create_new_batch'])
            if (has_batch_no and create_new_batch):
                d.batch_no = frappe.get_doc(dict(doctype=u'Batch', item=d.item_code, supplier=getattr(self, u'supplier', None), reference_doctype=self.doctype, reference_name=self.name)).insert().name
