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

$page_security = 'SA_ASSET_QUANTITY_ADJUSTMENT';
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_quantity_adjustment_requests_db.inc");
include_once($path_to_root . "/assets/includes/db/asset_quantity_adjustment_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_quantity_adjustment_ui.inc");
include_once($path_to_root . "/includes/db/group_companies_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();
}

page(_($help_context = "Asset Quantity Adjustment"), false, false, "", $js);

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

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

if(isset($_GET['Processed_AQAR_No'])){            
    $trans_no = $_GET['Processed_AQAR_No'];
    $trans_type = ST_ASSET_QUANTITY_ADJUSTMENT;
    $message = get_session_variable('ST_ASSET_QUANTITY_ADJUSTMENT_message');
    clear_session_variable('ST_ASSET_QUANTITY_ADJUSTMENT_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_quantity_adjustment_requests_inquiry.php", _("Process Another &Asset Quantity Adjustment"));
    display_footer_exit();
}

if(isset($_POST['Process_AQA'])){    
    $msg = can_process_asset_quantity_adjustment_header_and_line($asset_qty_ajt_req_no);   
    if($msg["success"] !== true){   
        display_error($msg['message']);
        return false;
    }
    
        begin_transaction();
        $saqa_msg = save_asset_quantity_adjustment($asset_qty_ajt_req_no, $msg['row_state'], $_POST['reference']);
        if($msg['success'] !== true){
            cancel_transaction();
            if(isset($saqa_msg['focus_to'])){
                set_focus($saqa_msg["focus_to"]);
            }
            display_error($saqa_msg['message']);
        }else{
            commit_transaction();
            set_session_variable("ST_ASSET_QUANTITY_ADJUSTMENT_message", $saqa_msg['message']);
            meta_forward($_SERVER['PHP_SELF'], "Processed_AQAR_No=$asset_qty_ajt_req_no");        
        }
}

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

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

global $Ajax;

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

start_form();

display_asset_quantity_adjustment_header($asset_qty_ajt_req_no); 
br();
display_asset_quantity_adjustment_details($asset_qty_ajt_req_no);
br();
if(($asset_qty_ajt_req_no > 0)){
    submit_center_first('Process_AQA', _("Process"), '', 'default');
}

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

end_table();

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

$Ajax->activate('asset_id');

br();

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

?>