'Returns items with BOM that already do not have a linked production order'

def get_production_order_items(self):

    items = []
    for i in (self.packed_items or self.items):
        bom = frappe.get_all(u'BOM', dict(item=i.item_code, is_active=True), order_by=u'is_default    desc')
        bom = (bom[0].name if bom else None)
        stock_qty = (i.qty if self.packed_items else i.stock_qty)
        items.append(dict(item_code=i.item_code, bom=bom, warehouse=i.warehouse, pending_qty=(stock_qty - flt(frappe.db.sql(u'select    sum(qty)    from    `tabProduction    Order`\n DCTB  DCTB  DCTB  DCTB  DCTB where    production_item=%s    and    sales_order=%s', (i.item_code, self.name))[0][0]))))
    return items
