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

$page_security = 'SA_TOOL_REQUEST_INQ';
if (!@$_GET['popup'])
	$path_to_root = "../..";
else	
	$path_to_root = "..";
include_once($path_to_root . "/includes/db_pager.inc");
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 . "/assets/includes/db/tool_requests_inquiry_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");

$js = '';

if ($SysPrefs->use_popup_windows) {
	$js .= get_js_open_window(1000, 500);
}

if (user_use_date_picker()) {
	$js .= get_js_date_picker();
}

page($help_context = "Outstanding Tool Requests", false, false, "", $js);
//---------------------------------------------------------------------------------------------

simple_page_mode(true);

if (isset($_GET['order_no']))
{
    $_POST['order_no'] = $_GET['order_no'];
}



function id($row)
{
    return get_asset_trans_view_str(ST_TOOL_REQUEST, $row['id']);
}

function reference($row)
{
    return $row['reference'];     
}

function tran_date($row)
{
   return sql2date($row['date']);
}

function comment($row)
{
    return $row['comment'];     
}

function project_description($row)
{
    return $row['project_description'];   
}

function employee_name($row)
{
    $name = get_group_company_employee_formatted_long_name($row['company_code'], $row['employee_code']);
    return $name;    
}

function location_name($row)
{
    return $row['location_name'];
    return get_trans_view_str(ST_PETTY_CASH, $iou);      
}


function approval_status($row)
{
    global $approval_statuses;
    return $approval_statuses[$row["approval_status"]];
}

function edit_link($row) 
{

    $modify = "ARNo";   
        if($row['approval_status'] == AS_NEW || $row['approval_status'] == AS_WITH_THE_APPROVER || 
            $row['approval_status'] == AS_BACK_TO_ORIGINATOR || $row['approval_status'] == AS_AWAITING_APPROVAL){
                return pager_link(_("Edit"), "/assets/tool_requests.php?$modify=". $row['id'], ICON_EDIT);   
        }else{
            return null;    
        }    
   
}

function issue_link($row)
{  
    if($row['approval_status'] == AS_APPROVED || $row['approval_status'] == AS_APPROVED){
        return pager_link(_("Issue"),"/assets/tool_issues.php?ARNo=".$row['id'], ICON_DOC);  
    }
    
    return null;
		
}

function return_link($row)
{
    return pager_link(_("Return"),"/assets/asset_returns.php?ARNo=".$row['id'], ICON_GL);   
}
//--------------------------------------------------------------------------------------------------


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

if (isset($_POST['_selected_id_update']))
{
	$selected_id = $_POST['selected_id'];
	//$Ajax->activate('_page_body');
}

//if (list_updated('employee_code')) 
	$Ajax->activate('_page_body');
//--------------------------------------------------------------------------------------------------

if (!@$_GET['popup'])
	start_form();
start_table();
    start_row();
        ref_cells(text_bold("Tool Request Id:"), "tool_request_id");
        date_cells("From Date:", 'from_date', '', null, -60);
        date_cells("To Date:", 'to_date', '', null, +15);
        locations_list_cells("Location:", 'location', null, 'Select Location', true);       
    end_row();  
end_table();

start_table();
    start_row();
        project_list_cells(_("Project:"), 'project_id', null, _('Select a project'), true);    
        group_companies_list_cells("Company:", 'group_company_code', null, 'All companies', true);    
        group_company_employees_simple_list_cells(_("Employee:"), 'employee_code', $_POST['group_company_code'], null, "All employees", true);    
        
        submit_cells('SearchOrders', _("Search"),'',_('Search'), 'default');
            end_row();
            
            
end_table(1);

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

$tool_request_id = "";
if(isset($_POST['tool_request_id'])){
  $tool_request_id = $_POST['tool_request_id'];
}

$from_date = "";
if(isset($_POST['from_date'])){
  $from_date = $_POST['from_date'];
}

$to_date = "";
if(isset($_POST['to_date'])){
  $to_date = $_POST['to_date'];
}

$location = "";
if(isset($_POST['location'])){
  $location = $_POST['location'];
}

$project_id = "";
if(isset($_POST['project_id'])){
  $project_id = $_POST['project_id'];
}


$company_code = "";
if(isset($_POST['group_company_code'])){
  $company_code = $_POST['group_company_code'];
}

$employee_code = "";
if(isset($_POST['employee_code'])){
  $employee_code = $_POST['employee_code'];
}


$sql = get_sql_for_outstanding_tool_request_inquiry($tool_request_id, $from_date, $to_date, $location,
                                        $project_id, $company_code, $employee_code);    

$cols = array(
            _("ID")=> array('fun'=>'id'),
            _("Reference")=> array('fun'=>'reference'),                         
            _("Project Name")=>array('fun'=>'project_description'),                
            _("Employee Name")=>array('fun'=>'employee_name'), 
            _("Location")=>array('fun'=>'location_name'), 
            _("Date")=> array('fun'=>'tran_date'),     
            _("Comment")=> array('fun'=>'comment'),
            _("Status")=> array('fun'=>'approval_status')
                        
             
);
    array_append($cols, array(
		array('insert'=>true, 'fun'=>'edit_link'),
		array('insert'=>true, 'fun'=>'issue_link')));

//------------------------------------------------------------------------------------------------      
//if (get_post('StockLocation') != $all_items) {
//	$cols[_("Location")] = 'skip';
//}
//---------------------------------------------------------------------------------------------------

$table =& new_db_pager('orders_tbl', $sql, $cols, null, null);

$table->width = "95%";

display_db_pager($table);

br();

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


?>
