'Adjust the Inventory stocks
reduce the components & increase the kits
- picks items which have an earlier expiry_date where they have them,
earlier purchase_data otherwise
Provide a pick list to ensure that the right stock items are used
to build the kits: inv_kitting_item'

@staticmethod
def inv_kitting_onaccept(form):

    form_vars = form.vars
    kitting_id = form_vars.id
    item_id = form_vars.item_id
    item_pack_id = form_vars.item_pack_id
    quantity = form_vars.quantity
    site_id = form_vars.site_id
    db = current.db
    s3db = current.s3db
    ktable = s3db.supply_kit_item
    ptable = db.supply_item_pack
    iitable = s3db.inv_inv_item
    insert = s3db.inv_kitting_item.insert
    inv_remove = s3db.inv_remove
    query = (ktable.parent_item_id == item_id)
    rows = db(query).select(ktable.item_id, ktable.quantity, ktable.item_pack_id)
    p_id_field = ptable.id
    p_qty_field = ptable.quantity
    pack_qty = db((p_id_field == item_pack_id)).select(p_qty_field, limitby=(0, 1)).first().quantity
    quantity = (quantity * pack_qty)
    ii_id_field = iitable.id
    ii_bin_field = iitable.bin
    ii_pack_field = iitable.item_pack_id
    ii_qty_field = iitable.quantity
    ii_expiry_field = iitable.expiry_date
    ii_purchase_field = iitable.purchase_date
    ii_src_field = iitable.item_source_no
    orderby = (ii_expiry_field | ii_purchase_field)
    expiry_date = None
    squery = ((((iitable.site_id == site_id) & (iitable.deleted == False)) & ((ii_expiry_field >= current.request.now) | (ii_expiry_field == None))) & (iitable.status == 0))
    for record in rows:
        pack_qty = db((p_id_field == record.item_pack_id)).select(p_qty_field, limitby=(0, 1)).first().quantity
        one_kit = (record.quantity * pack_qty)
        required = (one_kit * quantity)
        ritem_id = record.item_id
        query = (squery & (iitable.item_id == ritem_id))
        wh_items = db(query).select(ii_id_field, ii_qty_field, ii_expiry_field, ii_purchase_field, ii_pack_field, ii_bin_field, ii_src_field, orderby=orderby)
        for wh_item in wh_items:
            pack_qty = db((p_id_field == wh_item.item_pack_id)).select(p_qty_field, limitby=(0, 1)).first().quantity
            amount = (wh_item.quantity * pack_qty)
            if (amount > required):
                amount = required
            if wh_item.expiry_date:
                if (expiry_date is None):
                    expiry_date = wh_item.expiry_date
                elif (wh_item.expiry_date < expiry_date):
                    expiry_date = wh_item.expiry_date
            inv_remove(wh_item, amount)
            insert(site_id=site_id, kitting_id=kitting_id, item_id=ritem_id, item_pack_id=wh_item.item_pack_id, bin=wh_item.bin, item_source_no=wh_item.item_source_no, quantity=amount, inv_item_id=wh_item.id)
            required -= amount
            if (not required):
                break
    new_id = iitable.insert(site_id=site_id, item_id=item_id, item_pack_id=item_pack_id, quantity=quantity, expiry_date=expiry_date)
    s3db.update_super(iitable, dict(id=new_id))
