def adj_close():

    try:
        adj_id = request.args[0]
    except:
        redirect(URL(f="adj"))

    atable = s3db.inv_adj

    # Limit site_id to sites the user has permissions for
    error_msg = T("You do not have permission to adjust the stock level in this warehouse.")
    auth.permitted_facilities(table=atable, error_msg=error_msg)

    adj_rec = db(atable.id == adj_id).select(atable.status,
                                             atable.site_id,
                                             limitby=(0, 1)).first()
    if adj_rec.status != 0:
        session.error = T("This adjustment has already been closed.")

    if session.error:
        redirect(URL(c="inv", f="adj",
                     args=[adj_id]))

    aitable = s3db.inv_adj_item
    inv_item_table = s3db.inv_inv_item

    site_id = adj_rec.site_id
    # Go through all the adj_items
    query = (aitable.adj_id == adj_id) & \
            (aitable.deleted == False)
    adj_items = db(query).select()
    for adj_item in adj_items:
        if adj_item.inv_item_id is None:
            # Create a new stock item
            inv_item_id = inv_item_table.insert(site_id = site_id,
                                                item_id = adj_item.item_id,
                                                item_pack_id = adj_item.item_pack_id,
                                                currency = adj_item.currency,
                                                bin = adj_item.bin,
                                                pack_value = adj_item.old_pack_value,
                                                expiry_date = adj_item.expiry_date,
                                                quantity = adj_item.new_quantity,
                                                owner_org_id = adj_item.old_owner_org_id,
                                               )
            # Add the inventory item id to the adjustment record
            db(aitable.id == adj_item.id).update(inv_item_id = inv_item_id)
        elif adj_item.new_quantity is not None:
            # Update the existing stock item
            db(inv_item_table.id == adj_item.inv_item_id).update(item_pack_id = adj_item.item_pack_id,
                                                                 bin = adj_item.bin,
                                                                 pack_value = adj_item.old_pack_value,
                                                                 expiry_date = adj_item.expiry_date,
                                                                 quantity = adj_item.new_quantity,
                                                                 owner_org_id = adj_item.new_owner_org_id,
                                                                 status = adj_item.new_status,
                                                                )
    # Change the status of the adj record to Complete
    db(atable.id == adj_id).update(status=1)
    # Go to the Inventory of the Site which has adjusted these items
    (prefix, resourcename, id) = s3db.get_instance(s3db.org_site,
                                                   site_id)
    redirect(URL(c = prefix,
                 f = resourcename,
                 args = [id, "inv_item"]))

# =============================================================================
RESTful CRUD controller
dataset/ETHPy150Open sahana/eden/controllers/inv.py/adj_close