<?php
header ("Content-Type:text/xml");
include "start.php";
echo "<psync>";
//---------------------------------------------------------------------------------------------------------
                       
      
	 
 
 
          
	$query_user="select * from t_".$key_table_prefix."_user 
			where usr_name = '".$_REQUEST["u"]."' 
			and usr_password = '".$_REQUEST["p"]."'
			and usr_status_id = '1'
			";
			
			
	$result_user = mysqli_query($dbconn,$query_user);
	if($line_user = mysqli_fetch_array($result_user,MYSQLI_ASSOC))
	{
		
		$login_status="SUCCESS";
	     $message="Login Success";


		//register device:
		$query_device="select * from t_".$key_table_prefix."_device 
					where dev_android_id = '".$_REQUEST["d"]."' ";
		$result_device = mysqli_query($dbconn,$query_device);
		if($line_device = mysqli_fetch_array($result_device,MYSQLI_ASSOC))
		{
			//exist
			
			if($_REQUEST["a"]=="INVENTORY")
			{
			
			
			}
			else {
				$query_update="update t_".$key_table_prefix."_device 
							set dev_fcm_token =  '".$_REQUEST["t"]."' 
							where dev_android_id = '".$_REQUEST["d"]."' ";
				$result_update = mysqli_query($dbconn,$query_update);	
				
			}
			
			
			
			
			
		}
		else {
			
			if($_REQUEST["a"]=="INVENTORY")
			{
			
				$query_insert_device = "insert into t_".$key_table_prefix."_device
							(
								dev_android_id,
								dev_name,
								created_date,
								modified_date
								)
							values
							(
								'".$_REQUEST["d"]."',
								'Unname Device',
								'".date("Y-m-d H:i:s")."',
								'".date("Y-m-d H:i:s")."'
								
								)
							;";
				$result_insert_device = mysqli_query($dbconn,$query_insert_device);
						
			}
			else {
				
				$query_insert_device = "insert into t_".$key_table_prefix."_device
							(
								dev_android_id,
								dev_name,
								dev_fcm_token,
								created_date,
								modified_date
								)
							values
							(
								'".$_REQUEST["d"]."',
								'Unname Device',
								'".$_REQUEST["t"]."',
								'".date("Y-m-d H:i:s")."',
								'".date("Y-m-d H:i:s")."'
								
								)
							;";
				$result_insert_device = mysqli_query($dbconn,$query_insert_device);
						
			}
			
				
		}
		



	}
	else {
		
		
		$login_status="FAIL";
	     $message="Invalid Login";
	}
		
		

	
				 
echo "<login>
";
echo "<status>".$login_status."</status>
";

echo "<message>".str_replace("&","&amp;",$message)."</message>
";


echo "</login>
";    
         


//---------------------------------------------------------------------------------------------------------
echo "</psync>";
?>