<?php
function time_round($time){
	$times = strtotime($time);
	$minute = date('i', $times);
	if($minute <= 30)
	{
		$mint = $minute;
		$rtime = date("Y-m-d H:i:00",strtotime($time." -$mint minutes"));
	}
	else 
	{
		$mint = 60-$minute;
		$rtime = date("Y-m-d H:i:00",strtotime($time." +$mint minutes"));
	}
	return $rtime;
}

$con = mysqli_connect("localhost","root","","samsung");

$readerip = $_REQUEST["readerip"];
$tag_id = $_REQUEST["tag_id"];
$time_val=$_REQUEST["time"];
$timevar = strtotime($time_val);
$time=date("Y-m-d H:i:s",$timevar);


$select="select tag_id from tbl_master where tag_id='".$tag_id."'";
$result = mysqli_query($con,$select);
$countrow = mysqli_num_rows($result);
if ($countrow!=0) 
{
	if($readerip == '107.109.221.234')
	{
		$select_tag="select tag_id,time_in from log where tag_id = '$tag_id' and time_out is NULL order by id desc";
		$result_tag = mysqli_query($con,$select_tag);
						
		if(!mysqli_num_rows($result_tag))
		{
			//---------time round off-------------//
			$select_query="select vehicle_type from tbl_vehicle_master where tag_id = '$tag_id' or alias_tag = '$tag_id'";
			$result = mysqli_query($con,$select_query);
			$row = $result->fetch_array();
			if($row["vehicle_type"] == "Cab"){
				$times = time_round($time);
			}
			else{
				$times = date("Y-m-d H:i:s",$timevar);
			}
			//---------time round off-------------//
			
			$insertsql="insert into log (tag_id, time_in) values('".$tag_id."', '".$times."')";
			$result = mysqli_query($con,$insertsql);
		}
		else
		{
			$fetchrow = $result_tag->fetch_array();
			$timein=$fetchrow["time_in"];
						
			$dbtime = new DateTime($timein);
			$compsystime = new DateTime($time);
			

			$difference = $compsystime->diff($dbtime);
			$minutes = $difference->days * 24 * 60;
			$minutes += $difference->h *60;
			$minutes += $difference->i;
			
			//---------time round off-------------//
			$select_query="select vehicle_type from tbl_vehicle_master where tag_id = '$tag_id' or alias_tag = '$tag_id'";
			$result = mysqli_query($con,$select_query);
			$row = $result->fetch_array();
			if($row["vehicle_type"] == "Cab"){
				$times = time_round($time);
			}
			else{
				$times = date("Y-m-d H:i:s",$timevar);
			}
			//---------time round off-------------//
			
			if($minutes > 5)
			{
				$insertsql="insert into log (tag_id, time_in) values('".$tag_id."', '".$times."')";
				$result = mysqli_query($con,$insertsql);
			}
			else
			{
				echo "Time is Less than 5 Minutes";
			}
			
		}
	}
	else
	{
		//---------time round off-------------//
		$select_query="select vehicle_type from tbl_vehicle_master where tag_id = '$tag_id' or alias_tag = '$tag_id'";
		$result = mysqli_query($con,$select_query);
		$row = $result->fetch_array();
		if($row["vehicle_type"] == "Cab"){
			$times = time_round($time);
		}
		else{
			$times = date("Y-m-d H:i:s",$timevar);
		}
		//---------time round off-------------//
		$select_time_out="select id,tag_id,time_in from log where tag_id = '$tag_id' and time_out is NULL order by id asc";
		$result_time_out = mysqli_query($con,$select_time_out);
		$total_result_row = mysqli_num_rows($result_time_out);		
		if($total_result_row >= 2)
		{
			$total_row = $total_result_row-1;
			$i=1;
			while ($row = mysqli_fetch_array($result_time_out))
			{
				if($i<=$total_row){
					echo $updatesql="update log set time_out='2009-12-31 19:31:01' where tag_id = '".$tag_id."' and time_out IS NULL and id = '".$row['id']."'";
					$result = mysqli_query($con,$updatesql);
					$i++;
				}
				else
				{
					echo $updatesql="update log set time_out='".$times."' where tag_id = '".$tag_id."' and time_out IS NULL and id = '".$row['id']."'";
					$result = mysqli_query($con,$updatesql);
				}
			}
		}
		else{
			$updatesql="update log set time_out='".$times."' where tag_id = '".$tag_id."' and time_out IS NULL";
			$result = mysqli_query($con,$updatesql);
		}
		//$updatesql="update log set time_out='".$times."' where tag_id = '".$tag_id."' and time_out IS NULL";
		//$result = mysqli_query($con,$updatesql);
	}
}
else
{
	if($readerip == '107.109.221.234')
	{
		$insertsql="insert into unknowntag (tag_id, time_in) values('".$tag_id."', '".$time."')";
		$result = mysqli_query($con,$insertsql);
	}
	else
	{
		$updatesql="update unknowntag set time_out='".$time."' where tag_id = '".$tag_id."' and time_out IS NULL";
		$result = mysqli_query($con,$updatesql);
	}

}
?>