<html>
<head>
<title>PROGRAMACION DEL PERSONAL ASISTENCIAL</title>
<link rel="stylesheet" type="text/css" href="../../styles/Style.css">
</head>
<body>
<?php
date_default_timezone_set('UTC');
//$idj=$_GET['idj'];
//$ids=$_GET['ids'];
//$idtp=$_GET['idtp'];
//$ugid=$_GET['ugid'];
$anno=$_GET['anno'];
$mm=$_GET['mes'];
$idj=1;
$ids=2;
$idtp=1;
//$ugid='SCPHU4';
//$anno=2011;
//$mm=1;
$ugid=$_GET['ugid'];
//$ugid='SCPHU4';
//$anno=$_GET['anno'];
//$anno=2011;
//$mm = 1;
//$mm=$_GET['mes'];
session_start();


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

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");


//armando grupo de servicios y sus dependencias
$sqldepa="select j_id as codj, 0 as cods ,j_abrev as nom, j_desc as nomc from tb_jefatura where j_id='$idj'";
$sqlserv="select j_id as codj, s_id as cods, s_abrev as nom, s_desc as nomc from tb_servicio where j_id='$idj'";
$resultdepa = mysql_query($sqldepa);
$resultserv = mysql_query($sqlserv);
$a=0;
$cad="";
//$cad = $cad."<tr>";
$serv[] = array();
$nomserv[] = array();
//echo $sqlserv;
while ($rows= mysql_fetch_array($resultserv)){
$cad = $cad."<td width='10' class='facetfilaTD' colspan='4' align='center' >".$rows['nom']."</td>";
$a=$a+1;
$serv[$a]=$rows['cods'];
$nomserv[$a]=$rows['nomc'];
}
while ($rowj= mysql_fetch_array($resultdepa)){
$cad = $cad."<td width='10' class='facetfilaTD' colspan='4'  align='center' >".$rowj['nom']."</td>";
$a=$a+1;
$serv[$a]=$rowj['cods'];
$nomserv[$a]=$rowj['nomc'];
}




//algunos datos

$sqlper="select a.ug_id as codigo, CONCAT(a.ua_apepat,' ',a.ua_apemat,' ',a.ua_nom) as nombre, ";
$sqlper= $sqlper." (select j_desc from tb_jefatura where j_id=b.j_id) as jefatura, (select s_desc from tb_servicio where s_id=b.s_id and j_id=b.j_id) as servicio,";
$sqlper= $sqlper." (select c_desc from tb_cargo where c_id=a.c_id) as cargo, (select esp_desc from tb_especialidad where ua_idespec=a.ua_idespec) as especialidad,";
$sqlper= $sqlper." a.ua_numplanilla as planilla, a.ua_numesp as numespecialidad from tb_usuasist a inner join tb_usudep b on a.ug_id=b.ug_id where a.ug_id='$ugid'";
$resultper = mysql_query($sqlper);
while ($row= mysql_fetch_array($resultper)){
$codigo = $row['codigo'];
$nombre = $row['nombre'];
$jefatura = $row['jefatura'];
$servicio = $row['servicio'];
$cargo = $row['cargo'];
$especialidad = $row['especialidad'];
$numplanilla = $row['planilla'];
$numespecilidad = $row['numespecilidad'];
}

$arraymes = array();
$arraymes[] = array('idmes'=>'1','mes'=>'ENERO');
$arraymes[] = array('idmes'=>'2','mes'=>'FEBRERO');
$arraymes[] = array('idmes'=>'3','mes'=>'MARZO');
$arraymes[] = array('idmes'=>'4','mes'=>'ABRIL');
$arraymes[] = array('idmes'=>'5','mes'=>'MAYO');
$arraymes[] = array('idmes'=>'6','mes'=>'JUNIO');
$arraymes[] = array('idmes'=>'7','mes'=>'JULIO');
$arraymes[] = array('idmes'=>'8','mes'=>'AGOSTO');
$arraymes[] = array('idmes'=>'9','mes'=>'SETIEMBRE');
$arraymes[] = array('idmes'=>'10','mes'=>'OCTUBRE');
$arraymes[] = array('idmes'=>'11','mes'=>'NOVIEMBRE');
$arraymes[] = array('idmes'=>'12','mes'=>'DICIEMBRE');


//total de horas
$sqltotalhrs="select sum(chp_numgd) as tgd, sum(chp_numgn) as tgn, sum(chp_numho) as tho, sum(chp_numhe) as the from tb_consolhprog where ug_id='$ugid' and uap_anno='$anno' and uap_mes='$mm' and tp_id='$idtp'";
$resultthrs = mysql_query($sqltotalhrs);
//echo $sqltotalhrs."<br>";
	if(mysql_num_rows($resultthrs)==0){
		$tgd = 0;
		$tgn = 0;
		$tho = 0;
		$the = 0;
	}else{
		while ($rowth= mysql_fetch_array($resultthrs)){
		
		$tgd = $rowth['tgd'];
		$tgn = $rowth['tgn'];
		$tho = $rowth['tho'];
		$the = $rowth['the'];
		}
	}
$diasresta = $numeroDeDias-27;
$diasrestantes = $diasresta + 1;
$diascabecera = $diasresta + 16;
$cabeceramayor = $diascabecera + 16;
?>

<table align="center">
<tr><td align="center" colspan='<?php echo $cabeceramayor;?>'><b>FORMATO UNICO DE LA PROGRAMACION DEL PROFESIONAL - ESSALUD</b></td></tr>
<tr><td class='facetfilaTD'>RED ASISTENCIAL</td><td align="left" colspan='8'>INSTITUTO NACIONAL CARDIOVASCULAR</td><td colspan='7' class='facetfilaTD'>CENTRO ASISTENCIAL</td><td align="left" colspan='<?php echo $diascabecera;?>'>INSTITUTO NACIONAL CARDIOVASCULAR</td></tr>
<tr><td class='facetfilaTD'>DEPARTAMENTO</td><td align="left" colspan='8'><?php echo $jefatura;?> </td><td colspan='7' class='facetfilaTD'>SERVICIO</td><td align="left" colspan='<?php echo $diascabecera;?>'><?php echo $servicio;?> </td></tr>
<tr><td class='facetfilaTD'>ESPECIALIDAD</td><td align="left" colspan='8'><?php echo $especialidad;?> </td><td colspan='7' class='facetfilaTD'>CARGO</td><td align="left" colspan='8'><?php echo $cargo;?></td><td colspan='7' class='facetfilaTD'>NRO COLEGIATURA</td><td align="right" colspan='<?php echo $diasrestantes; ?>'><?php echo $numcolegiatura;?></td></tr>
<tr><td class='facetfilaTD'>APELLIDOS Y NOMBRES</td><td align="left" colspan='15'><?php echo $nombre;?> </td><td colspan='2' class='facetfilaTD'>NRO DNI</td><td align="left" colspan='6'><?php echo $dni;?></td><td colspan='7' class='facetfilaTD'>NRO ESPECIALIDAD</td><td align="right" colspan='<?php echo $diasrestantes; ?>'><?php echo $numespecilidad;?></td></tr>

