<?php
//session_start();
include("../bd/sql_variables.php");
$con= mysqli_connect("$sql_host", "$sql_usuario", "$sql_pass","$sql_db") or die(header ("No Disponible Servidor de Base de Datos"));


date_default_timezone_set("America/Lima"); setlocale(LC_TIME,"es_PE.UTF-8");

$idj=$_GET['idj'];
$ids=$_GET['ids'];
$fecha=$_GET['fecha'];
$ugid=$_GET['ugid'];


if(isset($_GET['tusu'])){
echo "<input type='hidden' id='tusu' name='tusu' value='1'>";
}else{
echo "<input type='hidden' id='tusu' name='tusu' value='0'>";
}

//$ugid=$_GET['ugid'];
$dia=substr($fecha, 0, 2);
$mm=substr($fecha, 3, 2);
$anno=substr($fecha, 6, 4);

//echo $ugid;
//nombre de la jefatura
$sqljefatura = "select j_desc as nom from tb_jefatura where j_id='$idj'";
$resultjefatura = mysqli_query($con,$sqljefatura);
while ($rowj= mysqli_fetch_array($resultjefatura)){
$nomjefatura = $rowj['nom']; 
}
//nombre de servicio
$sqlserv = "select s_desc as nom from tb_servicio where j_id='$idj' and s_id='$ids'";
$resultserv = mysqli_query($con,$sqlserv);
if(mysqli_num_rows($resultserv)!=0){
while ($rows= mysqli_fetch_array($resultserv)){
$nomservicio = $rows['nom']; 
}}else{
$nomservicio = "";	
}
echo "<input type='hidden' id='idj' name='idj' value='".$idj."'>";
echo "<input type='hidden' id='ids' name='ids' value='".$ids."'>";
echo "<input type='hidden' id='ugid' name='ugid' value='".$ugid."'>";
echo "<input type='hidden' id='dia' name='dia' value='".$dia."'>";
echo "<input type='hidden' id='mes' name='mes' value='".$mm."'>";
echo "<input type='hidden' id='anno' name='anno' value='".$anno."'>";

$arrayinfhrseje = array();

function fechadia($i,$mm, $anno){

$dia= date("w",mktime( 0, 0, 0, $mm, $i, $anno )); 
switch ($dia) {
    case 0:
        $diafecha= 'DOMINGO';
		break;
    case 1:
        $diafecha= 'LUNES';
        break;
    case 2:
        $diafecha= 'MARTES';
        break;
	case 3:
        $diafecha= 'MIERCOLES';
        break;
	case 4:
        $diafecha= 'JUEVES';
        break;
	case 5:
        $diafecha= 'VIERNES';
        break;
	case 6:
        $diafecha= 'SABADO';
        break;
}
$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');

$diafecha = $diafecha.", ".$i." DE ".$arraymes[$mm-1]['mes']." DEL ".$anno;

return $diafecha;
}

$sqlaskeje = "SELECT * FROM tb_usuaprueba where uap_anno='$anno' and uap_mes='$mm' and s_id='$ids' and j_id='$idj' and uap_tipoproc='3'";
$resultaskeje = mysqli_query($con,$sqlaskeje);
$validado =0;
if(mysqli_num_rows($resultaskeje)!=0){
$validado=1;
}

?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>SIPA - SISTEMA INTEGRADO DE LA PROGRAMACION ASISTENCIAL</title>
<link rel="stylesheet" type="text/css" href="../styles/Style.css">
<link rel="shortcut icon" href="logo.ico" type="image/x-icon">
<script language="JavaScript" type="text/javascript" src="ajax.js"></script>
</head>
<body>
<hr color='blue' size='3'>
<table align='center'>
<form action="menuprogramacion.php" method="GET">
<tr><td align='center'class='LlenarJefatura' height='30' width='650'><?php echo $nomjefatura;?></td></tr>
<tr><td align='center' class='LlenarServicio' height='30' width='650'><?php echo $nomservicio;?></td></tr>
</table>
<hr color='blue' size='3'>
<table  align='center'>
<tr><td align='center'><input type='button' class='llenarboton' name='btnGrabar' id='btnGrabar' value='Grabar' onClick='grabarejecucionhrs()' <?php if($validado==1){ echo " disabled ";} ?>></td>
<td><input type='button' class='llenarboton' name='btnRegresar' id='btnRegresar' value='Regresar' onClick='regresarEjeprogramacion()'></td>
<td align='center'><img src='../img/home.png' width='70' height='60' onClick='regresarMP()'></td></tr>
</table>
<div id='progheje'>
<table align='center'>
<tr><td colspan='10' align='center' class='LlenarTablaGarea'><?php echo fechadia($dia,$mm,$anno); ?></td></tr>

