<?php
header("Content-Type: application/vnd.ms-excel");
header("content-disposition: attachment;filename=listadopaciente.xls");

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='8'>LISTADO DEL PERSONAL</td></tr>";
$salida = $salida."<tr><td align='center' width='300'>APELLIDOS Y NOMBRES</td><td align='center' width='100' >SERVICIO</td><td align='center' width='200' >TIPO DE PERSONAL</td><td align='center' width='120' >F. INICIO</td><td align='center' width='120' >F. FIN</td><td align='center' width='80' >NRO DIAS</td><td align='center' width='70' >DIAS EN EL MES</td><td align='center' width='100' >NRO HORAS</td></tr>";
while ($rowl= mysql_fetch_array($resultlistado)){
$codigo=$rowl['cod'];
$nombre=$rowl['nombre'];
$tipopersona=$rowl['tipopersona'];
$servicio=$rowl['servicio'];

//ask por lo descansos medicos
if(isset($_GET['idestado']) && $_GET['idestado']!=0){
	if($_GET['idestado']==1){
	$sqlaskdm="select dm_id, date_format(dm_fechaini,'%d/%m/%Y') as fechaini , date_format(dm_fechafin,'%d/%m/%Y') as fechafin, dm_numdias, dm_estado from tb_descmed where ug_id='$codigo' and ((year(dm_fechaini)='$anno' and month(dm_fechaini)='$mm') or (year(dm_fechafin)='$anno' and month(dm_fechafin)='$mm')) and dm_estado='1'";
	}elseif($_GET['idestado']==2){
	$sqlaskdm="select dm_id, date_format(dm_fechaini,'%d/%m/%Y') as fechaini , date_format(dm_fechafin,'%d/%m/%Y') as fechafin, dm_numdias, dm_estado from tb_descmed where ug_id='$codigo' and ((year(dm_fechaini)='$anno' and month(dm_fechaini)='$mm') or (year(dm_fechafin)='$anno' and month(dm_fechafin)='$mm')) and dm_estado='0'";
	}
}else{
$sqlaskdm="select dm_id, date_format(dm_fechaini,'%d/%m/%Y') as fechaini , date_format(dm_fechafin,'%d/%m/%Y') as fechafin, dm_numdias, dm_estado from tb_descmed where ug_id='$codigo' and ((year(dm_fechaini)='$anno' and month(dm_fechaini)='$mm') or (year(dm_fechafin)='$anno' and month(dm_fechafin)='$mm'))";
}
//echo $sqlaskdm."<br>";
$resultdm = mysql_query($sqlaskdm);

while ($row= mysql_fetch_array($resultdm)){
$iddm=$row['dm_id'];
$fini=$row['fechaini'];
$ffin=$row['fechafin'];
$nhrs=$row['dm_numdias'];
$dmestado=$row['dm_estado'];


$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

$servicio="";

if($mm<10){
$mmm = '0'.$mm;
}

//pregunta por horas del descanso medicos afectado
if($annoini==$anno && $annofin==$anno){
if(($diaini>=1 && $mesini==$mm && $anno==$annoini) && ($diafin<=$numeroDeDias && $mesfin==$mm && $anno==$annofin)){
//consulta por las horas
$askhrsdm = "SELECT sum(b.h_horanum) as hrs, (select s_abrev from tb_servicio where s_id=a.hp_idserva and j_id=a.j_id) as servicio FROM tb_horaprog a inner join tb_horario b on a.h_id=b.h_id  inner join tb_area c on a.a_id=c.a_id and a.ga_id=c.ga_id ";
$askhrsdm = $askhrsdm. " where a.uap_anno='$anno' and a.uap_mes='$mm' and a.j_id='$idj' and a.hp_tipohora='0'";
$askhrsdm = $askhrsdm." and a.hp_dia between '$diaini' and '$diafin' and a.ug_id='$codigo' and c.a_desc <> 'CH' group by servicio";
$resulthrsdm = mysql_query($askhrsdm);
//echo $askhrsdm."<br>";

if(mysql_num_rows($resulthrsdm)>0){
while ($rowdm= mysql_fetch_array($resulthrsdm)){
	$horasdm = $rowdm['hrs'];
	$servicio=$rowdm['servicio'];
		if($servicio==""){
		$askdep = "select (select s_abrev from tb_servicio where s_id=a.s_id and j_id=a.j_id) as servicio from tb_usudep a where a.ug_id='$codigo'";
		$resultdep = mysql_query($askdep);
		if(mysql_num_rows($resultdep)>0){
		while ($rowd= mysql_fetch_array($resultdep)){
		$servicio = $rowd['servicio'];
			if($servicio==""){
				$askdep = "select (select j_abrev from tb_jefatura where j_id=a.j_id) as servicio from tb_usudep a where a.ug_id='$codigo'";
				$resultdep = mysql_query($askdep);
				if(mysql_num_rows($resultdep)>0){
				while ($rowd= mysql_fetch_array($resultdep)){
				$servicio = $rowd['servicio'];
				}
			}
			}
		}
		}
		}
		$numerohoras = $numerohoras + $horasdm;
	
		$numdiasmes = restaFechas($fini,$ffin) + 1;
		$salida = $salida."<tr><td >".$nombre."</td><td align='center' >".$servicio."</td><td align='center' >".$tipopersona."</td><td align='center' >".$fini."</td><td align='center' >".$ffin."</td><td align='center' >".$nhrs."</td><td align='center' >".$numdiasmes."</td><td align='center' >".$horasdm."</td></tr>";
		
		
		$aa = $aa +1;
	
	}
}else{
	//cantidad es 0
		$horasdm =0;
		$numdiasmes = 0;	
		if($servicio==""){
		$askdep = "select (select s_abrev from tb_servicio where s_id=a.s_id and j_id=a.j_id) as servicio from tb_usudep a where a.ug_id='$codigo'";
		$resultdep = mysql_query($askdep);
		if(mysql_num_rows($resultdep)>0){
		while ($rowd= mysql_fetch_array($resultdep)){
		$servicio = $rowd['servicio'];
			if($servicio==""){
				$askdep = "select (select j_abrev from tb_jefatura where j_id=a.j_id) as servicio from tb_usudep a where a.ug_id='$codigo'";
				$resultdep = mysql_query($askdep);
				if(mysql_num_rows($resultdep)>0){
				while ($rowd= mysql_fetch_array($resultdep)){
				$servicio = $rowd['servicio'];
				}
			}
			}
		}}}
		$numerohoras = $numerohoras + $horasdm;
	
		$numdiasmes = restaFechas($fini,$ffin) + 1;
	$salida = $salida."<tr><td >".$nombre."</td><td align='center' >".$servicio."</td><td align='center' >".$tipopersona."</td><td align='center' >".$fini."</td><td align='center' >".$ffin."</td><td align='center' >".$nhrs."</td><td align='center' >".$numdiasmes."</td><td align='center' >".$horasdm."</td></tr>";
	
	
	$aa = $aa +1;
	
}
}elseif(($diaini<=$numeroDeDias && $mesini==$mm && $annoini==$anno) && ($mesfin>$mm && $anno==$annofin)){

//consulta por las horas
$askhrsdm = "SELECT sum(b.h_horanum) as hrs, (select s_abrev from tb_servicio where s_id=a.hp_idserva and j_id=a.j_id) as servicio FROM tb_horaprog a inner join tb_horario b on a.h_id=b.h_id  inner join tb_area c on a.a_id=c.a_id and a.ga_id=c.ga_id ";
$askhrsdm = $askhrsdm. " where a.uap_anno='$anno' and a.uap_mes='$mm' and a.j_id='$idj' and a.hp_tipohora='0'";
$askhrsdm = $askhrsdm." and a.hp_dia between '$diaini' and '$numeroDeDias' and a.ug_id='$codigo' and c.a_desc <> 'CH' group by servicio";
//echo $askhrsdm."<br>";
$resulthrsdm = mysql_query($askhrsdm);
if(mysql_num_rows($resulthrsdm)>0){
	while ($rowdm= mysql_fetch_array($resulthrsdm)){
	$horasdm = $rowdm['hrs'];
	$servicio=$rowdm['servicio'];
		if($servicio==""){
		$askdep = "select (select s_abrev from tb_servicio where s_id=a.s_id and j_id=a.j_id) as servicio from tb_usudep a where a.ug_id='$codigo'";
		$resultdep = mysql_query($askdep);
		if(mysql_num_rows($resultdep)>0){
		while ($rowd= mysql_fetch_array($resultdep)){
		$servicio = $rowd['servicio'];
			if($servicio==""){
				$askdep = "select (select j_abrev from tb_jefatura where j_id=a.j_id) as servicio from tb_usudep a where a.ug_id='$codigo'";
				$resultdep = mysql_query($askdep);
				if(mysql_num_rows($resultdep)>0){
				while ($rowd= mysql_fetch_array($resultdep)){
				$servicio = $rowd['servicio'];
				}
			}
			}
		}}}
		$numerohoras = $numerohoras + $horasdm;
	
	$fffin = $numeroDeDias.'/'.$mmm.'/'.$anno;	
	$numdiasmes = restaFechas($fini,$fffin) + 1;
	$salida = $salida."<tr><td >".$nombre."</td><td align='center' >".$servicio."</td><td align='center' >".$tipopersona."</td><td align='center' >".$fini."</td><td align='center' >".$ffin."</td><td align='center' >".$nhrs."</td><td align='center' >".$numdiasmes."</td><td align='center' >".$horasdm."</td></tr>";
	
	
	$aa = $aa +1;
	}
 }else{
//cantidad es 0
	$horasdm =0;
	$numdiasmes = 0;
		if($servicio==""){
		$askdep = "select (select s_abrev from tb_servicio where s_id=a.s_id and j_id=a.j_id) as servicio from tb_usudep a where a.ug_id='$codigo'";
		$resultdep = mysql_query($askdep);
		if(mysql_num_rows($resultdep)>0){
		while ($rowd= mysql_fetch_array($resultdep)){
		$servicio = $rowd['servicio'];
			if($servicio==""){
				$askdep = "select (select j_abrev from tb_jefatura where j_id=a.j_id) as servicio from tb_usudep a where a.ug_id='$codigo'";
				$resultdep = mysql_query($askdep);
				if(mysql_num_rows($resultdep)>0){
				while ($rowd= mysql_fetch_array($resultdep)){
				$servicio = $rowd['servicio'];
				}
			}
			}
		}}}
		$numerohoras = $numerohoras + $horasdm;
		
	$fffin = $numeroDeDias.'/'.$mmm.'/'.$anno;	
	$numdiasmes = restaFechas($fini,$fffin) + 1;
	$salida = $salida."<tr><td >".$nombre."</td><td align='center' >".$servicio."</td><td align='center' >".$tipopersona."</td><td align='center' >".$fini."</td><td align='center' >".$ffin."</td><td align='center' >".$nhrs."</td><td align='center' >".$numdiasmes."</td><td align='center' >".$horasdm."</td></tr>";
	
	
	$aa = $aa +1;
	
}

}elseif(($mesini<$mm && $annoini==$anno) && ($diafin>=1 && $mesfin==$mm && $anno==$annofin)){

//consulta por las horas
$askhrsdm = "SELECT sum(b.h_horanum) as hrs, (select s_abrev from tb_servicio where s_id=a.hp_idserva and j_id=a.j_id) as servicio FROM tb_horaprog a inner join tb_horario b on a.h_id=b.h_id  inner join tb_area c on a.a_id=c.a_id and a.ga_id=c.ga_id ";
$askhrsdm = $askhrsdm. " where a.uap_anno='$anno' and a.uap_mes='$mm' and a.j_id='$idj' and a.hp_tipohora='0'";
$askhrsdm = $askhrsdm." and a.hp_dia between '1' and '$diafin' and a.ug_id='$codigo' and c.a_desc <> 'CH' group by servicio";
$resulthrsdm = mysql_query($askhrsdm);
//echo $askhrsdm."<br>";
if(mysql_num_rows($resulthrsdm)>0){
	while ($rowdm= mysql_fetch_array($resulthrsdm)){
	$horasdm = $rowdm['hrs'];
	$servicio=$rowdm['servicio'];
			
		if($servicio==""){
		$askdep = "select (select s_abrev from tb_servicio where s_id=a.s_id and j_id=a.j_id) as servicio from tb_usudep a where a.ug_id='$codigo'";
		$resultdep = mysql_query($askdep);
		if(mysql_num_rows($resultdep)>0){
		while ($rowd= mysql_fetch_array($resultdep)){
		$servicio = $rowd['servicio'];
			if($servicio==""){
				$askdep = "select (select j_abrev from tb_jefatura where j_id=a.j_id) as servicio from tb_usudep a where a.ug_id='$codigo'";
				$resultdep = mysql_query($askdep);
				if(mysql_num_rows($resultdep)>0){
				while ($rowd= mysql_fetch_array($resultdep)){
				$servicio = $rowd['servicio'];
				}
			}
			}
		}}}
		$numerohoras = $numerohoras + $horasdm;
		
		$fffini ='01/'.$mmm.'/'.$anno;
		$numdiasmes = restaFechas($fffini,$ffin) + 1;
	$salida = $salida."<tr><td >".$nombre."</td><td align='center' >".$servicio."</td><td align='center' >".$tipopersona."</td><td align='center' >".$fini."</td><td align='center' >".$ffin."</td><td align='center' >".$nhrs."</td><td align='center' >".$numdiasmes."</td><td align='center' >".$horasdm."</td></tr>";
	
	
	$aa = $aa +1;
	
	}
}else{
//cantidad es 0
	$horasdm =0;
	$numdiasmes=0;
		if($servicio==""){
		$askdep = "select (select s_abrev from tb_servicio where s_id=a.s_id and j_id=a.j_id) as servicio from tb_usudep a where a.ug_id='$codigo'";
		$resultdep = mysql_query($askdep);
		if(mysql_num_rows($resultdep)>0){
		while ($rowd= mysql_fetch_array($resultdep)){
		$servicio = $rowd['servicio'];
			if($servicio==""){
				$askdep = "select (select j_abrev from tb_jefatura where j_id=a.j_id) as servicio from tb_usudep a where a.ug_id='$codigo'";
				$resultdep = mysql_query($askdep);
				if(mysql_num_rows($resultdep)>0){
				while ($rowd= mysql_fetch_array($resultdep)){
				$servicio = $rowd['servicio'];
				}
			}
			}
		}}}
		$numerohoras = $numerohoras + $horasdm;
		
		$fffini ='01/'.$mmm.'/'.$anno;
		$numdiasmes = restaFechas($fffini,$ffin) + 1;
	$salida = $salida."<tr><td >".$nombre."</td><td align='center' >".$servicio."</td><td align='center' >".$tipopersona."</td><td align='center' >".$fini."</td><td align='center' >".$ffin."</td><td align='center' >".$nhrs."</td><td align='center' >".$numdiasmes."</td><td align='center' >".$horasdm."</td></tr>";
	
	
	$aa = $aa +1;
}
}
/*else{
//cantidad es 0
	$horasdm =0;
		
		if($servicio==""){
		$askdep = "select (select s_abrev from tb_servicio where s_id=a.s_id and j_id=a.j_id) as servicio from tb_usudep a where a.ug_id='$codigo'";
		$resultdep = mysql_query($askdep);
		if(mysql_num_rows($resultdep)>0){
		while ($rowd= mysql_fetch_array($resultdep)){
		$servicio = $rowd['servicio'];
			if($servicio==""){
				$askdep = "select (select j_abrev from tb_jefatura where j_id=a.j_id) as servicio from tb_usudep a where a.ug_id='$codigo'";
				$resultdep = mysql_query($askdep);
				if(mysql_num_rows($resultdep)>0){
				while ($rowd= mysql_fetch_array($resultdep)){
				$servicio = $rowd['servicio'];
				}
			}
			}
		}}}
		
		$salida = $salida."<tr><td>".$codigo."</td><td>".$nombre."</td><td>".$servicio."</td><td>".$tipopersona."</td><td>".$fini."</td><td>".$ffin."</td><td>".$nhrs."</td><td>".$horasdm."</td></tr>";
}
*/

////
}else{
if(($diaini<=$numeroDeDias && $mesini==$mm && $annoini==$anno) && ($diafin>=1 && $mesfin>=1 && $anno<$annofin)){

//consulta por las horas
$askhrsdm = "SELECT sum(b.h_horanum) as hrs, (select s_abrev from tb_servicio where s_id=a.hp_idserva and j_id=a.j_id) as servicio FROM tb_horaprog a inner join tb_horario b on a.h_id=b.h_id  inner join tb_area c on a.a_id=c.a_id and a.ga_id=c.ga_id ";
$askhrsdm = $askhrsdm. " where a.uap_anno='$anno' and a.uap_mes='$mm' and a.j_id='$idj' and a.hp_tipohora='0'";
$askhrsdm = $askhrsdm." and a.hp_dia between '1' and '31' and a.ug_id='$codigo' and c.a_desc <> 'CH' group by servicio";
//echo $askhrsdm."<br>";
$resulthrsdm = mysql_query($askhrsdm);
if(mysql_num_rows($resulthrsdm)>0){
	while ($rowdm= mysql_fetch_array($resulthrsdm)){
	$horasdm = $rowdm['hrs'];
	$servicio=$rowdm['servicio'];
		if($servicio==""){
		$askdep = "select (select s_abrev from tb_servicio where s_id=a.s_id and j_id=a.j_id) as servicio from tb_usudep a where a.ug_id='$codigo'";
		$resultdep = mysql_query($askdep);
		if(mysql_num_rows($resultdep)>0){
		while ($rowd= mysql_fetch_array($resultdep)){
		$servicio = $rowd['servicio'];
			if($servicio==""){
				$askdep = "select (select j_abrev from tb_jefatura where j_id=a.j_id) as servicio from tb_usudep a where a.ug_id='$codigo'";
				$resultdep = mysql_query($askdep);
				if(mysql_num_rows($resultdep)>0){
				while ($rowd= mysql_fetch_array($resultdep)){
				$servicio = $rowd['servicio'];
				}
			}
			}
		}}}
		$numerohoras = $numerohoras + $horasdm;
		
	$fffin = $numeroDeDias.'/'.$mmm.'/'.$anno;
	$numdiasmes = restaFechas($ffini,$fffin) + 1;
	$salida = $salida."<tr><td >".$nombre."</td><td align='center' >".$servicio."</td><td align='center' >".$tipopersona."</td><td align='center' >".$fini."</td><td align='center' >".$ffin."</td><td align='center' >".$nhrs."</td><td align='center' >".$numdiasmes."</td><td align='center' >".$horasdm."</td></tr>";
	
	
	$aa = $aa +1;
	}
 }else{
//cantidad es 0
	$horasdm =0;
			
		if($servicio==""){
		$askdep = "select (select s_abrev from tb_servicio where s_id=a.s_id and j_id=a.j_id) as servicio from tb_usudep a where a.ug_id='$codigo'";
		$resultdep = mysql_query($askdep);
		if(mysql_num_rows($resultdep)>0){
		while ($rowd= mysql_fetch_array($resultdep)){
		$servicio = $rowd['servicio'];
			if($servicio==""){
				$askdep = "select (select j_abrev from tb_jefatura where j_id=a.j_id) as servicio from tb_usudep a where a.ug_id='$codigo'";
				$resultdep = mysql_query($askdep);
				if(mysql_num_rows($resultdep)>0){
				while ($rowd= mysql_fetch_array($resultdep)){
				$servicio = $rowd['servicio'];
				}
			}
			}
		}}}
		$numerohoras = $numerohoras + $horasdm;
		
	$fffin = $numeroDeDias.'/'.$mmm.'/'.$anno;
	$numdiasmes = restaFechas($ffini,$fffin) + 1;
	$salida = $salida."<tr><td >".$nombre."</td><td align='center' >".$servicio."</td><td align='center' >".$tipopersona."</td><td align='center' >".$fini."</td><td align='center' >".$ffin."</td><td align='center' >".$nhrs."</td><td align='center' >".$numdiasmes."</td><td align='center' >".$horasdm."</td></tr>";
	
	
	$aa = $aa +1;
}
$numero = "Despues";
}elseif(($diaini>=1 && $mesini==12 && $annoini<$anno) && ($diafin>=1 && $mesfin==$mm && $anno==$annofin)){
//consulta por las horas
$askhrsdm = "SELECT sum(b.h_horanum) as hrs, (select s_abrev from tb_servicio where s_id=a.hp_idserva and j_id=a.j_id) as servicio FROM tb_horaprog a inner join tb_horario b on a.h_id=b.h_id  inner join tb_area c on a.a_id=c.a_id and a.ga_id=c.ga_id ";
$askhrsdm = $askhrsdm. " where a.uap_anno='$anno' and a.uap_mes='$mm' and a.j_id='$idj' and a.hp_tipohora='0'";
$askhrsdm = $askhrsdm." and a.hp_dia between '1' and '31' and a.ug_id='$codigo' and c.a_desc <> 'CH' group by servicio";
//echo $askhrsdm."<br>";
$resulthrsdm = mysql_query($askhrsdm);
if(mysql_num_rows($resulthrsdm)>0){
	while ($rowdm= mysql_fetch_array($resulthrsdm)){
	$horasdm = $rowdm['hrs'];
	$servicio=$rowdm['servicio'];
		if($servicio==""){
		$askdep = "select (select s_abrev from tb_servicio where s_id=a.s_id and j_id=a.j_id) as servicio from tb_usudep a where a.ug_id='$codigo'";
		$resultdep = mysql_query($askdep);
		if(mysql_num_rows($resultdep)>0){
		while ($rowd= mysql_fetch_array($resultdep)){
		$servicio = $rowd['servicio'];
			if($servicio==""){
				$askdep = "select (select j_abrev from tb_jefatura where j_id=a.j_id) as servicio from tb_usudep a where a.ug_id='$codigo'";
				$resultdep = mysql_query($askdep);
				if(mysql_num_rows($resultdep)>0){
				while ($rowd= mysql_fetch_array($resultdep)){
				$servicio = $rowd['servicio'];
				}
			}
			}
		}}}
		$numerohoras = $numerohoras + $horasdm;
		if($dmestado==0){
			$dmoutestado ="<img src='../img/red.png' width='17' height='17' value='".$codigo."-".$iddm."' onClick=validarDM(this.value)>";
		}else{
			$dmoutestado ="<img src='../img/green.png' width='17' height='17'>";
		}
	$fffini = '01/'.$mmm.'/'.$anno;
	$numdiasmes = restaFechas($fffini,$ffin) + 1;
	$salida = $salida."<tr><td >".$nombre."</td><td align='center' >".$servicio."</td><td align='center' >".$tipopersona."</td><td align='center' >".$fini."</td><td align='center' >".$ffin."</td><td align='center' >".$nhrs."</td><td align='center' >".$numdiasmes."</td><td align='center' >".$horasdm."</td></tr>";
	
	
	$aa = $aa +1;
	}
 }else{
//cantidad es 0
	$horasdm =0;
			
		if($servicio==""){
		$askdep = "select (select s_abrev from tb_servicio where s_id=a.s_id and j_id=a.j_id) as servicio from tb_usudep a where a.ug_id='$codigo'";
		$resultdep = mysql_query($askdep);
		if(mysql_num_rows($resultdep)>0){
		while ($rowd= mysql_fetch_array($resultdep)){
		$servicio = $rowd['servicio'];
			if($servicio==""){
				$askdep = "select (select j_abrev from tb_jefatura where j_id=a.j_id) as servicio from tb_usudep a where a.ug_id='$codigo'";
				$resultdep = mysql_query($askdep);
				if(mysql_num_rows($resultdep)>0){
				while ($rowd= mysql_fetch_array($resultdep)){
				$servicio = $rowd['servicio'];
				}
			}
			}
		}}}
		$numerohoras = $numerohoras + $horasdm;
		if($dmestado==0){
			$dmoutestado ="<img src='../img/red.png' width='17' height='17' value='".$codigo."-".$iddm."' onClick=validarDM(this.value)>";
		}else{
			$dmoutestado ="<img src='../img/green.png' width='17' height='17'>";
		}
	$fffini = '01/'.$mmm.'/'.$anno;
	$numdiasmes = restaFechas($fffini,$ffin) + 1;
	$salida = $salida."<tr><td >".$nombre."</td><td align='center' >".$servicio."</td><td align='center' >".$tipopersona."</td><td align='center' >".$fini."</td><td align='center' >".$ffin."</td><td align='center' >".$nhrs."</td><td align='center' >".$numdiasmes."</td><td align='center' >".$horasdm."</td></tr>";
	
	
	$aa = $aa +1;
}
$numero = "Antes";
}
}

}

}
$salida = $salida."<tr><td align='center' colspan='8'><b>NUMERO DE HORAS AFECTADAS  ".$numerohoras."</b></td></tr>";
$salida = $salida."</table>";


echo $salida;
?>
