<?php
//header("Access-Control-Allow-Origin: *");

//exit("HTTP_ORIGIN: ". $_SERVER['SERVER_NAME']);

function url_decode($string){
     return utf8_decode(urldecode( $string ));
}

/*
print("_SERVER['REQUEST_METHOD']: ". $_SERVER['REQUEST_METHOD'] ."\n"); 
var_dump($_POST); 
exit("<hr/>");
*/

function calcular_edad($fecha){ 
	//$fecha = 1980-09-16

	if($fecha!=null){
		$dias = explode('-', $fecha, 3);
		//$dias = mktime(0,0,0,$dias[1],$dias[0],$dias[2]);
		//mktime(0, 0, 0, MES, DIA, AÑO);
		$dias = mktime(0, 0, 0, $dias[1], $dias[2], $dias[0]);
		//int mktime ([ int $hour = date("H") [, int $minute = date("i") [, int $second = date("s") [, int $month = date("n") [, int $day = date("j") [, int $year = date("Y") [, int $is_dst = -1 ]]]]]]] )
		$edad = (int)((time()-$dias)/31556926 );
		
		return $edad;
	}else{
		return "";
	}
	
}
function calcular_edad2($fecha){
	//$fecha = 11/09/1980
	
	if($fecha!=null){
		$arrFecha = explode('/', $fecha, 3);
		//$dias = mktime(0,0,0,$dias[1],$dias[0],$dias[2]);
		$dias = mktime(0, 0, 0, $arrFecha[0], $arrFecha[1], $arrFecha[2]);
		//int mktime ([ int $hour = date("H") [, int $minute = date("i") [, int $second = date("s") [, int $month = date("n") [, int $day = date("j") [, int $year = date("Y") [, int $is_dst = -1 ]]]]]]] )
		$edad = (int)((time()-$dias)/31556926 );
		
		return $edad;
	}else{
		return "";
	}
	
}
// Formato: dd-mm-yy
//echo calcular_edad('01-10-1989'); // Resultado: 21


if($_SERVER['SERVER_NAME']!=="www.softwaremedicodigital.com"){

	if( isset($_SERVER['HTTP_ORIGIN']) ){
		$http_origin = $_SERVER['HTTP_ORIGIN'];

		if ($http_origin == "https://tci.serviciospgmlab.com" || $http_origin == "https://www.softwaremedicodigital.com" || $http_origin == "https://softwaremedicodigital.com" || $http_origin == "http://medicalcrm.mx"){  
		    header("Access-Control-Allow-Origin: $http_origin");
		}
	}

	//header('Access-Control-Allow-Origin: www.serviciospgmlab.com, www.softwaremedicodigital.com');

	//header("Access-Control-Allow-Headers: X-API-KEY, Origin, X-Requested-With, Content-Type, Accept, Access-Control-Request-Method");
	//header("Access-Control-Allow-Methods: GET, POST, OPTIONS, PUT, DELETE");
	//header("Access-Control-Allow-Methods: GET, POST");
	//header("Access-Control-Allow-Methods: POST");

	//header("Allow: GET, POST, OPTIONS, PUT, DELETE");
	//header("Allow: GET, POST");
	//header("Allow: POST");

	$strReferer = $_SERVER["HTTP_REFERER"];
	$arrReferer = explode("/", $strReferer);
	$strReferer = $arrReferer[2];

	$arrReferers = [];
	$arrReferers = ["tci.serviciospgmlab.com","www.softwaremedicodigital.com"];

	$boolEsta = in_array($strReferer, $arrReferers);
	$methodServer = $_SERVER['REQUEST_METHOD'];

	//exit("strReferer: ". $strReferer ." / boolEsta: ". $boolEsta ." / methodServer: ". $methodServer);

	if($methodServer !== "POST" || !$boolEsta ) {
	    //$sJSON = '{"Items":[]}';
	    //print($sJSON);
	    exit("No se puede ejecutar por este metodo.");
	}

}

include_once "../dbconection.php";

$arrUsuarios = array("","Administrador", "Facturacion", "Vendedor");
$method = $_REQUEST["method"];

