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

date_default_timezone_set('Africa/Gaborone');
$entry_date = date("Y-m-d H:i:s", time()); 

$vehicle_id = $_GET['vehicle_id_text'];

$table_name = $_SESSION['db_prefix']."_mileage";
$mileage_query = "SELECT tot_mileage FROM $table_name WHERE vehicle_id = '".$vehicle_id."' ORDER BY mileage_id DESC LIMIT 1";
$mileage_result = mySQL_query($mileage_query);
$mileage_num = mysql_num_rows($mileage_result);
if ($mileage_num!=0) {
$tot_mileage = mysql_result($mileage_result, 0, "tot_mileage");
}
else{
	$tot_mileage=0;
}




$record_date = $_GET['record_date'];
$fuel = $_GET['fuel_type_text'];
$qty = $_GET['qty_text'];
$price = $_GET['fuel_price_text'];
$oil_qty = $_GET['oil_qty_text'];
$oil_price = $_GET['oil_price_text'];
$other = $_GET['other_text'];
$other_price = $_GET['other_price_text'];
$mileage = $_GET['mileage_text'];
$provider = $_GET['provider_select'];
$reference = $_GET['reference_text'];
$received = $_GET['received_select'];
$notes = $_GET['notes_text'];



$table_name = $_SESSION['db_prefix']."_fuel_supply";
$fuel_query = "INSERT INTO $table_name (supply_date, vehicle_id, fuel, qty_supplied, price, reference, mileage, driver, supplier, notes, oil_qty, oil_price, other, other_price) VALUES ('".$record_date."', '".$vehicle_id."', '".$fuel."', '".$qty."', '".$price."', '".$reference."', '".$mileage."', '".$received."', '".$provider."', '".$notes."', '".$oil_qty."', '".$oil_price."', '".$other."', '".$other_price."') ";
execsql($fuel_query);

$tot_mileage = $tot_mileage+$mileage;


$table_name = $_SESSION['db_prefix']."_mileage";
$rec_mileage_query = "INSERT INTO $table_name (vehicle_id, rec_date, rec_mileage, tot_mileage, litres, price) VALUES ('".$vehicle_id."', '".$record_date."', '".$mileage."', '".$tot_mileage."', '".$qty."', '".$price."' ) ";
execsql($rec_mileage_query);

$table_name = $_SESSION['db_prefix']."_vehicle";
$mileage_query = "UPDATE $table_name SET clock_reading = '".$mileage."' WHERE vehicle_id = '".$vehicle_id."' ";
execsql($mileage_query);


$table_name = $_SESSION['db_prefix']."_tracking_transport";
   $trackingQuery = "INSERT INTO $table_name (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entry_date."' ,'".$_SESSION['username']."', '42', '".$vehicle_id."' ,'".$record_date."','".$qty."','".$tot_mileage."' )";
   $trackingResult = mySQL_query($trackingQuery);



if (isset($_GET['save_close_button']) )
	   { ?>
       <script>
	 		 window.location="../transport/transport_main.php?date=0";
		</script>
        <?php		 
	   }
	   
	if (isset($_GET['save_button']) )
	   {  ?>
       <script>
	        window.location="fuel_rec.php?vehicle_id="
	   </script>
        <?php		 
	   }
	   ?>

