#NEWLINE# def return_process( ) : #NEWLINE# #INDENT# '\n        Return some stock from a shipment back into the warehouse\n    ' #NEWLINE# try : #NEWLINE# #INDENT# send_id = request . args [ 0 ] #NEWLINE# #UNINDENT# except : #NEWLINE# #INDENT# redirect ( f = 'send' ) #NEWLINE# #UNINDENT# stable = s3db . inv_send #NEWLINE# if ( not auth . s3_has_permission ( 'update' , stable , record_id = send_id ) ) : #NEWLINE# #INDENT# session . error = T ( 'You do not have permission to return this sent shipment.' ) #NEWLINE# #UNINDENT# send_record = db ( ( stable . id == send_id ) ) . select ( stable . status , limitby = ( 0 , 1 ) ) . first ( ) #NEWLINE# inv_ship_status = s3db . inv_ship_status #NEWLINE# if ( send_record . status != inv_ship_status [ 'RETURNING' ] ) : #NEWLINE# #INDENT# session . error = T ( 'This shipment has not been returned.' ) #NEWLINE# #UNINDENT# if session . error : #NEWLINE# #INDENT# redirect ( URL ( c = 'inv' , f = 'send' , args = [ send_id ] ) ) #NEWLINE# #UNINDENT# invtable = s3db . inv_inv_item #NEWLINE# rtable = s3db . inv_recv #NEWLINE# tracktable = s3db . inv_track_item #NEWLINE# track_rows = db ( ( tracktable . send_id == send_id ) ) . select ( tracktable . id , tracktable . quantity , tracktable . return_quantity , tracktable . send_inv_item_id ) #NEWLINE# for track_item in track_rows : #NEWLINE# #INDENT# send_inv_id = track_item . send_inv_item_id #NEWLINE# return_qnty = track_item . return_quantity #NEWLINE# if ( return_qnty == None ) : #NEWLINE# #INDENT# return_qnty = 0 #NEWLINE# #UNINDENT# tracktable [ track_item . id ] = dict ( recv_quantity = ( track_item . quantity - return_qnty ) ) #NEWLINE# if return_qnty : #NEWLINE# #INDENT# db ( ( invtable . id == send_inv_id ) ) . update ( quantity = ( invtable . quantity + return_qnty ) ) #NEWLINE# #UNINDENT# #UNINDENT# stable [ send_id ] = dict ( status = inv_ship_status [ 'RECEIVED' ] , owned_by_user = None , owned_by_group = ADMIN ) #NEWLINE# recv_row = db ( ( tracktable . send_id == send_id ) ) . select ( tracktable . recv_id , limitby = ( 0 , 1 ) ) . first ( ) #NEWLINE# if recv_row : #NEWLINE# #INDENT# recv_id = recv_row . recv_id #NEWLINE# rtable [ recv_id ] = dict ( date = request . utcnow , status = inv_ship_status [ 'RECEIVED' ] , owned_by_user = None , owned_by_group = ADMIN ) #NEWLINE# #UNINDENT# db ( ( tracktable . send_id == send_id ) ) . update ( status = s3db . inv_tracking_status [ 'RECEIVED' ] ) #NEWLINE# redirect ( URL ( f = 'send' , args = [ send_id ] ) )