<?php
include('../include/session.php');
date_default_timezone_set('Africa/Gaborone');
$entryDate = date("Y-m-d H:i:s"); 


$fuel_id = $_POST['fuel_id_text'];


$table_name = $_SESSION['db_prefix']."_fuel_supply";
$fuel_query = "SELECT * FROM $table_name WHERE fuel_id = '".$fuel_id."'  ";
$fuel_result = mySQL_query($fuel_query);
$fuel_num = mysql_num_rows($fuel_result);

$supply_date_saved = mysql_result($fuel_result, 0, "supply_date");
$vehicle_id_saved = mysql_result($fuel_result, 0, "vehicle_id");
$fuel_saved = mysql_result($fuel_result, 0, "fuel");
$price_saved = mysql_result($fuel_result, 0, "price");
$qty_supplied_saved = mysql_result($fuel_result, 0, "qty_supplied");
$reference_saved = mysql_result($fuel_result, 0, "reference");
$mileage_saved = mysql_result($fuel_result, 0, "mileage");
$driver_saved = mysql_result($fuel_result, 0, "driver");
$supplier_saved = mysql_result($fuel_result, 0, "supplier");
$notes_saved = mysql_result($fuel_result, 0, "notes");
$oil_qty_saved = mysql_result($fuel_result, 0, "oil_qty");
$oil_price_saved = mysql_result($fuel_result, 0, "oil_price");
$other_saved = mysql_result($fuel_result, 0, "other");
$other_price_saved = mysql_result($fuel_result, 0, "other_price");

$supply_date = $_POST['supply_date'];
$fuel = $_POST['fuel_select'];
$price = $_POST['price_text'];
$qty_supplied = $_POST['qty_text'];
$reference = $_POST['reference_text'];
$mileage = $_POST['mileage_text'];
$driver = $_POST['received_select'];
$supplier = $_POST['provider_select'];
$oil_qty = $_POST['oil_qty_text'];
$oil_price = $_POST['oil_price_text'];
$other = $_POST['other_text'];
$other_price = $_POST['other_price_text'];
$notes = $_POST['notes_text'];


$table_name = $_SESSION['db_prefix']."_fuel_supply";
$supplier_query = "UPDATE $table_name SET supply_date = '".$supply_date."', fuel = '".$fuel."', price = '".$price."', qty_supplied = '".$qty_supplied."', reference = '".$reference."', mileage = '".$mileage."', driver = '".$driver."', supplier = '".$supplier."', oil_qty = '".$oil_qty."', oil_price = '".$oil_price."', other = '".$other."', other_price = '".$other_price."', notes = '".$notes."' WHERE fuel_id = '".$fuel_id."' ";
execsql($supplier_query);


$table_name = $_SESSION['db_prefix']."_tracking_transport";

if ($supply_date_saved!=$supply_date) {
	    $trackingQuery = "INSERT INTO $table_name (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entryDate."' ,'".$_SESSION['username']."', '45', '".$fuel_id."' ,'".$supply_date_saved."','".$supply_date."','Supply date' )";
		//echo '<br>id_number'.$trackingQuery;
			$trackingResult = mySQL_query($trackingQuery);
}

if ($fuel_saved!=$fuel) {
	    $trackingQuery = "INSERT INTO $table_name (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entryDate."' ,'".$_SESSION['username']."', '45', '".$fuel_id."' ,'".$fuel_saved."','".$fuel."','Fuel' )";
		//echo '<br>id_number'.$trackingQuery;
			$trackingResult = mySQL_query($trackingQuery);
}

if ($price_saved!=$price) {
	    $trackingQuery = "INSERT INTO $table_name (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entryDate."' ,'".$_SESSION['username']."', '45', '".$fuel_id."' ,'".$price_saved."','".$price."','Fuel price' )";
		//echo '<br>id_number'.$trackingQuery;
			$trackingResult = mySQL_query($trackingQuery);
}

