<?php



include_once "../dbconection.php";



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]);

		$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 "";

	}

	

}

// Formato: dd-mm-yy

//echo calcular_edad('01-10-1989'); // Resultado: 21



$arrUsuarios = array("","Administrador", "Facturacion", "Vendedor");



if(isset($_REQUEST['intCliente'])){ // Si es llamado por intUsuario

	//$query = "SELECT * FROM tblusuarios WHERE intUsuario = ". $_REQUEST["intUsuario"] ." ";



	$intEmpresa = $_REQUEST['intEmpresa'];

	

	$intCliente = $_REQUEST['intCliente'];

	$txtNombres = $_REQUEST['txtNombres'];

	$txtApellidos = $_REQUEST['txtApellidos'];

	$txtFecNac = $_REQUEST['txtFecNac'];

	$selectSexo = $_REQUEST['selectSexo'];

	

	$txtCompania = $_REQUEST['txtCompania'];

	$txtEmail = $_REQUEST['txtEmail'];

	$selectFormaContacto = $_REQUEST['selectFormaContacto'];

	$txtRfc = $_REQUEST['txtRfc'];

	$selectPais = $_REQUEST['selectPais'];

	$selectEstado = $_REQUEST['selectEstado'];

	$selectMunicipio = $_REQUEST['selectMunicipio'];

	$txtColonia = $_REQUEST['txtColonia'];

	$txtCalle = $_REQUEST['txtCalle'];

	$txtNumExt = $_REQUEST['txtNumExt'];

	$txtNumInt = $_REQUEST['txtNumInt'];

	$txtCodigoPostal = $_REQUEST['txtCodigoPostal'];

	if($txtCodigoPostal==""){

		$txtCodigoPostal = 'NULL';

	}

	$txtTelefono = $_REQUEST['txtTelefono'];

	$txtMovil = $_REQUEST['txtMovil'];



//intCliente	strEmail	strPassword	strNombre	strFormaContacto	strRfc	strPais	strEstado	

//strMunicipio	intCodigoPostal	strColonia	strCalle	strNumInt	strNumExt	strTelefono	  strMovil

//intEmpresa	intTipoPrecio datFechaAlta

	

	if($_REQUEST['intMode']==1){

		$query = "INSERT INTO tblclientes (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 tblclientes 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 intCliente = ".$intCliente."";

	

	}else if($_REQUEST['intMode']==3){

		$query = "DELETE FROM tblclientes WHERE intCliente = ".$intCliente."";

		

	}

	

	$sJSON = $query;

	//exit($sJSON);

	

	$sJSON = "";

	

	$queryResult = mysql_query($query);	

						

	//$sJSON = "";

	

	if($queryResult){

		$sJSON .= "exito";

	}else{

		$sJSON .= "error";

	}

	

}else if(isset($_REQUEST['intEmpresa'])){ // Si es llamado por intEmpresa

	

//	if(isset($_REQUEST['method'])){ // Si viene campo

//		if($_REQUEST['method'])=="companias"){

//			//$query = "SELECT * FROM tblclientes WHERE intEmpresa = ". $_REQUEST["intEmpresa"];

//			$query = "SELECT DISTINCT t1.strCompania FROM tblclientes t1, tblclientes t2 where t1.strCompania=t2.strCompania WHERE intEmpresa = ". $_REQUEST["intEmpresa"];

//		}else if($_REQUEST['method'])=="clientes"){

//			$query = "SELECT * FROM tblclientes WHERE intEmpresa = ". $_REQUEST["intEmpresa"];

//		}

//	}else{

		$query = "SELECT * FROM tblclientes WHERE intEmpresa = ". $_REQUEST["intEmpresa"];

		if(intval($_REQUEST['intCase'])==1){

			//$query .= " and strNombres like '".$_REQUEST['strCliente']."%' ";

		}else if(intval($_REQUEST['intCase'])==2){

			$query .= " and ( strNombres like '%".$_REQUEST['strCliente']."%' OR strApePat like '%".$_REQUEST['strCliente']."%' OR strApeMat like '%".$_REQUEST['strCliente']."%' ) ";

		}



//	}

	

	$queryResult = mysql_query($query);	

	$totalRows = mysql_num_rows($queryResult);

						

	$sJSON = '{"Items":[';

	

	if($totalRows > 0){

		

		while($data = mysql_fetch_assoc($queryResult)){

			$intCliente = $data['intCliente'];

			$strNombres = $data['strNombres'];

			$strApePat = $data['strApePat'];

			$strApeMat = $data['strApeMat'];

			$strFecNac = $data['datFecNac'];

			$strSexo = $data['strSexo'];

			$strCompania = $data['strCompania'];

			$strEmail = $data['strEmail'];

			$strPassword = $data['strPassword'];	



			$strFormaContacto = $data['strFormaContacto'];	

			

			$strRfc = $data['strRfc'];	

			$strPais = $data['strPais'];	

			$strEstado = $data['strEstado'];	

			$strMunicipio = $data['strMunicipio'];	

			$intCodigoPostal = $data['intCodigoPostal'];	

			$strColonia = $data['strColonia'];	

			$strCalle = $data['strCalle'];	

			$strNumInt = $data['strNumInt'];	

			$strNumExt = $data['strNumExt'];	



			$strTelefono = $data["strTelefono"];

			$strMovil = $data["strMovil"];



			$sJSON .= "{";

				$sJSON .= '"intCliente":"'.$intCliente.'"';

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

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

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

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

				//$strFecNac = 1980-09-16

				$strEdad = calcular_edad($strFecNac);

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

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



				if(isset($_REQUEST['method'])){ // Si viene campo

					if($_REQUEST['method']=="companias"){

						$sJSON .= ',"id":"'.$intCliente.'"';

						$sJSON .= ',"label":"'.$strCompania.'"';

					}else if($_REQUEST['method']=="companias-cliente"){

						$sJSON .= ',"id":"'.$intCliente.'"';

						$sJSON .= ',"label":"'.$strCompania." (".$strNombre.')"';

					}else if($_REQUEST['method']=="clientes"){

						$sJSON .= ',"id":"'.$intCliente.'"';

						$sJSON .= ',"label":"'.$strNombre.'"';

					}

				}else{

					$sJSON .= ',"id":"'.$intCliente.'"';

					$sJSON .= ',"label":"'.$strNombres.'"';

				}

				

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

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

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

				

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



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

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

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

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

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

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

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

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

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



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

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

			$sJSON .= "},";

		}

	}

	$sJSON = substr($sJSON, 0, -1);

	$sJSON .= "]}";

	

}



print($sJSON);



?>