<?php
/**********************************************************************
***********************************************************************/

$page_security = 'SA_ASSET_TRANSFER_REQUEST';
if (!@$_GET['popup'])
	$path_to_root = "..";
else	
	$path_to_root = "../..";

include_once($path_to_root . "/includes/session.inc");
include_once($path_to_root . "/includes/date_functions.inc");
include_once($path_to_root . "/includes/ui.inc");
include_once($path_to_root . "/includes/inventory.inc");
include_once($path_to_root . "/includes/data_checks.inc");
include_once($path_to_root . "/includes/approval_functions.inc");
include_once($path_to_root . "/projects/includes/db/project_payment_application_db.inc"); 
include_once($path_to_root . "/reporting/includes/reporting.inc");
include_once($path_to_root . "/assets/includes/db/asset_transfer_requests_db.inc");
include_once($path_to_root . "/includes/references.inc");
include_once($path_to_root . "/includes/ui/ui_lists_group_companies.inc");
include_once($path_to_root . "/includes/db/group_companies_db.inc");
include_once($path_to_root . "/assets/includes/db/assets_db.inc");
include_once($path_to_root . "/includes/project_cost_functions.inc");
include_once($path_to_root . "/assets/includes/ui/asset_transfer_requests_ui.inc");

$js = '';
if ($SysPrefs->use_popup_windows){
    $js .= get_js_open_window(900, 500);
}
if (user_use_date_picker()){
    $js .= get_js_date_picker();
}

page(_($help_context = "Asset Transfer Request"), false, false, "", $js);

//------------------------------------------------------------------------------------------------------
simple_page_mode(true);

$asset_tran_req_no = -1;
 
if(isset($_GET['ATRNo'])){
    $_POST['ATRNo'] = $_GET['ATRNo'];
    set_session_variable("ST_ASSET_TRANSFER_REQUEST_atr_no", $_POST['ATRNo']);
}

if(isset($_GET['NewAssetTransfer'])){
    $_POST['ATRNo'] = -1;
    set_session_variable("ST_ASSET_TRANSFER_REQUEST_atr_no", $_POST['ATRNo']);
}        
        
if(isset($_POST['ATRNo']) && $_POST['ATRNo'] > 0){
    $asset_tran_req_no = $_POST['ATRNo'];
}else{
    if(!(isset($_POST['ATRNo']) > 0)){
        $_POST['ATRNo'] = -1;
        set_session_variable("ST_ASSET_TRANSFER_REQUEST_atr_no", $_POST['ATRNo']);
    }else{
        $asset_tran_req_no = get_session_variable('ST_ASSET_TRANSFER_REQUEST_atr_no');
    }
}

function send_asset_transfer_request_back_to_originator($asset_tran_req_no){        
    $msg = can_process_asset_transfer_request_header($asset_tran_req_no);
    if (isset($_SESSION['wa_current_user']->username) && $_SESSION['wa_current_user']->username == 'admin') {
        return $msg;
    }
    
    if($msg['success'] !== true){
        return $msg;
    }
    
    begin_transaction();
     
        $arh = get_asset_transfer_request_header($asset_tran_req_no);
        $ar_msg = send_back_to_document_originator(ST_ASSET_TRANSFER_REQUEST, $asset_tran_req_no, $arh['to_project_id'], $arh['next_approval_group_id'], 
                                                   $_SESSION['wa_current_user']->user, $arh['row_state'], $arh['created_by']);

        if($ar_msg['success'] !== true){
            cancel_transaction();
            return $ar_msg;
        }
        
    commit_transaction();
    
    clear_session_variable("ST_ASSET_TRANSFER_REQUEST_atr_no");
    set_session_variable('ST_ASSET_TRANSFER_REQUEST_message', $ar_msg['message']);
    meta_forward($_SERVER['PHP_SELF'], "Processed_ATR_No=$asset_tran_req_no");        
}
 
function reject_asset_transfer_request($asset_tran_req_no){
    $msg = can_process_asset_transfer_request_header($asset_tran_req_no);
    if($msg['success'] !== true){
        return $msg;
    }
    
    begin_transaction();
        $arh = get_asset_transfer_request_header($asset_tran_req_no);
        $current_user_id = $_SESSION['wa_current_user']->user;
        $rj_msg = reject_document(ST_ASSET_TRANSFER_REQUEST, $asset_tran_req_no, $arh['to_project_id'], $arh['next_approval_group_id'], $current_user_id, $arh['row_state']);

        if($rj_msg['success'] !== true){
            cancel_transaction();
            return;
        }

    commit_transaction();
    
    clear_session_variable("ST_ASSET_TRANSFER_REQUEST_atr_no");
    set_session_variable('ST_ASSET_TRANSFER_REQUEST_messages', $rj_msg['message']);
    meta_forward($_SERVER['PHP_SELF'], "Processed_ATR_No=$asset_tran_req_no");
}

if(isset($_GET['Processed_ATR_No'])){            
    $trans_no = $_GET['Processed_ATR_No'];
    $trans_type = ST_ASSET_TRANSFER_REQUEST;
    $message = get_session_variable('ST_ASSET_TRANSFER_REQUEST_message');
    clear_session_variable('ST_ASSET_TRANSFER_REQUEST_message');
    
    display_notification_centered($message);
    display_note(print_document_link($trans_no, _("&Print this toll request"), true, $trans_type), 0, 0);
    hyperlink_no_params("$path_to_root/assets/asset_transfer_requests.php", _("Create Another &Asset Transfer Request"));
    hyperlink_no_params("$path_to_root/assets/inquiry/asset_transfer_requests_inquiry.php", _("Release Another &Asset Transfer Request"));
    hyperlink_no_params("$path_to_root/assets/inquiry/approve_asset_transfer_requests_inquiry.php", _("Approve Another &Asset Transfer Request"));
    display_footer_exit();
}

