<?php
/**********************************************************************
***********************************************************************/
$page_security = 'SA_CREATE_VEHICLE_ALLOCATIONS';
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 . "/transport/includes/db/vehicle_allocation_db.inc");
include_once($path_to_root . "/transport/includes/db/vehicle_requests_db.inc");
include_once($path_to_root . "/transport/includes/db/vehicle_packages_db.inc");
//include_once($path_to_root . "/transport/includes/db/approve_i_owe_you_enties_db.inc");
include_once($path_to_root . "/projects/includes/db/projects_db.inc");
include_once($path_to_root . "/includes/project_cost_functions.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 = "Allocate Vehicles"), false, false, "", $js);
//------------------------------------------------------------------------------------------
simple_page_mode(true);

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

function can_process_va($data)
{	

    if (!($data['project_id'] > 0))
    {
        $message = "Please select a project.";
        $set_focus = 'project_id';
        return array("success" => false, "focus_to" => $set_focus, "message" => $message);
    }
    
//    $project_id = $data['project_id'];    
//    $user_id = $_SESSION['wa_current_user']->user;
//    $cap = can_access_project($project_id, $user_id);
//    if($cap['success'] !== true) 
//    {
//        $message = $cap['message'];
//        $set_focus = 'project_id';
//        return array("success" => false, "focus_to" => $set_focus, "message" => $message);
//    }
    
    if (!($data['task_id'] > 0))
    {
        $message = "Please select a task.";
        $set_focus = 'task_id';
        return array("success" => false, "focus_to" => $set_focus, "message" => $message);
    }

    if (!($data['package_id'] > 0))
    {
        $message = "Please select a vehicle package.";
        $set_focus = 'package_id';
        return array("success" => false, "focus_to" => $set_focus, "message" => $message);
    }
    
    if (!($data['vehicle_id'] > 0))
    {
        $message = "Please select a vehicle.";
        $set_focus = 'vehicle_id';
        return array("success" => false, "focus_to" => $set_focus, "message" => $message);
    }
        
//    if (!check_num('amount',0))
//   	{
//        $message = "Amount entered is not numeric or negative.";
//        $set_focus = 'amount';
//        return array("success" => false, "focus_to" => $set_focus, "message" => $message);
//   	}

    if (!is_date($data['from_date']))
   	{
        $message = "From Date entered is not a valid date.";
        $set_focus = 'from_date';
        return array("success" => false, "focus_to" => $set_focus, "message" => $message);
   	}
        
    if (!is_date($data['to_date']))
   	{
        $message = "To Date entered is not a valid date.";
        $set_focus = 'to_date';
        return array("success" => false, "focus_to" => $set_focus, "message" => $message);
   	}
        
    if (date1_greater_date2($data['from_date'], $data['to_date']))
    {
        $message = ( _("From date can not be greater than to date"));
        $set_focus = ('to_date');
        return array("success" => false, "focus_to" => $set_focus, "message" => $message);            
    }       
        
    if(!is_time($data['from_time'])){
        $message = ( _("From time is not in valid time format"));
        $set_focus = ('from_time');
        return array("success" => false, "focus_to" => $set_focus, "message" => $message);
    }
    
    if(!is_time($data['to_time'])){
        $message = ( _("To time is not in valid time format"));
        $set_focus = ('to_time');
        return array("success" => false, "focus_to" => $set_focus, "message" => $message);
    }        
    
    if($data['from_time'] > $data['to_time'])
    {
        $message = ( _("From time can not be greater than To time"));
        $set_focus = ('from_time');
        return array("success" => false, "focus_to" => $set_focus, "message" => $message);            
    }  
    
    return array("success" => true, "message" => "");    
}

function create_va($data)
{
    $cd = can_process_va($data);
    if($cd["success"] !== true) 
    {          
        return array("success" => false, "message" => $cd["message"]);
    }
    
    $from_date_time['from_date_time'] = date2sql($data['from_date'])." ".$data['from_time'];
    $to_date_time['to_date_time'] = date2sql($data['to_date'])." ".$data['to_time'];
    
    if(isset($data['vehicle_request_id']) && $data['vehicle_request_id'] > 0){
        $vehicle_request_id = $data['vehicle_request_id'];
    }
    else{
        $vehicle_request_id = 0;
    }
    
    if(isset($data['driver_id']) && $data['driver_id'] > 0){
        $driver_id = $data['driver_id'];
    }
    else{
        $driver_id = 0;
    }
    
    $package_cost = get_vehicle_package_cost($data['package_id']);
    
    $rec_id = add_vehicle_allocation($vehicle_request_id, $data['project_id'],  $data['task_id'],  $data['package_id'], 
                                     $data['vehicle_id'], $driver_id, $package_cost, $from_date_time['from_date_time'],                               
                                     $to_date_time['to_date_time'], $data['passengers'], 
                                    $data['route'], $data['comment']);
    if($rec_id > 0){
        return array("success" => true, "trans_no" => $rec_id, "message" => "Vehicle allocation $rec_id has been added.");
    }
    else{
        return array("success" => false, "message" => "Vehicle allocation could not be added.");
    }
}

function update_va($data)
{
    $cd = can_process_va($data);
    if($cd["success"] !== true) 
    {
        return array("success" => false, "message" => $cd["message"]);
    }
    
    $va_id = $data['vehicle_allocation_id'];
    
    $from_date_time['from_date_time'] = date2sql($data['from_date'])." ".$data['from_time'];
    $to_date_time['to_date_time'] = date2sql($data['to_date'])." ".$data['to_time'];
    
    if(isset($data['vehicle_request_id']) && $data['vehicle_request_id'] > 0){
        $vehicle_request_id = $data['vehicle_request_id'];
    }
    else{
        $vehicle_request_id = 0;
    }
    
    if(isset($data['driver_id']) && $data['driver_id'] > 0){
        $driver_id = $data['driver_id'];
    }
    else{
        $driver_id = 0;
    }
    
    $package_cost = get_vehicle_package_cost($_POST['package_id']);
    
    $recs = update_vehicle_allocation($va_id, $vehicle_request_id, $data['project_id'],  
                                        $data['task_id'],  $data['package_id'], 
                                        $data['vehicle_id'], $driver_id, $package_cost, $from_date_time['from_date_time'],                               
                                        $to_date_time['to_date_time'], $data['passengers'], 
                                        $data['route'], $data['comment']);
     
    
    if($recs == 1){
        return array("success" => true, "trans_no" => $va_id, "message" => "Vehicle allocation $va_id has been updated.");
    }
    else{
        return array("success" => false, "message" => "Vehicle allocation $va_id could not be updated.");
    }
}

function add_va_and_close_vr(&$Mode){

    $line = build_va_line_array(-1);
    $iou_msg = create_va($line);
    if($iou_msg["success"] !== true){
        if(isset($iou_msg["focus_to"])){
            set_focus($iou_msg["focus_to"]);
        }
        display_error($iou_msg["message"]);
    }
    else{
        $va_id = $iou_msg["trans_no"];
    }    

    $Mode = 'RESET';
    
    $current_user_id = $_SESSION['wa_current_user']->user;
    $msg = close_vehicle_request($line, $current_user_id);

    if($msg["success"] !== true){
        return array("success" => true, "trans_no" => $va_id, "message" => $iou_msg["message"]);
    }
    else{
        $message = "Vehicle allocation $va_id has been created and request ".get_post('vehicle_request_id')." has been closed.";
        return array("success" => false, "message" => $message);
    }
      
}

function update_va_and_close_vr($line){

    $va_id = $line['vehicle_allocation_id'];
    
    $msg = update_va($line);
    if($msg["success"] !== true){
        if(isset($msg["focus_to"])){
            set_focus($msg["focus_to"]);
        }
        display_error($msg["message"]);
        return;
    }

    $current_user_id = $_SESSION['wa_current_user']->user;
    $msg = close_vehicle_request($line, $current_user_id);

    if($msg["success"] !== true){
        return array("success" => true, "trans_no" => $va_id, "message" => $msg["message"]);
    }
    else{
        $message = "Vehicle allocation $va_id has been updated and request ".$line['vehicle_request_id']." has been closed.";
        return array("success" => false, "message" => $message);
    }
}

function build_vr_array()
{
    $data = array();
    
    $result = get_open_vehicle_requests($_POST['filtering_from_date'], $_POST['filtering_to_date'], $_POST['project_id'], $_POST['originator_id']);   

    while ($myrow = db_fetch($result))
    {
        $line = array();
        
        $prefix = "vr_".$myrow["id"]."_";
        
        if(!(isset($_POST[$prefix.'check_vr_item']) && $_POST[$prefix.'check_vr_item'] > 0)){
            continue;
        }
        
        
        $line['vehicle_request_id'] = $myrow["id"];
        $line['from_date'] = $_POST[$prefix.'from_date'];
        $line['from_time'] = $_POST[$prefix.'from_time'];
        $line['to_date'] = $_POST[$prefix.'to_date'];
        $line['to_time'] = $_POST[$prefix.'to_time'];
        $line['project_id'] = $myrow['project_id'];
        $line['task_id'] = $myrow['task_id'];
        $line['package_id'] = $_POST[$prefix.'package_id'];
        $line['vehicle_id'] = $_POST[$prefix.'vehicle_id'];
        $line['driver_id'] = $_POST[$prefix.'driver_id'];
        $line['passengers'] = $myrow['passengers'];
        $line['route'] = $myrow['route'];
        $line['comment'] = $myrow['comment'];
        
        $data[] = $line;
    }
    
    return $data;
}

function build_va_array()
{
    $data = array();
    
    $result = get_open_vehicle_allocations();   

    while ($myrow = db_fetch($result))
    {
        $line = array();
        
        $prefix = "va_".$myrow["id"]."_";
        
        if(!(isset($_POST[$prefix.'check_va_item']) && $_POST[$prefix.'check_va_item'] > 0)){
            continue;
        }
        
        $line['vehicle_allocation_id'] = $myrow["id"];
        $line['vehicle_request_id'] = $myrow["vehicle_request_id"];
        $line['from_date'] = $_POST[$prefix.'from_date'];
        $line['from_time'] = $_POST[$prefix.'from_time'];
        $line['to_date'] = $_POST[$prefix.'to_date'];
        $line['to_time'] = $_POST[$prefix.'to_time'];
        $line['project_id'] = $myrow['project_id'];
        $line['task_id'] = $myrow['task_id'];
        $line['package_id'] = $_POST[$prefix.'package_id'];
        $line['vehicle_id'] = $_POST[$prefix.'vehicle_id'];
        $line['driver_id'] = $_POST[$prefix.'driver_id'];
        $line['passengers'] = $myrow['passengers'];
        $line['route'] = $myrow['route'];
        $line['comment'] = $myrow['comment'];
        
        $data[] = $line;
    }
    
    return $data;
}

function build_va_line_array($va_id)
{
    $line = array();

    if($va_id > 0){
        
    }
    else{
        $va_id = 0;
    }
    $line['vehicle_request_id'] = $_POST["vehicle_request_id"];
    $line['vehicle_allocation_id'] = $va_id;
    $line['from_date'] = $_POST['from_date'];
    $line['from_time'] = $_POST['from_time'];
    $line['to_date'] = $_POST['to_date'];
    $line['to_time'] = $_POST['to_time'];
    $line['project_id'] = $_POST['project_id'];
    $line['task_id'] = $_POST['task_id'];
    $line['package_id'] = $_POST['package_id'];
    $line['vehicle_id'] = $_POST['vehicle_id'];
    $line['driver_id'] = $_POST['driver_id'];
    $line['passengers'] = $_POST['passengers'];
    $line['route'] = $_POST['route'];
    $line['comment'] = $_POST['comment'];

    return $line;
}

function add_va_multiple($close = false)
{
    $count = 0;
    
    $data = build_vr_array();

    foreach ($data as $line) {        
        $cva = create_va($line);
        if($cva['success'] !== true){
            $set_focus = "";
            if(isset($cva['set_focus'])){
                $set_focus = $cva['set_focus'];
            }
            return array("success" => false, "set_focus" => $set_focus, "message" => "Vehicle Request ".$line["vehicle_request_id"]." - ".$cva['message']);
        }
        
        unset($_POST["vr_".$line["vehicle_request_id"]."_check_vr_item"]);
        $count++;
        
        if($close){
            $current_user_id = $_SESSION['wa_current_user']->user;
            $msg = close_vehicle_request($line, $current_user_id);
            if($msg['success'] !== true){
                return $msg;
            }
            
        }
    }
    
    if($count == 0){
        return array("success" => false, "set_focus" => "", "message" => "No Vehicle Allocations created.");
    }
    else{
        return array("success" => true, "set_focus" => "", "message" => "$count Vehicle Allocations created.");
    }
}

function close_vr_multiple()
{
    $count = 0;
    
    $data = build_vr_array();

    foreach ($data as $line) {        

        $count++;

        $current_user_id = $_SESSION['wa_current_user']->user;
        $msg = close_vehicle_request($line, $current_user_id);
        if($msg['success'] !== true){
            return $msg;
        }
        
    }
    
    if($count == 0){
        return array("success" => false, "set_focus" => "", "message" => "No Vehicle Requests closed.");
    }
    else{
        return array("success" => true, "set_focus" => "", "message" => "$count Vehicle Requests closed.");
    }
}


function update_va_multiple($close = false)
{
    $count = 0;
    
    $data = build_va_array();

    foreach ($data as $line) {        
        $cva = update_va($line);
        if($cva['success'] !== true){
            $set_focus = "";
            if(isset($cva['set_focus'])){
                $set_focus = $cva['set_focus'];
            }
            return array("success" => false, "set_focus" => $set_focus, "message" => "Vehicle Allocation ".$line["vehicle_allocation_id"]." - ".$cva['message']);
        }
        
        unset($_POST["va_".$line["vehicle_allocation_id"]."_check_va_item"]);
        
        $count++;
        
        if($close){
            $current_user_id = $_SESSION['wa_current_user']->user;
            close_vehicle_request($line, $current_user_id);
        }
    }
    
    if($count == 0){
        return array("success" => false, "set_focus" => "", "message" => "No Vehicle Allocations updated.");
    }
    else{
        return array("success" => true, "set_focus" => "", "message" => "$count Vehicle Allocations updated.");
    }
}


function display_open_vehicle_requests($filtering_from_date, $filtering_to_date, $project_id, $originator_id)
{
    
    display_heading2("Open Vehicle Requests");
    
    $result = get_open_vehicle_requests($filtering_from_date, $filtering_to_date, $project_id, $originator_id);   


    start_table(TABLESTYLE, "width=90%");
    $th = array("VR Id", _(''),_(''),_("Vehicle"), "Driver", _('From Date'), 'From Time',
                'To Date', 'To Time',  "");

    table_header($th);
    $k = 0; //row colour counter

    while ($myrow = db_fetch($result))
    {
        $prefix = "vr_".$myrow["id"]."_";
        
        alt_table_row_color($k);
        $s = get_trans_view_str(ST_VEHICLE_REQUEST, $myrow['id'],$myrow['id']);
        label_cell($s, "align=center");

//        label_cell("");
        $text = "Project: <b>".$myrow["project_description"]." </b>".spaces(10);
        $text .="Budget: <b>".$myrow["task_description"]."</b>".spaces(10);

        $text .="Route: <b>".$myrow["route"]."</b>".spaces(10);
        $text .="Passenagers: <b>".$myrow["passengers"]."</b>".spaces(10);
        $text .="Cost: <b>".$myrow["cost"]."</b>".spaces(10);
        $text .="Comment: <b>".$myrow["comment"]."</b>".spaces(10);
        label_cell($text, "colspan=09");
//        label_cell("");
        
        end_row();
            
//        $k--;
        alt_table_row_color($k);
        
//        label_cell($myrow["project_description"]);
//        label_cell($myrow["route"]);   
//        label_cell($myrow["package_description"]);
        
        label_cell("");
        
        label_cell("Vehicle Type: <b>".$myrow["vehicle_type_description"]."</b>");
        
        $_POST[$prefix.'package_id'] = $myrow['package_id'];
//        vehicle_packages_list_cells(null, $prefix.'package_id');
        label_cell("Package:"."<b>".$myrow["package_description"]."</b>");
        hidden($prefix.'package_id', $_POST[$prefix.'package_id']);
        

        $_POST[$prefix.'vehicle_id'] = $myrow['vehicle_id'];
        vehicles_list_cells(null, $prefix.'vehicle_id', null, '');
        
//        $_POST[$prefix.'driver_id'] = $myrow['driver_id'];
        drivers_list_cells(null, $prefix.'driver_id', null, '');
        
        $_POST[$prefix.'from_date'] = sql2date($myrow['from_date_time']);
        date_cells(null, $prefix.'from_date');
        
        $_POST[$prefix.'from_time'] = substr($myrow['from_date_time'], 11, 5);
        time_cells(null, $prefix.'from_time', null, 5);
        
        $_POST[$prefix.'to_date'] = sql2date($myrow['to_date_time']);
        date_cells(null, $prefix.'to_date');
        
        $_POST[$prefix.'to_time'] = substr($myrow['to_date_time'], 11, 5);
        time_cells(null, $prefix.'to_time', null, 5);

//        label_cell($myrow["comment"]);
        check_cells(null, $prefix.'check_vr_item');
//        edit_button_cell("Edit"."vr_".$myrow["id"], _("Edit"));    
//        delete_button_cell("Delete".$myrow["id"], _("Delete"));

        end_row();
        
        start_row();
        label_cell("");
        label_cell("-");
        label_cell("", "colspan=6 align=center");
        label_cell("-", "align=right");
        label_cell("");

        end_row();
    }
    //inactive_control_row($th);
    end_table(1);
}


//function display_open_vehicle_allocations()
//{
//    $result = get_open_vehicle_allocations();   
//
//    display_heading2("Open Vehicle Allocations");
//    
//    start_table(TABLESTYLE, "width=95%");
//    $th = array(_('VA Id'),"VR Id", _(''),_("Vehicle"), "Driver", _('From Date'),_('From Time'),
//                _('To Date'),_('To Time'), "", "", "");
//
//    table_header($th);
//    $k = 0; //row colour counter
//
//    while ($myrow = db_fetch($result))
//    {
//        $prefix = "va_".$myrow["id"]."_";
//        
//        alt_table_row_color($k);
//
//        $a = get_trans_view_str(ST_VEHICLE_ALLOCATION, $myrow['id'],$myrow['id']);
//        label_cell($a, "align=center");
//        
//        if($myrow["vehicle_request_id"] > 0){
//            $r = get_trans_view_str(ST_VEHICLE_REQUEST, $myrow['vehicle_request_id'],$myrow['vehicle_request_id']);
//            label_cell($r, "align=center");
////            label_cell($myrow["vehicle_request_id"], "align=center");
//        }
//        else{
//            label_cell("");
//        }
//        
////        label_cell("");
//        $text = "Project: <b>".$myrow["project_description"]."</b>".spaces(10);
//        $text .="Budget: <b>".$myrow["task_description"]."</b>".spaces(10);
//        
//        $text .="Route: <b>".$myrow["route"]."</b>".spaces(10);
//        $text .="Passenagers: <b>".$myrow["passengers"]."</b>".spaces(10);
//        $text .="Cost: <b>".$myrow["cost"]."</b>".spaces(10);
//        $text .="Comment: <b>".$myrow["comment"]."</b>".spaces(10);
//        label_cell($text, "colspan=09");
//        label_cell("");
//        
//        end_row();
//            
////        $k--;
//        alt_table_row_color($k);
//        
//        label_cell('');
//        label_cell('');   
//        
//        $_POST[$prefix.'package_id'] = $myrow['package_id'];
////        vehicle_packages_list_cells(null, $prefix.'package_id');
//        label_cell("Package: <b>".$myrow["package_description"]."</b>");
//        hidden($prefix.'package_id', $_POST[$prefix.'package_id']);
//        
//        $_POST[$prefix.'vehicle_id'] = $myrow['vehicle_id'];
//        vehicles_list_cells(null, $prefix.'vehicle_id', null, '');
//        
//        $_POST[$prefix.'driver_id'] = $myrow['driver_id'];
//        drivers_list_cells(null, $prefix.'driver_id', null, '');
//        
//        $_POST[$prefix.'from_date'] = sql2date($myrow['from_date_time']);
//        date_cells(null, $prefix.'from_date');
//        
//        $_POST[$prefix.'from_time'] = substr($myrow['from_date_time'], 11, 5);
//        time_cells(null, $prefix.'from_time', null, 5);
//        
//        $_POST[$prefix.'to_date'] = sql2date($myrow['to_date_time']);
//        date_cells(null, $prefix.'to_date');
//        
//        $_POST[$prefix.'to_time'] = substr($myrow['to_date_time'], 11, 5);
//        time_cells(null, $prefix.'to_time', null, 5);
//        
//        check_cells(null, $prefix.'check_va_item');
//
//        edit_button_cell("Edit".$myrow["id"], _("Edit"));    
//        delete_button_cell("Delete".$myrow["id"], _("Delete"));
//        
//        
//
//                end_row();
//        
//        start_row();
//        label_cell("");
//        label_cell("");
//        label_cell("-");
//        label_cell("", "colspan=5 align=center");
//        label_cell("-", "align=right");
//        label_cell("");
//        label_cell("");
//
//        end_row();
//    }
//    //inactive_control_row($th);
//    end_table(1);
//}

function display_va_edit_controls($Mode, $selected_id)
{
    if(!isset($_POST['amount'])){
    $_POST['amount'] = 0;
    }
    if(!isset($_POST['tran_date'])){
        $_POST['tran_date'] = '';
    }
    if(!isset($_POST['comments'])){
        $_POST['comments'] = '';
    }

    if(list_updated('vehicle_request_id') && $_POST['vehicle_request_id'] > 0){
        $vehicle_request = get_vehicle_request($_POST['vehicle_request_id']);
        $_POST['project_id'] = $vehicle_request["project_id"];
        $_POST['task_id'] = $vehicle_request["task_id"];
    }
    elseif(!isset($_POST['project_id']) && isset($_POST['vehicle_request_id']) && $_POST['vehicle_request_id'] > 0){
        $vehicle_request = get_vehicle_request($_POST['vehicle_request_id']);
        $_POST['project_id'] = $vehicle_request["project_id"];
        $_POST['task_id'] = $vehicle_request["task_id"];
    }

    $dec2 = 6;
    if ($Mode =='Edit')   
    {
        $myrow = get_vehicle_allocation($selected_id);

        $_POST['id'] = $myrow["id"];
        $_POST['vehicle_request_id'] = $myrow["vehicle_request_id"];

    //    if(!isset($_POST['project_id'])){
            $_POST['project_id'] = $myrow["project_id"];
    //    }
    //    if(!isset($_POST['task_id'])){
            $_POST['task_id'] = $myrow["task_id"];
    //    }

        $_POST['package_id'] = $myrow["package_id"];
        $_POST['vehicle_id'] = $myrow["vehicle_id"];   
        $_POST['driver_id'] = $myrow["driver_id"]; 
        $_POST['amount'] = $myrow["amount"];
        $_POST['from_date'] = sql2date($myrow["from_date_time"]);  
        $_POST['from_time'] = sql2time($myrow["from_date_time"]);
        $_POST['to_date'] = sql2date($myrow["to_date_time"]);  
        $_POST['to_time'] = sql2time($myrow["to_date_time"]);
        $_POST['passengers'] = $myrow["passengers"];
        $_POST['route'] = $myrow["route"];
        $_POST['comment'] = $myrow["comment"];

        //$selected_id = $_POST['ModifyMRNumber']; 

    }

    br();
//hidden('selected_id', $selected_id);

    start_table(TABLESTYLE2);

        vehicle_requests_list_row("Vehicle Request", "vehicle_request_id", null, "Vehicle allocation without a request", true);
        project_list_row(_("Project:"), 'project_id', null, _('Select a project'), true);    
        tasks_list_row(_("Budget:"), 'task_id', $_POST['project_id'], null, _('Select a Budget')); 
        vehicle_packages_list_row("Package", 'package_id', null, ('Select a package'));
//        asset_list_row(_("Vehicle:"), 'vehicle_id', null, 'Select a vehicle', false, AT_VEHICLE);
        vehicles_list_row("Vehicle:", 'vehicle_id', null, 'Select a vehicle');
        drivers_list_row("Driver:", 'driver_id', null, "Select a driver");
    //    $package_cost = get_vehicle_package_cost($_POST['package_id']);
    //    label_row("Amount:", number_format($package_cost,2));  
        date_row("From Date:", "from_date");
        time_row("From Time", 'from_time', null, 5);
        date_row("To Date:", "to_date");
        time_row("To Time", 'to_time', null, 5);
        text_row("Passengers:", 'passengers', null, 50, 200);
        text_row("Route:", 'route', null, 50, 500);
        textarea_row('Comments:', 'comment', null, 35, 2); 

        hidden('selected_id', $selected_id );

    end_table(1);
}
//--------------------------------------------------------------------------------------------------


if(isset($_POST['Add_VA_Multiple'])){
    $aiou = add_va_multiple();
    if($aiou["success"] !== true){
        if(isset($aiou["focus_to"])){
            set_focus($aiou["focus_to"]);
        }
        display_error($aiou["message"]);
    }
    else{
        $Mode = 'RESET';
        display_notification($aiou["message"]);
    }

}   

if(isset($_POST['Add_VA_Multiple_Close'])){
    $aiou = add_va_multiple(true);
    if($aiou["success"] !== true){
        if(isset($aiou["focus_to"])){
            set_focus($aiou["focus_to"]);
        }
        display_error($aiou["message"]);
    }
    else{
        $Mode = 'RESET';
        display_notification($aiou["message"]);
    }

} 

if(isset($_POST['Close_VR_Multiple'])){
    $aiou = close_vr_multiple();
    if($aiou["success"] !== true){
        if(isset($aiou["focus_to"])){
            set_focus($aiou["focus_to"]);
        }
        display_error($aiou["message"]);
    }
    else{
        $Mode = 'RESET';
        display_notification($aiou["message"]);
    }

} 


if(isset($_POST['Update_VA_Multiple'])){
    $aiou = update_va_multiple();
    if($aiou["success"] !== true){
        if(isset($aiou["focus_to"])){
            set_focus($aiou["focus_to"]);
        }
        display_error($aiou["message"]);
    }
    else{
        $Mode = 'RESET';
        display_notification($aiou["message"]);
    }

}   

if(isset($_POST['Update_VA_Multiple_Close'])){
    $aiou = update_va_multiple(true);
    if($aiou["success"] !== true){
        if(isset($aiou["focus_to"])){
            set_focus($aiou["focus_to"]);
        }
        display_error($aiou["message"]);
    }
    else{
        $Mode = 'RESET';
        display_notification($aiou["message"]);
    }

} 

if(isset($_POST['Add'])){
    $line = build_va_line_array($selected_id);
    $aiou = create_va($line);
    if($aiou["success"] !== true){
        if(isset($aiou["focus_to"])){
            set_focus($aiou["focus_to"]);
        }
        display_error($aiou["message"]);
    }
    else{
        $Mode = 'RESET';
        display_notification($aiou["message"]);
    }

}    
 
if(isset($_POST['Add_Close_VR'])){
    $aiou = add_va_and_close_vr($Mode);
    if($aiou["success"] !== true){
        if(isset($aiou["focus_to"])){
            set_focus($aiou["focus_to"]);
        }
        display_error($aiou["message"]);
    }
    else{
        $Mode = 'RESET';
        display_notification($aiou["message"]);
    }

}



if(isset($_POST['Update'])){
    $line = build_va_line_array($selected_id);
    $uva = update_va($line);
    if($uva["success"] !== true){
        if(isset($uva["focus_to"])){
            set_focus($uva["focus_to"]);
        }
        display_error($uva["message"]);
    }
    else{
        $Mode = 'RESET';
        display_notification($uva["message"]);
    }
 }


if(isset($_POST['Update_Close_VR'])){
    $line = build_va_line_array($selected_id);
    $aiou = update_va_and_close_vr($line);
    if($aiou["success"] !== true){
        if(isset($aiou["focus_to"])){
            set_focus($aiou["focus_to"]);
        }
        display_error($aiou["message"]);
    }
    else{
        $Mode = 'RESET';
        display_notification($aiou["message"]);
    }
}

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


//
//if(isset($_POST['Back_To_IOU_Originator'])){
//    send_iou_back_to_originator($Mode, $rec_id);
//}
//
//if (isset($_POST['Reject_IOU'])){
//    reject_iou($Mode, $rec_id);
//} 
//--------------------------------------------------------------------------------------------------

if ($Mode == 'Delete')
{
        $recs = delete_vehicle_allocation($selected_id);
        
        if($recs == 1){
                display_notification(_("Vehicle allocation has been deleted."));
        }
        else{
                display_error(_("Vehicle allocation could not be deleted."));
        }
	$Mode = 'RESET';
}

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

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

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

br(2);

start_table(TABLESTYLE_NOBORDER);
start_row();

date_cells(_("From:"), 'filtering_from_date', '', null, -3);
//if(!isset($_POST['filtering_from_date'])){
//    date_cells(_("From:"), 'filtering_from_date', '', null, -15);
//}
//else{
//    date_cells(_("From:"), 'filtering_from_date',$_POST['from_date']);
//}
date_cells(_("To:"), 'filtering_to_date', '', null, +14);
project_list_cells("Project:", 'project_id', null, "Select a project");
user_list_cells("Originator:", 'originator_id', null, '');

submit_cells('SearchOrders', _("Search"),'',_('Select documents'), 'default');

end_row();

end_table();

br(2);
    

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

display_open_vehicle_requests($_POST['filtering_from_date'], $_POST['filtering_to_date'], $_POST['project_id'], $_POST['originator_id']);

//br();

submit_center_first('Add_VA_Multiple', _("Create Vehicle Allocations"), '', 'default');
submit('Add_VA_Multiple_Close', 'Create VAs and Close Rquests', true, '', 'default');
submit_center_last('Close_VR_Multiple', _("Close Vehicle Requests"), '', 'default');

br(2);
//
//display_open_vehicle_allocations();
//
//submit_center_first('Update_VA_Multiple', _("Update Vehicle Allocations"), '', 'default');
//submit('Update_VA_Multiple_Close', 'Update VAs and Close Rquests', true, '', 'default');
//
//br(2);
//
//display_va_edit_controls($Mode, $selected_id);

//if(isset($_POST['status_code']) && $_POST['status_code'] == "A"){
//    submit("pay_salary_advance", "Pay",true, _('Pay Salary Advance'), 'default');
//    submit('RESET', _("Cancel"), true, _('Cancel edition'), 'cancel');
//}
//else{
// submit_add_or_update_center(!isset($_POST['ModifyMRNumber']), '', 'both');
//}

//if($selected_id > 0)
//{
//    submit_center_first('Update', _("Update Vehicle Allocation"), '', 'default');
//    submit('Update_Close_VR', 'Update and Close Request', true, '', 'default');
//    submit_center_last('cancel', _("Cancel"), _("Cancel Edition"), 'cancel');
////    
////    br();
////    submit_center_first('Back_To_IOU_Originator', "Back To Originator", '', 'default');
////    submit_center_last('Reject_IOU', "Reject", '', 'default');
//}
//else{
//        
//    submit_center_first('Add', _("Create Vehicle Allocation"), '', 'default');
//    submit('Add_Close_VR', 'Create and Close Rquest', true, '', 'default');
////    submit_center_last('cancel', _("Cancel"), _("Cancel Edition"), 'cancel');
//}


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

?>
