'When a track item record is being created with a tracking number
then the tracking number needs to be unique within the organisation.
If the inv. item is coming out of a warehouse then the inv. item details
need to be copied across (org, expiry etc)
If the inv. item is being received then their might be a selected bin
ensure that the correct bin is selected and save those details.'

@staticmethod
def inv_track_item_onvalidate(form):

    form_vars = form.vars
    send_inv_item_id = form_vars.send_inv_item_id
    if send_inv_item_id:
        db = current.db
        itable = db.inv_inv_item
        query = (itable.id == send_inv_item_id)
        record = db(query).select(limitby=(0, 1)).first()
        form_vars.item_id = record.item_id
        form_vars.item_source_no = record.item_source_no
        form_vars.expiry_date = record.expiry_date
        form_vars.bin = record.bin
        form_vars.owner_org_id = record.owner_org_id
        form_vars.supply_org_id = record.supply_org_id
        form_vars.pack_value = record.pack_value
        form_vars.currency = record.currency
        form_vars.inv_item_status = record.status
        stable = current.s3db.org_site
        query = (query & (itable.site_id == stable.id))
        record = db(query).select(stable.organisation_id, limitby=(0, 1)).first()
        form_vars.track_org_id = record.organisation_id
    if (not form_vars.recv_quantity):
        form_vars.recv_quantity = form_vars.quantity
    recv_bin = form_vars.recv_bin
    if recv_bin:
        if isinstance(recv_bin, list):
            if (recv_bin[1] != ''):
                recv_bin = recv_bin[1]
            else:
                recv_bin = recv_bin[0]
