<?php

include("../../bd/sql_variables.php");

$db_conexion= mysql_connect("$sql_host", "$sql_usuario", "$sql_pass") or die(header ("No Disponible Servidor de Base de Datos"));
mysql_select_db("$sql_db");

//$idesp=$_GET['esp'];
//$idtp=$_GET['tp'];

$ugid=$_GET['ugid'];

echo "<input type='hidden' id='ugid' name='ugid' value='".$ugid."'>";
//$div=$_GET['div'];

//$idtp=1;
//$ids=2;
//$idj=1;
//and b.tp_id='$idtp' 

function restaFechas($dFecIni, $dFecFin)
{
    $dFecIni = str_replace("-","",$dFecIni);
    $dFecIni = str_replace("/","",$dFecIni);
    $dFecFin = str_replace("-","",$dFecFin);
    $dFecFin = str_replace("/","",$dFecFin);

    ereg( "([0-9]{1,2})([0-9]{1,2})([0-9]{2,4})", $dFecIni, $aFecIni);
    ereg( "([0-9]{1,2})([0-9]{1,2})([0-9]{2,4})", $dFecFin, $aFecFin);

    $date1 = mktime(0,0,0,$aFecIni[2], $aFecIni[1], $aFecIni[3]);
    $date2 = mktime(0,0,0,$aFecFin[2], $aFecFin[1], $aFecFin[3]);

    return round(($date2 - $date1) / (60 * 60 * 24));
}

if(isset($_GET['idj'])&& isset($_GET['anno']) && isset($_GET['mes']) && isset($_GET['idtp'])){
$idj=$_GET['idj'];
$idtp=$_GET['idtp'];
$anno=$_GET['anno'];
$mm=$_GET['mes'];
}
//echo $idtp."<br>";
//listar servicios
$sqldepa="select j_id as codj, 0 as cods ,j_abrev as nom from tb_jefatura where j_id='$idj'";
$sqlserv="select j_id as codj, s_id as cods, s_abrev as nom from tb_servicio where j_id='$idj'";
$resultdepa = mysql_query($sqldepa);
$resultserv = mysql_query($sqlserv);
$arrayids = array();
while ($rowj= mysql_fetch_array($resultdepa)){
$arrayids[] = $rowj['codj'];
}
while ($rows= mysql_fetch_array($resultserv)){
$arrayids[] = $rows['cods'];
}


if(isset($_GET['ids']) && $_GET['ids']<>-1){
$ids = $_GET['ids'];
	if($_GET['idtp']!=0){
	$sqllistado = "select a.ug_id as cod, concat(a.ua_apepat,' ',a.ua_apemat,' ', a.ua_nom) as nombre, (select tp_desc from tb_tipoperson where tp_id=b.tp_id) as tipopersona, ";
	$sqllistado = $sqllistado." (select s_abrev from tb_servicio where s_id=b.s_id and j_id=b.j_id) as servicio from tb_usuasist a inner join tb_usudep b on ";
	$sqllistado = $sqllistado." a.ug_id = b.ug_id where b.j_id='$idj' and b.s_id='$ids' and b.tp_id='$idtp' and (a.ua_estado= 1 or a.ua_estado= 2) order by tipopersona, nombre";
	}else{
	$sqllistado = "select a.ug_id as cod, concat(a.ua_apepat,' ',a.ua_apemat,' ', a.ua_nom) as nombre, (select tp_desc from tb_tipoperson where tp_id=b.tp_id) as tipopersona, ";
	$sqllistado = $sqllistado." (select s_abrev from tb_servicio where s_id=b.s_id and j_id=b.j_id) as servicio from tb_usuasist a inner join tb_usudep b on ";
	$sqllistado = $sqllistado." a.ug_id = b.ug_id where b.j_id='$idj' and b.s_id='$ids' and (a.ua_estado= 1 or a.ua_estado= 2) order by tipopersona, nombre";
	}
}elseif($_GET['idtp']!=0){
$sqllistado = "select a.ug_id as cod, concat(a.ua_apepat,' ',a.ua_apemat,' ', a.ua_nom) as nombre, (select tp_desc from tb_tipoperson where tp_id=b.tp_id) as tipopersona, ";
$sqllistado = $sqllistado." (select s_abrev from tb_servicio where s_id=b.s_id and j_id=b.j_id) as servicio from tb_usuasist a inner join tb_usudep b on ";
$sqllistado = $sqllistado." a.ug_id = b.ug_id where b.j_id='$idj' and b.tp_id='$idtp'  and (a.ua_estado= 1 or a.ua_estado= 2) order by tipopersona, nombre";
}else{
$sqllistado = "select a.ug_id as cod, concat(a.ua_apepat,' ',a.ua_apemat,' ', a.ua_nom) as nombre, (select tp_desc from tb_tipoperson where tp_id=b.tp_id) as tipopersona, ";
$sqllistado = $sqllistado." (select s_abrev from tb_servicio where s_id=b.s_id and j_id=b.j_id) as servicio from tb_usuasist a inner join tb_usudep b on ";
$sqllistado = $sqllistado." a.ug_id = b.ug_id where b.j_id='$idj' and (a.ua_estado= 1 or a.ua_estado= 2) order by tipopersona, nombre";
}

