<?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_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_transfer_receipts_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();
}

global $systypes_array;

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

simple_page_mode(true);

$asset_req_trn_id = -1;
 
if(isset($_GET['ARTNo'])){
    $_POST['ARTNo'] = $_GET['ARTNo'];
    set_session_variable("ST_ASSET_TRANSFER_RECEIPT_at_no", $_POST['ARTNo']);
}
      
if(isset($_POST['ARTNo']) && $_POST['ARTNo'] > 0){
    $asset_req_trn_id = $_POST['ARTNo'];
}else{
    if(!(isset($_POST['ARTNo']) > 0)){
        $_POST['ARTNo'] = -1;
        set_session_variable("ST_ASSET_TRANSFER_RECEIPT_at_no", $_POST['ARTNo']);
    }else{
        $asset_req_trn_id = get_session_variable('ST_ASSET_TRANSFER_RECEIPT_at_no');
    }
}

if(isset($_GET['Processed_ATRcpt_No'])){            
    $trans_no = $_GET['Processed_ATRcpt_No'];
    $trans_type = ST_ASSET_TRANSFER_RECEIPT;
    $message = get_session_variable('ST_ASSET_TRANSFER_RECEIPT_message');
    clear_session_variable('ST_ASSET_TRANSFER_RECEIPT_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_requisition_transfers_inquiry.php", _("Process Another ".$systypes_array[ST_ASSET_REQUISITION_TRANSFER]));
    display_footer_exit();
}

if (isset($_POST['Process_ATR'])) {    
    $msg = can_process_asset_transfer_receipt_header_and_line($asset_req_trn_id);   
        if($msg["success"] !== true){   
            display_error($msg['message']);
//            return false;
        }else{
    
        begin_transaction();
        $msg = save_asset_transfer_receipt($asset_req_trn_id, $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_TRANSFER_RECEIPT_at_no", $asset_req_trn_id);
                meta_forward($_SERVER['PHP_SELF'], "Processed_ATRcpt_No=$asset_req_trn_id");        
            }
        }
}


//global $Ajax;

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

start_form();

display_asset_transfer_receipt_header($asset_req_trn_id); 

    display_asset_transfer_receipt_details($asset_req_trn_id);
    
    if(($asset_req_trn_id > 0)){
        submit_center_first('Calculate_ATR', _("Calculate_ATR"), '', 'default');
        submit_center_last('Process_ATR', _("Process"), '', 'default');    
    }

    hidden('ARTNo', $asset_req_trn_id);

    end_table(2);


//    $Ajax->activate('asset_id');


end_form();
end_page(false, false, false);        

?>