<?php
require '../Conexion/class.sql.inc.php';
$db = new EyeMySQLAdap();

session_start();

/*Se liberan casas separadas por 48 horas sin respuesta del cliente*/
$strSQL = "UPDATE house SET SeparationDate = null, Status=1 WHERE Status=3 AND  adddate(SeparationDate,INTERVAL 3 DAY) < NOW()";
$result = $db->query($strSQL);
$strSQL = "UPDATE sell SET ReservationDate = null, StatusSellID=1, HouseID = 0, MzID = 0, EtapaID = 0 WHERE StatusSellID=3 AND  adddate(ReservationDate,INTERVAL 3 DAY) < NOW()";
$result = $db->query($strSQL);


try {
   
	//if (true) 
	if (isset($_POST['Filsc'])) 
	{
		$varfil= $_POST['Filsc'];
		//$varfil= 'Ciudad';
		$arr = array();
	
	
		switch ($varfil)
		{
			case 'Asesor':
				{
					$arr[]=array("UserID"=>"-1","UserName"=>"Todos");
					$sql="select UserID,UserName from user";
				}
				break;
			
			case 'TipoDocumento':
				{
					$sql="select DocumentTypeID,Description  from documenttype";
				}
				break;
			case 'EstadoCivil':
				{
					$sql="select CivilStatusID,Description from civilstatus";
				}
				break;
			case 'MediosInformacion':
				{
					$sql="select InformativeMediaID,Description from informativemedia";
				}
				break;
			case 'Paises':
				{
					$sql="select CountryID,Description from country";
				}
				break;
			case 'Ciudad':
				{
					$varCountry= $_POST['Country'];
					//$varCountry= 1;
					$sql="select CityID ,Description from city where CountryID=".$varCountry;
				}
				break;			
			case 'ComboProject':{
					$sql = "select ProjectID, Description from project";
				}
				break;			
			case 'ComboEtapa':{
					$idProject  = isset($_POST['idProject'])  ? $_POST['idProject']  :  -1;
					$idEtapa	= isset($_POST['idEtapa'])  ? $_POST['idEtapa']  :  -1;					
					$sql = "select EtapaID, Description from etapa where" .($idProject!=-1? " ProjectID =".$idProject : " 1=1") . ($idEtapa!=-1? " AND EtapaID =" .$idEtapa : "");
				}
				break;
			case 'ComboEtapaCli':{
					$idProject  = isset($_POST['idProject'])  ? $_POST['idProject']  :  -1;							
					$sql = "select EtapaID, Description FROM etapa" .($idProject!=-1? " WHERE ProjectID =" .$idProject : "");
				}
				break;
			case 'ComboSituacion':{
					$sql = "select StatusHouseID, Description from statushouse";
				}
				break;
			case 'ComboManzana':{
					$idEtapa  = isset($_POST['idEtapa'])  ? $_POST['idEtapa']  :  -1;
					$idMz	  = isset($_POST['idMz'])  ? $_POST['idMz']  :  -1;
					$sql = "select MzID, Description from mz where ".($idEtapa!=-1? " EtapaID =" .$idEtapa : "1=1") .($idMz!=-1? " AND MzID =" .$idMz : "");
				}
				break;
			case 'ComboManzanaCli':{
					$idEtapa  = isset($_POST['idEtapa'])  ? $_POST['idEtapa']  :  -1;
					$sql = "select MzID, Description from mz" .($idEtapa!=-1? " WHERE EtapaID =" .$idEtapa : "");
				}
				break;
			case 'ComboSolar':{
					$idMz = isset($_POST['idMz']) ? $_POST['idMz'] : '-1';
					$all =  isset($_POST['All']) ? $_POST['All'] : '-1';
					if($all != 'true'){
						$sql = "select HouseID, Solar from house WHERE Status = 1" .($idMz!=-1? " AND MzID =" .$idMz : "");
					}
					else{
						$sql = "select HouseID, Solar from house " . " WHERE MzID =" .$idMz ;
					}
				}
					break;
			case 'ComboTipoCasas':{
					$sql = "select HouseSubTypeID, Description from housesubtype";
				}
				break;
			case 'NivelEducacion':{
					$sql = "select EducationLevelID, Description from educationlevel";
				}
				break;
			case 'ComboProfesion':{
					$sql = "select ProfessionID, Description from profession";
				}
				break;
			case 'ComboPosicion':{
					$sql = "select PositionID, Description from position";
				}
				break;
			case 'ComboAfiliado':{
					$sql = "select AfiliateInstitutionID, Description from afiliateinstitution";
				}
				break;
			case 'ComboParentesco':{
					$sql = "select FamiliarityID, Description from familiarity";
				}
				break;
			case 'ComboEstadoVenta':{
					$sql = "select StatusSellID, Description from statussell";
				}
				break;
			case 'ComboConstructor':{
					$EtapaID = $_POST['EtapaID'];
					$sql = "select ConstructorID, Description from constructor where EtapaID = " . $EtapaID;
				}
				break;
			case 'Nacionalidad':{
					$sql="select CountryID,Nationality as Description from country";
				}
				break;
			case 'FormaPago':{
					$sql="select PaymentTypeID, Description from paymenttype";
				}
				break;
		}

		$result=$db->query($sql);
		while($obj = mysql_fetch_object($result))
		{    
			$arr[] = $obj;
		}
		$rows=$db->countRows($result);
		Echo '({success:true,"totalcount":"'.$rows.'","Data":'.json_encode($arr).'})';
		mysql_free_result($result); 
	}
	else
	{
		Echo '({success:false,Datos:'.json_encode("Error al consultar...Por Favor Verifique!!!").'})';		
	}
}
catch (Exception $e)
{
	Echo '({success:false,Datos:'.json_encode($e->getMessage()).'})';		
}


?>