<?php

session_start();


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

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($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);
}




if (!empty($vehiclemovement_start))	{
	$vehiclemovement_start  = explodeword1($vehiclemovement_start );
	$vehiclemovement_start   = "'$vehiclemovement_start $starttime'";

}	
else {$vehiclemovement_start = 'null';}

if (!empty($vehiclemovement_end))	{
	$vehiclemovement_end  = explodeword1($vehiclemovement_end );
	$vehiclemovement_end   = "'$vehiclemovement_end $endtime'";

}	
else {$vehiclemovement_end = 'null';}


if($actionmode=='insert'){
	$query="select max(ifnull(vehiclemovement_reference,0))+1 from vehicleMovement ";
	$result  = mysql_query($query, $connection);
	$rows=mysql_fetch_array($result);	
	$ref = $rows[0];
	
	$query="Insert into vehiclemovement (vehicleMovement_reference,vehicleMovement_start,vehicleMovement_end, place_id,vehicle_id,employee_id,vehiclemovement_millage)
			values (".$ref.", ".$vehiclemovement_start.", ".$vehiclemovement_end.", ".$place_id." ,'".$vehicle_id."','".$employee_id."','".$vehiclemovement_millage."')";
	
	$result  = mysql_query($query, $connection);
	$vehiclemovement_id= mysql_insert_id($connection);
	$printreport=1;
	$message = mysql_error();
}
if($actionmode=='delete')
{	
	$query="delete from vehiclemovement where vehiclemovement_id='".$vehiclemovement_id."'";
	//echo$query;exit;
	$result  = mysql_query($query, $connection);
	$message = mysql_error();

}

if(($actionmode=='edit') and ($charges_id=='') and ($item_code=='')){
	$query_update=" update vehiclemovement set vehicleMovement_start=".$vehiclemovement_start.", ".
				  " vehicleMovement_end=".$vehiclemovement_end.", ".
				  "	place_id= $place_id  ".
				  "	,vehicle_id= '".$vehicle_id."'  ".
				  "	,employee_id= '".$employee_id."'  ".
				  "	,vehiclemovement_millage= '".$vehiclemovement_millage."'  ".
				  "	,vehiclemovement_millagetype= '".$vehiclemovement_millagetype."'  ".
				  "	where vehiclemovement_id='".$vehiclemovement_id."'";
	

	$resultedit = mysql_query($query_update, $connection);
	$message    = mysql_error();
	$printreport = 1;

}



if (($modedt=='add_vehiclemovementcharges') and ($actionmode=='edit') and ($charges_id!=''))
{


$query="Insert into vehiclemovementcharges (
				vehiclemovementcharges_amount, vehiclemovementcharges_note, vehiclemovement_id, charges_id
				)
			values (
			".$vehiclemovementcharges_amount.", '".$vehiclemovementcharges_note."', ".$vehiclemovement_id.", ".$charges_id."
				)";

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

	header ("Location: ./Form_VehicleMovement.php?vehiclemovement_id=$vehiclemovement_id&actionmode=edit&vTabFocus=Charges&message=".$message);
	exit;
}
else
if (($modedt=='edit_vehiclemovementcharges')  and ($actionmode=='edit') and ($vehiclemovementcharges_id!=''))
{

	$query_update=" update vehiclemovementcharges set vehiclemovementcharges_amount=".$vehiclemovementcharges_amount."
					,vehiclemovementcharges_note='".$vehiclemovementcharges_note."'
					,vehiclemovement_id = '".$vehiclemovement_id."'
					,charges_id = '".$charges_id."'
					where vehiclemovementcharges_id = '".$vehiclemovementcharges_id."'
					";
	$resultedit = mysql_query($query_update, $connection);
	$message    = mysql_error();
	header ("Location: ./Form_VehicleMovement.php?vehiclemovement_id=$vehiclemovement_id&actionmode=edit&vTabFocus=Charges&message=".$message);
	exit;
}
else
if (($modedt=='delete_vehiclemovementcharges')  and ($actionmode=='edit')  and ($vehiclemovementcharges_id!=''))
{
$query="delete from vehiclemovementcharges where vehiclemovementcharges_id='".$vehiclemovementcharges_id."'";
	$result  = mysql_query($query, $connection);
	$message = mysql_error();

	
header ("Location: ./Form_VehicleMovement.php?vehiclemovement_id=$vehiclemovement_id&actionmode=edit&vTabFocus=Charges&message=".$message);
	exit;
}

if (($modedtitems=='add_vehiclemovementitems') and ($actionmode=='edit') and ($item_code!=''))
{

if($vehiclemovementitems_quantity=='')
	$vehiclemovementitems_quantity = 0;
	
$query="Insert into vehiclemovementitems (
				vehiclemovementitems_quantity, vehiclemovementitems_note, vehiclemovement_id, item_code
				)
			values (
			".$vehiclemovementitems_quantity.", '".$vehiclemovementitems_note."', ".$vehiclemovement_id.", '".$item_code."'
				)";

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

	header ("Location: ./Form_VehicleMovement.php?vehiclemovement_id=$vehiclemovement_id&vTabFocus=Items&actionmode=edit&message=".$message);
	exit;
}
else
if (($modedtitems=='edit_vehiclemovementitems') and ($actionmode=='edit') and ($vehiclemovementitems_id!=''))
{

	$query_update=" update vehiclemovementitems set vehiclemovementitems_quantity=".$vehiclemovementitems_quantity."
					,vehiclemovementitems_note='".$vehiclemovementitems_note."'
					,vehiclemovement_id = '".$vehiclemovement_id."'
					,item_code = '".$item_code."'
					where vehiclemovementitems_id = '".$vehiclemovementitems_id."'
					";
					
	$resultedit = mysql_query($query_update, $connection);
	$message    = mysql_error();
	header ("Location: ./Form_VehicleMovement.php?vehiclemovement_id=$vehiclemovement_id&vTabFocus=Items&actionmode=edit&message=".$message);
	exit;
}
else
if (($modedtitems=='delete_vehiclemovementitems') and ($actionmode=='edit')  and ($vehiclemovementitems_id!=''))
{
$query="delete from vehiclemovementitems where vehiclemovementitems_id='".$vehiclemovementitems_id."'";
	$result  = mysql_query($query, $connection);
	$message = mysql_error();

	
header ("Location: ./Form_VehicleMovement.php?vehiclemovement_id=$vehiclemovement_id&vTabFocus=Items&actionmode=edit&message=".$message);
	exit;
}



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

if($actionmode=='delete')			   
	header ("Location: ./Form_VehicleMovementSearch.php?vehiclemovement_id=$vehiclemovement_id&actionmode=view&message=".$message);
else
	header ("Location: ./Form_VehicleMovement.php?vehiclemovement_id=$vehiclemovement_id&actionmode=view&message=".$message."&printreport=$printreport");

mysql_close($connection);


function explodeword1($date)
{

$trim = explode('/',$date);
$trim = "$trim[2]"."-"."$trim[1]"."-"."$trim[0]";
return $trim;
}
print <<<HERE
</form>
</body>

HERE;


?>
		           