<?php
include("../include/session.php");

	
	$user_id = $_SESSION['username'];
	date_default_timezone_set('Africa/Gaborone');
	$entry_date = date("Y-m-d H:i:s", time()); 
	$rec_date = date("Y-m-d");
	$year = date('Y');
	
	switch ($_SESSION['license']) {
	case '': $license = '0000-00-00';  break;	
    case 'January': $license = $year.'-01-31';  break;	
	case 'February': $license = $year.'-02-28'; break;	
	case 'March': $license = $year.'-03-31'; break;	
	case 'April': $license = $year.'-04-30'; break;
	case 'May': $license = $year.'-05-31'; break;
	case 'June': $license = $year.'-06-30'; break;
	case 'July': $license = $year.'-07-31'; break;
	case 'August': $license = $year.'-08-31'; break;
	case 'September': $license = $year.'-09-30'; break;
	case 'October': $license = $year.'-10-31'; break;
	case 'November': $license = $year.'-11-30'; break;
	case 'December': $license = $year.'-12-31'; break;
	}
	
	switch ($_SESSION['ba_license']) {
	case '': $ba_license = '0000-00-00';  break;
    case 'January': $ba_license = $year.'-01-31';  break;	
	case 'February': $ba_license = $year.'-02-28'; break;	
	case 'March': $ba_license = $year.'-03-31'; break;	
	case 'April': $ba_license = $year.'-04-30'; break;
	case 'May': $ba_license = $year.'-05-31'; break;
	case 'June': $ba_license = $year.'-06-30'; break;
	case 'July': $ba_license = $year.'-07-31'; break;
	case 'August': $ba_license = $year.'-08-31'; break;
	case 'September': $ba_license = $year.'-09-30'; break;
	case 'October': $ba_license = $year.'-10-31'; break;
	case 'November': $ba_license = $year.'-11-30'; break;
	case 'December': $ba_license = $year.'-12-31'; break;
	}
	
	 
	$table_name = $_SESSION['db_prefix']."_vehicle";
	$vehicle_query = "INSERT INTO $table_name ( `registration_no`, `make`, `model`,   `fuel`, `average_consumption`, `department`, `service_interval`, `clock_reading`, `chassis_no`, `engine_no`, `road_license_date`, `ba_license_date`, `notes`) VALUES ( trim('". $_SESSION['reg_no']."'), trim('".$_SESSION['make']."'),  trim('".$_SESSION['model']."'),  trim('".$_SESSION['fuel']."'),  trim('".$_SESSION['average']."'), trim('".$_SESSION['department']."'), trim('".$_SESSION['service_interval']."'),  trim('".$_SESSION['clock_reading']."'),  trim('".$_SESSION['chassis_no']."'),  trim('".$_SESSION['engine_no']."'),  '".$license."',  '".$ba_license."',  trim('".$_SESSION['notes']."'))";
	execsql($vehicle_query);
	
	
$table_name = $_SESSION['db_prefix']."_vehicle";
$vehicle_id_query = "SELECT  vehicle_id FROM $table_name  ORDER BY vehicle_id DESC LIMIT 1 ";
$vehicle_id_result = mySQL_query($vehicle_id_query);
$vehicle_id = mysql_result($vehicle_id_result, 0, "vehicle_id");
	
	$table_name2 = $_SESSION['db_prefix']."_mileage";
	$meter_query = "INSERT INTO $table_name2 (vehicle_id, rec_date, interval_period, tot_mileage, service_reading, recorded_from) VALUES ('".$vehicle_id."',  trim('".$rec_date."'),  trim('".$_SESSION['service_interval']."'),  trim('".$_SESSION['clock_reading']."'),  trim('".($_SESSION['service_interval']+$_SESSION['clock_reading'])."'),  trim('initial entry') )";
	execsql($meter_query);
	

	
		
	$table_name = $_SESSION['db_prefix']."_tracking_store";
   $trackingQuery = "INSERT INTO $table_name (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entry_date."' ,'".$_SESSION['username']."', '18', '' ,'". $_SESSION['reg_no']."','".$_SESSION['make']."','".$_SESSION['model']."' )";
   $trackingResult = mySQL_query($trackingQuery);


if (isset($_GET['new_button']) )
	   {
		 $action='vehicleWiz0.php';
	   }
		 
	if (isset($_GET['finish_button']) )
	  {
		$action='vehicle_details.php?vehicle_id='.$vehicle_id ;
	  }	
	 
     $today = date("Y-m-d");
	 $_SESSION['reg_no'] = "";
	 $_SESSION['vehicle_no'] = "";
     $_SESSION['make'] = "";
	 $_SESSION['model'] = "";
	 $_SESSION['fuel'] = "";
	 $_SESSION['department'] = "";
	 
	 
	 $_SESSION['service_interval'] = "";
	 $_SESSION['clock_reading'] = "";
	 $_SESSION['chassis_no'] = "";
	 $_SESSION['engine_no'] = "";
	 $_SESSION['reg_no'] = "";
	 $_SESSION['license'] = "";
	 $_SESSION['ba_license'] = "";
	 
	 $_SESSION['notes'] ="";	
	
	 
?>

<script language="javascript" type="text/javascript">
        
		 window.location="<?php echo $action ?> ";
        
       </script>



