<?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/approval_functions.inc");
include_once($path_to_root . "/reporting/includes/reporting.inc");
include_once($path_to_root . "/assets/includes/db/asset_transfer_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/direct_asset_transfers_ui.inc");
include_once($path_to_root . "/assets/includes/db/assets_db.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"), false, false, "", $js);

simple_page_mode(true);

$at_id = 0;
 
if(isset($_GET['ATNo'])){
    $_POST['ATNo'] = $_GET['ATNo'];
}

if (isset($_POST['ATNo']) && $_POST['ATNo'] > 0) {
    $at_id = $_POST['ATNo'];
} else {
    $_POST['ATNo'] = -1;
}

function reject_asset_transfer($at_id) {
    $msg = can_process_asset_transfer_header($at_id);
        if($msg['success'] !== true){
            return $msg;
        }
    
    begin_transaction();
    
    $nof_af_row = update_asset_transfer_approval_status($at_id, AS_NEW, AS_REJECTED);
        if ($nof_af_row != 1) {
            cancel_transaction();
            return array("success" => false, "message" => "Could not reject Asset Transfer Record #$at_id.");
        }

    commit_transaction();
    
    set_session_variable('ST_ASSET_TRANSFER_messages', "Asset Transfer Record #$at_id is rejected.");
    meta_forward($_SERVER['PHP_SELF'], "Processed_AT_No=$at_id");
}

if(isset($_GET['Processed_AT_No'])){            
    $trans_no = $_GET['Processed_AT_No'];
    $trans_type = ST_ASSET_TRANSFER;
    $message = get_session_variable('ST_ASSET_TRANSFER_message');
    clear_session_variable('ST_ASSET_TRANSFER_message');
    
    display_notification_centered($message);
//    display_note(print_document_link($trans_no, _("&Print this toll"), true, $trans_type), 0, 0);
    hyperlink_no_params("$path_to_root/assets/direct_asset_transfers.php", _("Create Another &Asset Transfer"));
    hyperlink_no_params("$path_to_root/assets/inquiry/asset_transfer_inquiry.php", _("Approve Another &Asset Transfer"));
    display_footer_exit();
}

if (isset($_POST['Update_Release_AT'])) {
    $msg = can_approve_asset_transfer($at_id);
        if ($msg['success'] !== true) {
            display_error($msg['message']);
            return false;
        } 
    
    $msg = can_process_asset_transfer_header($at_id);
        if ($msg['success'] !== true) {
            display_error($msg['message']);
            return false;
        } 
    
    begin_transaction();
    
    $msg = approve_asset_transfer($at_id, $msg['row_state']);
        if ($msg['success'] !== true) {
            cancel_transaction();
            display_error($msg['message']);
        }
}

if (isset($_POST['Reject_AT'])) {
    $msg = reject_asset_transfer($at_id);  
        if($msg['success'] !== true){
            display_error($msg['message']);
            return false;
        } 
}

if (isset($_POST['Save_Asset_Transfer_Header'])) {
    
    $validate = can_process_asset_transfer_header($at_id);   
        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_header($at_id, $validate["row_state"]);
        if ($msg['success'] !== true) {
            cancel_transaction();
            display_error($msg['message']);
        } else {
            commit_transaction();
            $Mode = 'RESET';
            display_notification($msg['message']);
        }
}

if (isset($_POST['Add_Asset_Transfer']) || isset($_POST['Update_Asset_tranfer'])) {    

    $msg = can_process_asset_transfer_header_line($at_id);   
        if ($msg["success"] !== true) {   
            display_error($msg['message']);
            return false;
        }
    
    begin_transaction();
        $msg = save_asset_transfers($at_id, $selected_id, $msg['row_state']);
            if ($msg['success'] !== true) {
                cancel_transaction();
                display_error($msg['message']);
            } else {
                commit_transaction();
                display_notification($msg['message']);
                $Mode = 'RESET';
            }
}

if ($Mode == 'Delete') {
    $can_delete_msg = can_delete_asset_transfer_details_line($at_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_detail($selected_id);
                if ($recs != 1) {
                    cancel_transaction();
                    display_error(_("Could not delete Asset Transfer Line #$selected_id"));
                    return false;
                }

                $msg = update_asset_transfer_header_total($at_id);
                    if ($msg['success'] !== true) {
                        display_error($msg["message"]);
                        return false;
                    } else {
                        commit_transaction();
                        display_notification(_("Asset Transfer Line #$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_transfers_header($at_id);

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

    submit_center_first('Update_Release_AT', "Process", '', 'default');
    submit_center_last('Reject_AT', "Reject", '', 'default');	
}
 
//------------------------------------------------------------------------------------------------------------
 
br(2);

display_asset_transfers_details($at_id, (! isset($selected_id)) ? -1 : $selected_id);

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

if ($at_id > 0) {
    hidden('ATNo', $at_id);
}


$Ajax->activate('asset_id');

br();

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

?>