// Service : appstartnotify
 
//$option = JRequest::getVar('option','com_jewebservices','','string');
//$post = JRequest::get ('post');  
//$uri = JURI::getInstance();
//$url= $uri->root();
$this->_table_prefix = "#__jetour_";
global $HTTP_RAW_POST_DATA;
 
$post = json_decode($HTTP_RAW_POST_DATA, true);
/*echo "<pre>";
print_r($post);
exit;*/
//$post['devicetoken'] = 474;
/*$post['deviceaccess'] = 0;
$post['user_id'] = 2;*/


if($post['devicetoken']){
			$sql="SELECT id,user_id FROM ".$this->_table_prefix."appalldevices WHERE devicetoken ='".$post['devicetoken']."' AND deviceaccess=".$post['deviceaccess'] ;	
			$db->setQuery($sql);
			$devicefound = $db->loadObject();
			
			if(count($devicefound)){
				if($devicefound->user_id!=0){
					if($devicefound->user_id!=$post['user_id'])
						$update='UPDATE #__jetour_appalldevices SET user_id="'.$post['user_id'].'",deviceaccess="'.$post['deviceaccess'].'" WHERE  devicetoken="'.$post['devicetoken'].'"';				
					else
						$update='UPDATE #__jetour_appalldevices SET deviceaccess="'.$post['deviceaccess'].'" WHERE  devicetoken="'.$post['devicetoken'].'"';				
				}else{
				$update='UPDATE #__jetour_appalldevices SET user_id="'.$post['user_id'].'",deviceaccess="'.$post['deviceaccess'].'" WHERE  devicetoken="'.$post['devicetoken'].'"';				
				}
				$db->setQuery($update);
				$db->Query();
				$arr = array('success' => 1, 'message' => "Updated" ); 
 				echo json_encode($arr);exit;
			
			}else{
				$deinsert='INSERT INTO #__jetour_appalldevices (`user_id`,`deviceaccess`,`devicetoken`) VALUES ("'.$post['user_id'].'","'.$post['deviceaccess'].'","'.$post['devicetoken'].'")';
				$db->setQuery($deinsert);
				$db->Query();
				$arr = array('success' => 1, 'message' => "Inserted" ); 
 				echo json_encode($arr);exit;
			}
	

}else{
	$arr = array('success' => 0, 'message' => "Devicetoken not correct" ); 
 	echo json_encode($arr);exit;
}