<?php
$calendario = $calendario."<tr><td class='facetfilaTD'>MES QUE INICIA</td><td align='left' colspan='3'>".$arraymes[$mm-1]['mes']."</td>";

//HORAS ORDINARIAS

for ($i=1;$i<=$numeroDeDias;$i++){

$dia= date("w",mktime( 0, 0, 0, $mm, $i, $anno )); 
switch ($dia) {
    case 0:
        $calendario = $calendario."<td align='center' class='facetdataTDR'><b>DOM</b></td>";
        break;
    case 1:
        $calendario = $calendario."<td align='center' class='facetdataTD'>LUN</td>";
        break;
    case 2:
        $calendario = $calendario."<td align='center' class='facetdataTD'>MAR</td>";
        break;
	case 3:
        $calendario = $calendario."<td align='center' class='facetdataTD'>MIE</td>";
        break;
	case 4:
        $calendario = $calendario."<td align='center' class='facetdataTD'>JUE</td>";
        break;
	case 5:
        $calendario = $calendario."<td align='center' class='facetdataTD'>VIE</td>";
        break;
	case 6:
        $calendario = $calendario."<td align='center' class='facetdataTD'>SAB</td>";
        break;
}
}
$calendario = $calendario."<td class='facetfilaTD' rowspan='2' align='center'>TOTAL DE<BR>HORAS</td></tr>";
$calendario = $calendario."<tr><td class='facetfilaTD'>AREAS / ACTIVIDADES</td><td class='facetfilaTD' colspan='2' align='center' >TURNO</td><td class='facetfilaTD'>NRO HORAS</td>";

