<?php

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

//class DescansoRadiacion extends Conectar
class DescansoRadiacion 
{
	//private $visitas=array();
	private $lis_desc_a=array();
	private $lis_desc_p=array();
	private $lis_desrad_per=array();
	private $lis_f_descrad_per=array();
	private $lis_descrad_per_am=array();
	private $iddr;
	
//	public function __construct()
	//	{
		//	$this->visitas=array();
		//}
	public function buscar_id_descrad($con, $ugid)
	{
		$sql="select dr_id from tb_descrad where ug_id='$ugid' order by dr_id desc limit 1";
		$res=mysqli_query($con, $sql);
		while ($reg=mysqli_fetch_assoc($res))
		{
			$this->iddr=$reg['dr_id'];
		}
			return $this->iddr;
	}
	
	public function consultar_descrad($con,$codper, $fecha)
	{
		$sql="select * FROM tb_descrad where ug_id='$codper' and date('$fecha') between date(dr_fechaini) and date(dr_fechafin)";
		$res=mysqli_query($con, $sql);
		while ($reg=mysqli_fetch_assoc($res))
		{
			$this->lis_desc_a[]=$reg;
		}
			return $this->lis_desc_a;
	}
	
	public function consultar_descrad_anterior($con,$ugid, $fechaini)
	{
		$sql="select * from tb_descrad where ug_id='$ugid' and date('$fechaini') between date(dr_fechaini) and date(dr_fechafin) and dr_estado=0";
		$res=mysqli_query($con, $sql);
		while ($reg=mysqli_fetch_assoc($res))
		{
			$this->lis_desc_a[]=$reg;
		}
			return $this->lis_desc_a;
	}
	
	public function consultar_descrad_posterior($con,$ugid, $fechafin)
	{
		$sql="select * from tb_descrad where ug_id='$ugid' and '$fechafin' between dr_fechaini and dr_fechafin and dr_estado=0";
		$res=mysqli_query($con, $sql);
		while ($reg=mysqli_fetch_assoc($res))
		{
			$this->lis_desc_p[]=$reg;
		}
			return $this->lis_desc_p;
	}
	
	public function consultar_descrad_anterior_id($con,$ugid, $fechaini,$numid)
	{
		$sql="select * from tb_descrad where ug_id='$ugid' and '$fechaini' between dr_fechaini and dr_fechafin and dr_id not in ('$numid') and (dr_estado=0 or dr_estado=1)";
		$res=mysqli_query($con, $sql);
		while ($reg=mysqli_fetch_assoc($res))
		{
			$this->lis_desc_a[]=$reg;
		}
			return $this->lis_desc_a;
	}
	
	public function consultar_descrad_posterior_id($con,$ugid, $fechafin,$numid)
	{
		$sql="select * from tb_descrad where ug_id='$ugid' and '$fechafin' between dr_fechaini and dr_fechafin and dr_id not in ('$numid') and (dr_estado=0 or dr_estado=1)";
		$res=mysqli_query($con, $sql);
		while ($reg=mysqli_fetch_assoc($res))
		{
			$this->lis_desc_p[]=$reg;
		}
			return $this->lis_desc_p;
	}
	
	public function insert_dr_prog($con, $numid,$ugid,$periodo,$fechaini,$fechafin,$numdias)
	{
		$sql="insert into tb_descrad values('$numid','$ugid','$periodo','$fechaini','$fechafin','$numdias',0)";
		//echo $sql;
		$res=mysqli_query($con, $sql);
	}
	
	public function consultar_descrad_personal($con,$ugid,$estado)
	{
		$sql="select dr_id as id, concat(date_format(dr_fechaini,'%d/%m/%Y'),' - ', date_format(dr_fechafin,'%d/%m/%Y'),' (', dr_numdias,')') ";
		$sql= $sql." as dias, dr_periodo as periodo from tb_descrad where ug_id='$ugid' and dr_estado='$estado' order by dr_fechaini desc"; 
		//echo $sql;
		$res=mysqli_query($con, $sql);
		while ($reg=mysqli_fetch_assoc($res))
		{
			$this->lis_desrad_per[]=$reg;
		}
			return $this->lis_desrad_per;
	}
	
	public function consultar_fechas_descrad_personal($con, $ugid,$numid,$estado) 
	{
		$sql="select date_format(dr_fechaini,'%d/%m/%Y') as fechaini, date_format(dr_fechafin,'%d/%m/%Y') as fechafin, dr_periodo as periodo from tb_descrad where ug_id='$ugid' and dr_id='$numid' and dr_estado='$estado'";
			//echo $sql;
		$res=mysqli_query($con, $sql);
		while ($reg=mysqli_fetch_assoc($res))
		{
			$this->lis_f_descrad_per[]=$reg;
		}
			return $this->lis_f_descrad_per;
	}
	
	public function modificar_fechas_descrad($con,$fechaini, $fechafin, $numdias, $numid, $ugid)
	{
		$sql="update tb_descrad set dr_fechaini='$fechaini', dr_fechafin='$fechafin', dr_numdias='$numdias' where dr_id='$numid' and ug_id='$ugid' and dr_estado=0";
		$res=mysqli_query($con, $sql);
	}
	
	public function modificar_estado_descrad($con, $numid, $ugid)
	{
		$sql="update tb_descrad set dr_estado=2 where dr_id='$numid' and ug_id='$ugid' and dr_estado=0";
		//echo $sql;
		$res=mysqli_query($con, $sql);
	}
	
	public function modificar_estado_descrad_mes($con,$ugid,$anno,$mes,$estini,$estfin)
	{
		$sql="update tb_descrad set dr_estado='$estfin' where ug_id='$cod' and dr_estado='$estini' and year(dr_fechaini)='$anno' and month(dr_fechaini)='$mes'";
		$res=mysqli_query($con, $sql);
	}
	
	public function listar_descrad_per_anno_mes_estado($con, $ugid,$anno,$mes,$estado)
	{
		$sql="select * from tb_descrad where year(dr_fechaini)='$anno' and month(dr_fechaini)='$mes'and dr_estado='$estado' and ug_id='$ugid'";
		$res=mysqli_query($con, $sql);
		while ($reg=mysqli_fetch_assoc($res))
		{
			$this->lis_f_descrad_per[]=$reg;
		}
			return $this->lis_f_descrad_per;
	}
	
	public function listar_descrad_per_anno_mes($con,$cod,$anno,$mm,$estado)
	{
		$sql="select dr_id as id, concat(date_format(dr_fechaini,'%d/%m/%Y'),' AL ', date_format(dr_fechafin,'%d/%m/%Y')) as fecha from tb_descrad where ((year(dr_fechaini)='$anno' and month(dr_fechaini)='$mm') or (year(dr_fechafin)='$anno' and month(dr_fechafin)='$mm')) and dr_estado='$estado' and ug_id='$cod'";
		$res=mysqli_query($con, $sql);
		while ($reg=mysqli_fetch_assoc($res))
		{
			$this->lis_descrad_per_am[]=$reg;
		}
			return $this->lis_descrad_per_am;
	}
}
?>