<?php

//require_once("class.conectar.php");

//class HoraPermiso extends Conectar
class HoraPermiso
{
	//private $visitas=array();
	private $lis_hper_a=array();
	private $lis_hper_p=array();
	private $lis_hper_per=array();
	private $lis_f_hper_per=array();
	private $lis_hper_tipo=array();
	private $bus_hper_tipo=array();
	private $lis_hper_ask=array();
	private $idhper;
	
//	public function __construct()
	//	{
		//	$this->visitas=array();
		//}
		
		//busca el ultimo id
	public function buscar_id_hrpermiso($con,$ugid)
	{
		$sql="select hrp_id from tb_horapermiso where ug_id='$ugid' order by hrp_id desc limit 1";
		$res=mysqli_query($con, $sql);
		while ($reg=mysqli_fetch_assoc($res))
		{
			$this->idhper=$reg['hrp_id'];
		}
			return $this->idhper;
	}
	
	public function consultar_hpermiso_ask($con,$ugid, $anno,$mes,$dia, $hini, $hfin)
	{
		$sql="select * from tb_horapermiso where ug_id='$ugid' and year(hrp_fecha) = '$anno' and month(hrp_fecha) = '$mes' and day(hrp_fecha) = '$dia' and   hrp_horaini between '$hini' and '$hfin' and hrp_horafin  between '$hini' and '$hfin'";
		//(hrp_horaini < '$hini' and hrp_horafin => '$hini')
		//echo $sql."<br>";
		$res=mysqli_query($con, $sql);
		if($res){
		while ($reg=mysqli_fetch_assoc($res))
		{
			$this->lis_hper_ask[]=$reg;
		}
			return $this->lis_hper_ask;
		}
	}
	
	public function consultar_hpermiso_anterior($con,$ugid, $anno,$mes,$dia, $hinia)
	{
		$sql="select * from tb_horapermiso where ug_id='$ugid' and year(hrp_fecha) = '$anno' and month(hrp_fecha) = '$mes' and day(hrp_fecha) = '$dia' and (hrp_horaini < '$hinia' and hrp_horafin > '$hinia')";
		
		$res=mysqli_query($con, $sql);
		while ($reg=mysqli_fetch_assoc($res))
		{
			$this->lis_hper_a[]=$reg;
		}
			return $this->lis_hper_a;
	}
	
	public function consultar_hpermiso_posterior($con,$ugid, $anno,$mes,$dia, $hfina)
	{
		$sql="select * from tb_horapermiso where ug_id='$ugid' and year(hrp_fecha) = '$anno' and month(hrp_fecha) = '$mes' and day(hrp_fecha) = '$dia' and (hrp_horaini < '$hfina' and hrp_horafin > '$hfina')";
		$res=mysqli_query($con, $sql);
		while ($reg=mysqli_fetch_assoc($res))
		{
			$this->lis_hper_p[]=$reg;
		}
			return $this->lis_hper_p;
	}
	
		public function consultar_hpermiso_anterior_id($con,$ugid, $anno,$mes,$dia,$numid,$hinia)
	{
		$sql="select * from tb_horapermiso where ug_id='$ugid' and year(hrp_fecha) = '$anno' and month(hrp_fecha) = '$mes' and day(hrp_fecha) = '$dia' and (hrp_horaini < '$hinia' and hrp_horafin > '$hinia') and hrp_id not in ('$numid')";
		$res=mysqli_query($con, $sql);
		while ($reg=mysqli_fetch_assoc($res))
		{
			$this->lis_hper_a[]=$reg;
		}
			return $this->lis_hper_a;
	}
	