//echo $sqllistado;
$resultlistado = mysql_query($sqllistado);
//echo $sqllistado;

$numerohoras="";
$aa=0;
$salida = "<table border='0' align='center' class='TableDiseno' >";
$salida = $salida."<tr><td align='center' colspan='7' class='LlenarTablaGareaTitulo'>LISTADO DEL PERSONAL</td></tr>";
$salida = $salida."<tr><td align='center' width='300' class='LlenarTablaGareaTituloUNO'>APELLIDOS Y NOMBRES</td><td align='center' width='100' class='LlenarTablaGareaTituloUNO'>SERVICIO</td><td align='center' width='200' class='LlenarTablaGareaTituloUNO'>TIPO DE PERSONAL</td><td align='center' width='120' class='LlenarTablaGareaTituloUNO'>FECHA</td><td align='center' width='150' class='LlenarTablaGareaTituloUNO'>HORARIO</td><td align='center' width='80' class='LlenarTablaGareaTituloUNO'>NRO HORAS</td><td align='center' width='200' class='LlenarTablaGareaTituloUNO'>OBSERVACIONES</td></tr>";
while ($rowl= mysql_fetch_array($resultlistado)){
$codigo=$rowl['cod'];
$nombre=$rowl['nombre'];
$tipopersona=$rowl['tipopersona'];
$servicio=$rowl['servicio'];

//ask por las horas permisos
$sqlaskdm="select hrp_id as id, date_format(a.hrp_fecha,'%d/%m/%Y') as fecha , concat(date_format(a.hrp_horaini,'%h:%i %p'),' - ',date_format(a.hrp_horafin,'%h:%i %p')) as horario, a.hrp_numhora as numhrs, (select thp_desc from tb_tphoraper where thp_id=a.thp_id) as observ from tb_horapermiso a where a.ug_id='$codigo' and  year(a.hrp_fecha)='$anno' and month(a.hrp_fecha)='$mm' ";

//echo $sqlaskdm."<br>";
$resultdm = mysql_query($sqlaskdm);

while ($row= mysql_fetch_array($resultdm)){
$idhp=$row['id'];
$fecha=$row['fecha'];
$hora=$row['horario'];
$nhrs=$row['numhrs'];
$obs=$row['observ'];


$diaini=substr($fini, 0, 2);
$diafin=substr($ffin, 0, 2);
$mesini=substr($fini, 3, 2);
$mesfin=substr($ffin, 3, 2);
$annoini=substr($fini, 6, 4);
$annofin=substr($ffin, 6, 4);

$mes = mktime( 0, 0, 0, $mm, 1, $anno ); 
$numeroDeDias = intval(date("t",$mes)); 

//pregunta por servicicios


$salida = $salida."<tr><td class ='FacetDataTextC'>".$nombre."</td><td align='center' class ='FacetDataTextC'>".$servicio."</td><td align='center' class ='FacetDataTextC'>".$tipopersona."</td><td align='center' class ='FacetDataTextC'>".$fecha."</td><td align='center' class ='FacetDataTextC'>".$hora."</td><td align='center' class ='FacetDataTextC'>".$nhrs."</td><td align='center' class ='FacetDataTextC'>".$obs."</td></tr>";
$numerohoras = $numerohoras + $nhrs;			
}	
}
$salida = $salida."<tr><td align='center' colspan='7'><b>NUMERO DE HORAS AFECTADAS  ".$numerohoras."</b></td></tr>";
$salida = $salida."</table>";

echo $salida;


?>

