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

$page_security = 'SA_ASSET_QUANTITY_ADJUSTMENT_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_quantity_adjustment_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_quantity_adjustment_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 Quantity Adjustment Request"), 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_REQUEST_aqar_no", $_POST['AQARNo']);
}

if(isset($_GET['NewAssetQtyAdjust'])){
    $_POST['AQARNo'] = -1;
    set_session_variable("ST_ASSET_QUANTITY_ADJUSTMENT_REQUEST_aqar_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_REQUEST_aqar_no", $_POST['AQARNo']);
    }else{
        $asset_qty_ajt_req_no = get_session_variable('ST_ASSET_QUANTITY_ADJUSTMENT_REQUEST_aqar_no');
    }
}

function send_tool_request_back_to_originator($asset_qty_ajt_req_no){        
    $msg = can_process_asset_quantity_adjustment_request_header($asset_qty_ajt_req_no);
    if($msg['success'] !== true){
        return $msg;
    }
    
    begin_transaction();
     
        $arh = get_asset_quantity_adjustment_request_header($asset_qty_ajt_req_no);
        $ar_msg = send_back_to_document_originator(ST_ASSET_QUANTITY_ADJUSTMENT_REQUEST, $asset_qty_ajt_req_no, $arh['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();
            display_error_cancel($ar_msg['message']);
            return;
        }
        
    commit_transaction();
    
    clear_session_variable("ST_ASSET_QUANTITY_ADJUSTMENT_REQUEST_aqar_no");
    set_session_variable('ST_ASSET_QUANTITY_ADJUSTMENT_REQUEST_message', $ar_msg['message']);
    meta_forward($_SERVER['PHP_SELF'], "Processed_ATR_No=$asset_qty_ajt_req_no");        
}
 
function reject_asset_quantity_adjustment_request($asset_qty_ajt_req_no){
    $msg = can_process_asset_quantity_adjustment_request_header($asset_qty_ajt_req_no);
    if($msg['success'] !== true){
        return $msg;
    }
    
    begin_transaction();
        $arh = get_asset_quantity_adjustment_request_header($asset_qty_ajt_req_no);
        $current_user_id = $_SESSION['wa_current_user']->user;
        $rj_msg = reject_document(ST_ASSET_QUANTITY_ADJUSTMENT_REQUEST, $asset_qty_ajt_req_no, $arh['project_id'], $arh['next_approval_group_id'], $current_user_id, $arh['row_state']);

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

    commit_transaction();
    
    clear_session_variable("ST_ASSET_QUANTITY_ADJUSTMENT_REQUEST_aqar_no");
    set_session_variable('ST_ASSET_QUANTITY_ADJUSTMENT_REQUEST_message', $rj_msg['message']);
    meta_forward($_SERVER['PHP_SELF'], "Processed_ATR_No=$asset_qty_ajt_req_no");
}

if(isset($_GET['Processed_ATR_No'])){            
    $trans_no = $_GET['Processed_ATR_No'];
    $trans_type = ST_ASSET_QUANTITY_ADJUSTMENT_REQUEST;
    $message = get_session_variable('ST_ASSET_QUANTITY_ADJUSTMENT_REQUEST_message');
    clear_session_variable('ST_ASSET_QUANTITY_ADJUSTMENT_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_quantity_adjustment_requests.php", _("Create Another &Asset Quantity Adjustment Request"));
    hyperlink_no_params("$path_to_root/assets/inquiry/asset_quantity_adjustment_requests_inquiry.php", _("Release Another &Asset Quantity Adjustment Request"));
    hyperlink_no_params("$path_to_root/assets/inquiry/approve_asset_quantity_adjustment_requests_inquiry.php", _("Approve Another &Asset Quantity Adjustment Request"));
    display_footer_exit();
}

if(isset($_POST['Update_Release_AQAR'])){
    $msg = can_release_asset_quantity_adjustment_request($asset_qty_ajt_req_no);
    if($msg['success'] !== true){
        display_error($msg['message']);
        return false;
    } 
    
    $msg = can_process_asset_quantity_adjustment_request_header($asset_qty_ajt_req_no);
    if($msg['success'] !== true){
        display_error($msg['message']);
        return false;
    } 

    $msg = release_asset_quantity_adjustment_request($asset_qty_ajt_req_no, $msg['row_state']);
    if($msg['success'] !== true){
        cancel_transaction();
        display_error($msg['message']);
    }
}

if(isset($_POST['Back_To_Originator_AQAR'])){
    $msg = send_tool_request_back_to_originator($asset_qty_ajt_req_no); 
    if($msg['success'] !== true){
        display_error($msg['message']);
        return false;
    } 
    
}

if(isset($_POST['Reject_AQAR'])){
    $msg = reject_asset_quantity_adjustment_request($asset_qty_ajt_req_no);  
    if($msg['success'] !== true){
        display_error($msg['message']);
        return false;
    } 
}

if(isset($_POST['Save_Asset_Quantity_Adjustment_Request_Header'])){
    
    $validate = can_process_asset_quantity_adjustment_request_header($asset_qty_ajt_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_quantity_adjustment_request_header($asset_qty_ajt_req_no, $validate["row_state"]);
    
    if($msg['success'] !== true){
        cancel_transaction();
        display_error($msg['message']);
    }else{
        commit_transaction();
        set_session_variable("ST_ASSET_QUANTITY_ADJUSTMENT_REQUEST_aqar_no", $msg['aqar_id']);
        $_POST['AQARNo'] = $msg['aqar_id'];
        $Mode = 'RESET';
        display_notification($msg['message']);
    }
}

if(isset($_POST['Add_Asset_Quantity_Adjustment_Request']) || isset($_POST['Update_Asset_Quantity_Adjustment_Request'])){    

    $cpaar_msg = can_process_asset_quantity_adjustment_request_header_line($asset_qty_ajt_req_no);   
    if($cpaar_msg["success"] !== true){   
        display_error($cpaar_msg['message']);
        return false;
    }
    
    begin_transaction();
        $msg = save_asset_quantity_adjustment_request($asset_qty_ajt_req_no, $selected_id, $cpaar_msg['row_state']);
        if($msg['success'] !== true){
            cancel_transaction();
            display_error($msg['message']);
        }else{
            commit_transaction();
            set_session_variable("ST_ASSET_QUANTITY_ADJUSTMENT_REQUEST_aqar_no", $msg['aqar_id']);
            $asset_qty_ajt_req_no = $msg['aqar_id'];
            hidden('AQARNo', $msg['aqar_id']);
            display_notification($msg['message']);
            $Mode = 'RESET';
        }
}

if($Mode == 'Delete'){
    $can_delete_msg = can_delete_asset_quantity_adjustment_request_details_line($asset_qty_ajt_req_no);
    
    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_quantity_adjustment_request_detail($selected_id);
        if($recs != 1){
            cancel_transaction();
            display_error(_("Could not delete Asset quantity adjustment request item #$selected_id"));
            return false;
        }
        
        $msg = update_asset_quantity_adjustment_request_header_total($asset_qty_ajt_req_no);
        if($msg['success'] !== true){
            display_error($msg["message"]);
            return false;
        }else{
            commit_transaction();
            display_notification(_("Asset quantity adjustment 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']);
    unset($_POST['new_line_cost']);
} 

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

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

global $Ajax;

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

start_form();

display_asset_quantity_adjustment_request_header($asset_qty_ajt_req_no);

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

    submit_center_first('Update_Release_AQAR', "Release for Approval", '', 'default');
    submit('Back_To_Originator_AQAR', 'Back To Originator', true, '', 'default');
    submit_center_last('Reject_AQAR', "Reject", '', 'default');	
}
 
//------------------------------------------------------------------------------------------------------------
 
br(2);

display_asset_quantity_adjustment_request_details($asset_qty_ajt_req_no,  (!isset($selected_id)) ? -1 : $selected_id);

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

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

$Ajax->activate('asset_id');

br();

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

?>