	public function consultar_hpermiso_posterior_id($con,$ugid, $anno,$mes,$dia,$numid,$hfina)
	{
		$sql="select * from tb_horapermiso where ug_id='$ugid' and year(hrp_fecha) = '$anno' and month(hrp_fecha) = '$mes' and day(hrp_fecha) = '$dia' and (hrp_horaini < '$hfina' and hrp_horafin > '$hfina') and hrp_id not in ('$numid')";
		$res=mysqli_query($con, $sql);
		while ($reg=mysqli_fetch_assoc($res))
		{
			$this->lis_hper_p[]=$reg;
		}
			return $this->lis_hper_p;
	}
	public function insert_hpermiso_prog($con,$numid,$idthper,$ugid,$fecha,$horaini,$horafin,$numhrs)
	{
		$sql="insert into tb_horapermiso values('$numid','$ugid','$fecha','$horaini','$horafin','$numdias','$idtlic')";
		$res=mysqli_query($con, $sql);
	}
	
	public function consultar_hpermiso_personal($con,$ugid)
	{
		$sql="select hrp_id as id, thp_id as idthp, concat(date_format(hrp_fecha,'%d/%m/%Y'),'-',date_format(hrp_horaini,'%k:%i %p'),'-',date_format(hrp_horafin,'%k:%i %p'),' (',hrp_numhora,')') as hora ";
		$sql= $sql." from tb_horapermiso where ug_id='$ugid' order by hrp_fecha desc"; 
		//echo $sql;
		$res=mysqli_query($con, $sql);
		while ($reg=mysqli_fetch_assoc($res))
		{
			$this->lis_hper_per[]=$reg;
		}
			return $this->lis_hper_per;
	}
	
	public function consultar_detalle_hora_permiso($con,$codper,$fecha)
	{
		$sql="select a.hrp_id as id, a.thp_id as idthp, (select thp_desc from tb_tphoraper where thp_id=a.thp_id) as deschrsper, concat(date_format(a.hrp_horaini,'%k:%i %p'),'-',date_format(a.hrp_horafin,'%k:%i %p'),' (',a.hrp_numhora,')') as hora from tb_horapermiso a where a.ug_id='$codper' and date(hrp_fecha)= date('$fecha')"; 
		//echo $sql;
		$res=mysqli_query($con, $sql);
		while ($reg=mysqli_fetch_assoc($res))
		{
			$this->lis_hper_per[]=$reg;
		}
			return $this->lis_hper_per;
	}
	
	//falta modificar
	public function consultar_horas_permiso_personal($con,$ugid,$numid) 
	{
		$sql="select date_format(hrp_fecha,'%d/%m/%Y') as fecha, hrp_horaini as horai ,date_format(hrp_horaini,'%k:%i %p') as horaini,hrp_horafin as horaf, date_format(hrp_horafin,'%k:%i %p') as horafin from tb_horapermiso where ug_id='$ugid' and hrp_id='$numid'";
		$res=mysqli_query($con, $sql);
		while ($reg=mysqli_fetch_assoc($res))
		{
			$this->lis_f_hper_per[]=$reg;
		}
			return $this->lis_f_hper_per;
	}
	//falta modificar
	public function modificar_horas_permiso($con,$horaini, $horafin, $numhrs, $numid, $ugid, $idthper, $fecha)
	{
		$sql="update tb_horapermiso set hrp_horaini='$horaini', hrp_horafin='$horafin', hrp_numhora='$numhrs', thp_id='$idthper', hrp_fecha='$fecha' where hrp_id='$numid' and ug_id='$ugid'";
		$res=mysqli_query($con, $sql);
	}
	
	public function listar_tipo_hpermiso($con) 
	{
		$sql="select * from tb_tphoraper";
		$res=mysqli_query($con, $sql);
		while ($reg=mysqli_fetch_assoc($res))
		{
			$this->lis_lic_tipo[]=$reg;
		}
			return $this->lis_lic_tipo;
	}
	
	public function buscar_tipo_hpermiso($con,$idthper) 
	{
		$sql="select * from tb_tphoraper where thp_id='$idthper'";
		$res=mysqli_query($con, $sql);
		while ($reg=mysqli_fetch_assoc($res))
		{
			$this->bus_lic_tipo[]=$reg;
		}
			return $this->bus_lic_tipo;
	}

}
?>