'Update delivery status from Purchase Order for drop shipping'

def update_delivery_status(self):

    (tot_qty, delivered_qty) = (0.0, 0.0)
    for item in self.items:
        if item.delivered_by_supplier:
            item_delivered_qty = frappe.db.sql(u"select    sum(qty)\n DCTB  DCTB  DCTB  DCTB  DCTB from    `tabPurchase    Order    Item`    poi,    `tabPurchase    Order`    po\n DCTB  DCTB  DCTB  DCTB  DCTB where    poi.sales_order_item    =    %s\n DCTB  DCTB  DCTB  DCTB  DCTB  DCTB and    poi.item_code    =    %s\n DCTB  DCTB  DCTB  DCTB  DCTB  DCTB and    poi.parent    =    po.name\n DCTB  DCTB  DCTB  DCTB  DCTB  DCTB and    po.docstatus    =    1\n DCTB  DCTB  DCTB  DCTB  DCTB  DCTB and    po.status    =    'Delivered'", (item.name, item.item_code))
            item_delivered_qty = (item_delivered_qty[0][0] if item_delivered_qty else 0)
            item.db_set(u'delivered_qty', flt(item_delivered_qty), update_modified=False)
        delivered_qty += item.delivered_qty
        tot_qty += item.qty
    self.db_set(u'per_delivered', (flt((delivered_qty / tot_qty)) * 100), update_modified=False)
