'Process a Shipment'

@staticmethod
def inv_send_process():

    request = current.request
    try:
        send_id = request.args[0]
    except:
        redirect(URL(f='send'))
    T = current.T
    auth = current.auth
    db = current.db
    s3db = current.s3db
    stable = db.inv_send
    session = current.session
    if (not auth.s3_has_permission('update', stable, record_id=send_id)):
        session.error = T('You    do    not    have    permission    to    send    this    shipment.')
    send_record = db((stable.id == send_id)).select(stable.status, stable.sender_id, stable.send_ref, stable.req_ref, stable.site_id, stable.delivery_date, stable.recipient_id, stable.to_site_id, stable.comments, limitby=(0, 1)).first()
    if (send_record.status != SHIP_STATUS_IN_PROCESS):
        session.error = T('This    shipment    has    already    been    sent.')
    tracktable = db.inv_track_item
    siptable = s3db.supply_item_pack
    rrtable = s3db.req_req
    ritable = s3db.req_req_item
    query = ((tracktable.send_id == send_id) & (tracktable.deleted == False))
    track_items = db(query).select(tracktable.req_item_id, tracktable.quantity, tracktable.item_pack_id)
    if (not track_items):
        session.error = T('No    items    have    been    selected    for    shipping.')
    if session.error:
        redirect(URL(f='send', args=[send_id]))
    system_roles = auth.get_system_roles()
    ADMIN = system_roles.ADMIN
    db((stable.id == send_id)).update(date=request.utcnow, status=SHIP_STATUS_SENT, owned_by_user=None, owned_by_group=ADMIN)
    req_ref = send_record.req_ref
    req_rec = db((rrtable.req_ref == req_ref)).select(rrtable.id, limitby=(0, 1)).first()
    if req_rec:
        req_id = req_rec.id
        for track_item in track_items:
            req_item_id = track_item.req_item_id
            if req_item_id:
                req_pack_id = db((ritable.id == req_item_id)).select(ritable.item_pack_id, limitby=(0, 1)).first().item_pack_id
                req_p_qnty = db((siptable.id == req_pack_id)).select(siptable.quantity, limitby=(0, 1)).first().quantity
                t_qnty = track_item.quantity
                t_pack_id = track_item.item_pack_id
                inv_p_qnty = db((siptable.id == t_pack_id)).select(siptable.quantity, limitby=(0, 1)).first().quantity
                transit_quantity = ((t_qnty * inv_p_qnty) / req_p_qnty)
                db((ritable.id == req_item_id)).update(quantity_transit=(ritable.quantity_transit + transit_quantity))
        s3db.req_update_status(req_id)
    rtable = s3db.inv_recv
    recv_id = rtable.insert(sender_id=send_record.sender_id, send_ref=send_record.send_ref, req_ref=req_ref, from_site_id=send_record.site_id, eta=send_record.delivery_date, recipient_id=send_record.recipient_id, site_id=send_record.to_site_id, comments=send_record.comments, status=SHIP_STATUS_SENT, type=1)
    db((tracktable.send_id == send_id)).update(status=2, recv_id=recv_id)
    session.confirmation = T('Shipment    Items    sent    from    Warehouse')
    if req_rec:
        session.confirmation = T('Request    Status    updated')
    redirect(URL(f='send', args=[send_id, 'track_item']))
