<?php 
include 'config.php';
//date_default_timezone_set('Asia/Kolkata');
error_log("====================================================ramana====================");
$deviceID=$_GET['vehicle'];
$group=$_GET['group'];
$accountID=$_GET['accountID'];
$userID=$_GET['userID'];
$dumpUserID=$userID;
$fromDate=$_GET['fromDate'];
$toDate=$_GET['toDate'];
$reportFormate=$_GET['formate'];
$speedlimit=$_GET['speedlimit'];
if($accountID=="mythrilaxmipur"){
echo "Report Restricted";
die();
}
if (isset($_GET['fromDate'])) {
$fromDate=strtotime($_GET['fromDate']);
$toDate=strtotime($_GET['toDate']);
$starttime="0";//$setup[$accountID.".fromtime"];
$endtime="23";//$setup[$accountID.".totime"];
$toDayDate=strtotime(date('d-M-Y',$fromDate).'');
$d=date('d',$toDayDate);
$m=date('m',$toDayDate);
$y=date('Y',$toDayDate);
$fromDate=strtotime(date("d-m-Y h:i:s a", mktime($starttime,0,0,$m,$d,$y)));
$toDate=strtotime(date("d-m-Y h:i:s a", mktime($endtime,59,50,$m,$d,$y)));
}else{
$starttime="0";//$setup[$accountID.".fromtime"];
$endtime="23";//$setup[$accountID.".totime"];
$toDayDate=strtotime(date('d-M-Y',time()).'');
$d=date('d',$toDayDate);
$m=date('m',$toDayDate);
$y=date('Y',$toDayDate);
$fromDate=strtotime(date("d-m-Y h:i:s a", mktime($starttime,0,0,$m,$d,$y)));
$toDate=strtotime(date("d-m-Y h:i:s a", mktime($endtime,59,50,$m,$d,$y)));
}
if($reportFormate=="word"){
error_log($reportFormate.' report formate');
header('Cache-control: no-cache,must revalidate');
header("Access-Control-Allow-Origin: *");
header("Content-type: application/vnd.ms-word");
header("Content-Disposition: attachment;Filename=".$userID.".doc");
}else if($reportFormate=="excel"){
header('Cache-control: no-cache,must revalidate');
header('Content-type: application/json');
header("Access-Control-Allow-Origin: *");
header("Content-type: application/vnd.ms-excel");
header("Content-Disposition: attachment;Filename=".$userID.".xls");
}else if($reportFormate=="web"){
header('Cache-control: no-cache,must revalidate');
header("Access-Control-Allow-Origin: *");
header('Content-type: text/html');
header("Content-Disposition: attachment;Filename=".$userID.'_'.date('d-m-Y').".html");
}
$groupArray=array();
//echo 'satrt';
$overspeedintervel=15;//mints default
//$speedlimit=60;   //kmph
//  if($accountID=="gvkkerala" || $accountID=="mythrilaxmipur"){
OverSpeedlimitAndIntervel();
//get Over Speed Limit and inter vel
//}
if($overspeedintervel==""){
$overspeedintervel=10;
}
if($speedlimit==""){
$speedlimit=60;
}
//echo $overspeedintervel."  ".$speedlimit; die(); 
if($group=="selectall"){
if($userID != "admin" && $userID != "administrator"){
$groupArray=getGroups($gtsserver, $gtsusername, $gtspassword,$gtsdb,$accountID,$userID);
if(count($groupArray)==0){
$userID='admin';
$groupArray=getGroups($gtsserver, $gtsusername, $gtspassword,$gtsdb,$accountID,$userID);
}
}else{
$groupArray=getGroups($gtsserver, $gtsusername, $gtspassword,$gtsdb,$accountID,$userID);
}
}else{
$groupArray[0]=$group;
}
$ydata2 = array();
$groupLevel='';
$groupID='';
$result1='';
$inzonestatement="";
if($accountID=="vedanta"){
$inzonestatement="In Zone(Vedantaboundary)";
}
$summaryReport="<html><head><style type='text/css'>
.tablecss {
width:100%;
border-collapse: collapse;
color:black;
background:white;
font-family:Arial,Verdana,sans-serif;
font-size:13;
}
</style><script>
</script></head><body><div id='printarea'><center><h3>Summary $inzonestatement Over Speed Report(More than $speedlimit Kmph)</h3>User ID:<b style='font-size:16px' >$userID </b>|Branch:<b style='font-size:16px' >$group</b> <br> From:".date('d-M-Y h:i:s',$fromDate )."  To:".date('d-M-Y h:i:s',$toDate )." <table border='1' class='tablecss'><thead style='background:black;color:white'><th>S.NO</th><th>Vehicle ID</th><th>Branch</th><th>Group</th><th>No Of Times Crossed</th></thead><tbody>";
$table="<center>Detailed Report <table border='1' class='tablecss'><thead style='background:black;color:white'><th>Vehicle ID</th><th>Branch</th><th>Group</th><th>Speed</th><th>Address</th><th>Lat/Long</th><th>Time</th></thead><tbody>";
$sno=1;
$overspeedvehiclecount=0;
//$diff1 = abs($fromDate-time())/86400;
//$diff2 = abs($toDate-time())/86400;
//$server = $gtsserver;
//if($diff1>120 || $diff2>120){
//  $server = $backup_server;
//}
$server = $gtsserver;
$from = date('d-m-Y',(strtotime($_GET['fromDate'])));
$to = date('d-m-Y',(strtotime($_GET['toDate'])));
if($from!=date('d-m-Y') || $to!=date('d-m-Y')){
$server = $backup_server;
}
$gtsconnect = mysqli_connect($server, $gtsusername, $gtspassword) or die ("Unable to connect to the database: " . mysqli_error($gtsconnect));
mysqli_select_db($gtsconnect,$gtsdb);
$tacconnect = mysqli_connect($tacserver, $tacusername, $tacpassword) or die ("Unable to connect to the database: " . mysqli_error($tacconnect));
mysqli_select_db($tacconnect,$routeScheduleDB);
$appResponce=array();
for($gpindx=0;$gpindx<count($groupArray);$gpindx++){
$lastLocaton=array(array());
$groupID=$groupArray[$gpindx];
getLastLocation();
$countoverspeed=0;
for($lve=0;$lve<count($lastLocaton);$lve++){
$tripscount=0;
$tripsstartEndtime=array(array());
$tabletmp="";
if($accountID=="mythrilaxmipur"){
$scheduleqry="select * from RouteSchedule a ,RouteVehicles b where a.accountID=b.accountId and a.routeID=b.RouteId and b.accountID='$accountID'  and b.vehicleId='".$lastLocaton[$lve][0]."'";
// echo $scheduleqry;
$qry1 = mysqli_query($tacconnect,$scheduleqry) or die(mysqli_error($tacconnect));
// $qry1=$db->querySchool($scheduleqry);
while($row = mysqli_fetch_assoc($qry1)){
//stops for schedule
$scheduleID=$row["scheduleID"];
$routeID=$row["routeID"];
$bid=$row["branchID"];
$stopsqry = "select a.orderNo,a.stopID,b.description,b.latitude1,b.longitude1,a.arrivalTime,a.departTime from ScheduleStops a,gts.Geozone b where a.accountID='$accountID' and a.routeID='$routeID' and a.branchID='$bid' and a.scheduleID='$scheduleID' and b.geozoneID=a.stopID and b.accountID='$accountID'  order by a.orderNo asc";
// echo $stopsqry;
$stops = mysqli_query($tacconnect,$stopsqry) or die(mysqli_error($tacconnect));
// $stops=$db->querySchool($stopsqry);
$firststop="";
$endstop="";
$sno=0;
while($row1 = mysqli_fetch_assoc($stops)){
if($sno==0){
$firststop=$row1["stopID"];
}
$sno++;
$endstop=$row1["stopID"];
}
getZoneReport($accountID,$lastLocaton[$lve][0],$fromDate,$toDate,$firststop, $endstop,$gtsconnect);
}
}else{
$tripsstartEndtime[0][0]=$fromDate;
$tripsstartEndtime[0][1]=$toDate;
$tripscount++;
}
// echo $lastLocaton[$lve][0]. "Trip Count:".$tripscount."<br>";
//  var_dump( $tripsstartEndtime);
if($tripscount>0){
$countoverspeed=0;
$desc=explode("]",$lastLocaton[$lve][6]);
$rid=trim($desc[1],'[');
//   $table .='<tr><th colspan="7"> Vehicle ID: '.$rid.'_'.strtoupper($lastLocaton[$lve][0]).' </th></tr><tr>';
for($rindx=0;$rindx<count($tripsstartEndtime);$rindx++){
$fromDateTmp=$tripsstartEndtime[$rindx][0];
$toDateTmp=$tripsstartEndtime[$rindx][1];
// trips start
$deviceLevel='';
$ydata  =array(array());
$cumulativeDistance=0;
//  $query = "select timestamp,deviceID,analog0,statusCode,speedKPH,from_unixtime( timestamp, '%d-%m-%Y %h:%i:%s %p' ) as time1,odometerOffsetKM+odometerKM as 'odometerKM',latitude,longitude,address,analog1,heading as direction from EventData where accountID='$accountID' and deviceID='".$lastLocaton[$lve][0]."'  and timestamp between '$fromDate' and '$toDate' and speedKPH > '$speedlimit' and statusCode='61714' order by timestamp asc";//
$speedmaxlimit=120;
if($accountID=="vedanta"){
$speedmaxlimit=50;
}
//and geozoneID='$zoneID' 
$query = "select timestamp,deviceID,analog0,statusCode,speedKPH,from_unixtime( timestamp, '%d-%m-%Y %h:%i:%s %p' ) as time1,odometerOffsetKM+odometerKM as 'odometerKM',latitude,longitude,address,analog1,heading as direction from EventData where accountID='$accountID' and deviceID='".$lastLocaton[$lve][0]."'  and timestamp between '$fromDateTmp' and '$toDateTmp' and speedKPH > '$speedlimit' and speedKPH < '$speedmaxlimit' and statusCode='61714' order by timestamp asc";
if($accountID=="vedanta"){
$query = "select timestamp,deviceID,analog0,statusCode,speedKPH,from_unixtime( timestamp, '%d-%m-%Y %h:%i:%s %p' ) as time1,odometerOffsetKM+odometerKM as 'odometerKM',latitude,longitude,address,analog1,heading as direction from EventData where accountID='$accountID' and deviceID='".$lastLocaton[$lve][0]."'  and timestamp between '$fromDateTmp' and '$toDateTmp' and speedKPH > '$speedlimit' and speedKPH < '$speedmaxlimit' and statusCode='61714' and geozoneID='vedantaboundary' order by timestamp asc";
}
echo  $query;
$qry_result = mysqli_query($gtsconnect,$query) or die(mysqli_error($gtsconnect));
// echo $query; 
$j=0;
$catchOverspeedEventtime='no';
$vehiclestatrt='';
$findOverspeedIntervelTime=0;
$overspeeddata=array();
while($row = mysqli_fetch_assoc($qry_result))
{   
$ydata[$j][1]=$row['statusCode'];
$ydata[$j][2]= floor($row['speedKPH']);
$ydata[$j][3]=date('d-m-Y H:i:s',$row['timestamp']);
$ydata[$j][4]=$row['timestamp'];
$ydata[$j][5]=round($row['latitude'],5);
$ydata[$j][6]=round($row['longitude'],5);
$ydata[$j][7]=$row['address'];
$ydata[$j][8]=$row['deviceID'];
$dis11 = explode("]",$lastLocaton[$lve][2]);
$ydata[$j][9]=trim($dis11[2],'[');
$ydata[$j][10]=trim($dis11[1],'[');
$ydata[$j][11]=$lastLocaton[$lve][3];
$ydata[$j][13]=$lastLocaton[$lve][4];
$desc=explode("]",$lastLocaton[$lve][6]);
$rid=trim($desc[1],'[');
if($vehiclestatrt==$ydata[$j][8]){
$diff=($row['timestamp']-$findOverspeedIntervelTime);///60;
//   $diff=($row['timestamp']-$ydata[$j-1][4])/60;
if($diff>$overspeedintervel){ 
$overspeeddata[$countoverspeed]["speed"]=$ydata[$j][2];
$overspeeddata[$countoverspeed]["address"]=$ydata[$j][7];
$overspeeddata[$countoverspeed]["latlng"]=$ydata[$j][5]."/".$ydata[$j][6];
$overspeeddata[$countoverspeed]["time"]=$ydata[$j][3];
$countoverspeed++;
$findOverspeedIntervelTime=$ydata[$j][4];
if($accountID=="srinivasa") {
$tabletmp .='<tr><td>'.$rid.'_'.strtoupper($ydata[$j][8]).'</td><td>  '.strtoupper($ydata[$j][9]).'</td><td>  '.strtoupper($ydata[$j][10]).' </td><td> '.$ydata[$j][2].' Kmph</td><td>  '.$ydata[$j][7]."</td><td>".$ydata[$j][5]."/".$ydata[$j][6]."</td><td>".$ydata[$j][3]."</td></tr>";
}  else {
$tabletmp .='<tr><td>'.strtoupper($ydata[$j][8]).'</td><td>  '.strtoupper($ydata[$j][9]).'</td><td>  '.strtoupper($ydata[$j][10]).' </td><td> '.$ydata[$j][2].' Kmph</td><td>  '.$ydata[$j][7]."</td><td>".$ydata[$j][5]."/".$ydata[$j][6]."</td><td>".$ydata[$j][3]."</td></tr>";
}
}
}
if($vehiclestatrt!=$ydata[$j][8] ){ 
$vehiclestatrt=$ydata[$j][8];
$overspeeddata[$countoverspeed]["speed"]=$ydata[$j][2];
$overspeeddata[$countoverspeed]["address"]=$ydata[$j][7];
$overspeeddata[$countoverspeed]["latlng"]=$ydata[$j][5]."/".$ydata[$j][6];
$overspeeddata[$countoverspeed]["time"]=$ydata[$j][3];
$countoverspeed++;
$findOverspeedIntervelTime=$ydata[$j][4];
//  $table .='<tr><th colspan="6"> Vehicle ID: '.$rid.'_'.strtoupper($ydata[$j][8]).' </th></tr><tr><td>'. strtoupper($ydata[$j][8]).'</td><td>  '.strtoupper($ydata[$j][9]).'</td><td>  '.strtoupper($ydata[$j][10]).' </td><td> '.$ydata[$j][2].' Kmph</td><td>  '.$ydata[$j][7]."</td><td>".$ydata[$j][5]."/".$ydata[$j][6]."</td><td>".$ydata[$j][3]."</td></tr>";
$tabletmp .='<tr><td>'. strtoupper($ydata[$j][8]).'</td><td>  '.strtoupper($ydata[$j][9]).'</td><td>  '.strtoupper($ydata[$j][10]).' </td><td> '.$ydata[$j][2].' Kmph</td><td>  '.$ydata[$j][7]."</td><td>".$ydata[$j][5]."/".$ydata[$j][6]."</td><td>".$ydata[$j][3]."</td></tr>";
}
$j++;
}//while
unset($ydata);
}//trips end loop
if($countoverspeed>0){
$appResponce[$overspeedvehiclecount]["groupID"]=$groupID;
$appResponce[$overspeedvehiclecount]["vehicleID"]=$lastLocaton[$lve][0];
$appResponce[$overspeedvehiclecount]["description"]=$lastLocaton[$lve][6];
$appResponce[$overspeedvehiclecount]["displayName"]=$lastLocaton[$lve][7];
$appResponce[$overspeedvehiclecount]["noofcrossed"]=$countoverspeed;
$appResponce[$overspeedvehiclecount]["speedlimit"]=$speedlimit;
$appResponce[$overspeedvehiclecount]["data"]=$overspeeddata; 
$overspeedvehiclecount++;
$dis11 = explode("]",$lastLocaton[$lve][2]);
$table .='<tr><th colspan="7"> Vehicle ID: '.$rid.'_'.strtoupper($lastLocaton[$lve][0]).' </th></tr><tr>'.$tabletmp;
$summaryReport .="<tr><td>".($sno++)."</td><td>".$rid.'__'.strtoupper($lastLocaton[$lve][0])."</td><td>".strtoupper(trim($dis11[2],'['))."</td><td>".strtoupper(trim($dis11[1],'['))."</td><td>".$countoverspeed."</td></tr>";
}
}//trip count end if
}
}
echo '{"status":"true","markers": ' . json_encode(utf8ize($appResponce)) .'}';
//echo $summaryReport.'</tbody></table>'.$table.'</tbody></table></div></center></body></html>';
mysqli_close($gtsconnect);
mysqli_close($tacconnect);
function utf8ize($d) {
if (is_array($d)) {
foreach ($d as $k => $v) {
$d[$k] = utf8ize($v);
}
} else if (is_string ($d)) {
return utf8_encode($d);
}
return $d;
}
function getZoneReport($accountID,$deviceID,$fromDate1,$toDate,$startZone,$endZone,$gtsconnect){
global $db,$tripsstartEndtime,$tripscount,$gtsserver, $gtsusername, $gtspassword,$gtsdb;
//  $gtsconnect = mysqli_connect($gtsserver, $gtsusername, $gtspassword) or die ("Unable to connect to the database: " . mysqli_error($gtsconnect));
//  mysqli_select_db($gtsconnect,$gtsdb);
$zoneqry="select a.geozoneID,b.description,timestamp,deviceID,analog0,statusCode,speedKPH,from_unixtime( timestamp, '%d-%m-%Y %h:%i:%s %p' ) as time1,odometerOffsetKM+odometerKM as 'odometerKM',latitude,longitude,address,analog1,heading as direction from EventData a,Geozone b where a.accountID='$accountID' and deviceID='".$deviceID."'  and timestamp between '$fromDate1' and '$toDate'  and a.geozoneID=b.geozoneID and statusCode in (61968,62000) and b.accountID='$accountID'   order by timestamp asc";// a.geozoneID in ('$startZone','$endZone') order by timestamp asc";
$evnts=mysqli_query($gtsconnect,$zoneqry) or die(mysqli_error($gtsconnect));//$db->queryGTS($zoneqry);
$tripfound="";
$tripstarttime=0;
$tripendtime=0;
$foundzone="";
// echo "First Sript============================<br>";
while($row1 = mysqli_fetch_assoc($evnts)){
//  echo $row1["geozoneID"]."  ".$row1["statusCode"]."  ".$row1["timestamp"]."<br>";
//find first depart
if((strtolower($startZone)==strtolower($row1["geozoneID"]) || strtolower($endZone)==strtolower($row1["geozoneID"])) && $row1["statusCode"]=="62000" && $tripfound==""){
$tripfound="yes";
//echo "Trip Start ".$row1["geozoneID"]."==== End zone:".$endZone." <br>";
$tripstarttime=$row1["timestamp"];
$foundzone=$row1["geozoneID"];
}
//for last varlid depart
if((strtolower($startZone)==strtolower($row1["geozoneID"]) || strtolower($endZone)==strtolower($row1["geozoneID"])) &&  $foundzone==$row1["geozoneID"] && ($row1["statusCode"]=="62000" || $row1["statusCode"]=="61968") && $tripfound=="yes"){
//echo "Valid ".$foundzone."  ".$row1["geozoneID"]."<br>";
$tripfound="yes";
$tripstarttime=$row1["timestamp"];
$foundzone=$row1["geozoneID"];
}
// echo "middle==".$row1["geozoneID"]."  ".$foundzone."  ".$tripfound."   ".$row1["statusCode"]."<br>";
if((strtolower($endZone)==strtolower($row1["geozoneID"]) || strtolower($startZone)==strtolower($row1["geozoneID"])) && $foundzone!=$row1["geozoneID"] && $tripfound=="yes" && ($row1["statusCode"]=="62000" || $row1["statusCode"]=="61968")){
//  echo "Trip End<br>";
$noincompletedtrips++;
$tripendtime=$row1["timestamp"];
$tripsstartEndtime[$tripscount][0]=$tripstarttime;
$tripsstartEndtime[$tripscount][1]=$tripendtime;
$tripscount++;
//  echo "Trip count".$tripscount;
$tripfound="yes";
//   echo "Trip Start second ".$row1["geozoneID"]."====  <br>";
$tripstarttime=$row1["timestamp"];
$foundzone=$row1["geozoneID"];
// $table .="<tr><td>".($rowcnt++)."</td><td>".$group."</td><td>".$deviceID."</td><td>".$routeID."</td><td>".$startZone."</td><td>".date('d-m-Y H:i:s',$tripstarttime)."</td><td>".$endZone."</td><td>".date('d-m-Y H:i:s',$tripendtime)."</td><td><a href='".ROOT_URL."pages/report/historytracking/?groupID=".$group."&vehicleID=".$deviceID."&routeID=".$routeID."&fromDate=".date('d-m-Y H:i:s',$tripstarttime)."&toDate=".date('d-m-Y H:i:s',$tripendtime)."' target='_blank'>route</a></td></tr>";
$tripendtime=0;
$tripstarttime=$row1["timestamp"];
}
//$eventTime=$row1["timestamp"];
}
//   mysqli_close($gtsconnect);
}
function getLastLocation(){
global $gtsserver, $gtsusername, $gtspassword,$gtsdb,$accountID,$lastLocaton,$userID,$groupID,$deviceID;
$gtsconnect = mysqli_connect($gtsserver, $gtsusername, $gtspassword) or die ("Unable to connect to the database: " . mysqli_error($gtsconnect));
mysqli_select_db($gtsconnect,$gtsdb);
$vehicleGroup=array();
$vehiclecontact=array();
$vehiclecontact=  vehiclecontact();
$query="select a.displayName,a.deviceID as deviceID,a.lastGPSTimestamp as lastGPSTimestamp,a.description as description,a.vehicleModel as vehicleModel,a.uniqueID as uniqueID from Device a,DeviceList b where a.accountID='$accountID' and b.groupID='$groupID' and b.accountID='$accountID' and a.deviceID=b.deviceID ";
$vehicleGroup=getGroupVehicle(); 
if(($userID == "admin" || $userID == "administrator") && $groupID=="selectall"){
$vehicleGroup=getGroupVehicle();
$query = "SELECT a.displayName,a.deviceID as deviceID,a.lastGPSTimestamp as lastGPSTimestamp,a.description as description,a.vehicleModel as vehicleModel,a.uniqueID as uniqueID FROM Device a WHERE a.accountID ='$accountID' ";
}
if($deviceID != 'selectall'){
$vehicleGroup=getGroupVehicle();
$query = "SELECT a.displayName,a.deviceID as deviceID,a.lastGPSTimestamp as lastGPSTimestamp,a.description as description,a.vehicleModel as vehicleModel,a.uniqueID as uniqueID FROM Device a WHERE a.accountID ='$accountID' and a.deviceID='$deviceID' ";
}
// echo $query;
$qry_result = mysqli_query($gtsconnect, $query) or die(mysqli_error($gtsconnect));
$i=0;
while($row = mysqli_fetch_assoc($qry_result)){
$lastLocaton[$i][0]=$row['deviceID'];
$lastLocaton[$i][1]=$row['lastGPSTimestamp'];
if($userID == "admin" || $userID == "administrator" || $userID=="tracking" || $userID=="monitoring" || $userID == "avoidDuplecate"){
if($vehicleGroup[$row['deviceID']]=="" || $vehicleGroup[$row['deviceID']]==null){  $vehicleGroup[$row['deviceID']]="ungroup";}
$lastLocaton[$i][2]="[".$row['deviceID']."][".$vehicleGroup[$row['deviceID']]."][".$vehicleGroup[$row['deviceID']]."]";
} else{
if($row['groupID']==""){$row['groupID']="ungroup";}
$lastLocaton[$i][2]="[".$row['deviceID']."][".$row['groupID']."][".$row['groupID']."]";
}
//       $lastLocaton[$i][2]=$row['description'];
$lastLocaton[$i][3]=$row['vehicleModel'];
$lastLocaton[$i][4]=$vehiclecontact[$row['deviceID']]==null?"0000000000":$vehiclecontact[$row['deviceID']];//$row['simPhoneNumber'];
$lastLocaton[$i][5]=$row['uniqueID'];
$lastLocaton[$i][6]=$row['description'];
$lastLocaton[$i][7]=$row['displayName']==""?$row['deviceID']:$row['displayName'];;
//  echo 'raman';
$i++;
}
mysqli_close($gtsconnect);
}
function getGroupVehicle(){
global $id,$gtsserver,$gtsusername,$gtspassword,$gtsdb,$accountID;
$result=array();
$gtsconnect = mysqli_connect($gtsserver, $gtsusername, $gtspassword) or die ("Unable to connect to the database: " . mysqli_error($tacconnect));
mysqli_select_db($gtsconnect,$gtsdb);
$query="select groupID,deviceID from DeviceList where accountID='$accountID'";
$qry1 = mysqli_query($gtsconnect,$query) or die(mysqli_error($gtsconnect));
$rowcount=0;
while($row = mysqli_fetch_assoc($qry1)){
$result[$row['deviceID']]=$row['groupID'];
}
if($rowcount==0){
$result[]='';
}
mysqli_close($gtsconnect);
return $result;
}
function vehiclecontact(){
global $gtsserver, $gtsusername, $gtspassword,$gtsdb,$accountID,$userID,$groupID;
$gtsconnect = mysqli_connect($gtsserver, $gtsusername, $gtspassword) or die ("Unable to connect to the database: " . mysqli_error($gtsconnect));
mysqli_select_db($gtsconnect,$gtsdb);
$query="select deviceID,contactPhone from Driver  where accountID='$accountID'";
$lastLocaton=array();
$qry_result = mysqli_query($gtsconnect,$query) or die(mysqli_error($gtsconnect));
while($row = mysqli_fetch_assoc($qry_result)){
$lastLocaton[$row['deviceID']]=$row['contactPhone'];
}
mysqli_close($gtsconnect);
return $lastLocaton;
}
function OverSpeedlimitAndIntervel(){
global $tacserver, $tacusername, $tacpassword,$tacdb,$accountID,$userID,$groupID,$overspeedintervel,$speedlimit;
$tacconnect = mysqli_connect($tacserver, $tacusername, $tacpassword) or die ("Unable to connect to the database: " . mysqli_error($tacconnect));
mysqli_select_db($tacconnect,$tacdb);
$query="select overspeedlimit,overspeedintervel from AccountUserSettings  where accountID='$accountID' and userID='$userID'";
$lastLocaton=array();
$qry_result = mysqli_query($tacconnect,$query) or die(mysqli_error($tacconnect));
while($row = mysqli_fetch_assoc($qry_result)){
// $speedlimit=$row['overspeedlimit'];
$overspeedintervel=$row["overspeedintervel"];
}
mysqli_close($tacconnect);
}
function getGroups($gtsserver, $gtsusername, $gtspassword,$gtsdb,$accounts,$userID){
$result=array();
$gtsconnect = mysqli_connect($gtsserver, $gtsusername, $gtspassword) or die ("Unable to connect to the database: " . mysqli_error($gtsconnect));
mysqli_select_db($gtsconnect,$gtsdb);
$vehicleDetails='';
if($userID == "admin" || $userID == "administrator"){
$vehicleDetails="select DISTINCT groupID from DeviceGroup where accountID='$accounts'";
}else{
$vehicleDetails="select DISTINCT groupID from GroupList where accountID='$accounts' and userID='$userID'";
}
error_log($vehicleDetails);
$qry1 = mysqli_query($gtsconnect,$vehicleDetails) or die(mysqli_error($gtsconnect));
$rowcount=0;
while($row = mysqli_fetch_assoc($qry1)){
$result[$rowcount]=$row['groupID'];
$rowcount++;
}
if($rowcount==0){
$result[$rowcount]='';
}
mysqli_close($gtsconnect);
return $result;
}
function distance1($lat1, $lon1, $lat2, $lon2) {
if($lat1 == $lat2 && $lon1 == $lon2){
return 0;
}else{$theta = $lon1 - $lon2;
$dist = sin(deg2rad($lat1)) * sin(deg2rad($lat2)) +  cos(deg2rad($lat1)) * cos(deg2rad($lat2)) * cos(deg2rad($theta));
$dist = acos($dist);
$dist = rad2deg($dist);
$miles = $dist * 60 * 1.1515;
return $miles* 1.609344;}
}
?>