<?php

session_start();


if($_SESSION['dbName']==""){
header("location:../index.php");
}
header("Content-type: text/html; charset=".$_SESSION['encodingmode']);
include("config.inc.php");
include("function.inc.php");


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

if(	$_SESSION['encodingmode'] == 'utf8' || $_SESSION['encodingmode'] == 'utf-8'){
	mysql_query("SET NAMES 'utf8'"); 
	mysql_query('SET CHARACTER SET utf8'); 
	mysql_set_charset('utf8', $connection);
}

$mode 				= $_POST['mode'];
$fromplace_name 	= $_POST['fromplace_name'];
$toplace_name 		= $_POST['toplace_name'];
$place_millage 		= $_POST['place_millage'];
$place_millagetype 	= $_POST['place_millagetype'];
$tripAllowance 		= $_POST['tripAllowance'];
$tripType 			= $_POST['tripType'];
$usageLtrs 			= $_POST['usageLtrs'];
$estimationDays 	= $_POST['estimationDays'];
$consumptionPerDist = $_POST['consumptionPerDist'];
$consumption 		= $_POST['consumption'];
$chargeRate 		= $_POST['chargeRate'];

$getmode = $_GET['getmode'];
$Typeid  = $_GET['Typeid'];

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

if ($place_millage=='') 		$place_millage		= 0;
if ($tripAllowance=='') 		$tripAllowance		= 0;
if ($usageLtrs=='') 			$usageLtrs			= 0;
if ($estimationDays=='') 		$estimationDays		= 0;
if ($consumptionPerDist=='') 	$consumptionPerDist = 0;
if ($consumption=='') 			$consumption		= 0;
if ($chargeRate=='') 			$chargeRate			= 0;


if ($getmode == 'setConsumption'){
	$val = getConsumption($Typeid, $connection);
	echo "[BRK]";
	echo $val;
	exit;
}

// Recalculate Consumption
$val = getConsumption($tripType, $connection);
if ($val == '') $val = 0;
$consumption = $val*$usageLtrs;

if ($usageLtrs != 0)
$consumptionPerDist = $place_millage/$usageLtrs;
if ($tripType == '')	$tripType			= "NULL";

if($mode=='insert'){
		
	$query="Insert into Place (fromplace_name,toplace_name,place_millage,place_millagetype, place_tripAllowance, place_tripTypeId, place_usageLtrs, place_estimationDays, 
			place_ConsumptionPerDist, place_Consumption, place_ChargesRate )
			values (".GetSQLValueString($fromplace_name,'text').",".GetSQLValueString($toplace_name,'text').",".$place_millage.",".GetSQLValueString($place_millagetype,'text').",
			".$tripAllowance.", ".$tripType.", ".$usageLtrs.", ".$estimationDays.", ".$consumptionPerDist.", ".$consumption.", ".$chargeRate.")";

	//echo$query;				
	
	$result  = mysql_query($query, $connection);
	$message = mysql_error();
}

if($mode=='delete')
{	
	$query="delete from Place where Place_Id='".$Place_Id."'";
	//echo$query;exit;
	$result  = mysql_query($query, $connection);
	$message = mysql_error();

	
}


if($mode=='edit'){
	$query_update=" update Place set	fromplace_name = ".GetSQLValueString($fromplace_name,'text').",
										toplace_name = ".GetSQLValueString($toplace_name,'text').",
										place_millage = ".$place_millage.",
										place_millagetype = ".GetSQLValueString($place_millagetype,'text').",
										place_tripAllowance = ".$tripAllowance.", 
										place_tripTypeId = ".$tripType.", 
										place_usageLtrs = ".$usageLtrs.", 
										place_estimationDays = ".$estimationDays.",
										place_ConsumptionPerDist = ".$consumptionPerDist.", 
										place_Consumption = ".$consumption.", 
										place_ChargesRate = ".$chargeRate."
					where Place_Id='".$Place_Id."'";// echo $query_update;exit;
	$resultedit = mysql_query($query_update, $connection);
	$message    = mysql_error();
}

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

mysql_close($connection);

echo "<form name='place' method='POST' action='Form_Place.php' >
		<input type=hidden name='mode' value=''>
		<input type=hidden name='actionmode' value=''>
		<input type=hidden name='message' value='$message'>
		<input type=hidden name='Place_Id' value=''>
	</form>
	<script>document.place.submit();</script>";

function getConsumption($id, $connection){
	$query = "select ifnull(fuel_amountPerLtr,0) from fueltype where fueltype_id = $id ";
	$result = mysql_query($query,$connection);
	$row = mysql_fetch_row($result);
	return $row[0];
}	

?>