for ($i=1;$i<=$numeroDeDias;$i++){

$dia= date("w",mktime( 0, 0, 0, $mm, $i, $anno )); 
switch ($dia) {
    case 0:
        $calendario = $calendario."<td align='center' class='facetfilaTD'><b>$i</b></td>";
        break;
    case 1:
    case 2:
    case 3:
    case 4:
    case 5:
    case 6:
        $calendario = $calendario."<td align='center' class='facetfilaTD'>$i</td>";;
        break;
}
}
$calendario = $calendario."</tr>";
$hordinarias='';
$hextras='';
$hordinariasapoyo='';
$hextrasapoyo='';
//horas ordinarias
$hordinariasencabeza = $hordinariasencabeza."<tr><td align='left' colspan='".$cabeceramayor."' class='facetfilaTD'><b>HORAS ORDINARIAS</b></td></tr>";
		//pregunta por el grupo area
			$sqlgarea="select distinct(a.ga_id) as ida, (select ga_desc from tb_garea where ga_id=a.ga_id) as garea from tb_horaprog a where a.tp_id='$idtp' and a.uap_anno='$anno' and a.uap_mes='$mm' and a.j_id='$idj' and a.s_id='$ids' and a.hp_idserva='$ids' and a.ug_id='$ugid' and a.hp_tipohora='0' order by ida asc";
			//echo $sqlgarea."<br>";
			$resultga = mysql_query($sqlgarea);
			if(mysql_num_rows($resultga)!=0){
			while ($rowga= mysql_fetch_array($resultga)){
			$idga= $rowga['ida'];
			$garea= $rowga['garea'];
			$hordinarias=$hordinarias."<tr><td class='facetfilaTD' colspan='".$cabeceramayor."'>".$garea."</td></tr>";
			//}
					//pregunta por las areas
						$sqlarea ="select distinct(a_id) as ida from tb_horaprog where ga_id='$idga' and tp_id='$idtp' and uap_anno='$anno' and uap_mes='$mm' and j_id='$idj' and hp_idserva='$ids' and s_id='$ids' and ug_id='$ugid' and hp_tipohora='0'";
						$resulta = mysql_query($sqlarea);
						//echo $sqlarea."<br>";
						if(mysql_num_rows($resulta)!=0){
						while ($row= mysql_fetch_array($resulta)){
						$ida=$row['ida'];
						$sqlcantturno = "select a.a_id as codarea, (select a_desc from tb_area where a_id=a.a_id and ga_id=a.ga_id) as area, count(distinct(a.h_id)) as idhora from tb_horaprog a where a.j_id='$idj' and a.s_id='$ids' and a.uap_anno='$anno' and a.uap_mes='$mm' and a.tp_id='$idtp' and a.a_id='$ida' and a.ga_id='$idga' and a.hp_tipohora='0' and a.ug_id='$ugid' group by a.a_id";
						//echo $sqlcantturno."<br>";
						$resultcantturno = mysql_query($sqlcantturno);
						//echo $sqlcantturno."<br><br>";
						while ($rownt= mysql_fetch_array($resultcantturno)){
						$area= $rownt['area'];
						$cantreg = $rownt['idhora'];
						$hordinarias=$hordinarias."<tr><td rowspan='".$cantreg."'>".$area."</td>";
						//pregunta por el horario
						$sqlhora="select distinct(h_id) as idhora from tb_horaprog where j_id='$idj' and s_id='$ids' and uap_anno='$anno' and uap_mes='$mm' and tp_id='$idtp' and a_id='$ida' and ga_id='$idga' and ug_id='$ugid' and hp_tipohora='0'";
						$resulthora=mysql_query($sqlhora);
						//echo $sqlhora."<br><br>";
						while ($rowh= mysql_fetch_array($resulthora)){
						$idh=$rowh['idhora'];
						//pregunta por el turno
						$sqlturno = "select h_id, concat(date_format(h_horaini,'%k:%i %p'),' - ',date_format(h_horafin,'%k:%i %p')) as hora, h_horaturn as turno, h_horanum as numhora from tb_horario where h_id='$idh'";
						$resultturno = mysql_query($sqlturno);
						//echo $sqlturno."<br><br>";
						while ($rowtt= mysql_fetch_array($resultturno)){
						$hordinarias=$hordinarias."<td align='center'>".$rowtt['hora']."</td><td align='center'>".$rowtt['turno']."</td><td align='center'>".$rowtt['numhora']."</td>";
						}
						for ($j=1;$j<=$numeroDeDias;$j++){
						$sqlconsultaturno="select (select h_horanum from tb_horario where h_id=a.h_id) as hora  from tb_horaprog a where a.j_id='$idj' and a.s_id='$ids' and a.uap_mes='$mm' and a.uap_anno='$anno' and a.hp_dia='$j' and a.h_id='$idh' and a.a_id='$ida' and a.ga_id='$idga' and a.tp_id='$idtp' and a.hp_idserva='$ids' and a.hp_tipohora=0 and a.ug_id='$ugid'";
						//echo $sqlconsultaturno."<br>";
						//echo $sqlconsultaturno."<br>";
						$resultcontur = mysql_query($sqlconsultaturno);
						$dia= date("w",mktime( 0, 0, 0, $mm, $j, $anno )); 
						if(mysql_num_rows($resultcontur)==0){
						switch ($dia) {
						    case 0: $hordinarias=$hordinarias."<td width='10' align='center' class ='FacetDataText'> </td>"; break;
						    case 1: 
						    case 2:
						    case 3:
						    case 4:
						    case 5:
						    case 6:
						        $hordinarias=$hordinarias."<td width='10' align='center' class ='FacetDataTextR'> </td>"; break;
						}

						}else{
						while($rowt= mysql_fetch_array($resultcontur)){
						$hrord = $rowt['hora'];
							switch ($dia) {
						    case 0: $hordinarias=$hordinarias."<td width='10' align='center' class ='FacetDataText'>".$hrord."</td>"; break;
						    case 1: 
						    case 2:
						    case 3:
						    case 4:
						    case 5:
						    case 6:
						        $hordinarias=$hordinarias."<td width='10'  align='center' class ='FacetDataTextR'>".$hrord."</td>"; break;
						}}
						}
								
													
						}
						
										
						
						
		//horas por turno
		$askhrsturno = "select sum(b.h_horanum) as sumhrs from tb_horaprog a inner join tb_horario b on a.h_id=b.h_id where a.j_id='$idj' and a.s_id='$ids' and a.uap_mes='$mm' and a.uap_anno='$anno' and a.h_id='$idh' and a.a_id='$ida' and a.ga_id='$idga' and a.tp_id='$idtp' and a.hp_idserva='$ids' and a.hp_tipohora=0 and a.ug_id='$ugid' group by a.h_id";
		$resultaskhrst = mysql_query($askhrsturno);
		if(mysql_num_rows($resultaskhrst)==0){
			$hordinarias=$hordinarias."<td width='10' class ='FacetDataTextR'>0</td></tr>";
		}else{
			while($rowht= mysql_fetch_array($resultaskhrst)){
			$totalhorasturno = $rowht['sumhrs'];
			$hordinarias=$hordinarias."<td width='10' class='facetfilaTD'  align='center'>".$totalhorasturno."</td></tr>";
			}
		}
	}	
	}
		}
	}
	//sub cantidades de los grupos de areas
						for ($k=1;$k<=$numeroDeDias;$k++){
							$sqlaskhrsga = "select sum(b.h_horanum) as numhrs from tb_horaprog a inner join tb_horario b on a.h_id = b.h_id where a.j_id='$idj' and a.s_id='$ids' and a.uap_mes='$mm' and a.uap_anno='$anno' and a.hp_dia='$k' and a.ga_id='$idga' and a.tp_id='$idtp' and a.hp_idserva='$ids' and a.hp_tipohora=0 and a.ug_id='$ugid'";
							//echo $sqlaskhrsga."<br>";
							$resultaskhrstga = mysql_query($sqlaskhrsga);
							if(mysql_num_rows($resultaskhrstga)!=0){
							while($rownh= mysql_fetch_array($resultaskhrstga)){
							$numhrs = $rownh['numhrs'];
							$subtotalhrs=$subtotalhrs."<td width='10'><b>".$numhrs."</b></td>";
							}
							}else{
							//echo $sqlaskhrsga."<br>";
							$subtotalhrs=$subtotalhrs."<td width='10'><b>0</b></td>";
							}
						}
						$sqlsumahrs="select sum(b.h_horanum) as tothrs from tb_horaprog a inner join tb_horario b on a.h_id = b.h_id where a.j_id='$idj' and a.s_id='$ids' and a.uap_mes='$mm' and a.uap_anno='$anno' and a.ga_id='$idga' and a.tp_id='$idtp' and a.hp_idserva='$ids' and a.hp_tipohora=0 and a.ug_id='$ugid'";
						$resultsumahrs = mysql_query($sqlsumahrs);
						while($rowth= mysql_fetch_array($resultsumahrs)){
									$tothrs = $rowth['tothrs'];
						}
						
    $subtotalhrsa="<tr><td align='left' colspan='4'>SUB TOTAL DE HORAS</td>".$subtotalhrs."<td width='10'>".$tothrs."</td></tr>";	
	$hordinarias=$hordinarias.$subtotalhrsa;
	$subtotalhrs="";
	}
		
	}
	
	//total de hrs ordinarias
	
	for ($p=1;$p<=$numeroDeDias;$p++){
							$sqlaskhrt = "select sum(b.h_horanum) as numhrs from tb_horaprog a inner join tb_horario b on a.h_id = b.h_id where a.j_id='$idj' and a.s_id='$ids' and a.uap_mes='$mm' and a.uap_anno='$anno' and a.hp_dia='$p' and a.tp_id='$idtp' and a.hp_idserva='$ids' and a.hp_tipohora=0 and a.ug_id='$ugid'";
							//echo $sqlaskhrsga."<br>";
							$resultaskhrt = mysql_query($sqlaskhrt);
							if(mysql_num_rows($resultaskhrt)!=0){
							while($rownh= mysql_fetch_array($resultaskhrt)){
							$numhrs = $rownh['numhrs'];
							$totalhrs=$totalhrs."<td width='10'><b>".$numhrs."</b></td>";
							}
							}else{
							//echo $sqlaskhrsga."<br>";
							$totalhrs=$totalhrs."<td width='10'><b>0</b></td>";
							}
						}
						$sqlsumath="select sum(b.h_horanum) as tothrs from tb_horaprog a inner join tb_horario b on a.h_id = b.h_id where a.j_id='$idj' and a.s_id='$ids' and a.uap_mes='$mm' and a.uap_anno='$anno' and a.tp_id='$idtp' and a.hp_idserva='$ids' and a.hp_tipohora=0 and a.ug_id='$ugid'";
						$resultsumatr = mysql_query($sqlsumath);
						while($rowth= mysql_fetch_array($resultsumatr)){
									$tothrs = $rowth['tothrs'];
						}
						
    $totalhrsord="<tr><td align='left' colspan='4'>TOTAL DR HORAS ORDINARIAS</td>".$totalhrs."<td width='10'>".$tothrs."</td></tr>";	
	$hordinarias=$hordinarias.$totalhrsord;
	$totalhrs="";
	//final de horas ordinarias
	//final de horas ordinarias del servicio

/////////////////////////////////////////////////////////horas ordinarias en otros servicios

$hordinariasapoyoencabeza = $hordinariasapoyoencabeza."<tr><td align='left' colspan='".$cabeceramayor."' class='facetfilaTD'><b>HORAS ORDINARIAS</b></td></tr>";

