<?php

if($annoini==$anno or $annofin==$anno){
//pregunta si existe turnos suspendidos
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;
		if($dmestado==0){
			//$dmoutestado ="<img src='../img/red.png' width='17' height='17' value='".$codigo."-".$iddm.".' onClick=validarDM(this.value)>";
			$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'>";
		}
		$numdiasmes = restaFechas($fini,$ffin) + 1;
		$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'>".$fini."</td><td align='center' class ='FacetDataTextC'>".$ffin."</td><td align='center' class ='FacetDataTextC'>".$nhrs."</td><td align='center' class ='FacetDataTextC'>".$numdiasmes."</td><td align='center' class ='FacetDataTextC'>".$horasdm."</td><td align='center' class ='FacetDataTextC'>".$dmoutestado."</td><td align='center' class ='FacetDataTextC'><input type='checkbox' name='lista[".$aa."]' id='lista[".$aa."]'</td></tr>";
		$cadena1 = $cadena1."<input type='hidden' id='idcoda[".$aa."]' name='idcoda[".$aa."]' value='".$codigo."'>";
		$cadena2 = $cadena2."<input type='hidden' id='iddma[".$aa."]' name='iddma[".$aa."]' value='".$iddm."'>";
		$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;
		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'>";
		}
		$numdiasmes = restaFechas($fini,$ffin) + 1;
	$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'>".$fini."</td><td align='center' class ='FacetDataTextC'>".$ffin."</td><td align='center' class ='FacetDataTextC'>".$nhrs."</td><td align='center' class ='FacetDataTextC'>".$numdiasmes."</td><td align='center' class ='FacetDataTextC'>".$horasdm."</td><td align='center' class ='FacetDataTextC'>".$dmoutestado."</td><td align='center' class ='FacetDataTextC'><input type='checkbox' name='lista[".$aa."]' id='lista[".$aa."]'</td></tr>";
	$cadena1 = $cadena1."<input type='hidden' id='idcoda[".$aa."]' name='idcoda[".$aa."]' value='".$codigo."'>";
	$cadena2 = $cadena2."<input type='hidden' id='iddma[".$aa."]' name='iddma[".$aa."]' value='".$iddm."'>";
	$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;
		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'>";
		}
	$fffin = $numeroDeDias.'/'.$mmm.'/'.$anno;	
	$numdiasmes = restaFechas($fini,$fffin) + 1;
	$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'>".$fini."</td><td align='center' class ='FacetDataTextC'>".$ffin."</td><td align='center' class ='FacetDataTextC'>".$nhrs."</td><td align='center' class ='FacetDataTextC'>".$numdiasmes."</td><td align='center' class ='FacetDataTextC'>".$horasdm."</td><td align='center' class ='FacetDataTextC'>".$dmoutestado."</td><td align='center' class ='FacetDataTextC'><input type='checkbox' name='lista[".$aa."]' id='lista[".$aa."]'</td></tr>";
	$cadena1 = $cadena1."<input type='hidden' id='idcoda[".$aa."]' name='idcoda[".$aa."]' value='".$codigo."'>";
	$cadena2 = $cadena2."<input type='hidden' id='iddma[".$aa."]' name='iddma[".$aa."]' value='".$iddm."'>";
	$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;
		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'>";
		}
	$fffin = $numeroDeDias.'/'.$mmm.'/'.$anno;	
	$numdiasmes = restaFechas($fini,$fffin) + 1;
	$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'>".$fini."</td><td align='center' class ='FacetDataTextC'>".$ffin."</td><td align='center' class ='FacetDataTextC'>".$nhrs."</td><td align='center' class ='FacetDataTextC'>".$numdiasmes."</td><td align='center' class ='FacetDataTextC'>".$horasdm."</td><td align='center' class ='FacetDataTextC'>".$dmoutestado."</td><td align='center' class ='FacetDataTextC'><input type='checkbox' name='lista[".$aa."]' id='lista[".$aa."]'</td></tr>";
	$cadena1 = $cadena1."<input type='hidden' id='idcoda[".$aa."]' name='idcoda[".$aa."]' value='".$codigo."'>";
	$cadena2 = $cadena2."<input type='hidden' id='iddma[".$aa."]' name='iddma[".$aa."]' value='".$iddm."'>";
	$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;
		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 class ='FacetDataTextC'>".$nombre."</td><td align='center' class ='FacetDataTextC'>".$servicio."</td><td align='center' class ='FacetDataTextC'>".$tipopersona."</td><td align='center' class ='FacetDataTextC'>".$fini."</td><td align='center' class ='FacetDataTextC'>".$ffin."</td><td align='center' class ='FacetDataTextC'>".$nhrs."</td><td align='center' class ='FacetDataTextC'>".$numdiasmes."</td><td align='center' class ='FacetDataTextC'>".$horasdm."</td><td align='center' class ='FacetDataTextC'>".$dmoutestado."</td><td align='center' class ='FacetDataTextC'><input type='checkbox' name='lista[".$aa."]' id='lista[".$aa."]'</td></tr>";
	$cadena1 = $cadena1."<input type='hidden' id='idcoda[".$aa."]' name='idcoda[".$aa."]' value='".$codigo."'>";
	$cadena2 = $cadena2."<input type='hidden' id='iddma[".$aa."]' name='iddma[".$aa."]' value='".$iddm."'>";
	$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;
		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 class ='FacetDataTextC'>".$nombre."</td><td align='center' class ='FacetDataTextC'>".$servicio."</td><td align='center' class ='FacetDataTextC'>".$tipopersona."</td><td align='center' class ='FacetDataTextC'>".$fini."</td><td align='center' class ='FacetDataTextC'>".$ffin."</td><td align='center' class ='FacetDataTextC'>".$nhrs."</td><td align='center' class ='FacetDataTextC'>".$numdiasmes."</td><td align='center' class ='FacetDataTextC'>".$horasdm."</td><td align='center' class ='FacetDataTextC'>".$dmoutestado."</td><td align='center' class ='FacetDataTextC'><input type='checkbox' name='lista[".$aa."]' id='lista[".$aa."]'</td></tr>";
	$cadena1 = $cadena1."<input type='hidden' id='idcoda[".$aa."]' name='idcoda[".$aa."]' value='".$codigo."'>";
	$cadena2 = $cadena2."<input type='hidden' id='iddma[".$aa."]' name='iddma[".$aa."]' value='".$iddm."'>";
	$aa = $aa +1;
}
}

}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;
		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'>";
		}
	$fffin = $numeroDeDias.'/'.$mmm.'/'.$anno;
	$numdiasmes = restaFechas($ffini,$fffin) + 1;
	$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'>".$fini."</td><td align='center' class ='FacetDataTextC'>".$ffin."</td><td align='center' class ='FacetDataTextC'>".$nhrs."</td><td align='center' class ='FacetDataTextC'>".$numdiasmes."</td><td align='center' class ='FacetDataTextC'>".$horasdm."</td><td align='center' class ='FacetDataTextC'>".$dmoutestado."</td><td align='center' class ='FacetDataTextC'><input type='checkbox' name='lista[".$aa."]' id='lista[".$aa."]'</td></tr>";
	$cadena1 = $cadena1."<input type='hidden' id='idcoda[".$aa."]' name='idcoda[".$aa."]' value='".$codigo."'>";
	$cadena2 = $cadena2."<input type='hidden' id='iddma[".$aa."]' name='iddma[".$aa."]' value='".$iddm."'>";
	$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'>";
		}
	$fffin = $numeroDeDias.'/'.$mmm.'/'.$anno;
	$numdiasmes = restaFechas($ffini,$fffin) + 1;
	$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'>".$fini."</td><td align='center' class ='FacetDataTextC'>".$ffin."</td><td align='center' class ='FacetDataTextC'>".$nhrs."</td><td align='center' class ='FacetDataTextC'>".$numdiasmes."</td><td align='center' class ='FacetDataTextC'>".$horasdm."</td><td align='center' class ='FacetDataTextC'>".$dmoutestado."</td><td align='center' class ='FacetDataTextC'><input type='checkbox' name='lista[".$aa."]' id='lista[".$aa."]'</td></tr>";
	$cadena1 = $cadena1."<input type='hidden' id='idcoda[".$aa."]' name='idcoda[".$aa."]' value='".$codigo."'>";
	$cadena2 = $cadena2."<input type='hidden' id='iddma[".$aa."]' name='iddma[".$aa."]' value='".$iddm."'>";
	$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 class ='FacetDataTextC'>".$nombre."</td><td align='center' class ='FacetDataTextC'>".$servicio."</td><td align='center' class ='FacetDataTextC'>".$tipopersona."</td><td align='center' class ='FacetDataTextC'>".$fini."</td><td align='center' class ='FacetDataTextC'>".$ffin."</td><td align='center' class ='FacetDataTextC'>".$nhrs."</td><td align='center' class ='FacetDataTextC'>".$numdiasmes."</td><td align='center' class ='FacetDataTextC'>".$horasdm."</td><td align='center' class ='FacetDataTextC'>".$dmoutestado."</td><td align='center' class ='FacetDataTextC'><input type='checkbox' name='lista[".$aa."]' id='lista[".$aa."]'</td></tr>";
	$cadena1 = $cadena1."<input type='hidden' id='idcoda[".$aa."]' name='idcoda[".$aa."]' value='".$codigo."'>";
	$cadena2 = $cadena2."<input type='hidden' id='iddma[".$aa."]' name='iddma[".$aa."]' value='".$iddm."'>";
	$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 class ='FacetDataTextC'>".$nombre."</td><td align='center' class ='FacetDataTextC'>".$servicio."</td><td align='center' class ='FacetDataTextC'>".$tipopersona."</td><td align='center' class ='FacetDataTextC'>".$fini."</td><td align='center' class ='FacetDataTextC'>".$ffin."</td><td align='center' class ='FacetDataTextC'>".$nhrs."</td><td align='center' class ='FacetDataTextC'>".$numdiasmes."</td><td align='center' class ='FacetDataTextC'>".$horasdm."</td><td align='center' class ='FacetDataTextC'>".$dmoutestado."</td><td align='center' class ='FacetDataTextC'><input type='checkbox' name='lista[".$aa."]' id='lista[".$aa."]'</td></tr>";
	$cadena1 = $cadena1."<input type='hidden' id='idcoda[".$aa."]' name='idcoda[".$aa."]' value='".$codigo."'>";
	$cadena2 = $cadena2."<input type='hidden' id='iddma[".$aa."]' name='iddma[".$aa."]' value='".$iddm."'>";
	$aa = $aa +1;
}
$numero = "Antes";
}
}

?>