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

$page_security = 'SA_TOOL_RETURN';
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 . "/reporting/includes/reporting.inc");
include_once($path_to_root . "/projects/includes/db/project_boq_items_db.inc"); 
include_once($path_to_root . "/includes/common_functions.inc");
include_once($path_to_root . "/taxes/db/tax_groups_db.inc");
include_once($path_to_root . "/assets/includes/db/tool_returns_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/tool_usage_entry_db.inc");
include_once($path_to_root . "/assets/includes/db/assets_db.inc");
include_once($path_to_root . "/assets/includes/ui/tool_usage_entry_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 = "Tool Usage Entry"));

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

$ai_no = -1;
 
if (isset($_GET['AINo']))
{
    $_POST['AINo'] = $_GET['AINo'];
    set_session_variable("ST_TOOL_RETURN_ai_no", $_POST['AINo']);
}

if (isset($_GET['NewOrder']))
{
    $_POST['AINo'] = -1;
    set_session_variable("ST_TOOL_RETURN_ai_no", $_POST['AINo']);
}        
        
if (isset($_POST['AINo']) && $_POST['AINo'] > 0){
    $ai_no = $_POST['AINo'];
}
else{
    $ai_no = get_session_variable("ST_TOOL_RETURN_ai_no");    
}

if (isset($_GET['Added_AI_Id'])) 
{
    //return;
        
    $trans_no = $_GET['Added_AI_Id'];
    $trans_type = ST_TOOL_RETURN;
    $message = "Tool return $trans_no has been created.";
    
	display_notification_centered($message);
        
	display_note(print_document_link($trans_no, _("&Print this tool return"), true, $trans_type), 0, 0);

	hyperlink_no_params("$path_to_root/assets/inquiry/tool_issues_inquiry.php", _("Create another &tool return"));

//	hyperlink_params("$path_to_root/admin/attachments.php", _("Add an Attachment"), "filterType=$trans_type&trans_no=$trans_no");

	display_footer_exit();
}


if(isset($_POST['Add_Asset_Return'])){      
       
    $cppd_msg = can_process_ar($ai_no);
    if($cppd_msg['success'] === true){
        begin_transaction();
        $save_ar_msg = save_ar($ai_no);
        if($save_ar_msg['success'] !== true){
            cancel_transaction();
        }else{
            commit_transaction();
            $Mode = 'RESET';
            clear_session_variable("ST_TOOL_RETURN_ai_no");
            meta_forward($_SERVER['PHP_SELF'], "Added_AI_Id=$ai_no");   
        }
    }
    else{
        display_error($cppd_msg['message']);
    }

}

if (isset($_POST['cancel'])){
    $Mode = 'RESET';
    $selected_id = -1;
    unset ($_POST);
}  

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

//if ($Mode == 'Delete')
//{
//        $recs = delete_project_dispatch_request_item($selected_id);
//               
//        if($recs == 1){
//                display_notification(_("Project dispatch request item has been deleted."));
//        }
//        else{
//                display_error(_("Project dispatch request item could not be deleted."));
//        }
//	$Mode = 'RESET';
//}

if ($Mode == 'RESET')
{
	$selected_id = -1;
}
    $Ajax->activate('_page_body');
    $Ajax->activate('pd_items');
//--------------------------------------------------------------------------------------------------------------

start_form();

display_tool_usage_entry_header($ai_no);   

br(2);

//------------------------------------------------------------------------------------------------------------
         
div_start('pd_items');
display_tool_usage_entry_details($ai_no);
 
div_end();

end_table(1);
//-----------------------------------------------------------------------------------------------------------
 
submit_center_first('Add_Asset_Return', _("Process"), '', 'default');
submit_center_last('cancel', _("Cancel"), _("Cancel Edition"), 'cancel');

br();

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


?>
