'When a track item record is created and it is linked to an inv_item
then the inv_item quantity will be reduced.'

@staticmethod
def inv_track_item_onaccept(form):

    db = current.db
    s3db = current.s3db
    tracktable = db.inv_track_item
    inv_item_table = db.inv_inv_item
    stable = db.inv_send
    rtable = db.inv_recv
    siptable = db.supply_item_pack
    supply_item_add = s3db.supply_item_add
    form_vars = form.vars
    id = form_vars.id
    record = form.record
    if form_vars.send_inv_item_id:
        stock_item = db((inv_item_table.id == form_vars.send_inv_item_id)).select(inv_item_table.id, inv_item_table.quantity, inv_item_table.item_pack_id, limitby=(0, 1)).first()
    elif record:
        stock_item = record.send_inv_item_id
    else:
        stock_item = None
    if (form_vars.quantity and stock_item):
        stock_quantity = stock_item.quantity
        stock_pack = db((siptable.id == stock_item.item_pack_id)).select(siptable.quantity, limitby=(0, 1)).first().quantity
        if record:
            if (record.send_inv_item_id != None):
                old_track_pack_quantity = db((siptable.id == record.item_pack_id)).select(siptable.quantity, limitby=(0, 1)).first().quantity
                stock_quantity = supply_item_add(stock_quantity, stock_pack, record.quantity, old_track_pack_quantity)
        try:
            new_track_pack_quantity = db((siptable.id == form_vars.item_pack_id)).select(siptable.quantity, limitby=(0, 1)).first().quantity
        except:
            new_track_pack_quantity = record.item_pack_id.quantity
        newTotal = supply_item_add(stock_quantity, stock_pack, (- float(form_vars.quantity)), new_track_pack_quantity)
        db((inv_item_table.id == stock_item)).update(quantity=newTotal)
    if (form_vars.send_id and form_vars.recv_id):
        send_ref = db((stable.id == form_vars.send_id)).select(stable.send_ref, limitby=(0, 1)).first().send_ref
        db((rtable.id == form_vars.recv_id)).update(send_ref=send_ref)
    rrtable = s3db.table('req_req')
    if rrtable:
        use_req = True
        ritable = s3db.req_req_item
    else:
        use_req = False
    if (use_req and record and record.req_item_id):
        req_id = db((ritable.id == record.req_item_id)).select(ritable.req_id, limitby=(0, 1)).first().req_id
        req_ref = db((rrtable.id == req_id)).select(rrtable.req_ref, limitby=(0, 1)).first().req_ref
        db((stable.id == form_vars.send_id)).update(req_ref=req_ref)
        if form_vars.recv_id:
            db((rtable.id == form_vars.recv_id)).update(req_ref=req_ref)
    if (record and (record.status == TRACK_STATUS_UNLOADING) and record.recv_quantity):
        recv_rec = db((rtable.id == record.recv_id)).select(rtable.site_id, rtable.type).first()
        recv_site_id = recv_rec.site_id
        query = ((((((((((((inv_item_table.site_id == recv_site_id) & (inv_item_table.item_id == record.item_id)) & (inv_item_table.item_pack_id == record.item_pack_id)) & (inv_item_table.currency == record.currency)) & (inv_item_table.status == record.inv_item_status)) & (inv_item_table.pack_value == record.pack_value)) & (inv_item_table.expiry_date == record.expiry_date)) & (inv_item_table.bin == record.recv_bin)) & (inv_item_table.owner_org_id == record.owner_org_id)) & (inv_item_table.item_source_no == record.item_source_no)) & (inv_item_table.status == record.inv_item_status)) & (inv_item_table.supply_org_id == record.supply_org_id))
        inv_item_row = db(query).select(inv_item_table.id, limitby=(0, 1)).first()
        if inv_item_row:
            inv_item_id = inv_item_row.id
            db((inv_item_table.id == inv_item_id)).update(quantity=(inv_item_table.quantity + record.recv_quantity))
        else:
            source_type = 0
            if form_vars.send_inv_item_id:
                source_type = db((inv_item_table.id == form_vars.send_inv_item_id)).select(inv_item_table.source_type, limitby=(0, 1)).first().source_type
            elif (recv_rec.type == 2):
                source_type = 1
            else:
                source_type = 2
            inv_item = dict(site_id=recv_site_id, item_id=record.item_id, item_pack_id=record.item_pack_id, currency=record.currency, pack_value=record.pack_value, expiry_date=record.expiry_date, bin=record.recv_bin, owner_org_id=record.owner_org_id, supply_org_id=record.supply_org_id, quantity=record.recv_quantity, item_source_no=record.item_source_no, source_type=source_type, status=record.inv_item_status)
            realm_entity = current.auth.get_realm_entity(inv_item_table, inv_item)
            inv_item.update(realm_entity=realm_entity)
            inv_item_id = inv_item_table.insert(**inv_item)
        if (use_req and record.req_item_id):
            req_item = db((ritable.id == record.req_item_id)).select(ritable.quantity_fulfil, ritable.item_pack_id, limitby=(0, 1)).first()
            req_quantity = req_item.quantity_fulfil
            req_pack_quantity = db((siptable.id == req_item.item_pack_id)).select(siptable.quantity, limitby=(0, 1)).first().quantity
            track_pack_quantity = db((siptable.id == record.item_pack_id)).select(siptable.quantity, limitby=(0, 1)).first().quantity
            quantity_fulfil = supply_item_add(req_quantity, req_pack_quantity, record.recv_quantity, track_pack_quantity)
            db((ritable.id == record.req_item_id)).update(quantity_fulfil=quantity_fulfil)
            s3db.req_update_status(req_id)
        db((tracktable.id == id)).update(recv_inv_item_id=inv_item_id, status=TRACK_STATUS_ARRIVED)
        if (record.quantity != record.recv_quantity):
            query = ((tracktable.recv_id == record.recv_id) & (tracktable.adj_item_id != None))
            adj_rec = db(query).select(tracktable.adj_item_id, limitby=(0, 1)).first()
            adjitemtable = s3db.inv_adj_item
            if adj_rec:
                adj_id = db((adjitemtable.id == adj_rec.adj_item_id)).select(adjitemtable.adj_id, limitby=(0, 1)).first().adj_id
            else:
                adjtable = s3db.inv_adj
                irtable = s3db.inv_recv
                recv_rec = db((irtable.id == record.recv_id)).select(irtable.recipient_id, irtable.site_id, irtable.comments, limitby=(0, 1)).first()
                adj_id = adjtable.insert(adjuster_id=recv_rec.recipient_id, site_id=recv_rec.site_id, adjustment_date=current.request.now.date(), category=0, status=1, comments=recv_rec.comments)
            adj_item_id = adjitemtable.insert(reason=0, adj_id=adj_id, inv_item_id=record.send_inv_item_id, item_id=record.item_id, item_pack_id=record.item_pack_id, old_quantity=record.quantity, new_quantity=record.recv_quantity, currency=record.currency, old_pack_value=record.pack_value, new_pack_value=record.pack_value, expiry_date=record.expiry_date, bin=record.recv_bin, comments=record.comments)
            db((tracktable.id == id)).update(adj_item_id=adj_item_id)