if(isset($_POST['Update_Release_ATR'])){
    $msg = can_release_asset_transfer_request($asset_tran_req_no);
    if($msg['success'] !== true){
        display_error($msg['message']);
        return false;
    } 
    
    $msg = can_process_asset_transfer_request_header($asset_tran_req_no);
    if($msg['success'] !== true){
        display_error($msg['message']);
        return false;
    } 
    
    begin_transaction();
    $msg = release_asset_transfer_request($asset_tran_req_no, $msg['row_state']);
    if($msg['success'] !== true){
        cancel_transaction();
        display_error($msg['message']);
    }
}

if(isset($_POST['Back_To_Originator_ATR'])){
    $msg = send_asset_transfer_request_back_to_originator($asset_tran_req_no); 
    if($msg['success'] !== true){
        display_error($msg['message']);
        return false;
    } 
}

if(isset($_POST['Reject_ATR'])){
    $msg = reject_asset_transfer_request($asset_tran_req_no);  
    if($msg['success'] !== true){
        display_error($msg['message']);
        return false;
    } 
}

if(isset($_POST['Save_Asset_Transfer_Request_Header'])){
    
    $validate = can_process_asset_transfer_request_header($asset_tran_req_no);   
    
    if($validate["success"] !== true){          
        if(isset($validate["focus_to"])){
            set_focus($validate["focus_to"]);      
        }
        display_error($validate['message']);
        return false;
    }
       
    begin_transaction();
    $msg = save_asset_transfer_request_header($asset_tran_req_no, $validate["row_state"]);
    
    if($msg['success'] !== true){
        cancel_transaction();
        display_error($msg['message']);
    }else{
        commit_transaction();
        set_session_variable("ST_ASSET_TRANSFER_REQUEST_atr_no", $msg['atr_id']);
        $_POST['ATRNo'] = $msg['atr_id'];
        $Mode = 'RESET';
        display_notification($msg['message']);
    }
}

if(isset($_POST['Add_Asset_Transfer_Request']) || isset($_POST['Update_Asset_tranfer_Request'])){    

    $msg = can_process_asset_transfer_request_header_line($asset_tran_req_no);   
    if($msg["success"] !== true){   
        display_error($msg['message']);
        return false;
    }
    
    begin_transaction();
        $msg = save_asset_transfer_request($asset_tran_req_no, $selected_id, $msg['row_state']);
        if($msg['success'] !== true){
            cancel_transaction();
            display_error($msg['message']);
        }else{
            commit_transaction();
            set_session_variable("ST_ASSET_TRANSFER_REQUEST_atr_no", $msg['atr_id']);
            $asset_tran_req_no = $msg['atr_id'];
            hidden('ATRNo', $msg['atr_id']);
            display_notification($msg['message']);
            $Mode = 'RESET';
        }
}

if($Mode == 'Delete'){
    $can_delete_msg = can_delete_asset_transfer_request_details_line($asset_tran_req_no, $selected_id);
    
    if($can_delete_msg['success'] !== true){
        if(isset($can_delete_msg["focus_to"])){
            set_focus($can_delete_msg["focus_to"]);
        }
        display_error($can_delete_msg["message"]);
    }else{
        begin_transaction();
        $recs = delete_asset_transfer_request_detail($selected_id);

        if($recs != 1){
            cancel_transaction();
            display_error(_("Could not delete Asset Transfer Request item #$selected_id"));
            return false;
        }
        
        $msg = update_asset_transfer_request_header_total($asset_tran_req_no);
        if($msg['success'] !== true){
            display_error($msg["message"]);
            return false;
        }else{
            commit_transaction();
            display_notification(_("Asset Transfer Request item #$selected_id has been deleted."));
        }
    }
    $Mode = 'RESET';
}

if(isset($_POST['cancel']) || (isset($Mode) && $Mode == 'RESET')){
    $Mode = 'RESET';
    $selected_id = -1;
    unset($_POST['asset_id']);
    unset($_POST['_asset_id_edit']);
    unset($_POST['quantity']);
    unset($_POST['from_site_id']);
    unset($_POST['to_site_id']);
    unset($_POST['line_comment']);
} 

//--------------------------------------------------------------------------------------------------------

if ($Mode == 'RESET'){
    $selected_id = -1;
}

global $Ajax;

$Ajax->activate('_page_body');
$Ajax->activate('billing_items');
    
//--------------------------------------------------------------------------------------------------------------

start_form();

display_asset_transfer_request_header($asset_tran_req_no);

if(($asset_tran_req_no > 0)){
    submit_center_first('Save_Asset_Transfer_Request_Header', _("Save"), '', 'default');
    br(2);

    submit_center_first('Update_Release_ATR', "Release for Approval", '', 'default');
    submit('Back_To_Originator_ATR', 'Back To Originator', true, '', 'default');
    submit_center_last('Reject_ATR', "Reject", '', 'default');	
}
 
//------------------------------------------------------------------------------------------------------------
 
br(2);

display_asset_transfer_request_details($asset_tran_req_no,  (!isset($selected_id)) ? -1 : $selected_id);

unset($_POST['asset_id']);
$Ajax->activate("asset_list");
hidden('ATRNo', $asset_tran_req_no);

end_table();

//-----------------------------------------------------------------------------------------------------------

$Ajax->activate('asset_id');

br();

if (!@$_GET['popup']){
    end_form();
    end_page(@$_GET['popup'], false, false);        
}

?>