'Check that the required_total can be removed from the inv_record
if there is insufficient stock then set up the total to being
what is in stock otherwise set it to be the required total.
If the update flag is true then remove it from stock.
The current total is what has already been removed for this
transaction.'

@staticmethod
def inv_remove(inv_rec, required_total, required_pack_value=1, current_track_total=0, update=True):

    db = current.db
    inv_item_table = db.inv_inv_item
    siptable = db.supply_item_pack
    inv_p_qnty = db((siptable.id == inv_rec.item_pack_id)).select(siptable.quantity, limitby=(0, 1)).first().quantity
    inv_qnty = (inv_rec.quantity * inv_p_qnty)
    cur_qnty = (current_track_total * inv_p_qnty)
    req_qnty = (required_total * required_pack_value)
    if (cur_qnty == req_qnty):
        return required_total
    if ((inv_qnty + cur_qnty) > req_qnty):
        send_item_quantity = req_qnty
        new_qnty = (((inv_qnty + cur_qnty) - req_qnty) / inv_p_qnty)
    else:
        send_item_quantity = (inv_qnty + cur_qnty)
        new_qnty = 0
    send_item_quantity = (send_item_quantity / inv_p_qnty)
    if update:
        if new_qnty:
            db((inv_item_table.id == inv_rec.id)).update(quantity=new_qnty)
        else:
            db((inv_item_table.id == inv_rec.id)).update(deleted=True)
    return send_item_quantity
