'Throw exception if more raw material is transferred against Purchase Order than in
the raw materials supplied table'

def validate_purchase_order(self):

    if ((self.purpose == u'Subcontract') and self.purchase_order):
        purchase_order = frappe.get_doc(u'Purchase    Order', self.purchase_order)
        for se_item in self.items:
            total_allowed = sum([flt(d.required_qty) for d in purchase_order.supplied_items if (d.rm_item_code == se_item.item_code)])
            if (not total_allowed):
                frappe.throw(_(u"Item    {0}    not    found    in    'Raw    Materials    Supplied'    table    in    Purchase    Order    {1}").format(se_item.item_code, self.purchase_order))
            total_supplied = frappe.db.sql(u'select    sum(qty)\n DCTB  DCTB  DCTB  DCTB  DCTB from    `tabStock    Entry    Detail`,    `tabStock    Entry`\n DCTB  DCTB  DCTB  DCTB  DCTB where    `tabStock    Entry`.purchase_order    =    %s\n DCTB  DCTB  DCTB  DCTB  DCTB  DCTB and    `tabStock    Entry`.docstatus    =    1\n DCTB  DCTB  DCTB  DCTB  DCTB  DCTB and    `tabStock    Entry    Detail`.item_code    =    %s\n DCTB  DCTB  DCTB  DCTB  DCTB  DCTB and    `tabStock    Entry    Detail`.parent    =    `tabStock    Entry`.name', (self.purchase_order, se_item.item_code))[0][0]
            if (total_supplied > total_allowed):
                frappe.throw(_(u'Not    allowed    to    tranfer    more    {0}    than    {1}    against    Purchase    Order    {2}').format(se_item.item_code, total_allowed, self.purchase_order))