//buscando programacion por servicio
for($i=0;$i<count($serv)-1;$i++){
$idso = $serv[$i+1];
$sqlask="select * from tb_horaprog where ug_id='$ugid' and uap_anno='$anno' and uap_mes='$mm' and  tp_id='$idtp' and s_id='$ids' and hp_idserva='$idso' and hp_idserva not in ('$ids')  and j_id='$idj' and hp_tipohora='0'";
//echo $sqlask."<br>";
$resultask = mysql_query($sqlask);
	if(mysql_num_rows($resultask)!=0){
			$nomservicio = $nomserv[$i+1];
			$hordinariasapoyo=$hordinariasapoyo."<tr><td class='facetfilaTD' colspan='".$cabeceramayor."'>APOYO AL ".$nomservicio."</td></tr>";
		//pregunta por el grupo area
			$sqlgarea="select distinct(a.ga_id) as ida, (select ga_desc from tb_garea where ga_id=a.ga_id) as garea from tb_horaprog a where a.tp_id='$idtp' and a.uap_anno='$anno' and a.uap_mes='$mm' and a.j_id='$idj' and a.s_id='$ids' and a.hp_idserva='$idso' and a.ug_id='$ugid' and a.hp_tipohora='0' order by ida asc";
			//echo $sqlgarea."<br>";
			$resultga = mysql_query($sqlgarea);
			if(mysql_num_rows($resultga)!=0){
			while ($rowga= mysql_fetch_array($resultga)){
			$idga= $rowga['ida'];
			$garea= $rowga['garea'];
			$hordinariasapoyo=$hordinariasapoyo."<tr><td class='facetfilaTD' colspan='".$cabeceramayor."'>".$garea."</td></tr>";
			//}
					//pregunta por las areas
						$sqlarea ="select distinct(a_id) as ida from tb_horaprog where ga_id='$idga' and tp_id='$idtp' and uap_anno='$anno' and uap_mes='$mm' and j_id='$idj' and hp_idserva='$idso' and s_id='$ids' and hp_tipohora='0' and ug_id='$ugid'";
						$resulta = mysql_query($sqlarea);
						//echo $sqlarea."<br>";
						if(mysql_num_rows($resulta)!=0){
						while ($row= mysql_fetch_array($resulta)){
						$ida=$row['ida'];
						$sqlcantturno = "select a.a_id as codarea, (select a_desc from tb_area where a_id=a.a_id and ga_id=a.ga_id) as area, count(distinct(a.h_id)) as idhora from tb_horaprog a where a.j_id='$idj' and a.s_id='$ids' and a.hp_idserva='$idso' and a.uap_anno='$anno' and a.uap_mes='$mm' and a.tp_id='$idtp' and a.a_id='$ida' and a.ga_id='$idga' and a.hp_tipohora='0' and a.ug_id='$ugid' group by a.a_id";
						//echo $sqlcantturno."<br>";
						$resultcantturno = mysql_query($sqlcantturno);
						//echo $sqlcantturno."<br><br>";
						while ($rownt= mysql_fetch_array($resultcantturno)){
						$area= $rownt['area'];
						$cantreg = $rownt['idhora'];
						$hordinariasapoyo=$hordinariasapoyo."<tr><td rowspan='".$cantreg."'>".$area."</td>";
						//pregunta por el horario
						$sqlhora="select distinct(h_id) as idhora from tb_horaprog where j_id='$idj' and s_id= '$ids' and hp_idserva='$idso' and uap_anno='$anno' and uap_mes='$mm' and tp_id='$idtp' and a_id='$ida' and ga_id='$idga' and ug_id='$ugid' and hp_tipohora='0'";
						$resulthora=mysql_query($sqlhora);
						//echo $sqlhora."<br><br>";
						while ($rowh= mysql_fetch_array($resulthora)){
						$idh=$rowh['idhora'];
						//pregunta por el turno
						$sqlturno = "select h_id, concat(date_format(h_horaini,'%k:%i %p'),' - ',date_format(h_horafin,'%k:%i %p')) as hora, h_horaturn as turno, h_horanum as numhora from tb_horario where h_id='$idh'";
						$resultturno = mysql_query($sqlturno);
						//echo $sqlturno."<br><br>";
						while ($rowtt= mysql_fetch_array($resultturno)){
						$hordinariasapoyo=$hordinariasapoyo."<td align='center'>".$rowtt['hora']."</td><td align='center'>".$rowtt['turno']."</td><td align='center'>".$rowtt['numhora']."</td>";
						}
						for ($j=1;$j<=$numeroDeDias;$j++){
						$sqlconsultaturno="select (select h_horanum from tb_horario where h_id=a.h_id) as hora  from tb_horaprog a where a.j_id='$idj' and a.s_id='$ids' and a.uap_mes='$mm' and a.uap_anno='$anno' and a.hp_dia='$j' and a.h_id='$idh' and a.a_id='$ida' and a.ga_id='$idga' and a.tp_id='$idtp' and a.hp_idserva='$idso' and a.hp_tipohora=0 and a.ug_id='$ugid'";
						//echo $sqlconsultaturno."<br>";
						//echo $sqlconsultaturno."<br>";
						$resultcontur = mysql_query($sqlconsultaturno);
						$dia= date("w",mktime( 0, 0, 0, $mm, $j, $anno )); 
						if(mysql_num_rows($resultcontur)==0){
						switch ($dia) {
						    case 0: $hordinariasapoyo=$hordinariasapoyo."<td width='10' align='center' class ='FacetDataText'> </td>"; break;
						    case 1: 
						    case 2:
						    case 3:
						    case 4:
						    case 5:
						    case 6:
						        $hordinariasapoyo=$hordinariasapoyo."<td width='10' align='center' class ='FacetDataTextR'> </td>"; break;
						}

						}else{
						while($rowt= mysql_fetch_array($resultcontur)){
						$hrord = $rowt['hora'];
							switch ($dia) {
						    case 0: $hordinariasapoyo=$hordinariasapoyo."<td width='10' align='center' class ='FacetDataText'>".$hrord."</td>"; break;
						    case 1: 
						    case 2:
						    case 3:
						    case 4:
						    case 5:
						    case 6:
						        $hordinariasapoyo=$hordinariasapoyo."<td width='10'  align='center' class ='FacetDataTextR'>".$hrord."</td>"; break;
						}}
						}
								
													
						}
						
										
						
						
		//horas por turno
		$askhrsturno = "select sum(b.h_horanum) as sumhrs from tb_horaprog a inner join tb_horario b on a.h_id=b.h_id where a.j_id='$idj' and a.s_id='$ids' and a.uap_mes='$mm' and a.uap_anno='$anno' and a.h_id='$idh' and a.a_id='$ida' and a.ga_id='$idga' and a.tp_id='$idtp' and a.hp_idserva='$idso' and a.hp_tipohora=0 and a.ug_id='$ugid' group by a.h_id";
		$resultaskhrst = mysql_query($askhrsturno);
		if(mysql_num_rows($resultaskhrst)==0){
			$hordinariasapoyo=$hordinariasapoyo."<td width='10' class ='FacetDataTextR'>0</td></tr>";
		}else{
			while($rowht= mysql_fetch_array($resultaskhrst)){
			$totalhorasturno = $rowht['sumhrs'];
			$hordinariasapoyo=$hordinariasapoyo."<td width='10' class='facetfilaTD'  align='center'>".$totalhorasturno."</td></tr>";
			}
		}
	}	
	}
		}
	}
	//sub cantidades de los grupos de areas
					
						for ($k=1;$k<=$numeroDeDias;$k++){
							$sqlaskhrsga = "select sum(b.h_horanum) as numhrs from tb_horaprog a inner join tb_horario b on a.h_id = b.h_id where a.j_id='$idj' and a.s_id='$ids' and a.uap_mes='$mm' and a.uap_anno='$anno' and a.hp_dia='$k' and a.ga_id='$idga' and a.tp_id='$idtp' and a.hp_idserva='$idso' and a.hp_tipohora=0 and a.ug_id='$ugid'";
							//echo $sqlaskhrsga."<br>";
							$resultaskhrstga = mysql_query($sqlaskhrsga);
							if(mysql_num_rows($resultaskhrstga)!=0){
							while($rownh= mysql_fetch_array($resultaskhrstga)){
							$numhrs = $rownh['numhrs'];
							$subtotalhrs=$subtotalhrs."<td width='10'><b>".$numhrs."</b></td>";
							}
							}else{
							//echo $sqlaskhrsga."<br>";
							$subtotalhrs=$subtotalhrs."<td width='10'><b>0</b></td>";
							}
						}
						$sqlsumahrs="select sum(b.h_horanum) as tothrs from tb_horaprog a inner join tb_horario b on a.h_id = b.h_id where a.j_id='$idj' and a.uap_mes='$mm' and a.uap_anno='$anno' and a.ga_id='$idga' and a.tp_id='$idtp' and a.s_id='$ids' and a.hp_idserva='$idso' and a.hp_tipohora=0 and a.ug_id='$ugid'";
						$resultsumahrs = mysql_query($sqlsumahrs);
						while($rowth= mysql_fetch_array($resultsumahrs)){
									$tothrs = $rowth['tothrs'];
						}
						
    $subtotalhrsa="<tr><td align='left' colspan='4'>SUB TOTAL DE HORAS</td>".$subtotalhrs."<td width='10'>".$tothrs."</td></tr>";	
	$hordinariasapoyo=$hordinariasapoyo.$subtotalhrsa;
	$subtotalhrs="";
	}
		
	}
	
	//total de hrs ordinarias
	
	for ($p=1;$p<=$numeroDeDias;$p++){
							$sqlaskhrt = "select sum(b.h_horanum) as numhrs from tb_horaprog a inner join tb_horario b on a.h_id = b.h_id where a.j_id='$idj' and a.s_id='$ids' and a.uap_mes='$mm' and a.uap_anno='$anno' and a.hp_dia='$p' and a.tp_id='$idtp' and a.hp_idserva='$idso' and a.hp_tipohora=0 and a.ug_id='$ugid'";
							//echo $sqlaskhrsga."<br>";
							$resultaskhrt = mysql_query($sqlaskhrt);
							if(mysql_num_rows($resultaskhrt)!=0){
							while($rownh= mysql_fetch_array($resultaskhrt)){
							$numhrs = $rownh['numhrs'];
							$totalhrs=$totalhrs."<td width='10'><b>".$numhrs."</b></td>";
							}
							}else{
							//echo $sqlaskhrsga."<br>";
							$totalhrs=$totalhrs."<td width='10'><b>0</b></td>";
							}
						}
						$sqlsumath="select sum(b.h_horanum) as tothrs from tb_horaprog a inner join tb_horario b on a.h_id = b.h_id where a.j_id='$idj' and a.s_id='$ids' and a.uap_mes='$mm' and a.uap_anno='$anno' and a.tp_id='$idtp' and a.hp_idserva='$idso' and a.hp_tipohora=0 and a.ug_id='$ugid'";
						$resultsumatr = mysql_query($sqlsumath);
						while($rowth= mysql_fetch_array($resultsumatr)){
									$tothrs = $rowth['tothrs'];
						}
					
    $totalhrsord="<tr><td align='left' colspan='4'>TOTAL DR HORAS ORDINARIAS</td>".$totalhrs."<td width='10'>".$tothrs."</td></tr>";	
	$hordinariasapoyo=$hordinariasapoyo.$totalhrsord;
	$totalhrs="";
	//final de horas ordinarias
	
	}
	}
	

