def commit_req():
    """
        Function to commit items for a Request
        - i.e. copy data from a req into a commitment
        arg: req_id
        vars: site_id
    """

    req_id = request.args[0]
    site_id = request.vars.get("site_id")

    table = s3db.req_req
    r_req = db(table.id == req_id).select(table.type,
                                          limitby=(0, 1)).first()

    # User must have permissions over facility which is sending
    (prefix, resourcename, id) = s3db.get_instance(s3db.org_site, site_id)
    if not site_id or not auth.s3_has_permission("update",
                                                 "%s_%s" % (prefix,
                                                            resourcename),
                                                 record_id=id):
        session.error = T("You do not have permission to make this commitment.")
        redirect(URL(c="req", f="req",
                     args=[req_id]))

    # Create a new commit record
    commit_id = s3db.req_commit.insert(date = request.utcnow,
                                       req_id = req_id,
                                       site_id = site_id,
                                       type = r_req.type
                                       )

    # Only select items which are in the warehouse
    ritable = s3db.req_req_item
    iitable = s3db.inv_inv_item
    query = (ritable.req_id == req_id) & \
            (ritable.quantity_fulfil < ritable.quantity) & \
            (iitable.site_id == site_id) & \
            (ritable.item_id == iitable.item_id) & \
            (ritable.deleted == False)  & \
            (iitable.deleted == False)
    req_items = db(query).select(ritable.id,
                                 ritable.quantity,
                                 ritable.item_pack_id,
                                 iitable.item_id,
                                 iitable.quantity,
                                 iitable.item_pack_id)

    citable = s3db.req_commit_item
    for req_item in req_items:
        req_pack_quantity = req_item.req_req_item.pack_quantity()
        req_item_quantity = req_item.req_req_item.quantity * req_pack_quantity

        inv_item_quantity = req_item.inv_inv_item.quantity * \
                            req_item.inv_inv_item.pack_quantity()

        if inv_item_quantity > req_item_quantity:
            commit_item_quantity = req_item_quantity
        else:
            commit_item_quantity = inv_item_quantity
        commit_item_quantity = commit_item_quantity / req_pack_quantity

        if commit_item_quantity:
            req_item_id = req_item.req_req_item.id
            commit_item_id = citable.insert(commit_id = commit_id,
                                            req_item_id = req_item_id,
                                            item_pack_id = req_item.req_req_item.item_pack_id,
                                            quantity = commit_item_quantity
                                            )

            # Update the req_item.commit_quantity & req.commit_status
            s3base.s3_store_last_record_id("req_commit_item", commit_item_id)
            form = Storage()
            form.vars = Storage(
                    req_item_id = req_item_id
                )
            s3db.req_commit_item_onaccept(form)

    # Redirect to commit
    redirect(URL(c="req", f="commit",
                 args=[commit_id, "commit_item"]))

# =============================================================================
dataset/ETHPy150Open sahana/eden/controllers/req.py/commit_req/original