<?php
/**********************************************************************
***********************************************************************/
$page_security = 'SA_VEHICLE_RUNNING_CHART_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 . "/transport/includes/db/vehicle_running_chart_entry_db.inc");
include_once($path_to_root . "/transport/includes/db/vehicle_running_chart_inquiry_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 = "Vehicle Running Chart Inquiry"), false, false, "", $js);

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

if (isset($_GET['project_id']))
{
	$_POST['project_id'] = $_GET['project_id'];
}
if (isset($_GET['from_date']))
{
	$_POST['required_by_from'] = $_GET['from_date'];
}
if (isset($_GET['to_date']))
{
	$_POST['required_by_to'] = $_GET['to_date'];
}


function view_allocation($row)
{
//	global $trans_type;
    if($row['vehicle_allocation_id'] == 0){
        return null;
    }
    
	return get_transport_trans_view_str(ST_VEHICLE_ALLOCATION, $row['vehicle_allocation_id']);
}

function view_vrc($row)
{
	return get_transport_trans_view_str(ST_VEHICLE_RUNNING_CHART, $row['id']);
}

function view_vrc_batch($row)
{
    if($row['batch_no'] == 0){
        return null;
    }
	return get_transport_trans_view_str(ST_VEHICLE_RUNNING_CHART_BATCH, $row['batch_no']);
}


function convert_from_date($row)
{
  return substr($row['start_date_time'], 0,16);
}

function convert_to_date($row)
{
  return substr($row['end_date_time'],0,16);
}

function format_amount($row)
{
  return number_format($row['cost'],2);
}

function format_batch_no($row)
{
    if($row['batch_no'] > 0){
        return $row['batch_no'];
    }
    else{
        return null;
    }
}

if (get_post('SearchOrders')) 
{
	$Ajax->activate('orders_tbl');
}


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

start_table(TABLESTYLE_NOBORDER);
start_row();

ref_cells(_("VRC #:"), 'vrc_id', '',null, '', false);
date_cells(_("From:"), 'required_by_from', '', null, -15);
date_cells(_("To:"), 'required_by_to', '', null, +15);
ref_cells(_("Batch #:"), 'batch_no', '',null, '', false);

//ref_cells(_("MR Ref"), 'mr_reference', '',null, '', false);

end_row();
end_table();
//
start_table(TABLESTYLE_NOBORDER);
start_row();

//stock_items_list_cells(_("Item:"), 'SelectStockFromList', null, true, false);

//yesno_list_cells("Completed :", 'mr_complete');

vehicles_list_cells(_("Vehicle:"), 'vehicle_id', null, '');
project_list_cells("Project :", 'project_id', null, true, false);
supplier_list_cells("Supplier:", 'supplier_id', null, '');
submit_cells('SearchOrders', _("Search"),'',_('Select documents'), 'default');
//hidden('order_view_mode', $_POST['order_view_mode']);
//hidden('type', $trans_type);

end_row();

end_table(1);
//---------------------------------------------------------------------------------------------
//	Orders inquiry table
//
$sql = get_sql_for_vehicle_running_chart_inquiry(@$_POST['required_by_from'], @$_POST['required_by_to'],
            @$_POST['project_id'], @$_POST['vrc_id'],@$_POST['vehicle_id'], @$_POST['supplier_id'], @$_POST['batch_no']);


	$cols = array(
                _("VRC #") => array('fun' => 'view_vrc'),   
                _("VA #") => array('fun' => 'view_allocation'),   
                _("Project"), 
                _("Budget"),
                _("Vehicle"),
                _("Plate No"),
                _("From Date")=> array('fun' => 'convert_from_date'), 
                _("To Date")=> array('fun' => 'convert_to_date'), 
                _("Meter Start")=> array('align' => 'right'), 
                _("Meter End")=> array('align' => 'right'), 
                _("Cost")=> array('fun' => 'format_amount', 'align' => 'right'), 
                _("Route"),
                _("Supplier") ,
                _("Batch")=> array('fun' => 'view_vrc_batch', 'align' => 'center'), 
		);

//	array_append($cols, array(
//        array('insert'=>true, 'fun'=>'edit_link'),
//        array('insert'=>true, 'fun'=>'order_link', 'align' => 'center'), 
//		array('insert'=>true, 'fun'=>'dispatch_link', 'align' => 'center'), 
//        array('insert'=>true, 'fun'=>'return_link', 'align' => 'center'), 
//		));


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

$table->width = "90%";

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

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