<?php
$codigohora1='';
$matriz='';
//pregunta por el tipo de personal
$sqltpersonal = "select a.tp_id as cod, (select tp_desc from tb_tipoperson where tp_id=a.tp_id) as descripcion from tb_areatipopersona a where a.j_id='$idj' and a.s_id='$ids' ";
$resulttpersonal = mysqli_query($con,$sqltpersonal);
while ($rowtp= mysqli_fetch_array($resulttpersonal)){
$idtp= $rowtp['cod'];
$nomtp=$rowtp['descripcion'];
$matriz=$matriz."<tr><td colspan='10' align='center' class ='llenarTabla' >".$nomtp."</td></tr>";
$matriz=$matriz."<tr><td class ='llenarTabla' align='center'>AREA</td><td colspan='3' align='center' class ='llenarTabla' >MAŅANA</td><td colspan='3' align='center' class ='llenarTabla' >TARDE</td><td colspan='3' align='center' class ='llenarTabla' >NOCHE</td></tr>";
//busca el las areas a programar
$gareaprog="select distinct a.ga_id as idga, b.ga_desc as nomga from tb_horareprog".$anno." a inner join tb_garea b on a.ga_id = b.ga_id where a.hr_dia='$dia' and a.uap_anno='$anno' and a.uap_mes='$mm' and a.hr_idserva='$ids' and a.j_id='$idj' and a.tp_id='$idtp' order by idga";
//echo $gareaprog."<br>";
$resultreprog = mysqli_query($con,$gareaprog);
if(mysqli_num_rows($resultreprog)==0){
$matriz =$matriz."<tr><td colspan='10' align='center' class ='FacetDataTextC'>NO EXISTE PROGRAMACION DEL DIA </td></tr>";
}else{
while ($row= mysqli_fetch_array($resultreprog)){
$idga=$row['idga'];
$nomga=$row['nomga'];
$matriz=$matriz."<tr><td colspan='10' align='center' class='LlenarTablaGarea'>".$nomga."</td></tr>";
//pregunta por el area prog
$areaprog="select distinct a.a_id as ida, b.a_desc as noma from tb_horareprog".$anno." a inner join tb_area b on a.a_id = b.a_id and a.ga_id=b.ga_id where a.hr_dia='$dia' and a.uap_anno='$anno' and a.uap_mes='$mm' and a.hr_idserva='$ids' and a.j_id='$idj' and a.tp_id='$idtp' and a.ga_id='$idga' order by b.a_id";
$resultaprog = mysqli_query($con,$areaprog);
if(mysqli_num_rows($resultaprog)==0){
$matriz =$matriz."<tr><td colspan='10' align='center' class ='FacetDataTextC'>PROGRAMACION LIBRE</td></tr>";
}else{
while ($rowap= mysqli_fetch_array($resultaprog)){
$codigohora='';
$cabmatriz='';
$matrizb='';
$ida=$rowap['ida'];
$noma=$rowap['noma'];
$matrizb=$matrizb."<tr><td width='110' class='facetfilaTD'>".$noma."</td>";
//pregunta por personal programado en la manana o diurno
$personalman="select a.ug_id as ugid, a.s_id as ids, a.ut_codprog as codprog, (select concat(left(ua_nom,2),'. ',ua_apepat,' ',left(ua_apemat,2),'.') from tb_usuasist where ug_id=a.ug_id) as apepat, c.h_id as idh, concat(date_format(c.h_horaini,'%k:%i %p'), ' - ',date_format(c.h_horafin,'%k:%i %p')) as hora,c.h_horaturn as turno, a.hr_tipohora as thora from tb_horareprog".$anno." a inner join tb_horario c on a.h_id = c.h_id where a.hr_dia='$dia' and a.uap_anno='$anno' and a.uap_mes='$mm' and a.hr_idserva='$ids' and a.j_id='$idj' and a.tp_id='$idtp' and a.ga_id='$idga' and a.a_id='$ida' and c.h_horaturn in ('D','M') and c.h_horatipo=1";
$resultperprogman = mysqli_query($con,$personalman);
//echo $personalman."<br>";

if(mysqli_num_rows($resultperprogman)==0){
$cabmatriz =$cabmatriz."<td align='center' colspan='3' class='LlenarHorario'></td>";
$matrizb =$matrizb."<td width='150' align='center' colspan='3' class ='FacetDataTextC'>LIBRE</td>";

//pregunta por personal programado en la tarde sola
$personaltar="select a.ug_id as ugid, a.s_id as ids, a.ut_codprog as codprog, (select concat(left(ua_nom,2),'. ',ua_apepat,' ',left(ua_apemat,2),'.') from tb_usuasist where ug_id=a.ug_id) as apepat, c.h_id as idh, concat(date_format(c.h_horaini,'%k:%i %p'), ' - ',date_format(c.h_horafin,'%k:%i %p')) as hora, c.h_horaturn as turno, a.hr_tipohora as thora from tb_horareprog".$anno." a inner join tb_horario c on a.h_id = c.h_id where a.hr_dia='$dia' and a.uap_anno='$anno' and a.uap_mes='$mm' and a.hr_idserva='$ids' and a.j_id='$idj' and a.tp_id='$idtp' and a.ga_id='$idga' and a.a_id='$ida' and c.h_horaturn in ('T') and c.h_horatipo=1";
//echo $personaltar;
$resultperprogtar = mysqli_query($con,$personaltar);
if(mysqli_num_rows($resultperprogtar)==0){
$cabmatriz =$cabmatriz."<td align='center' colspan='3' class='LlenarHorario'></td>";
$matrizb =$matrizb."<td width='150' align='center' colspan='3' class ='FacetDataTextC'>LIBRE</td>";
//$matrizb =$matrizb."<td align='center' class ='FacetDataTextC'>LIBRE</td>";
}else{
while ($rowpt= mysqli_fetch_array($resultperprogtar)){
$codigohora=$codigohora.$rowpt['idh'];
$cabmatriz =$cabmatriz."<td align='center' colspan='3' class='LlenarHorario'>".$rowpt['hora']."</td>";
$matrizb =$matrizb."<td align='left' class ='FacetDataTextC'>".$rowpt['codprog']."</td>";
$matrizb =$matrizb."<td width='150' align='center' class ='FacetDataTextC'>".$rowpt['apepat'];
if($rowpt['thora']==1){
$matrizb =$matrizb." ( Ext ) ";
$arrayinfhrseje[] = array('codprog'=>$rowpt['codprog'],'idh'=>$rowpt['idh'],'idga'=>$idga,'ida'=>$ida,'idtp'=>$idtp,'tipohora'=>1);
}else{
$arrayinfhrseje[] = array('codprog'=>$rowpt['codprog'],'idh'=>$rowpt['idh'],'idga'=>$idga,'ida'=>$ida,'idtp'=>$idtp,'tipohora'=>0);
}
$matrizb =$matrizb."</td>";
$matrizb =$matrizb."<td align='center' class ='FacetDataTextC'><input type='checkbox' name='hreje[]' ";
//pregunta
$codprog = $rowpt['codprog'];
$idh = $rowpt['idh'];
$codper = $rowpt['ugid'];
$idso = $rowpt['ids'];
$sqlask ="select he_dia from tb_horaeje".$anno." where he_dia='$dia' and ut_codprog='$codprog' and a_id='$ida' and ga_id='$idga' and h_id='$idh' and tp_id='$idtp' and ug_id='$codper' and uap_anno='$anno' and uap_mes='$mm' and j_id='$idj' and s_id ='$idso' and he_idserva='$ids'";
$resultaskeje = mysqli_query($con,$sqlask);
if(mysqli_num_rows($resultaskeje)!=0){
//terminapregunta
$matrizb =$matrizb." checked ";
}
$matrizb =$matrizb."></td>";
}}//termina la tarde sola

}else{
while ($rowp= mysqli_fetch_array($resultperprogman)){
if($rowp['turno']=='D'){
$codigohora=$codigohora.$rowp['idh'];
$cabmatriz =$cabmatriz."<td align='center' colspan='6' class='LlenarHorario'>".$rowp['hora']."</td>";
$matrizb =$matrizb."<td align='left' class ='FacetDataTextC'>".$rowp['codprog']."</td>";
$matrizb =$matrizb."<td width='300' align='center' colspan='4' class ='FacetDataTextC'>".$rowp['apepat'];
if($rowp['thora']==1){
$matrizb =$matrizb." ( Ext ) ";
$arrayinfhrseje[] = array('codprog'=>$rowp['codprog'],'idh'=>$rowp['idh'],'idga'=>$idga,'ida'=>$ida,'idtp'=>$idtp,'tipohora'=>1);
}else{
$arrayinfhrseje[] = array('codprog'=>$rowp['codprog'],'idh'=>$rowp['idh'],'idga'=>$idga,'ida'=>$ida,'idtp'=>$idtp,'tipohora'=>0);
}
$matrizb =$matrizb."</td>";
$matrizb =$matrizb."<td align='center' class ='FacetDataTextC'><input type='checkbox' name='hreje[]'";
//pregunta
$codprog = $rowp['codprog'];
$idh = $rowp['idh'];
$codper = $rowp['ugid'];
$idso = $rowp['ids'];
$sqlask ="select he_dia from tb_horaeje".$anno." where he_dia='$dia' and ut_codprog='$codprog' and a_id='$ida' and ga_id='$idga' and h_id='$idh' and tp_id='$idtp' and ug_id='$codper' and uap_anno='$anno' and uap_mes='$mm' and j_id='$idj' and s_id ='$idso' and he_idserva='$ids'";
//echo $sqlask;
$resultaskeje = mysqli_query($con,$sqlask);
if(mysqli_num_rows($resultaskeje)!=0){
//terminapregunta
$matrizb =$matrizb." checked ";
}
$matrizb =$matrizb."></td>";
}
if($rowp['turno']=='M'){
$codigohora=$codigohora.$rowp['idh'];
$cabmatriz =$cabmatriz."<td align='center' colspan='3' class='LlenarHorario'>".$rowp['hora']."</td>";
$matrizb =$matrizb."<td align='left' class ='FacetDataTextC'>".$rowp['codprog']."</td>";
$matrizb =$matrizb."<td width='150' align='center' class ='FacetDataTextC'>".$rowp['apepat'];
if($rowp['thora']==1){
$matrizb =$matrizb." ( Ext ) ";
$arrayinfhrseje[] = array('codprog'=>$rowp['codprog'],'idh'=>$rowp['idh'],'idga'=>$idga,'ida'=>$ida,'idtp'=>$idtp,'tipohora'=>1);
}else{
$arrayinfhrseje[] = array('codprog'=>$rowp['codprog'],'idh'=>$rowp['idh'],'idga'=>$idga,'ida'=>$ida,'idtp'=>$idtp,'tipohora'=>0);
}
$matrizb =$matrizb."</td>";
$matrizb =$matrizb."<td align='center' class ='FacetDataTextC'><input type='checkbox' name='hreje[]'";
//pregunta
$codprog = $rowp['codprog'];
$idh = $rowp['idh'];
$codper = $rowp['ugid'];
$idso = $rowp['ids'];
$sqlask ="select he_dia from tb_horaeje".$anno." where he_dia='$dia' and ut_codprog='$codprog' and a_id='$ida' and ga_id='$idga' and h_id='$idh' and tp_id='$idtp' and ug_id='$codper' and uap_anno='$anno' and uap_mes='$mm' and j_id='$idj' and s_id ='$idso' and he_idserva='$ids'";
//echo $sqlask;
$resultaskeje = mysqli_query($con,$sqlask);
if(mysqli_num_rows($resultaskeje)!=0){
//terminapregunta
$matrizb =$matrizb." checked ";
}
$matrizb =$matrizb."></td>";

//pregunta por personal programado en la tarde
$personaltar="select a.ug_id as ugid, a.s_id as ids, a.ut_codprog as codprog, (select concat(left(ua_nom,2),'. ',ua_apepat,' ',left(ua_apemat,2),'.') from tb_usuasist where ug_id=a.ug_id) as apepat, c.h_id as idh, concat(date_format(c.h_horaini,'%k:%i %p'), ' - ',date_format(c.h_horafin,'%k:%i %p')) as hora,c.h_horaturn as turno, a.hr_tipohora as thora from tb_horareprog".$anno." a inner join tb_horario c on a.h_id = c.h_id where a.hr_dia='$dia' and a.uap_anno='$anno' and a.uap_mes='$mm' and a.hr_idserva='$ids' and a.j_id='$idj' and a.tp_id='$idtp' and a.ga_id='$idga' and a.a_id='$ida' and c.h_horaturn in ('T') and c.h_horatipo=1";
//echo $personaltar;
$resultperprogtar = mysqli_query($con,$personaltar);
if(mysqli_num_rows($resultperprogtar)==0){
$cabmatriz =$cabmatriz."<td align='center' colspan='3' class='LlenarHorario' ></td>";
$matrizb =$matrizb."<td width='120' align='center' colspan='3' class ='FacetDataTextC'>LIBRE</td>";
}else{
while ($rowpt= mysqli_fetch_array($resultperprogtar)){
$codigohora=$codigohora.$rowpt['idh'];
$cabmatriz =$cabmatriz."<td align='center' colspan='3' class='LlenarHorario'>".$rowpt['hora']."</td>";
$matrizb =$matrizb."<td align='left' class ='FacetDataTextC'>".$rowpt['codprog']."</td>";
$matrizb =$matrizb."<td width='150' align='center' class ='FacetDataTextC'>".$rowpt['apepat'];
if($rowpt['thora']==1){
$matrizb =$matrizb." ( Ext ) ";
$arrayinfhrseje[] = array('codprog'=>$rowpt['codprog'],'idh'=>$rowpt['idh'],'idga'=>$idga,'ida'=>$ida,'idtp'=>$idtp,'tipohora'=>1);
}else{
$arrayinfhrseje[] = array('codprog'=>$rowpt['codprog'],'idh'=>$rowpt['idh'],'idga'=>$idga,'ida'=>$ida,'idtp'=>$idtp,'tipohora'=>0);
}
$matrizb =$matrizb."</td>";
$matrizb =$matrizb."<td align='center' class ='FacetDataTextC'><input type='checkbox' name='hreje[]' ";
//pregunta
$codprog = $rowpt['codprog'];
$idh = $rowpt['idh'];
$codper = $rowpt['ugid'];
$idso = $rowpt['ids'];
$sqlask ="select he_dia from tb_horaeje".$anno." where he_dia='$dia' and ut_codprog='$codprog' and a_id='$ida' and ga_id='$idga' and h_id='$idh' and tp_id='$idtp' and ug_id='$codper' and uap_anno='$anno' and uap_mes='$mm' and j_id='$idj' and s_id ='$idso' and he_idserva='$ids'";
$resultaskeje = mysqli_query($con,$sqlask);
if(mysqli_num_rows($resultaskeje)!=0){
//terminapregunta
$matrizb =$matrizb." checked ";
}
$matrizb =$matrizb."></td>";
}}}//fin manana
}}//fin del diruno o manana

//personal programado para la noche
$personalnoc="select a.ug_id as ugid, a.s_id as ids, a.ut_codprog as codprog, (select concat(left(ua_nom,2),'. ',ua_apepat,' ',left(ua_apemat,2),'.') from tb_usuasist where ug_id=a.ug_id) as apepat, c.h_id as idh, concat(date_format(c.h_horaini,'%k:%i %p'), ' - ',date_format(c.h_horafin,'%k:%i %p')) as hora,c.h_horaturn as turno, a.hr_tipohora as thora from tb_horareprog".$anno." a inner join tb_horario c on a.h_id = c.h_id where a.hr_dia='$dia' and a.uap_anno='$anno' and a.uap_mes='$mm' and a.hr_idserva='$ids' and a.j_id='$idj' and a.tp_id='$idtp' and a.ga_id='$idga' and a.a_id='$ida' and c.h_horaturn in ('N') and c.h_horatipo=1";
$resultperprognoc = mysqli_query($con,$personalnoc);

//echo $personalnoc."<br>";
if(mysqli_num_rows($resultperprognoc)==0){
$cabmatriz =$cabmatriz."<td align='center' colspan='3' class='LlenarHorario'></td></tr>";
$matrizb =$matrizb."<td width='150' align='center' colspan='3' class ='FacetDataTextC'>LIBRE</td></tr>";
}else{
while ($rowpn= mysqli_fetch_array($resultperprognoc)){
$codigohora=$codigohora.$rowpn['idh'];
$cabmatriz =$cabmatriz."<td align='center' colspan='3' class='LlenarHorario'>".$rowpn['hora']."</td></tr>";
$matrizb =$matrizb."<td align='left' class ='FacetDataTextC'>".$rowpn['codprog']."</td>";
$matrizb =$matrizb."<td width='150' align='center' class ='FacetDataTextC'>".$rowpn['apepat'];
if($rowpn['thora']==1){
$matrizb =$matrizb." ( Ext ) ";
$arrayinfhrseje[] = array('codprog'=>$rowpn['codprog'],'idh'=>$rowpn['idh'],'idga'=>$idga,'ida'=>$ida,'idtp'=>$idtp,'tipohora'=>1);
}else{
$arrayinfhrseje[] = array('codprog'=>$rowpn['codprog'],'idh'=>$rowpn['idh'],'idga'=>$idga,'ida'=>$ida,'idtp'=>$idtp,'tipohora'=>0);
}
$matrizb =$matrizb."</td>";
$matrizb =$matrizb."<td align='center' class ='FacetDataTextC'><input type='checkbox' name='hreje[]'";
//pregunta
$codprog = $rowpn['codprog'];
$idh = $rowpn['idh'];
$codper = $rowpn['ugid'];
$idso = $rowpn['ids'];
$sqlask ="select he_dia from tb_horaeje".$anno." where he_dia='$dia' and ut_codprog='$codprog' and a_id='$ida' and ga_id='$idga' and h_id='$idh' and tp_id='$idtp' and ug_id='$codper' and uap_anno='$anno' and uap_mes='$mm' and j_id='$idj' and s_id ='$idso' and he_idserva='$ids'";
$resultaskeje = mysqli_query($con,$sqlask);
if(mysqli_num_rows($resultaskeje)!=0){
//terminapregunta
$matrizb =$matrizb." checked ";
}
$matrizb =$matrizb."></td></tr>";
}
}//fin noche
//pone y evalua cabecera horario
if($codigohora1!=$codigohora){
$matriz =$matriz."<td class='LlenarHorario'></td>".$cabmatriz.$matrizb;
$codigohora1=$codigohora;
}else{
$matriz =$matriz.$matrizb;
}
}}//fin del area programada

}}//programacion de grupo area
}//fin tipo personal
//echo "<input type='hidden' id='datos[]' name='datos[]' value='".$arrayinfhrseje."'>";
$matriz =$matriz."</table>";
echo $matriz."<br>";
//session_start(); 
//for ($ii=0;$ii<count($arrayinfhrseje);$ii++){
//echo $arrayinfhrseje[$ii]['tipohora']."<br>";
//}
$datosper = serialize($arrayinfhrseje); 
echo "<input type='hidden' id='datosper' name='datosper' value='".$datosper."'>";
//$_SESSION['datosper']=$datosper;
?>
</div>
</body>
</html>