///////////////////////////////////////////////////////////////
		
//horas extras
		
$hextrasencabeza = $hextrasencabeza."<tr><td align='left' colspan='".$cabeceramayor."' class='facetfilaTD'><b>HORAS EXTRAS</b></td></tr>";

///HORAS EXTRAS EN EL SERVICIO

		//pregunta por el grupo area
			$sqlgarea="select distinct(a.ga_id) as ida, (select ga_desc from tb_garea where ga_id=a.ga_id) as garea from tb_horaprog a where a.tp_id='$idtp' and a.uap_anno='$anno' and a.uap_mes='$mm' and a.j_id='$idj' and a.s_id='$ids' and a.hp_idserva='$ids' and a.ug_id='$ugid' and a.hp_tipohora='1' order by ida asc";
			//echo $sqlgarea."<br>";
			$resultga = mysql_query($sqlgarea);
			if(mysql_num_rows($resultga)!=0){
			while ($rowga= mysql_fetch_array($resultga)){
			$idga= $rowga['ida'];
			$garea= $rowga['garea'];
			$hextras=$hextras."<tr><td class='facetfilaTD' colspan='".$cabeceramayor."'>".$garea."</td></tr>";
			
					//pregunta por las areas
						$sqlarea ="select distinct(a_id) as ida from tb_horaprog where ga_id='$idga' and tp_id='$idtp' and uap_anno='$anno' and uap_mes='$mm' and j_id='$idj' and hp_idserva='$ids' and s_id='$ids' and hp_tipohora='1'";
						$resulta = mysql_query($sqlarea);
						//echo $sqlarea."<br>";
						if(mysql_num_rows($resulta)!=0){
						while ($row= mysql_fetch_array($resulta)){
						$ida=$row['ida'];
						$sqlcantturno = "select a.a_id as codarea, (select a_desc from tb_area where a_id=a.a_id and ga_id=a.ga_id) as area, count(distinct(a.h_id)) as idhora from tb_horaprog a where a.j_id='$idj' and a.s_id='$ids' and a.hp_idserva='$ids' and a.uap_anno='$anno' and a.uap_mes='$mm' and a.tp_id='$idtp' and a.a_id='$ida' and a.ga_id='$idga' and a.hp_tipohora='1' group by a.a_id";
						//echo $sqlcantturno."<br>";
						$resultcantturno = mysql_query($sqlcantturno);
						//echo $sqlcantturno."<br><br>";
						while ($rownt= mysql_fetch_array($resultcantturno)){
						$area= $rownt['area'];
						$cantreg = $rownt['idhora'];
						$hextras=$hextras."<tr><td rowspan='".$cantreg."'>".$area."</td>";
						//pregunta por el horario
						$sqlhora="select distinct(h_id) as idhora from tb_horaprog where j_id='$idj' and hp_idserva='$ids' and s_id='$ids' and uap_anno='$anno' and uap_mes='$mm' and tp_id='$idtp' and a_id='$ida' and ga_id='$idga' and hp_tipohora='1'";
						$resulthora=mysql_query($sqlhora);
						//echo $sqlhora."<br><br>";
						while ($rowh= mysql_fetch_array($resulthora)){
						$idh=$rowh['idhora'];
						//pregunta por el turno
						$sqlturno = "select h_id, concat(date_format(h_horaini,'%k:%i %p'),' - ',date_format(h_horafin,'%k:%i %p')) as hora, h_horaturn as turno, h_horanum as numhora from tb_horario where h_id='$idh'";
						$resultturno = mysql_query($sqlturno);
						//echo $sqlturno."<br><br>";
						while ($rowtt= mysql_fetch_array($resultturno)){
						$hextras=$hextras."<td align='center'>".$rowtt['hora']."</td><td align='center'>".$rowtt['turno']."</td><td align='center'>".$rowtt['numhora']."</td>";
						}
						for ($i=1;$i<=$numeroDeDias;$i++){
						$sqlconsultaturno="select (select h_horanum from tb_horario where h_id=a.h_id) as hora  from tb_horaprog a where a.j_id='$idj' and a.s_id='$ids' and a.uap_mes='$mm' and a.uap_anno='$anno' and a.hp_dia='$i' and a.h_id='$idh' and a.a_id='$ida' and a.ga_id='$idga' and a.tp_id='$idtp' and a.hp_idserva='$ids' and a.hp_tipohora=1 and a.ug_id='$ugid'";
						//echo $sqlconsultaturno."<br>";
						//echo $sqlconsultaturno."<br>";
						$resultcontur = mysql_query($sqlconsultaturno);
						$dia= date("w",mktime( 0, 0, 0, $mm, $i, $anno )); 
						if(mysql_num_rows($resultcontur)==0){
						switch ($dia) {
						    case 0: $hextras=$hextras."<td width='10' align='center' class ='FacetDataText'> </td>"; break;
						    case 1: 
						    case 2:
						    case 3:
						    case 4:
						    case 5:
						    case 6:
						        $hextras=$hextras."<td width='10' align='center' class ='FacetDataTextR'> </td>"; break;
						}

						}else{
						while($rowt= mysql_fetch_array($resultcontur)){
						$hrord = $rowt['hora'];
							switch ($dia) {
						    case 0: $hextras=$hextras."<td width='10' align='center' class ='FacetDataText'>".$hrord."</td>"; break;
						    case 1: 
						    case 2:
						    case 3:
						    case 4:
						    case 5:
						    case 6:
						        $hextras=$hextras."<td width='10'  align='center' class ='FacetDataTextR'>".$hrord."</td>"; break;
						}}
						}
						}
		//horas por turno
		$askhrsturno = "select sum(b.h_horanum) as sumhrs from tb_horaprog a inner join tb_horario b on a.h_id=b.h_id where a.j_id='$idj' and a.s_id='$ids' and a.uap_mes='$mm' and a.uap_anno='$anno' and a.h_id='$idh' and a.a_id='$ida' and a.ga_id='$idga' and a.tp_id='$idtp' and a.hp_idserva='$ids' and a.hp_tipohora=1 and a.ug_id='$ugid' group by a.h_id";
		$resultaskhrst = mysql_query($askhrsturno);
		if(mysql_num_rows($resultaskhrst)==0){
			$hextras=$hextras."<td width='10' class ='FacetDataTextR'>0</td></tr>";
		}else{
			while($rowht= mysql_fetch_array($resultaskhrst)){
			$totalhorasturno = $rowht['sumhrs'];
			$hextras=$hextras."<td width='10' class='facetfilaTD'  align='center'>".$totalhorasturno."</td></tr>";
			}
		}
	}	
	}
		}
	}	
///PARA REVISAR  PARA ABAJO
	//sub cantidades de los grupos de areas
						for ($kk=1;$kk<=$numeroDeDias;$kk++){
							$sqlaskhrsga = "select sum(b.h_horanum) as numhrs from tb_horaprog a inner join tb_horario b on a.h_id = b.h_id where a.j_id='$idj' and a.s_id='$ids' and a.uap_mes='$mm' and a.uap_anno='$anno' and a.hp_dia='$kk' and a.ga_id='$idga' and a.tp_id='$idtp' and a.hp_idserva='$ids' and a.hp_tipohora=1 and a.ug_id='$ugid'";
							//echo $sqlaskhrsga."<br>";
							$resultaskhrstga = mysql_query($sqlaskhrsga);
							if(mysql_num_rows($resultaskhrstga)!=0){
							while($rownh= mysql_fetch_array($resultaskhrstga)){
							$numhrs = $rownh['numhrs'];
							$subtotalhrs=$subtotalhrs."<td width='10'><b>".$numhrs."</b></td>";
							}
							}else{
							//echo $sqlaskhrsga."<br>";
							$subtotalhrs=$subtotalhrs."<td width='10'><b>0</b></td>";
							}
						}
						$sqlsumahrs="select sum(b.h_horanum) as tothrs from tb_horaprog a inner join tb_horario b on a.h_id = b.h_id where a.j_id='$idj' and a.s_id='$ids' and a.uap_mes='$mm' and a.uap_anno='$anno' and a.ga_id='$idga' and a.tp_id='$idtp' and a.hp_idserva='$ids' and a.hp_tipohora=1 and a.ug_id='$ugid'";
						$resultsumahrs = mysql_query($sqlsumahrs);
						while($rowth= mysql_fetch_array($resultsumahrs)){
									$tothrs = $rowth['tothrs'];
						}
						
    $subtotalhrsa="<tr><td align='left' colspan='4'>SUB TOTAL DE HORAS</td>".$subtotalhrs."<td width='10'>".$tothrs."</td></tr>";	
	$hextras=$hextras.$subtotalhrsa;
	$subtotalhrs="";
	
	
	
	}}
		//total de hrs extras
	if($hextras !=''){
	for ($a=1;$a<=$numeroDeDias;$a++){
							$sqlaskhrt = "select sum(b.h_horanum) as numhrs from tb_horaprog a inner join tb_horario b on a.h_id = b.h_id where a.j_id='$idj' and a.s_id='$ids' and a.uap_mes='$mm' and a.uap_anno='$anno' and a.hp_dia='$a' and a.tp_id='$idtp' and a.hp_idserva='$ids' and a.hp_tipohora=1 and a.ug_id='$ugid'";
							//echo $sqlaskhrt."<br>";
							$resultaskhrt = mysql_query($sqlaskhrt);
							if(mysql_num_rows($resultaskhrt)!=0){
							while($rownh= mysql_fetch_array($resultaskhrt)){
							$numhrs = $rownh['numhrs'];
							$totalhrs=$totalhrs."<td width='10'><b>".$numhrs."</b></td>";
							}
							}else{
							//echo $sqlaskhrsga."<br>";
							$totalhrs=$totalhrs."<td width='10'><b>0</b></td>";
							}
						}
						$sqlsumath="select sum(b.h_horanum) as tothrs from tb_horaprog a inner join tb_horario b on a.h_id = b.h_id where a.j_id='$idj' and a.s_id='$ids' and a.uap_mes='$mm' and a.uap_anno='$anno' and a.tp_id='$idtp' and a.hp_idserva='$ids' and a.hp_tipohora=1 and a.ug_id='$ugid'";
						$resultsumatr = mysql_query($sqlsumath);
						while($rowth= mysql_fetch_array($resultsumatr)){
									$tothrs = $rowth['tothrs'];
						}
						
    $totalhrsord="<tr><td align='left' colspan='4'>TOTAL DE HORAS EXTRAS</td>".$totalhrs."<td width='10'>".$tothrs."</td></tr>";	
	$hextras=$hextras.$totalhrsord;
	$totalhrs="";
	}
		



