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

$page_security = 'SA_TOOL_ISSUE_INQ';
$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 . "/sales/includes/sales_ui.inc");
include_once($path_to_root . "/reporting/includes/reporting.inc");
//include_once($path_to_root . "/assets/includes/db/vehicle_requests_db.inc");
include_once($path_to_root . "/assets/includes/db/tool_issues_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(800, 500);
if (user_use_date_picker())
	$js .= get_js_date_picker();
page(_($help_context = "Tool Issues Inquiry"), false, false, "", $js);

if (isset($_GET['Approves']))
{
    $_SESSION['current_user_for_mr'] = $_SESSION['wa_current_user']->user;
}


function asset_issue_id($row)
{  
    return get_asset_trans_view_str(ST_TOOL_ISSUE, $row['asset_issue_id']);
}

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

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

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


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

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

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

function return_link($row)
{
    if($row['fully_returned']){
        return null;
    }
    
    $modify = "AINo";
        return pager_link( _("Return"), "/assets/tool_returns.php?$modify=".$row['asset_issue_id'], ICON_RECEIVE);    
}




    $Ajax->activate('orders_tbl');



if (!@$_GET['popup'])
	start_form();

start_table();
start_row();
    ref_cells(text_bold("Tool Issue Id:"), "asset_issue_id");
    date_cells(_("From Date:"), 'from_date', '', null, -60, null, null, null, true);
    date_cells(_("To Date:"), 'to_date', '', null, +15, null, null, null, true);
    asset_locations_list_cells(_("Issuing Location:"), 'issuing_location', null, 'All issuing Locations', true);
//    asset_list_cells("Asset:", 'asset_id', null, 'Select an asset', true, null);
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);
//    group_company_employees_simple_list_cells($label, $name, $company_code, $selected_id, $all_option, $submit_on_change);
 
    submit_cells('SearchOrders', _("Search"),'',_('Select documents'), 'default');

end_row();


end_table(1);

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

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

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

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

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

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

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

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

  $sql = get_sql_for_asset_issue_inquiry($project_id, $asset_issue_id, $employee_code,  
                                            $issuing_location, $from_date, $to_date, $group_company_code);    
    
    $cols = array(
                _("ID")=> array('fun'=>'asset_issue_id', 'align'=>'right'),
                _("Reference"),
                _("Project")=> array('fun'=>'project_description'),
                _("Date")=> array('fun'=>'tran_date'),
                _("Employee Name")=> array('fun'=>'employee_code'),               
                _("Location")=>array('fun'=>'location_name'),             
                _("Comment")=> array('fun'=>'comment')

            );
    
    array_append($cols, array(
        array('insert'=>true, 'fun'=>'return_link')       
    ));


$table =& new_db_pager('orders_tbl', $sql, $cols);
//$table->set_marker('check_overdue', _("Marked items are overdue."));

$table->width = "95%";

display_db_pager($table);
submit_center('Update', _("Update"), true, '', null);

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



?>
