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

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

$vehicle_id = $_GET['vehicle_id_text'];
$department = $_GET['department_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'];
$mileage = $_GET['mileage_text'];
$reference = $_GET['reference_text'];
$received = $_GET['received_text'];
$notes = $_GET['notes_text'];


$table_name = $_SESSION['db_prefix']."_fuel_supply";
$last_query = "SELECT mileage FROM $table_name WHERE vehicle_id = '".$vehicle_id."' ORDER BY fuel_id DESC LIMIT 1 ";
$last_result = mySQL_query($last_query);
$last_num = mysql_num_rows($last_result);
if ($last_num>0) { 
$last_supplied= mysql_result($last_result, 0, "mileage");
}
else {
$last_supplied = 0;
}


$table_name = $_SESSION['db_prefix']."_vehicle";
$average_query = "SELECT average_consumption FROM $table_name WHERE vehicle_id = '".$vehicle_id."' ";
$average_result = mySQL_query($average_query);
$average = mysql_result($average_result, 0, "average_consumption");

$covered = $mileage - $last_supplied;
$usage = $covered/$qty;



$table_name = $_SESSION['db_prefix']."_fuel_supply";
$fuel_query = "INSERT INTO $table_name (supply_date, vehicle_id, fuel, qty_supplied, mileage, rec_no, driver, notes, department) VALUES ('".$record_date."', '".$vehicle_id."', '".$fuel."', '".$qty."', '".$mileage."', '".$reference."', '".$received."', '".$notes."', '".$department."' ) ";
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) VALUES ('".$vehicle_id."', '".$record_date."', '".$mileage."', '".$tot_mileage."', '".$qty."') ";
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);

$message_text = '';
if ($fuel=='diesel') {
$table_name2 = $_SESSION['db_prefix']."_store_items";
$quantityQuery = "SELECT quantity, price, minimumStock, subCategory, item_name, minimumStock FROM $table_name2 WHERE store_item_id = '10000' ";
$quantityResult = execsql($quantityQuery);
$store_item_id = '10000';
$total_quantity = mysql_result($quantityResult, 0, "quantity");
$price = mysql_result($quantityResult, 0, "price");
$min_stock = mysql_result($quantityResult, 0, "minimumStock");
$group_name = mysql_result($quantityResult, 0, "subCategory");
$item_name = mysql_result($quantityResult, 0, "item_name");
$min_stock = mysql_result($quantityResult, 0, "minimumStock");

$table_name = $_SESSION['db_prefix']."_store_items";
$update_query = "UPDATE $table_name SET quantity = quantity - '".$qty."' WHERE store_item_id = '10000' ";
execsql($update_query);

$total_quantity=$total_quantity-$qty;

if ($total_quantity<$min_stock) {
	  include('../include/min_stock_alert.php');
	  $message_text = $message_text.'<tr><td>'.$item_name.'</td><td>'.$group_name.'</td><td align=center>'.$total_quantity.'</td><td align=center>'.$min_stock.'</td></tr>';
}


}

$table_name3 = $_SESSION['db_prefix']."_item_history";		
	$historyQuery = "INSERT INTO $table_name3 (store_item_id, entry, reference, quantity, total, date, entryDate, userId) VALUES ('10000' ,'30','".$vehicle_id."','".$qty."', '".$total_quantity."','".$record_date."','".$entry_date."','".$userId."')";
	$historyResult = mySQL_query($historyQuery);


$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']."', '30', '".$vehicle_id."' ,'".$record_date."','".$qty."','".$tot_mileage."' )";
   $trackingResult = mySQL_query($trackingQuery);
   
   
include('../include/fuel_usage_alert.php');


if ($message_text!='') {
	   include('../include/min_stock_alert_email.php');
	}

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