////////////////////FIN H. EXTRAS EN EL SERVICIO
//buscando programacion por servicio
$hextrasapoyoencabeza = $hextrasapoyoencabeza."<tr><td align='left' colspan='".$cabeceramayor."' class='facetfilaTD'><b>HORAS EXTRAS</b></td></tr>";

for($i=0;$i<count($serv)-1;$i++){
$idso = $serv[$i+1];
$sqlask="select * from tb_horaprog where ug_id='$ugid' and uap_anno='$anno' and uap_mes='$mm' and s_id='$ids' and hp_idserva='$idso' and hp_idserva not in ('$ids') and tp_id='$idtp' and j_id='$idj' and hp_tipohora='1'";
//echo $sqlask."<br>";
$resultask = mysql_query($sqlask);
	if(mysql_num_rows($resultask)!=0){
	
			$nomservicio = $nomserv[$i+1];
			//echo $nomservicio."<br>";
			$hextrasapoyo=$hextrasapoyo."<tr><td class='facetfilaTD' colspan='".$cabeceramayor."'>APOYO AL ".$nomservicio."</td></tr>";
		//pregunta por el grupo area
			$sqlgarea="select distinct(a.ga_id) as ida, (select ga_desc from tb_garea where ga_id=a.ga_id) as garea from tb_horaprog a where a.tp_id='$idtp' and a.uap_anno='$anno' and a.uap_mes='$mm' and a.j_id='$idj' and a.s_id='$ids' and a.hp_idserva='$idso' and a.ug_id='$ugid' and a.hp_tipohora='1' order by ida asc";
			//echo $sqlgarea."<br>";
			$resultga = mysql_query($sqlgarea);
			if(mysql_num_rows($resultga)!=0){
			while ($rowga= mysql_fetch_array($resultga)){
			$idga= $rowga['ida'];
			$garea= $rowga['garea'];
			$hextrasapoyo=$hextrasapoyo."<tr><td class='facetfilaTD' colspan='".$cabeceramayor."'>".$garea."</td></tr>";
			
					//pregunta por las areas
						$sqlarea ="select distinct(a_id) as ida from tb_horaprog where ga_id='$idga' and tp_id='$idtp' and uap_anno='$anno' and uap_mes='$mm' and j_id='$idj' and hp_idserva='$idso' and s_id='$ids' and hp_tipohora='1'";
						$resulta = mysql_query($sqlarea);
						//echo $sqlarea."<br>";
						if(mysql_num_rows($resulta)!=0){
						while ($row= mysql_fetch_array($resulta)){
						$ida=$row['ida'];
						$sqlcantturno = "select a.a_id as codarea, (select a_desc from tb_area where a_id=a.a_id and ga_id=a.ga_id) as area, count(distinct(a.h_id)) as idhora from tb_horaprog a where a.j_id='$idj' and a.s_id='$ids' and a.hp_idserva='$idso' and a.uap_anno='$anno' and a.uap_mes='$mm' and a.tp_id='$idtp' and a.a_id='$ida' and a.ga_id='$idga' and a.hp_tipohora='1' group by a.a_id";
						//echo $sqlcantturno."<br>";
						$resultcantturno = mysql_query($sqlcantturno);
						//echo $sqlcantturno."<br><br>";
						while ($rownt= mysql_fetch_array($resultcantturno)){
						$area= $rownt['area'];
						$cantreg = $rownt['idhora'];
						$hextrasapoyo=$hextrasapoyo."<tr><td rowspan='".$cantreg."'>".$area."</td>";
						//pregunta por el horario
						$sqlhora="select distinct(h_id) as idhora from tb_horaprog where j_id='$idj' and s_id='$ids' and hp_idserva='$idso' and uap_anno='$anno' and uap_mes='$mm' and tp_id='$idtp' and a_id='$ida' and ga_id='$idga' and hp_tipohora='1'";
						$resulthora=mysql_query($sqlhora);
						//echo $sqlhora."<br><br>";
						while ($rowh= mysql_fetch_array($resulthora)){
						$idh=$rowh['idhora'];
						//pregunta por el turno
						$sqlturno = "select h_id, concat(date_format(h_horaini,'%k:%i %p'),' - ',date_format(h_horafin,'%k:%i %p')) as hora, h_horaturn as turno, h_horanum as numhora from tb_horario where h_id='$idh'";
						$resultturno = mysql_query($sqlturno);
						//echo $sqlturno."<br><br>";
						while ($rowtt= mysql_fetch_array($resultturno)){
						$hextrasapoyo=$hextrasapoyo."<td align='center'>".$rowtt['hora']."</td><td align='center'>".$rowtt['turno']."</td><td align='center'>".$rowtt['numhora']."</td>";
						}
						for ($i=1;$i<=$numeroDeDias;$i++){
						$sqlconsultaturno="select (select h_horanum from tb_horario where h_id=a.h_id) as hora  from tb_horaprog a where a.j_id='$idj' and a.s_id='$ids' and a.uap_mes='$mm' and a.uap_anno='$anno' and a.hp_dia='$i' and a.h_id='$idh' and a.a_id='$ida' and a.ga_id='$idga' and a.tp_id='$idtp' and a.hp_idserva='$idso' and a.hp_tipohora=1 and a.ug_id='$ugid'";
						//echo $sqlconsultaturno."<br>";
						//echo $sqlconsultaturno."<br>";
						$resultcontur = mysql_query($sqlconsultaturno);
						$dia= date("w",mktime( 0, 0, 0, $mm, $i, $anno )); 
						if(mysql_num_rows($resultcontur)==0){
						switch ($dia) {
						    case 0: $hextrasapoyo=$hextrasapoyo."<td width='10' align='center' class ='FacetDataText'> </td>"; break;
						    case 1: 
						    case 2:
						    case 3:
						    case 4:
						    case 5:
						    case 6:
						        $hextrasapoyo=$hextrasapoyo."<td width='10' align='center' class ='FacetDataTextR'> </td>"; break;
						}

						}else{
						while($rowt= mysql_fetch_array($resultcontur)){
						$hrord = $rowt['hora'];
							switch ($dia) {
						    case 0: $hextrasapoyo=$hextrasapoyo."<td width='10' align='center' class ='FacetDataText'>".$hrord."</td>"; break;
						    case 1: 
						    case 2:
						    case 3:
						    case 4:
						    case 5:
						    case 6:
						        $hextrasapoyo=$hextrasapoyo."<td width='10'  align='center' class ='FacetDataTextR'>".$hrord."</td>"; break;
						}}
						}
						}
		//horas por turno
		$askhrsturno = "select sum(b.h_horanum) as sumhrs from tb_horaprog a inner join tb_horario b on a.h_id=b.h_id where a.j_id='$idj' and a.s_id='$ids' and a.uap_mes='$mm' and a.uap_anno='$anno' and a.h_id='$idh' and a.a_id='$ida' and a.ga_id='$idga' and a.tp_id='$idtp' and a.hp_idserva='$idso' and a.hp_tipohora=1 and a.ug_id='$ugid' group by a.h_id";
		$resultaskhrst = mysql_query($askhrsturno);
		if(mysql_num_rows($resultaskhrst)==0){
			$hextrasapoyo=$hextrasapoyo."<td width='10' class ='FacetDataTextR'>0</td></tr>";
		}else{
			while($rowht= mysql_fetch_array($resultaskhrst)){
			$totalhorasturno = $rowht['sumhrs'];
			$hextrasapoyo=$hextrasapoyo."<td width='10' class='facetfilaTD'  align='center'>".$totalhorasturno."</td></tr>";
			}
		}
	}	
	}
		}
	}	
	
	//sub cantidades de los grupos de areas
						for ($i=1;$i<=$numeroDeDias;$i++){
							$sqlaskhrsga = "select sum(b.h_horanum) as numhrs from tb_horaprog a inner join tb_horario b on a.h_id = b.h_id where a.j_id='$idj' and a.s_id='$ids' and a.uap_mes='$mm' and a.uap_anno='$anno' and a.hp_dia='$i' and a.ga_id='$idga' and a.tp_id='$idtp' and a.hp_idserva='$idso' and a.hp_tipohora=1 and a.ug_id='$ugid'";
							//echo $sqlaskhrsga."<br>";
							$resultaskhrstga = mysql_query($sqlaskhrsga);
							if(mysql_num_rows($resultaskhrstga)!=0){
							while($rownh= mysql_fetch_array($resultaskhrstga)){
							$numhrs = $rownh['numhrs'];
							$subtotalhrs=$subtotalhrs."<td width='10'><b>".$numhrs."</b></td>";
							}
							}else{
							//echo $sqlaskhrsga."<br>";
							$subtotalhrs=$subtotalhrs."<td width='10'><b>0</b></td>";
							}
						}
						$sqlsumahrs="select sum(b.h_horanum) as tothrs from tb_horaprog a inner join tb_horario b on a.h_id = b.h_id where a.j_id='$idj' and a.s_id='$ids' and a.uap_mes='$mm' and a.uap_anno='$anno' and a.ga_id='$idga' and a.tp_id='$idtp' and a.hp_idserva='$idso' and a.hp_tipohora=1 and a.ug_id='$ugid'";
						$resultsumahrs = mysql_query($sqlsumahrs);
						while($rowth= mysql_fetch_array($resultsumahrs)){
									$tothrs = $rowth['tothrs'];
						}
						
    $subtotalhrsa="<tr><td align='left' colspan='4'>SUB TOTAL DE HORAS</td>".$subtotalhrs."<td width='10'>".$tothrs."</td></tr>";	
	$hextrasapoyo=$hextrasapoyo.$subtotalhrsa;
	$subtotalhrs="";
	
	
	
	}}
		//total de hrs extras
	
	for ($i=1;$i<=$numeroDeDias;$i++){
							$sqlaskhrt = "select sum(b.h_horanum) as numhrs from tb_horaprog a inner join tb_horario b on a.h_id = b.h_id where a.j_id='$idj' and a.s_id='$ids' and a.uap_mes='$mm' and a.uap_anno='$anno' and a.hp_dia='$i' and a.tp_id='$idtp' and a.hp_idserva='$idso' and a.hp_tipohora=1 and a.ug_id='$ugid'";
							//echo $sqlaskhrsga."<br>";
							$resultaskhrt = mysql_query($sqlaskhrt);
							if(mysql_num_rows($resultaskhrt)!=0){
							while($rownh= mysql_fetch_array($resultaskhrt)){
							$numhrs = $rownh['numhrs'];
							$totalhrs=$totalhrs."<td width='10'><b>".$numhrs."</b></td>";
							}
							}else{
							//echo $sqlaskhrsga."<br>";
							$totalhrs=$totalhrs."<td width='10'><b>0</b></td>";
							}
						}
						$sqlsumath="select sum(b.h_horanum) as tothrs from tb_horaprog a inner join tb_horario b on a.h_id = b.h_id where a.j_id='$idj' and a.s_id='$ids' and a.uap_mes='$mm' and a.uap_anno='$anno' and a.tp_id='$idtp' and a.hp_idserva='$idso' and a.hp_tipohora=1 and a.ug_id='$ugid'";
						$resultsumatr = mysql_query($sqlsumath);
						while($rowth= mysql_fetch_array($resultsumatr)){
									$tothrs = $rowth['tothrs'];
						}
						
    $totalhrsord="<tr><td align='left' colspan='4'>TOTAL DR HORAS EXTRAS</td>".$totalhrs."<td width='10'>".$tothrs."</td></tr>";	
	$hextrasapoyo=$hextrasapoyo.$totalhrsord;
	$totalhrs="";
	
	
	
	//////termina las horas extras
	}}

//$hextras=$hextras."</table>";	
		
		
$final=$final."</table>";	
			//while ($rowhs= mysql_fetch_array($resulthoras)){
		//$gd = $rowhs['gd'];
		//$gn = $rowhs['gn'];
		//$ho = $rowhs['ho'];
		//$he = $rowhs['he'];
	
		//$registro=$registro."<td>".$gd."</td><td>".$gn."</td><td>".$ho."</td><td>".$he."</td>";
	//}
	
//}

if ($hordinarias !=''){
$detalleprogramacion = $detalleprogramacion.$calendario.$hordinariasencabeza.$hordinarias;
}
if ($hextras!=''){
$detalleprogramacion = $detalleprogramacion.$calendario.$hextrasencabeza.$hextras;
//echo $detalleprogramacion;
}
if ($hordinariasapoyo !=''){
$detalleprogramacion = $detalleprogramacion.$calendario.$hordinariasapoyoencabeza.$hordinariasapoyo;
}
if ($hextrasapoyo !=''){
$detalleprogramacion = $detalleprogramacion.$calendario.$hextrasapoyoencabeza.$hextrasapoyo;
//echo $hextrasapoyo;
}

echo $detalleprogramacion.$final;
?>
</form>
</body>
</html>
