<?php

session_start();


if($_SESSION['dbName']==""){
header("location:../index.php");
}

include("config.inc.php");
include("function.inc.php");
include("form_tracking_functions.php");

$connection = mysql_connect($config['dbServer'],$config['dbUser'],$config['dbPass']) or die("Could not connect to DB");
mysql_select_db($HTTP_SESSION_VARS['dbName'],$connection) or die("Could not find DB");

//User Access
$querylog=" select ifNull(login_belongstogrp,0) As FI_BelongsToGrp from ".$HTTP_SESSION_VARS['dbLabel'].".login where user_account='".$HTTP_SESSION_VARS['useraccount']."' and Upper(Login_Schema) = Upper('".$HTTP_SESSION_VARS['dbName']."')  ";
$resultlog=mysql_query($querylog,$connection) or die(mysql_error());
while($rows=mysql_fetch_array($resultlog)){
	extract($rows);
}

$actionmode 			= $_POST['actionmode']; 	if ($actionmode == '') $actionmode = $_GET['actionmode'];
$fueltype_id 			= $_POST['fueltype_id']; 	if ($fueltype_id == '') $fueltype_id= $_GET['fueltype_id'];
$fueltype_description 	= $_POST['fueltype_description'];
$fueltype_AmntLtr 		= $_POST['fueltype_AmntLtr'];

$mainmode	= $_GET['mainmode'];
$newdate	= $_GET['newdate'];
$newqty		= $_GET['newqty'];
$newamount	= $_GET['newamount'];
$newtotal	= $_GET['newtotal'];
$newremark	= $_GET['newremark'];
$carfuel_id	= $_GET['carfuel_id'];
$vehicle_id	= $_GET['vehicle_id'];
 
if ($fueltype_AmntLtr == '') $fueltype_AmntLtr = 0;
$fueltype_description = ereg_replace("'", "\'", $fueltype_description);

$ndate = explode('/',$newdate);
$newdate = $ndate[2]."-".$ndate[1]."-".$ndate[0];

if($newremark == '') $newremark = "NULL";
else $newremark = "'".ereg_replace("'", "\'", trim($newremark))."'";
	
if ($mainmode=='insertcarfuel'){
	$insertcarfule="insert into car_fuel (f_date, f_qty, f_amount, f_total, f_remark, vehicle_id)
	values('$newdate','$newqty','$newamount','$newtotal',$newremark,'$vehicle_id')";
	
	$result  = mysql_query($insertcarfule, $connection);
	$message = mysql_error();
	draw_fuel($connection,$carfuel_id);
	exit;
}

if ($mainmode=='getcarfuel'){
	draw_fuel($connection,$carfuel_id);
	exit;
}
if ($mainmode=='cleardata'){
	draw_fuel($connection,$carfuel_id);
	//header ("Location: ./Form_TrackingFuel.php?fueltype_id=$fueltype_id&mode=view&message=".$message);
	exit;
}

if ($mainmode=='updatecarfuel'){
	$updatecarfuel="update car_fuel set f_date='$newdate',f_qty='$newqty',f_amount='$newamount',f_total='$newtotal',
	f_remark=$newremark,vehicle_id='$vehicle_id' where f_id='$carfuel_id'"  ;
	
	$result  = mysql_query($updatecarfuel, $connection);
	$message = mysql_error();
	draw_fuel($connection,$fueltype_id,$carfuel_id);
	exit;
}
if ($mainmode=='deletecarfuel'){
	$updatecarfuel="delete from  car_fuel where f_id='$carfuel_id'"  ;
	$result  = mysql_query($updatecarfuel, $connection);
	$message = mysql_error();
	draw_fuel($connection,'');
	exit;
}



if($actionmode=='insert'){
		
	$query="Insert into fueltype (fueltype_description, fuel_amountPerLtr)
			values ('".$fueltype_description."', ".$fueltype_AmntLtr.")";

	//echo$query;				
	
	$result  = mysql_query($query, $connection);
	$fueltype_id = mysql_insert_id($connection);
	//echo $fueltype_id;
	//exit;
	$message = mysql_error();

			   

}

if($actionmodedelete=='delete')
{	
	$query="delete from fueltype where fueltype_id='".$fueltypeid."'";
	//echo$query;exit;
	$result  = mysql_query($query, $connection);
	$message = mysql_error();

	
}


if($actionmode=='edit'){
	$query_update=" update fueltype set	fueltype_description='".$fueltype_description."', fuel_amountPerLtr = ".$fueltype_AmntLtr."  ".		  
				  "	where fueltype_id='".$fueltype_id."'";
					  
					  
	$resultedit = mysql_query($query_update, $connection);
	$message    = mysql_error();


}



$message = ereg_replace(' ','%20',$message);
mysql_close($connection);

if($actionmode=='delete')			   
	header ("Location: ./Form_TrackingFuelSearch.php?fueltype_Id=$fueltype_Id&mode=view&message=".$message);

else{
	echo "<form name='fueltype' method='post' action='Form_TrackingFuel.php'>
		<input type=hidden name='mode' value=''>
		<input type=hidden name='actionmode' value=''>
		<input type=hidden name='message' value='$message'>
		<input type=hidden name='fueltype_id' value=''>
		<input type='hidden' name='PTabIndex' id='PTabIndex' value='$PTabIndex'>
		</form>
		<script>
		document.fueltype.submit();
		</script>";
		exit;
}






?>