<?php

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

include_once($path_to_root . "/includes/session.inc");
include_once($path_to_root . "/includes/ui.inc");
include_once($path_to_root . "/includes/approval_functions.inc");
include_once($path_to_root . "/assets/includes/db/asset_transfer_requests_db.inc");
include_once($path_to_root . "/assets/includes/db/asset_transfer_db.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_requisition_transfers_ui.inc");
include_once($path_to_root . "/assets/includes/db/asset_locations_db.inc"); 
include_once($path_to_root . "/reporting/includes/reporting.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 Requisition Transfers"), 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_REQUISITION_TRANSFER_at_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_REQUISITION_TRANSFER_at_no", $_POST['ATRNo']);
    }else{
        $asset_tran_req_no = get_session_variable('ST_ASSET_REQUISITION_TRANSFER_at_no');
    }
}

global $systypes_array;

if(isset($_GET['Processed_ATR_No'])){            
    $trans_no = $_GET['Processed_ATR_No'];
    $trans_type = ST_ASSET_REQUISITION_TRANSFER;
    $message = get_session_variable('ST_ASSET_REQUISITION_TRANSFER_message');
    clear_session_variable('ST_ASSET_REQUISITION_TRANSFER_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/inquiry/asset_transfer_requesitions_inquiry.php", _("Process Another ".$systypes_array[ST_ASSET_REQUISITION_TRANSFER]));
    display_footer_exit();
}

if (isset($_POST['Process_AT'])) {    
    $msg = can_process_asset_transfer_header_and_line($asset_tran_req_no);   
        if($msg["success"] !== true){   
            display_error($msg['message']);
            return false;
        }
    
    begin_transaction();
        $msg = save_asset_requisition_transfer($asset_tran_req_no, $msg['row_state']);
            if ($msg['success'] !== true) {
                cancel_transaction();
                if (isset($msg['focus_to'])) {
                    set_focus($msg["focus_to"]);
                }
                display_error($msg['message']);
            } else {
                commit_transaction();
                set_session_variable("ST_ASSET_REQUISITION_TRANSFER_at_no", $asset_tran_req_no);
                set_session_variable('ST_ASSET_REQUISITION_TRANSFER_message', $msg['message']);
                meta_forward($_SERVER['PHP_SELF'], "Processed_ATR_No=$asset_tran_req_no");        
            }
}

global $Ajax;

$Ajax->activate('_page_body');

start_form();

    display_asset_requisition_transfer_header($asset_tran_req_no); 

    display_asset_requisition_transfer_details($asset_tran_req_no);
    
    if(($asset_tran_req_no > 0)){
        submit_center_first('Calculate', _("Calculate"), '', 'default');
        submit_center_last('Process_AT', _("Process"), '', 'default');    
    }

    hidden('ATRNo', $asset_tran_req_no);


    $Ajax->activate('asset_id');


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

?>