if ($qty_supplied_saved!=$qty_supplied) {
	    $trackingQuery = "INSERT INTO $table_name (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entryDate."' ,'".$_SESSION['username']."', '45', '".$fuel_id."' ,'".$qty_supplied_saved."','".$qty_supplied."','Fuel quantity' )";
		//echo '<br>id_number'.$trackingQuery;
			$trackingResult = mySQL_query($trackingQuery);
}

if ($reference_saved!=$reference) {
	    $trackingQuery = "INSERT INTO $table_name (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entryDate."' ,'".$_SESSION['username']."', '45', '".$fuel_id."' ,'".$reference_saved."','".$reference."','Reference' )";
		//echo '<br>id_number'.$trackingQuery;
			$trackingResult = mySQL_query($trackingQuery);
}

if ($mileage_saved!=$mileage) {
	    $trackingQuery = "INSERT INTO $table_name (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entryDate."' ,'".$_SESSION['username']."', '45', '".$fuel_id."' ,'".$mileage_saved."','".$mileage."','Mileage' )";
		//echo '<br>id_number'.$trackingQuery;
			$trackingResult = mySQL_query($trackingQuery);
}

if ($driver_saved!=$driver) {
	    $trackingQuery = "INSERT INTO $table_name (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entryDate."' ,'".$_SESSION['username']."', '45', '".$fuel_id."' ,'".$driver_saved."','".$driver."','Received by' )";
		//echo '<br>id_number'.$trackingQuery;
			$trackingResult = mySQL_query($trackingQuery);
}

if ($supplier_saved!=$supplier) {
	    $trackingQuery = "INSERT INTO $table_name (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entryDate."' ,'".$_SESSION['username']."', '45', '".$fuel_id."' ,'".$supplier_saved."','".$supplier."','Provider' )";
		//echo '<br>id_number'.$trackingQuery;
			$trackingResult = mySQL_query($trackingQuery);
}

if ($oil_qty_saved!=$oil_qty) {
	    $trackingQuery = "INSERT INTO $table_name (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entryDate."' ,'".$_SESSION['username']."', '45', '".$fuel_id."' ,'".$oil_qty_saved."','".$oil_qty."','Oil qty' )";
		//echo '<br>id_number'.$trackingQuery;
			$trackingResult = mySQL_query($trackingQuery);
}

if ($oil_price_saved!=$oil_price) {
	    $trackingQuery = "INSERT INTO $table_name (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entryDate."' ,'".$_SESSION['username']."', '45', '".$fuel_id."' ,'".$oil_price_saved."','".$oil_price."','Oil price' )";
		//echo '<br>id_number'.$trackingQuery;
			$trackingResult = mySQL_query($trackingQuery);
}

if ($other_saved!=$other) {
	    $trackingQuery = "INSERT INTO $table_name (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entryDate."' ,'".$_SESSION['username']."', '45', '".$fuel_id."' ,'".$other_saved."','".$other."','Other' )";
		//echo '<br>id_number'.$trackingQuery;
			$trackingResult = mySQL_query($trackingQuery);
}

if ($other_price_saved!=$other_price) {
	    $trackingQuery = "INSERT INTO $table_name (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entryDate."' ,'".$_SESSION['username']."', '45', '".$fuel_id."' ,'".$other_price_saved."','".$other_price."','Other price' )";
		//echo '<br>id_number'.$trackingQuery;
			$trackingResult = mySQL_query($trackingQuery);
}

if ($notes_saved!=$notes) {
	    $trackingQuery = "INSERT INTO $table_name (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entryDate."' ,'".$_SESSION['username']."', '45', '".$fuel_id."' ,'".$notes_saved."','".$notes."','Notes' )";
		//echo '<br>id_number'.$trackingQuery;
			$trackingResult = mySQL_query($trackingQuery);
}




if (isset($_POST['save_close_button']) )
	   { ?>
       <script>
	 		 window.location="transport_main.php?date=0";
		</script>
        <?php		 
	   }
	   
	if (isset($_POST['save_button']) )
	   {  ?>
       <script>
	          window.location="edit_fuel_rec.php?fuel_id=<?php echo $fuel_id?>"
	   </script>
        <?php		 
	   }
	   ?>