if($method=="abc"){ // Si es llamado por intUsuario

    $intMedico = $_REQUEST['intMedico'];

	if($_REQUEST['intMode']==1 || $_REQUEST['intMode']==2){ //INSERTAR O ACTUALIZAR

    	//$intEmpresa = $_REQUEST['intEmpresa'];
    	
    	$txtMedico = $_REQUEST['txtMedico'];
    	
    	$txtFecNac = $_REQUEST['txtFecNac'];
    	$selectSexo = $_REQUEST['selectSexo'];
    	
    	$txtEmail = $_REQUEST['txtEmail'];
    	$txtTelefono = $_REQUEST['txtTelefono'];

		$txtDomicilio = $_REQUEST['txtDomicilio'];
		$txtColonia = $_REQUEST['txtColonia'];
		$txtCiudad = $_REQUEST['txtCiudad'];
		$txtEstado = $_REQUEST['txtEstado'];

		$txtCP = $_REQUEST['txtCP'];
		if($txtCP==""){
			$txtCP = 'NULL';
		}

	}

	if($_REQUEST['intMode']==1){
		/*
		$query = "INSERT INTO tblclientes2 (strEmail, strPassword, strNombres, strApellidos, datFecNac, strSexo, strCompania, strFormaContacto, strRfc,
		strPais, strEstado, strMunicipio, intCodigoPostal, 
		strColonia, strCalle, strNumExt, strNumInt ,
		strTelefono, strMovil, intEmpresa ) 
		VALUES ('". $txtEmail ."','','". $txtNombres ."','". $txtApellidos ."','". $txtFecNac ."','". $selectSexo ."','". $txtCompania ."','". $selectFormaContacto ."','". $txtRfc ."',
				'". $selectPais ."','". $selectEstado ."','". $selectMunicipio ."',". $txtCodigoPostal .",
				'". $txtColonia ."','". $txtCalle ."','". $txtNumExt ."','". $txtNumInt ."',
				'". $txtTelefono ."','". $txtMovil ."',". $intEmpresa .")";
        */
	}else if($_REQUEST['intMode']==2){ 
	    
		$query = "UPDATE tblmedicos2020 SET strEmail = '".$txtEmail ."', strPassword = '', strNombres = '".$txtNombres ."', strApellidos = '".$txtApellidos ."', datFecNac = '".$txtFecNac ."' , strSexo = '". $selectSexo ."',
		strCompania = '".$txtCompania ."', strFormaContacto = '". $selectFormaContacto ."', strRfc = '". $txtRfc ."',
		strPais = '".$selectPais."', strEstado = '".$selectEstado."', strMunicipio = '".$selectMunicipio."', intCodigoPostal = ".$txtCodigoPostal.",
		strColonia = '".$txtColonia."', strCalle = '".$txtCalle."', strNumExt = '".$txtNumExt."', strNumInt = '".$txtNumInt."',
		strTelefono = '".$txtTelefono."', strMovil = '".$txtMovil."' 
		WHERE intMedico = ".$intMedico."";

	}else if($_REQUEST['intMode']==3){ //ACTUALIZO LATITUD Y LONGITUD
	
		$strLat = $_REQUEST['strLat'];
		$strLng = $_REQUEST['strLng'];

		$query = "UPDATE tblmedicos2020 SET strLatitud='". $strLat ."', strLongitud='". $strLng ."'";  
		$query .= " WHERE intMedico = ".$intMedico."";

	}
	
	$sJSON = $query;
	//exit($sJSON);
	
	$sJSON = "";
	
	$queryResult = $mysqli->query($query);

	if($_REQUEST['intMode']==1 || $_REQUEST['intMode']==5){
		if($queryResult){
			$insertId = $mysqli->insert_id;
			$sJSON .= '{"exito":"si","msg":"Medico agregado con exito", "insertId":"'. $insertId .'"}';
		}else{
			$sJSON .= '{"exito":"no","msg":"Error al agregar el Medico", "insertId":""}';
		}
	}else{	
		if($queryResult){
			$sJSON .= '{"exito":"si","msg":"Medico actualizado con exito", "insertId":""}';
		}else{
			$sJSON .= '{"exito":"no","msg":"Error en la ejecución", "insertId":""}';
		}
	}
	
	
}else if($method=="consultar"){ // Si es llamado por intEmpresa
	
	$bool = true;
	
	$query = "SELECT * FROM tblmedicos2020 WHERE 1=1 ";
	
	if(isset($_REQUEST['selectEspecialidad'])){
	    
		//$query .= " and strEspecialidad like '%".$_REQUEST['selectEspecialidad']."%' ";
		$query .= " and strEspecialidad like '".$_REQUEST['selectEspecialidad']."' ";
		
	}elseif(isset($_REQUEST['strFecha'])){
	    
	    $strIdsMedicos = "";
	    $query1 = "SELECT * FROM tblrutas WHERE datFecha='". $_REQUEST['strFecha'] ."' AND intVendedor=". $_REQUEST['intVendedor'] ." ORDER BY intRuta ASC";
    		
    	$queryResult1 = $mysqli->query($query1);
    	$totalRows1 = mysqli_num_rows($queryResult1);
    						
    	if($totalRows1 > 0){
    		
    		while($data1 = mysqli_fetch_assoc($queryResult1)){
    		    
    			$intRuta = $data1['intRuta'];
			    $query2 = "SELECT * FROM tblrutasdetalle WHERE intRuta=". $intRuta ." ORDER BY intRutaDetalle ASC";

            	$queryResult2 = $mysqli->query($query2);
            	$totalRows2 = mysqli_num_rows($queryResult2);
            						
            	if($totalRows2 > 0){
            		while($data2 = mysqli_fetch_assoc($queryResult2)){
            			$intMedico = $data2['intMedico'];
    				    $strIdsMedicos .= $intMedico .",";
            		}
            	}
    		}  	
    	}else{
    	    $bool = false;
    	}
    	
    	if($strIdsMedicos!=""){
    	    $strIdsMedicos = substr($strIdsMedicos, 0, -1);
    		$query .= " and intMedico IN (". $strIdsMedicos .") ";
    	}

	}elseif(isset($_REQUEST['strMedico'])){
		
		//$query .= " and strMedico like '%".$_REQUEST['strMedico']."%' "; 
		$query .= " and strMedico like '".$_REQUEST['strMedico']."' "; 

	}elseif(isset($_REQUEST['strMedicos'])){
		
		$query .= " and strMedico IN (". $_REQUEST['strMedicos'] .") "; 
		//$query .= " and strMedico IN (". utf8_encode( $_REQUEST['strMedicos'] ) .") "; 

	}

    if( isset($_REQUEST['chkConUbicacion']) ){
        $chkConUbicacion = $_REQUEST['chkConUbicacion'];
        if( $chkConUbicacion=="true" ){
	    	//$query .= " AND strLatitud IS NOT NULL AND strLongitud IS NOT NULL ";
	    	$query .= " AND strLatitud IS NOT NULL AND strLongitud IS NOT NULL AND strLatitud!='NO ENCONTRADA' AND strLongitud!='NO ENCONTRADA' ";
        }else{
          	//$query .= " AND strLatitud IS NULL AND strLongitud IS NULL ";
          	$query .= " AND strLatitud='NO ENCONTRADA' AND strLongitud='NO ENCONTRADA' ";
        }
	}

	$data = "";
	if($data=="colonia"){
    	$query .= " AND strColonia like '%". $strColonia ."%'";
	}
	if($data=="ciudad"){
    	$query .= " AND strCiudad like '%". $strCiudad ."%'";
	}
	if($data=="estado"){
    	$query .= " AND strEstado like '%". $strEstado ."%'";
	}

    $query .= " ORDER BY strMedico ASC";
    $query .= " LIMIT 300";
       
	//$start = $_REQUEST["start"];
	//$limit = $_REQUEST["limit"];
	
	$start = 0;
	$limit = 20;

	//$query .= " limit ". $start .",". $limit;
    exit($query);

    $sJSON = '{"query":"'. $query .'"';

    if($bool){
        
        //mysqli_set_charset($mysqli, "utf8mb4");
    	//$mysqli->set_charset("utf8mb4_spanish_ci");

		
    	$queryResult = $mysqli->query($query);
    	$totalRows = mysqli_num_rows($queryResult);
    						    	
    	if($totalRows > 0){
    		
    		//$sJSON = '{"Items":[';
    		$sJSON .= ',"Items":[';
    		
        	while($data = mysqli_fetch_assoc($queryResult)){
    		    
    			$intMedico = $data['intMedico'];

    			$strMedico = $data['strMedico'];
    			
    			//$strMedico = utf8_encode($data['strMedico']);
    			
    			//$strMedicoEncode = urlencode($strMedico);
    			//$strMedicoEncode = url_decode(utf8_decode($strMedico));

    			$intPacientes = $data['intPacientes'];
    			$strMonto = $data['strMonto'];
    
    			$strEspecialidad = $data['strEspecialidad'];
    			$strEmail = $data['strEmail'];
    
    			$strSexo = $data['strSexo'];
    			$strFecNac = $data['strFecNac'];
    			
    			$strConsultorio = $data['strConsultorio'];
    
    			$strDomicilio = $data['strDomicilio'];	
    			$strColonia = $data['strColonia'];	
    			$strCiudad = $data['strCiudad'];	
    			$strEstado = $data['strEstado'];	
    			
    			$strCp = $data['strCp'];	
    			$strTelefono = $data["strTelefono"];
    
    			$strLatitud = $data['strLatitud'];	
    			$strLongitud = $data['strLongitud'];	
    			
    			$intActivo = $data['intActivo'];	
    
    			$sJSON .= "{";
    				$sJSON .= '"intMedico":"'. $intMedico .'"';

    				$sJSON .= ',"strMedico":"'. $strMedico .'"';

					$strMedicoEncode = url_decode(utf8_decode($strMedico));
					$sJSON .= ',"strMedicoEncode":"'. $strMedicoEncode .'"';

    				//$strEdad = calcular_edad2($strFecNac);
    				
    				$sJSON .= ',"strSexo":"'.$strSexo.'"';
    				$sJSON .= ',"strFecNac":"'.$strFecNac.'"';
    				
    				$sJSON .= ',"intPacientes":"'. $intPacientes .'"';
    				$sJSON .= ',"strMonto":"'. $strMonto .'"';
    				
    				$sJSON .= ',"strEspecialidad":"'. $strEspecialidad .'"';
    				$sJSON .= ',"strEmail":"'. $strEmail .'"';
    
    				$sJSON .= ',"strConsultorio":"'. $strConsultorio .'"';
    				
    				$sJSON .= ',"strDomicilio":"'. $strDomicilio .'"';
    				$sJSON .= ',"strColonia":"'. $strColonia .'"';
    				$sJSON .= ',"strCiudad":"'. $strCiudad .'"';
    				$sJSON .= ',"strEstado":"'. $strEstado .'"';
    
    				$sJSON .= ',"strCp":"'. $strCp .'"';
    				$sJSON .= ',"strTelefono":"'.$strTelefono.'"';
    
    				$sJSON .= ',"strLatitud":"'. $strLatitud .'"';
    				$sJSON .= ',"strLongitud":"'. $strLongitud .'"';
    
    			$sJSON .= "},";
    		}
    	
    		$sJSON = substr($sJSON, 0, -1);
    		
    		$sJSON .= "]}";
    
    	}else{

        	$sJSON = '{"query":"'. $query .'","Items":[], "Msg": "No hay datos para esta consulta."}';
    	    
    	}
	}else{
    
    	$sJSON = '{"query":"'. $query .'","Items":[], "Msg": "No hay datos para esta consulta."}';
	    
	}

	
}else if($method=="getespecialidades"){ // Si es llamado por intEmpresa
	
	$query = "SELECT distinct strEspecialidad FROM tblmedicos2020 WHERE strEspecialidad is not null AND strEspecialidad<>'' ";
    $query .= " ORDER BY strEspecialidad ASC";
		
	$queryResult = $mysqli->query($query);
	$totalRows = mysqli_num_rows($queryResult);
						
	$sJSON = '{"Items":[';
	
	if($totalRows > 0){
		
		while($data = mysqli_fetch_assoc($queryResult)){
		    
			$strEspecialidad = $data['strEspecialidad'];

			$sJSON .= "{";
				$sJSON .= '"strEspecialidad":"'. $strEspecialidad .'"';
			$sJSON .= "},";
		}
	
		$sJSON = substr($sJSON, 0, -1);
	}

	$sJSON .= "]}";
	
}else if($method=="getvendedores"){ // Si 
	
	$query = "SELECT * FROM tblusuariosrutas ";
    $query .= " WHERE intTipo=2 ORDER BY intUsuario ASC";
		
	$queryResult = $mysqli->query($query);
	$totalRows = mysqli_num_rows($queryResult);
						
	$sJSON = '{"Items":[';
	
	if($totalRows > 0){
		
		while($data = mysqli_fetch_assoc($queryResult)){
		    
			$id = $data['intUsuario'];
			$strNombre = $data['strNombre'];

			$sJSON .= "{";
				$sJSON .= '"id":"'. $id .'"';
				$sJSON .= ',"strNombre":"'. $strNombre .'"';
				//$sJSON .= '"strLogin":"'. $strLogin .'"';
				//$sJSON .= '"strPassword":"'. $strPassword .'"';
				//$sJSON .= '"intTipo":"'. $intTipo .'"';
			$sJSON .= "},";
		}
	
		$sJSON = substr($sJSON, 0, -1);
	}

	$sJSON .= "]}";

}else if($method=="getrutas"){ // 
	
	$query = "SELECT * FROM tblrutas ";
    $query .= " ORDER BY intRuta ASC";
		
	$queryResult = $mysqli->query($query);
	$totalRows = mysqli_num_rows($queryResult);
						
	$sJSON = '{"Items":[';
	
	if($totalRows > 0){
		
		while($data = mysqli_fetch_assoc($queryResult)){
		    
			$intRuta = $data['intRuta'];
			$strRuta = $data['strRuta'];
			$intUsuario = $data['intUsuario'];
			$intVendedor = $data['intVendedor'];
			$intActiva = $data['intActiva'];
			$datFechaAlta = $data['datFechaAlta'];

			$sJSON .= "{";
				$sJSON .= '"intRuta":"'. $intRuta .'"';
				$sJSON .= ',"strRuta":"'. $strRuta .'"';
				$sJSON .= ',"intUsuario":"'. $intUsuario .'"';
				$sJSON .= ',"intVendedor":"'. $intVendedor .'"';
				$sJSON .= ',"intActiva":"'. $intActiva .'"';
				$sJSON .= ',"datFechaAlta":"'. $datFechaAlta .'"';
			$sJSON .= "},";
		}
	
		$sJSON = substr($sJSON, 0, -1);
	}

	$sJSON .= "]}";

}else if($method=="guardarruta"){ // 

	$sJSON = "";
	
	$strRuta = $_REQUEST['strRuta'];
	$intUsuario = $_REQUEST['intUsuario'];
	$intVendedor = $_REQUEST['intVendedor'];
	$datFecha = $_REQUEST['datFecha'];

	$strIdsMedicos = $_REQUEST['strIdsMedicos'];
    $arrItems = explode(",", $strIdsMedicos);
    
	$query = "INSERT INTO tblrutas ( strRuta, intUsuario, intVendedor, datFecha, datFechaAlta ) 
	VALUES ('". $strRuta ."',". $intUsuario .",". $intVendedor .",'". $datFecha ."', CURRENT_TIMESTAMP)";
    //exit($query);
    
	$queryResult = $mysqli->query($query);

	if($queryResult){
		$intRuta = $mysqli->insert_id;
		//$intRuta = mysqli_insert_id($mysqli);
		
		$sJSON .= '{"exito":"si","msg":"Ruta agregada con exito", "insertId":"'. $intRuta .'"}';
		
        for($n=0; $n<count($arrItems); $n++){
								
			$intMedico = $arrItems[$n];
			
			$sqlQuery = "INSERT INTO tblrutasdetalle  (intRuta, intMedico) VALUES (". $intRuta .",". $intMedico .")";

			$mysqli->query($sqlQuery);
		
		}

	}else{
		$sJSON .= '{"exito":"no","msg":"Error al agregar el detalle de la Ruta", "insertId":""}';
	}
		
}else if($method=="getColCiuEdo"){ // 
	
	$strTipo = $_REQUEST["strTipo"];

	$query = "SELECT ";
    $query .= " DISTINCT ". $strTipo ." as label ";

   	$query .= " FROM tblmedicos2020 ";
	
	$query .= " WHERE 1=1 ";
	
	if($strTipo="strColonia"){
		$query .= " AND strCiudad = '". $strCiudad ."'";
	}
	if($strTipo="strCiudad"){
		$query .= " AND strEstado = '". $strEstado ."'";
	}

    $query .= " ORDER BY ". $strTipo ." ASC";
	
	//exit("$query");

	$queryResult = $mysqli->query($query);
	$totalRows = mysqli_num_rows($queryResult);
						
	$sJSON = '{"arrItems":[';
	
	if($totalRows > 0){
		
		while($data = mysqli_fetch_assoc($queryResult)){
		    
			$label = $data['label'];

			$sJSON .= "{";
				$sJSON .= '"label":"'. $label .'"';
			$sJSON .= "},";
		}
	
		$sJSON = substr($sJSON, 0, -1);
	}

	$sJSON .= "]}";


}else if($method=="actualizar"){ // Si es llamado por intEmpresa
	
	$bool = true;
	
	$query = "SELECT * FROM tblmedicos2020 WHERE 1=1 ";
	
	if(isset($_REQUEST['strMedico'])){
		
		//$query .= " and strMedico like '%".$_REQUEST['strMedico']."%' "; 
		$query .= " and strMedico like '".$_REQUEST['strMedico']."' "; 

	}elseif(isset($_REQUEST['strMedicos'])){
		
		$query .= " and strMedico IN (". $_REQUEST['strMedicos'] .") "; 
		//$query .= " and strMedico IN (". utf8_encode( $_REQUEST['strMedicos'] ) .") "; 

	}

	$data = "";
	if($data=="estado"){
    	$query .= " AND strEstado like '%". $strEstado ."%'";
	}

    $query .= " ORDER BY strMedico ASC";
    //$query .= " LIMIT 300";
    	
	$start = 0;
	$limit = 20;

	//$query .= " limit ". $start .",". $limit;
    //exit($query);

    $sJSON = '{"query":"'. $query .'"';

    if($bool){
        
        //mysqli_set_charset($mysqli, "utf8mb4");
    	//$mysqli->set_charset("utf8mb4_spanish_ci");

    	$queryResult = $mysqli->query($query);
    	$totalRows = mysqli_num_rows($queryResult);
    						    	
    	if($totalRows > 0){
    	    		
    		$sJSON .= ',"intRows":"'. $totalRows .'","arrItems":[';
    		
        	while($data = mysqli_fetch_assoc($queryResult)){
    		    
    			$intMedico = $data['intMedico'];
    			$strMedico = $data['strMedico'];
    			
    			//$strMedico = utf8_encode($data['strMedico']);
    			
    			//$strMedicoEncode = urlencode($strMedico);
    			//$strMedicoEncode = url_decode(utf8_decode($strMedico));

    			//$intPacientes = $data['intPacientes'];
    			//$strMonto = $data['strMonto'];
    
    			$strEspecialidad = $data['strEspecialidad'];
    			$strEmail = $data['strEmail'];
    
    			$strSexo = $data['strSexo'];
    			$strFecNac = $data['strFecNac'];
    			
    			$strConsultorio = $data['strConsultorio'];
    
    			$strDomicilio = $data['strDomicilio'];	
    			$strColonia = $data['strColonia'];	
    			$strCiudad = $data['strCiudad'];	
    			$strEstado = $data['strEstado'];	
    			
    			$strCp = $data['strCp'];	
    			$strTelefono = $data["strTelefono"];
    
    			$strLatitud = $data['strLatitud'];	
    			$strLongitud = $data['strLongitud'];	
    			
    			$intActivo = $data['intActivo'];	
    
    			$sJSON .= "{";
    				$sJSON .= '"intMedico":"'. $intMedico .'"';

    				$sJSON .= ',"strMedico":"'. $strMedico .'"';

					$strMedicoEncode = url_decode(utf8_decode($strMedico));
					$sJSON .= ',"strMedicoEncode":"'. $strMedicoEncode .'"';
   				
    				$sJSON .= ',"strSexo":"'.$strSexo.'"';
    				$sJSON .= ',"strFecNac":"'.$strFecNac.'"';
    				   				
    				$sJSON .= ',"strEspecialidad":"'. $strEspecialidad .'"';
    				$sJSON .= ',"strEmail":"'. $strEmail .'"';
    
    				$sJSON .= ',"strConsultorio":"'. $strConsultorio .'"';
    				
    				$sJSON .= ',"strDomicilio":"'. $strDomicilio .'"';
    				$sJSON .= ',"strColonia":"'. $strColonia .'"';
    				$sJSON .= ',"strCiudad":"'. $strCiudad .'"';
    				$sJSON .= ',"strEstado":"'. $strEstado .'"';
    
    				$sJSON .= ',"strCp":"'. $strCp .'"';
    				$sJSON .= ',"strTelefono":"'.$strTelefono.'"';
    
    				$sJSON .= ',"strLatitud":"'. $strLatitud .'"';
    				$sJSON .= ',"strLongitud":"'. $strLongitud .'"';
    
    			$sJSON .= "},";
    		}
    	
    		$sJSON = substr($sJSON, 0, -1);
    		
    		$sJSON .= "]}";
    
    	}else{

        	$sJSON = '{"query":"'. $query .'","arrItems":[], "Msg": "No hay datos para esta consulta."}';
    	    
    	}
	}else{
    
    	$sJSON = '{"query":"'. $query .'","arrItems":[], "Msg": "No hay datos para esta consulta."}';
	    
	}


}

print($sJSON);

?>