'Update Status of Request & components'

@staticmethod
def commit_ondelete(row):

    db = current.db
    s3db = current.s3db
    id = row.id
    ctable = s3db.req_commit
    fks = db((ctable.id == id)).select(ctable.deleted_fk, limitby=(0, 1)).first().deleted_fk
    req_id = json.loads(fks)['req_id']
    rtable = s3db.req_req
    req = db((rtable.id == req_id)).select(rtable.id, rtable.type, rtable.commit_status, limitby=(0, 1)).first()
    if (not req):
        return
    req_id = req.id
    type = req.type
    if (type == 1):
        ritable = s3db.req_req_item
        query = ((ritable.req_id == req_id) & (ritable.deleted == False))
        ritems = db(query).select(ritable.id, ritable.item_pack_id, ritable.quantity)
        citable = s3db.req_commit_item
        query = ((((ctable.req_id == req_id) & (citable.commit_id == ctable.id)) & (citable.commit_id != id)) & (citable.deleted == False))
        citems = db(query).select(citable.item_pack_id, citable.quantity)
        commit_qty = {}
        for item in citems:
            item_pack_id = item.item_pack_id
            if (item_pack_id in commit_qty):
                commit_qty[item_pack_id] += (item.quantity * item.pack_quantity())
            else:
                commit_qty[item_pack_id] = (item.quantity * item.pack_quantity())
        complete = False
        for item in ritems:
            if (item.item_pack_id in commit_qty):
                quantity_commit = commit_qty[item.item_pack_id]
                db((ritable.id == item.id)).update(quantity_commit=quantity_commit)
                req_quantity = (item.quantity * item.pack_quantity())
                if (quantity_commit >= req_quantity):
                    complete = True
                else:
                    complete = False
        if complete:
            db((rtable.id == req_id)).update(commit_status=REQ_STATUS_COMPLETE)
        elif (not citems):
            db((rtable.id == req_id)).update(commit_status=REQ_STATUS_NONE)
        else:
            db((rtable.id == req_id)).update(commit_status=REQ_STATUS_PARTIAL)
    elif (type == 3):
        rstable = s3db.req_req_skill
        query = ((rstable.req_id == req_id) & (rstable.deleted == False))
        rskills = db(query).select(rstable.id, rstable.skill_id, rstable.quantity)
        cstable = s3db.req_commit_skill
        query = ((((ctable.req_id == req_id) & (cstable.commit_id == ctable.id)) & (cstable.commit_id != id)) & (cstable.deleted == False))
        cskills = db(query).select(cstable.skill_id, cstable.quantity)
        commit_qty = {}
        for skill in cskills:
            multi_skill_id = skill.skill_id
            for skill_id in multi_skill_id:
                if (skill_id in commit_qty):
                    commit_qty[skill_id] += skill.quantity
                else:
                    commit_qty[skill_id] = skill.quantity
        complete = False
        for skill in rskills:
            multi_skill_id = skill.skill_id
            quantity_commit = 0
            for skill_id in multi_skill_id:
                if (skill_id in commit_qty):
                    if (commit_qty[skill_id] > quantity_commit):
                        quantity_commit = commit_qty[skill_id]
            db((rstable.id == skill.id)).update(quantity_commit=quantity_commit)
            req_quantity = skill.quantity
            if (quantity_commit >= req_quantity):
                complete = True
            else:
                complete = False
        if complete:
            db((rtable.id == req_id)).update(commit_status=REQ_STATUS_COMPLETE)
        elif (not cskills):
            db((rtable.id == req_id)).update(commit_status=REQ_STATUS_NONE)
        else:
            db((rtable.id == req_id)).update(commit_status=REQ_STATUS_PARTIAL)
    elif (type == 9):
        if (req.commit_status != REQ_STATUS_NONE):
            db((rtable.id == req_id)).update(commit_status=REQ_STATUS_NONE)
