<?php 
  include "settings.php";
  include "logger.php";
  
  $db = new mysqli($DB_HOST,$DB_USER,$DB_PASS,$DB_NAME);
  $db->query("SET CHARSET SET utf8");
  $db->query("SET NAMES 'utf8'");
  if (mysqli_connect_errno()) {
    echo "Error connecting to DB";
    exit;
  }
  writeToLogFile( ' -> '.date('Y-m-d H:i:s').' <> synchro <> '.$_POST['data'] );

  $data = $_POST['data'];
  $data = stripslashes($data);
  $arraydata = (array)json_decode($data);
    
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 
  if ($arraydata['func']=='tms_facttrip')
  {
	//$startLine = $arraydata["startLine"];
	$lastsynch = $arraydata["lastsynch"];
	$iduser = $arraydata["iduser"];
	
	/*if($lastsynch){
		$where = " AND tt.`Last_modification_time`>'".$lastsynch."'";
	}else{
		$where = "AND tt.`StartTime` BETWEEN '".date('Y-m-d')." 00:00:00' AND '".date('Y-m-d')." 23:59:59'";
	}*/
	
	/*if($lastsynch){
		$where = "AND tt.`StartTime` BETWEEN '".$lastsynch." 00:00:00' AND '".date('Y-m-d')." 23:59:59' AND tp.`Executing`=1";
	}else{
		$mn = strtotime(date('Y-m-d',mktime(1,1,1,date('m'),date('d')-3,date('Y'))));
		$nmn = date('Y-m-d',$mn);
		$where = "AND tt.`StartTime` BETWEEN '".$nmn." 00:00:00' AND '".date('Y-m-d')." 23:59:59' AND tp.`Executing`=1";
	}*/
	
	//$where = "AND tt.`StartTime` BETWEEN '".date('Y-m-d')." 00:00:00' AND '".date('Y-m-d')." 23:59:59' AND tp.`Executing`=1";
	$where = "AND tt.`StartTime` BETWEEN '".date('Y-m-d')." 00:00:00' AND '".date('Y-m-d')." 23:59:59'";
	
	$i=0;

	$query = "SELECT tt.`ID`, tt.`Closed`, tt.`Project_Id`, tt.`Car_ID`, tt.`Name`, tt.`WalkerNum`, tt.`TripPrice`, tt.`Distance`, tt.`Time`, tt.`Comments`, tt.`StartTime`, tt.`EndTime`, td.`ID` FROM `tms_facttrip` tt LEFT JOIN `tms_car` tc ON tt.`Car_ID`=tc.`Car_ID` LEFT JOIN `tms_device` td ON tc.`Device_id`=td.`ID` INNER JOIN `tms_project` tp ON tt.`Project_Id`=tp.`Project_ID` WHERE td.`ID`=".$iduser." ".$where.";";
	//$query = "SELECT tt.`ID`, tt.`Closed`, tt.`Project_Id`, tt.`Car_ID`, tt.`Name`, tt.`WalkerNum`, tt.`TripPrice`, tt.`Distance`, tt.`Time`, tt.`Comments`, tt.`StartTime`, tt.`EndTime`, td.`ID`,tt.`Last_modification_time` FROM `tms_facttrip` tt LEFT JOIN `tms_car` tc ON tt.`Car_ID`=tc.`Car_ID` LEFT JOIN `tms_device` td ON tc.`Device_id`=td.`ID` INNER JOIN `tms_project` tp ON tt.`Project_Id`=tp.`Project_ID` WHERE td.`ID`=".$iduser." ".$where.";";
  
  //$query = "SELECT tt.`ID`, tt.`Closed`, tt.`Project_Id`, tt.`Car_ID`, tt.`Name`, tt.`WalkerNum`, tt.`TripPrice`, tt.`Distance`, tt.`Time`, tt.`Comments`, tt.`StartTime`, tt.`EndTime`, td.`ID` FROM `tms_facttrip` tt LEFT JOIN `tms_car` tc ON tt.`Car_ID`=tc.`Car_ID` LEFT JOIN `tms_device` td ON tc.`Device_id`=td.`ID` INNER JOIN `tms_project` tp ON tt.`Project_Id`=tp.`Project_ID` WHERE td.`ID`=(SELECT t_cr.`Device_id` FROM tms_driver t_dr LEFT JOIN tms_car t_cr ON t_dr.`Driver_ID`=t_cr.`Driver_id` WHERE t_cr.`DeletedRecord`=0 AND t_dr.`DeletedRecord`=0 AND t_dr.`Device_id`=".$iduser." LIMIT 1) ".$where;
	//$query = "SELECT tt.`ID`, tt.`Closed`, tt.`Project_Id`, tt.`Car_ID`, tt.`Name`, tt.`WalkerNum`, tt.`TripPrice`, tt.`Distance`, tt.`Time`, tt.`Comments`, tt.`StartTime`, tt.`EndTime`, td.`ID` FROM `tms_facttrip` tt LEFT JOIN `tms_car` tc ON tt.`Car_ID`=tc.`Car_ID` LEFT JOIN `tms_device` td ON tc.`Device_id`=td.`ID` WHERE td.`ID`=".$iduser." ";
    writeToLogFile( ' -> '.date('Y-m-d H:i:s').' <> synchro tms_facttrip <> '.$query );
	if ($stmt = $db->prepare($query)){
      $stmt->execute();
	  $stmt->bind_result($TripId,$Closed,$Project_Id,$Car_ID,$Name,$WalkerNum,$TripPrice,$Distance,$Time,$Comments,$StartTime,$EndTime,$User_ID);
      //$stmt->bind_result($TripId,$Closed,$Project_Id,$Car_ID,$Name,$WalkerNum,$TripPrice,$Distance,$Time,$Comments,$StartTime,$EndTime,$User_ID,$Last_modification_time);
      $Model = array();
	  while ($stmt->fetch()) {
        $Model[$i]= 
        array(
		'TID'=>$TripId,
    'CL'=>$Closed,
		'PID'=>$Project_Id,
		'CID'=>$Car_ID,
		'N'=>addslashes($Name),
		'WN'=>$WalkerNum,
		'PR'=>$TripPrice,
		'D'=>$Distance,
		'T'=>$Time,
		'C'=>addslashes($Comments),
		'ST'=>$StartTime,
		'ET'=>$EndTime,
		'UID'=>$User_ID//,
		//'LM'=>addslashes($Last_modification_time)
        );
        $i++;
      }
	  
	/*if($i < 1000){
      $arrayEndModel = array('EndModel'=>'true');
      array_push($Model,$arrayEndModel);
    }*/
	
      $stmt->close();
      $db->close();
    }
    echo json_encode($Model);
	writeToLogFile( ' -> '.date('Y-m-d H:i:s').' <> synchro tms_facttrip <> '.json_encode($Model) );
  }
  
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////  
  if ($arraydata['func']=='tms_checkpoint')
  {
	//$startLine = $arraydata["startLine"];
	
	$lastsynch = $arraydata["lastsynch"];
	$iduser = $arraydata["iduser"];
	
	/*if($lastsynch){
		$where = " AND fc.`Last_modification_time`>'".$lastsynch."'";
	}else{
		$where = "AND ft.`StartTime` BETWEEN '".date('Y-m-d')." 00:00:00' AND '".date('Y-m-d')." 23:59:59'";
	}*/
	
	/*if($lastsynch){ $where = " AND ft.`StartTime` BETWEEN '".$lastsynch." 00:00:00' AND '".date('Y-m-d')." 23:59:59'"; }else{
		$mn = strtotime(date('Y-m-d',mktime(1,1,1,date('m'),date('d')-3,date('Y'))));
		$nmn = date('Y-m-d',$mn);
		$where = "AND ft.`StartTime` BETWEEN '".$nmn." 00:00:00' AND '".date('Y-m-d')." 23:59:59'";
	}*/
	
	$where = "AND ft.`StartTime` BETWEEN '".date('Y-m-d')." 00:00:00' AND '".date('Y-m-d')." 23:59:59'";
	
	$i=0;
    
  $query = "SELECT ft.`ID`,fc.`Id`,fc.`StartCheckTime`,fc.`EndCheckTime`,fc.`Description`,fc.`Necessary`,fc.`Name`,fc.`GeoX`,fc.`GeoY`,fc.`PointState`,fc.`Date`,fc.`OrderInTrip`,fc.`Distance`,fc.`Time`,fc.`WaitTime`,fc.`LateTime`,ASTEXT(fc.`Route`),fc.`EstimateTime`,fc.`Address`,fc.`DurationInPoint`,fc.`IsOrderPoint`,fc.`StartWorkTime`,fc.`EndWorkTime`,fc.`Code`,fc.`ClientName` FROM `tms_facttrip` ft INNER JOIN `tms_checkpoint` fc ON ft.`ID`=fc.`FactTripID` LEFT JOIN `tms_car` tc ON ft.`Car_ID`=tc.`Car_ID` LEFT JOIN `tms_device` td ON tc.`Device_id`=td.`ID` WHERE td.`ID`=".$iduser." ".$where." ORDER BY fc.`EstimateTime` ASC;";
  //$query = "SELECT ft.`ID`,fc.`Id`,fc.`StartCheckTime`,fc.`EndCheckTime`,fc.`Description`,fc.`Necessary`,fc.`Name`,fc.`GeoX`,fc.`GeoY`,fc.`PointState`,fc.`Date`,fc.`OrderInTrip`,fc.`Distance`,fc.`Time`,fc.`WaitTime`,fc.`LateTime`,ASTEXT(fc.`Route`),fc.`EstimateTime`,fc.`Address`,fc.`DurationInPoint`,fc.`IsOrderPoint`,fc.`StartWorkTime`,fc.`EndWorkTime`,fc.`Code`,fc.`ClientName`,fc.`Last_modification_time` FROM `tms_facttrip` ft INNER JOIN `tms_checkpoint` fc ON ft.`ID`=fc.`FactTripID` LEFT JOIN `tms_car` tc ON ft.`Car_ID`=tc.`Car_ID` LEFT JOIN `tms_device` td ON tc.`Device_id`=td.`ID` WHERE td.`ID`=".$iduser." ".$where." ORDER BY fc.`EstimateTime` ASC;";
  
  //$query = "SELECT ft.`ID`,fc.`Id`,fc.`StartCheckTime`,fc.`EndCheckTime`,fc.`Description`,fc.`Necessary`,fc.`Name`,fc.`GeoX`,fc.`GeoY`,fc.`PointState`,fc.`Date`,fc.`OrderInTrip`,fc.`Distance`,fc.`Time`,fc.`WaitTime`,fc.`LateTime`,ASTEXT(fc.`Route`),fc.`EstimateTime`,fc.`Address`,fc.`DurationInPoint`,fc.`IsOrderPoint`,fc.`StartWorkTime`,fc.`EndWorkTime`,fc.`Code`,fc.`ClientName` FROM `tms_facttrip` ft INNER JOIN `tms_checkpoint` fc ON ft.`ID`=fc.`FactTripID` LEFT JOIN `tms_car` tc ON ft.`Car_ID`=tc.`Car_ID` LEFT JOIN `tms_device` td ON tc.`Device_id`=td.`ID` WHERE td.`ID`=(SELECT t_cr.`Device_id` FROM tms_driver t_dr LEFT JOIN tms_car t_cr ON t_dr.`Driver_ID`=t_cr.`Driver_id` WHERE t_cr.`DeletedRecord`=0 AND t_dr.`DeletedRecord`=0 AND t_dr.`Device_id`=".$iduser." LIMIT 1) ".$where." ORDER BY fc.`EstimateTime` ASC";
	//$query = "SELECT ft.`ID`,fc.`Id`,fc.`StartCheckTime`,fc.`EndCheckTime`,fc.`Description`,fc.`Necessary`,fc.`Name`,fc.`GeoX`,fc.`GeoY`,fc.`PointState`,fc.`Date`,fc.`OrderInTrip`,fc.`Distance`,fc.`Time`,fc.`WaitTime`,fc.`LateTime`,ASTEXT(fc.`Route`),fc.`EstimateTime`,fc.`Address`,fc.`DurationInPoint`,fc.`IsOrderPoint`,fc.`StartWorkTime`,fc.`EndWorkTime`,fc.`Code`,fc.`ClientName` FROM `tms_facttrip` ft INNER JOIN `tms_checkpoint` fc ON ft.`ID`=fc.`FactTripID` LEFT JOIN `tms_car` tc ON ft.`Car_ID`=tc.`Car_ID` LEFT JOIN `tms_device` td ON tc.`Device_id`=td.`ID` WHERE td.`ID`=".$iduser." ORDER BY fc.`EstimateTime` ASC";
	writeToLogFile( ' -> '.date('Y-m-d H:i:s').' <> synchro tms_checkpoint <> '.$query );
	if($stmt = $db->prepare($query)){
      $stmt->execute();
      $stmt->bind_result($TripId,$Id,$StartCheckTime,$EndCheckTime,$Description,$Necessary,$Name,$GeoX,$GeoY,$PointState,$Date,$OrderInTrip,$Distance,$Time,$WaitTime,$LateTime,$Route,$EstimateTime,$Address,$DurationInPoint,$Type,$StartWorkTime,$EndWorkTime,$Code,$ClientName);
	  //$stmt->bind_result($TripId,$Id,$StartCheckTime,$EndCheckTime,$Description,$Necessary,$Name,$GeoX,$GeoY,$PointState,$Date,$OrderInTrip,$Distance,$Time,$WaitTime,$LateTime,$Route,$EstimateTime,$Address,$DurationInPoint,$Type,$StartWorkTime,$EndWorkTime,$Code,$ClientName,$Last_modification_time);
      $Model = array();
	  while ($stmt->fetch()){
        $Model[$i]=array(
		'TID'=>$TripId,
		'ID'=>$Id,
        'SCT'=>$StartCheckTime,
		'ECT'=>$EndCheckTime,
		'D'=>addslashes($Description),
		'NSS'=>$Necessary,
		'N'=>addslashes($Name),
		'GX'=>$GeoX,
		'GY'=>$GeoY,
		'PS'=>$PointState,
		'DT'=>$Date,
		'OIT'=>$OrderInTrip,
		'DS'=>$Distance,
		'T'=>$Time,
		'WT'=>$WaitTime,
		'LT'=>$LateTime,
		'R'=>$Route,
		'ET'=>$EstimateTime,
		'A'=>addslashes($Address),
		'DIP'=>$DurationInPoint,
		'TY'=>$Type,
		'SW'=>$StartWorkTime,
		'EW'=>$EndWorkTime,
		'CD'=>$Code,
		'CN'=>addslashes($ClientName)//,
		//'LM'=>addslashes($Last_modification_time)
        );
        $i++;
      }
	
      $stmt->close();
      $db->close();
    }
    echo json_encode($Model);
	writeToLogFile( ' -> '.date('Y-m-d H:i:s').' <> synchro tms_trippoint <> '.json_encode($Model) );
  }
 /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
 if ($arraydata['func']=='tms_checkpointcargo')
 {
	$lastsynch = $arraydata["lastsynch"];
	$iduser = $arraydata["iduser"];
	/*if($lastsynch){ $where = " AND ft.`StartTime` BETWEEN '".$lastsynch." 00:00:00' AND '".date('Y-m-d')." 23:59:59'"; }else{
		$mn = strtotime(date('Y-m-d',mktime(1,1,1,date('m'),date('d')-3,date('Y'))));
		$nmn = date('Y-m-d',$mn);
		$where = "AND ft.`StartTime` BETWEEN '".$nmn." 00:00:00' AND '".date('Y-m-d')." 23:59:59'";
	}*/
	$where = "AND ft.`StartTime` BETWEEN '".date('Y-m-d')." 00:00:00' AND '".date('Y-m-d')." 23:59:59'";
	
	
	$i=0;
/*$query = "SELECT fcc.`ID`,ft.`TripId`,fcc.`CheckPoint_ID`,fcc.`Name`,du.`Name`,fcc.`CargoValueVolume`,fcc.`CargoValueWeight`,fcc.`Comments`,fcc.`State`,fcc.`RealCargoValue`,fcc.`CargoValueCount`,fcc.`Cost` FROM `tms_facttrip` ft
INNER JOIN `tms_checkpoint` fc ON ft.`ID`=fc.`FactTripID`
INNER JOIN `tms_checkpointcargo` fcc ON fc.`Id`=fcc.`CheckPoint_ID`
INNER JOIN `tms_dimensionunit` du ON du.`Id`=fcc.`DimensionUnitWeight_ID`
LEFT JOIN `tms_car` tc ON ft.`Car_ID`=tc.`Car_ID`
LEFT JOIN `tms_device` td ON tc.`Device_id`=td.`ID`
WHERE td.`ID`=".$iduser." ".$where." ORDER BY fc.`EstimateTime` ASC;";*/

/*$query = "SELECT fcc.`ID`,ft.`ID`,fcc.`CheckPoint_ID`,fcc.`Name`,du.`Name`,fcc.`CargoValueVolume`,fcc.`CargoValueWeight`,fcc.`Comments`,fcc.`State`,fcc.`RealCargoValue`,fcc.`CargoValueCount`,fcc.`Cost` FROM `tms_facttrip` ft
INNER JOIN `tms_checkpoint` fc ON ft.`ID`=fc.`FactTripID`
INNER JOIN `tms_checkpointcargo` fcc ON fc.`Id`=fcc.`CheckPoint_ID`
INNER JOIN `tms_dimensionunit` du ON du.`Id`=fcc.`DimensionUnitWeight_ID`
LEFT JOIN `tms_car` tc ON ft.`Car_ID`=tc.`Car_ID`
LEFT JOIN `tms_device` td ON tc.`Device_id`=td.`ID`
WHERE td.`ID`=(SELECT t_cr.`Device_id` FROM tms_driver t_dr LEFT JOIN tms_car t_cr ON t_dr.`Driver_ID`=t_cr.`Driver_id` WHERE t_cr.`DeletedRecord`=0 AND t_dr.`DeletedRecord`=0 AND t_dr.`Device_id`=".$iduser." LIMIT 1)
".$where." ORDER BY fc.`EstimateTime` ASC ";*/

$query = "SELECT fcc.`ID`,ft.`ID`,fcc.`CheckPoint_ID`,fcc.`Name`,du.`Name`,fcc.`CargoValueVolume`,fcc.`CargoValueWeight`,fcc.`Comments`,fcc.`State`,fcc.`RealCargoValue`,fcc.`CargoValueCount`,fcc.`Cost` FROM `tms_facttrip` ft
INNER JOIN `tms_checkpoint` fc ON ft.`ID`=fc.`FactTripID`
INNER JOIN `tms_checkpointcargo` fcc ON fc.`Id`=fcc.`CheckPoint_ID`
INNER JOIN `tms_dimensionunit` du ON du.`Id`=fcc.`DimensionUnitWeight_ID`
LEFT JOIN `tms_car` tc ON ft.`Car_ID`=tc.`Car_ID`
LEFT JOIN `tms_device` td ON tc.`Device_id`=td.`ID`
WHERE td.`ID`=".$iduser."
".$where.";";

	writeToLogFile( ' -> '.date('Y-m-d H:i:s').' <> synchro tms_checkpointcargo <> '.$query );
	
	if($stmt = $db->prepare($query)){
      $stmt->execute();
      $stmt->bind_result($ID,$TripId,$CheckPoint_ID,$Name,$DimensionUnit,$CargoValueVolume,$CargoValueWeight,$Comments,$State,$RealCargoValue,$CargoValueCount,$Cost);
      $Model = array();
	  while ($stmt->fetch()){
        $Model[$i]=array(
		'ID'=>$ID,
		'TID'=>$TripId,
        'CID'=>$CheckPoint_ID,
		'N'=>addslashes($Name),
		'DU'=>$DimensionUnit,
		'CVV'=>$CargoValueVolume,
		'CVW'=>$CargoValueWeight,
		'CO'=>addslashes($Comments),
		'ST'=>$State,
		'RCV'=>$RealCargoValue,
		'CVC'=>$CargoValueCount,
		'CT'=>$Cost
        );
        $i++;
      }
	
      $stmt->close();
      $db->close();
    }
	echo json_encode($Model);
	writeToLogFile( ' -> '.date('Y-m-d H:i:s').' <> synchro tms_checkpointcargo <> '.json_encode($Model) );
 }
 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
 
 /*if ($arraydata['func']=='tms_device')
  {
    $i=0;
      
    $query = "SELECT ID, Login, Pwd, Name, deviceSettings FROM tms_device";
    writeToLogFile( ' -> '.date('Y-m-d H:i:s').' <> synchro tms_checkpoint <> '.$query );
    if($stmt = $db->prepare($query)){
        $stmt->execute();
        $stmt->bind_result($ID, $Login, $Pwd, $Name, $deviceSettings);
        while ($stmt->fetch()){
          $Model[$i]=array(
      'ID'=>$ID,
      'Login'=>$Login,
      'Pwd'=>$Pwd,
      'Name'=>$Name,
      'deviceSettings'=>$deviceSettings
          );
          $i++;
        }
    
        $stmt->close();
        $db->close();
      }
      echo json_encode($Model);
    writeToLogFile( ' -> '.date('Y-m-d H:i:s').' <> synchro tms_trippoint <> '.json_encode($Model) );
  }*/
  
  if ($arraydata['func']=='ms_user')
  {
    //$userId = $arraydata["id"];
    //$lastSynch = $arraydata["lastSynch"];
    $startLine = $arraydata["startLine"];
    $endLine = $startLine + 999;
    
    $i=0;
    $query = "SELECT ID, Login, Pwd, Name, deviceSettings FROM tms_device";
    
    writeToLogFile( ' -> '.date('Y-m-d H:i:s').' <> synchro tms_device <> '.$query );
         
    if($stmt = $db->prepare($query))
    {
      $stmt->execute();
      $stmt->bind_result($ID, $Login, $Pwd, $Name, $deviceSettings);
      $Model = array();
	  while ($stmt->fetch())
      {
        $Model[$i]=array(
                        'id'=>$ID,
                        'login'=>$Login,
                        'password'=>$Pwd,
                        'name'=>addslashes($Name)
                        );
        $i++;
      }
    
      $stmt->close();
      $db->close();
    }
    
    if ($i < 999)
    {
      $arrayEndModel = array('EndModel'=>'true');
      array_push($Model,$arrayEndModel);
    }
    
    echo json_encode($Model);
    writeToLogFile( ' -> '.date('Y-m-d H:i:s').' <> synchro tms_device <> '.json_encode($Model) );
  }
  
 
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 
  if ($arraydata['func']=='tms_mob_task')
  {
    $lastsynch = $arraydata["lastsynch"];
	$iduser = $arraydata["iduser"];
    
	if($lastsynch){
		$where = " AND mt.`Last_modification_time`>'".$lastsynch."'";
	}else{
		//$where = "AND mt.`Date` BETWEEN '".date('Y-m-d')." 00:00:00' AND '".date('Y-m-d')." 23:59:59'";
		$where = "AND ft.`StartTime` BETWEEN '".date('Y-m-d')." 00:00:00' AND '".date('Y-m-d')." 23:59:59'";
	}
	
	
    $i=0;
    //$query = "SELECT ID,Name,Address,Contact_person,Description,State,CancelComments,Date,TabletId,TripId FROM tms_mob_task ".$where." ORDER BY Date DESC;";
    
	$query = "SELECT mt.ID,mt.Name,mt.Address,mt.Contact_person,mt.Description,mt.State,mt.CancelComments,mt.Date,mt.TabletId,mt.TripId,mt.`Last_modification_time` FROM tms_mob_task mt INNER JOIN `tms_facttrip` ft ON mt.`TripId` = ft.`ID` WHERE mt.`TabletId`=".$iduser." ".$where.";";
	//$query = "SELECT mt.ID,mt.Name,mt.Address,mt.Contact_person,mt.Description,mt.State,mt.CancelComments,mt.Date,mt.TabletId,mt.TripId FROM tms_mob_task mt INNER JOIN `tms_facttrip` ft ON mt.`TripId` = ft.`ID` WHERE mt.`TabletId`=(SELECT t_d.`Device_id` FROM tms_car t_c LEFT JOIN tms_driver t_d ON t_c.`Driver_id` = t_d.`Driver_ID` WHERE t_c.`Device_id`= ".$iduser." AND t_c.`DeletedRecord`=0) ".$where.";";
	
	
    writeToLogFile( ' -> '.date('Y-m-d H:i:s').' <> synchro tms_mob_task <> '.$query );
         
    if($stmt = $db->prepare($query))
    {
      $stmt->execute();
      $stmt->bind_result($ID, $Name, $Address, $Contact_person, $Description, $State, $CancelComments, $Date, $TabletId, $TripId, $Last_modification_time);
      $Model = array();
	  while ($stmt->fetch())
      {
        $Model[$i]=array(
                        'ID'=>$ID,
                        'Name'=>addslashes($Name),
                        'Address'=>addslashes($Address),
                        'Contact_person'=>addslashes($Contact_person),
						'Description'=>addslashes($Description),
						'State'=>$State,
						'CancelComments'=>addslashes($CancelComments),
						'Date'=>$Date,
						'TabletId'=>$TabletId,
						'TripId'=>$TripId,
						'Last_modification_time'=>$Last_modification_time
                        );
        $i++;
      }
    
      $stmt->close();
      $db->close();
    }
    
    echo json_encode($Model);
    writeToLogFile( ' -> '.date('Y-m-d H:i:s').' <> synchro tms_mob_task <> '.json_encode($Model) );
  }
  
  
	if ($arraydata['func']=='LastSynch')
	{
		echo json_encode($Model[0] = array('lastsynch'=>date('Y-m-d H:i:s')));
	}
	
	
	
	if ($arraydata['func']=='tms_speedometer_value')
	{
    $lastsynch = $arraydata["lastsynch"];
	$iduser = $arraydata["iduser"];
    
    $i=0;
	$query = "SELECT `Date`,`Start`,`End` FROM `tms_speedometer_value` WHERE `DeviceId`=".$iduser." AND DATE(`Date`) = DATE(NOW());";
    writeToLogFile( ' -> '.date('Y-m-d H:i:s').' <> synchro tms_speedometer_value <> '.$query );  
    if($stmt = $db->prepare($query))
    {
      $stmt->execute();
      $stmt->bind_result($Date, $Start, $End);
      $Model = array();
	  while ($stmt->fetch())
      {
        $Model[$i]=array(
                        'Date'=>$Date,
                        'Start'=>$Start,
                        'End'=>$End
                        );
        $i++;
      }
    
      $stmt->close();
      $db->close();
    }
    
    echo json_encode($Model);
    writeToLogFile( ' -> '.date('Y-m-d H:i:s').' <> synchro tms_speedometer_value <> '.json_encode($Model) );
	}
  
?>