<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>
<script type="text/javascript">
function suspenderper(idtp,idh,idga,ida,he,cod,turno){ 
   //alert("hola mundo");
	var idj = document.getElementById('idj').value;
	var ids = document.getElementById('ids').value;
	//var ugid = document.getElementById('ugid').value;
	var dia = document.getElementById('dia').value;
	var mes = document.getElementById('mes').value;
	var anno = document.getElementById('anno').value;
	var ugid = document.getElementById('ugid').value;
	//alert(cod);
    window.open("suspenderpersonal.php?idj="+idj+"&ids="+ids+"&dia="+dia+"&mes="+mes+"&anno="+anno+"&idtp="+idtp+"&idga="+idga+"&ida="+ida+"&idh="+idh+"&cod="+cod+"&ugid="+ugid +"&he="+he+"&turno="+turno,"","height=300,width=600,scrollbars=NO");
} 
function nuevoper(idtp,turno,idga,ida){ 
   
   //alert(idtp+"-"+turno);
	var idj = document.getElementById('idj').value;
	var ids = document.getElementById('ids').value;
	//var ugid = document.getElementById('ugid').value;
	var dia = document.getElementById('dia').value;
	var mes = document.getElementById('mes').value;
	var anno = document.getElementById('anno').value;
	//alert(ugid);
	window.open("programanuevoper.php?idj="+idj+"&ids="+ids+"&dia="+dia+"&mes="+mes+"&anno="+anno+"&idtp="+idtp+"&idga="+idga+"&ida="+ida+"&turno="+turno,"","height=200,width=600,scrollbars=NO");
    //window.open("programanuevoper.php?idj="+idj+"ids="+ids+"ugid="+ugid+"dia="+dia+"mes="+mes+"anno="+anno+"idtp="+idtp+"turno="+turno,"","height=300,width=600,scrollbars=NO");
	
} 

function CuentaHoras(){ 
    var idj = document.getElementById('idj').value;
	var ids = document.getElementById('ids').value;
	var dia = document.getElementById('dia').value;
	var mes = document.getElementById('mes').value;
	var anno = document.getElementById('anno').value;
	window.open("cuentahoras.php?idj="+idj+"&ids="+ids+"&dia="+dia+"&mes="+mes+"&anno="+anno,"","height=300,width=600,scrollbars=YES");
}

</script>
</head>
<?php
//session_start();
include("../bd/sql_variables.php");
include("validareprogramacion.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");
$ugid=$_GET['ugid'];
$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'>";
}

$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 = mysql_query($sqljefatura);
while ($rowj= mysql_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 = mysql_query($sqlserv);
while ($rows= mysql_fetch_array($resultserv)){
$nomservicio = $rows['nom']; 
}
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' value='".$dia."'>";
echo "<input type='hidden' id='mes' value='".$mm."'>";
echo "<input type='hidden' id='anno' value='".$anno."'>";



$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');

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;
}

?>
<body>
<hr color='blue' size='3'>
<table align='center'>
<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' name='btnRegresar' id='btnRegresar' value='Regresar' onClick='regresarReprogramacion()'></td>
<td><input type='button' name='btnCuentaHrs' id='btnCuentaHrs' value='Cuenta Hrs' onClick='CuentaHoras()'></td>
<td align='center'><input type='button' value='Regresar Menu Principal' name='retornaM' id='retornaM' onClick='regresarMP()'></td></tr></table>
<table align='center'>
<tr><td colspan='10' align='center' class='LlenarTablaGarea'><?php echo fechadia($dia,$mm,$anno); ?></td></tr>
<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>
<?php

$codigohora1='';
$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' order by idga";
$resultreprog = mysql_query($gareaprog);
if(mysql_num_rows($resultreprog)==0){
$matriz =$matriz."<tr><td colspan='10' align='center' class='LlenarTablaGarea'>NO EXISTE PROGRAMACION DEL DIA </td></tr>";
}else{
while ($row= mysql_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 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 = mysql_query($sqltpersonal);
while ($rowtp= mysql_fetch_array($resulttpersonal)){
$idtp= $rowtp['cod'];
$nomtp=$rowtp['descripcion'];
$matriz=$matriz."<tr><td colspan='10' align='center' class ='llenarTabla'>".$nomtp."</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 ida";
$resultaprog = mysql_query($areaprog);

if(mysql_num_rows($resultaprog)==0){
$matriz =$matriz."<tr><td colspan='10' align='center' class ='FacetDataTextC'>PROGRAMACION LIBRE</td></tr>";
}else{
while ($rowap= mysql_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.ut_codprog as codprog, concat(left(b.ua_nom,2),'. ',b.ua_apepat,' ',left(b.ua_apemat,2),'.') 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_usuasist b on a.ug_id = b.ug_id 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 = mysql_query($personalman);
//echo $personalman;
//1 D,M - 2 M - 3 T - 4 N
if(mysql_num_rows($resultperprogman)==0){
$cabmatriz =$cabmatriz."<td align='center' colspan='3' class='LlenarHorario'></td>";
$matrizb =$matrizb."<td align='left' class ='FacetDataTextC'></td>";
$matrizb =$matrizb."<td width='150' align='center' class ='FacetDataTextC'>LIBRE</td>";
$matrizb =$matrizb."<td align='center' class ='FacetDataTextC'><button name='btnreprog' width='10' onClick='nuevoper(".$idtp.",1,".$idga.",".$ida.")'> A </button></td>";

//pregunta por personal programado en la tarde sola
$personaltar="select a.ug_id as ugid, a.ut_codprog as codprog, concat(left(b.ua_nom,2),'. ',b.ua_apepat,' ',left(b.ua_apemat,2),'.') 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_usuasist b on a.ug_id = b.ug_id 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 = mysql_query($personaltar);
if(mysql_num_rows($resultperprogtar)==0){
$cabmatriz =$cabmatriz."<td align='center' colspan='3' class='LlenarHorario'></td>";
$matrizb =$matrizb."<td align='left' class ='FacetDataTextC'></td>";
$matrizb =$matrizb."<td width='150' align='center' class ='FacetDataTextC'>LIBRE</td>";
$matrizb =$matrizb."<td align='center' class ='FacetDataTextC'><button name='btnreprog' width='10' onClick='nuevoper(".$idtp.",3,".$idga.",".$ida.")'> A </button></td>";
}else{
while ($rowpt= mysql_fetch_array($resultperprogtar)){
$codigohora=$codigohora.$rowpt['idh'];
$idh=$rowpt['idh'];
$codprog=$rowpt['codprog'];
$valor = validareprog($anno, $mm, $dia, $idj, $ids, $idtp, $codprog, $idga, $ida, $idh);
$cabmatriz =$cabmatriz."<td align='center' colspan='3' class='LlenarHorario' >".$rowpt['hora']."</td>";
$matrizb =$matrizb."<td align='left' class ='FacetDataTextC' width='10'>".$rowpt['codprog']."</td>";
//$codprog = (int)$codprog ;
////////dm
$matrizb =$matrizb."<td width='150' align='center' ";
if($valor==5){$matrizb =$matrizb." class='FacetDataTextCDM'>";}else{$matrizb =$matrizb." class='FacetDataTextC'>";}
$matrizb =$matrizb.$rowpt['apepat'];
$he=0;
if($rowpt['thora']==1){
$matrizb =$matrizb." ( EXT ) ";
$he=1;
}
$matrizb =$matrizb."</td><td class ='FacetDataTextC'><img src='../img/S.jpg' width='17' height='17' value='$codprog' onClick=suspenderper(".$idtp.",".$idh.",".$idga.",".$ida.",".$he.",this.value,3);></td>";

}}//termina la tarde sola

}else{
while ($rowp= mysql_fetch_array($resultperprogman)){
if($rowp['turno']=='D'){
$codigohora=$codigohora.$rowp['idh'];
$idh=$rowp['idh'];
$cabmatriz =$cabmatriz."<td align='center' colspan='6' class='LlenarHorario'>".$rowp['hora']."</td>";
$matrizb =$matrizb."<td align='left' width='10' class ='FacetDataTextC'>".$rowp['codprog']."</td>";
$codprog=$rowp['codprog'];
$valor = validareprog($anno, $mm, $dia, $idj, $ids, $idtp, $codprog, $idga, $ida, $idh);
//$codprog = (int)$codprog ;
////////dm
$matrizb =$matrizb."<td width='300' align='center' colspan='4' ";
if($valor==5){$matrizb =$matrizb." class='FacetDataTextCDM'>";}else{$matrizb =$matrizb." class='FacetDataTextC'>";}
$matrizb =$matrizb.$rowp['apepat'];
////
$matrizb =$matrizb."<td class ='FacetDataTextC'><img src='../img/S.jpg' width='17' height='17' value='$codprog' onClick=suspenderper(".$idtp.",".$idh.",".$idga.",".$ida.",0,this.value,1);></td>";
}
if($rowp['turno']=='M'){
$codigohora=$codigohora.$rowp['idh'];
$idh=$rowp['idh'];
$cabmatriz =$cabmatriz."<td align='center' colspan='3' class='LlenarHorario'>".$rowp['hora']."</td>";
$matrizb =$matrizb."<td align='left' width='10' class ='FacetDataTextC'>".$rowp['codprog']."</td>";
$codprog=$rowp['codprog'];
$valor = validareprog($anno, $mm, $dia, $idj, $ids, $idtp, $codprog, $idga, $ida, $idh);
//$codprog = (int)$codprog ;

////////dm
$matrizb =$matrizb."<td width='150' align='center' ";
if($valor==5){$matrizb =$matrizb." class='FacetDataTextCDM'>";}else{$matrizb =$matrizb." class='FacetDataTextC'>";}
$matrizb =$matrizb.$rowp['apepat'];
////
$he=0;
if($rowp['thora']==1){
$matrizb =$matrizb." ( EXT ) ";
$he=1;
}
$matrizb =$matrizb."</td><td class ='FacetDataTextC'><img src='../img/S.jpg' width='17' height='17' value='$codprog' onClick=suspenderper(".$idtp.",".$idh.",".$idga.",".$ida.",".$he.",this.value,1);></td>";


//pregunta por personal programado en la tarde
$personaltar="select a.ug_id as ugid, a.ut_codprog as codprog, concat(left(b.ua_nom,2),'. ',b.ua_apepat,' ',left(b.ua_apemat,2),'.') 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_usuasist b on a.ug_id = b.ug_id 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 = mysql_query($personaltar);
if(mysql_num_rows($resultperprogtar)==0){
$cabmatriz =$cabmatriz."<td align='center' colspan='3' class='LlenarHorario'></td>";
$matrizb =$matrizb."<td align='left' class ='FacetDataTextC'></td>";
$matrizb =$matrizb."<td width='150' align='center' class ='FacetDataTextC'>LIBRE</td>";
$matrizb =$matrizb."<td align='center' class ='FacetDataTextC'><button name='btnreprog' width='10' onClick='nuevoper(".$idtp.",3,".$idga.",".$ida.")'> A </button></td>";
}else{
while ($rowpt= mysql_fetch_array($resultperprogtar)){
$codigohora=$codigohora.$rowpt['idh'];
$idh=$rowpt['idh'];
$cabmatriz =$cabmatriz."<td align='center' colspan='3' class='LlenarHorario'>".$rowpt['hora']."</td>";
$matrizb =$matrizb."<td align='left' class ='FacetDataTextC'>".$rowpt['codprog']."</td>";
$codprog=$rowpt['codprog'];
$valor = validareprog($anno, $mm, $dia, $idj, $ids, $idtp, $codprog, $idga, $ida, $idh);
//$codprog = (int)$codprog ;

////////dm
$matrizb =$matrizb."<td width='150' align='center' ";
if($valor==5){$matrizb =$matrizb." class='FacetDataTextCDM'>";}else{$matrizb =$matrizb." class='FacetDataTextC'>";}
$matrizb =$matrizb.$rowpt['apepat'];
////
$he=0;
if($rowpt['thora']==1){
$matrizb =$matrizb." ( EXT ) ";
$he=1;
}
$matrizb =$matrizb."</td><td class ='FacetDataTextC'><img src='../img/S.jpg' width='17' height='17' value='$codprog' onClick=suspenderper(".$idtp.",".$idh.",".$idga.",".$ida.",".$he.",this.value,3);></td>";
}}}//fin manana
}}//fin del diruno o manana

//personal programado para la noche
$personalnoc="select a.ug_id as ugid, a.ut_codprog as codprog, concat(left(b.ua_nom,2),'. ',b.ua_apepat,' ',left(b.ua_apemat,2),'.') 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_usuasist b on a.ug_id = b.ug_id 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 = mysql_query($personalnoc);
//echo $personalnoc;
if(mysql_num_rows($resultperprognoc)==0){
$cabmatriz =$cabmatriz."<td align='center' colspan='3' class='LlenarHorario'></td></tr>";
$matrizb =$matrizb."<td align='left' class ='FacetDataTextC'></td>";
$matrizb =$matrizb."<td width='150' align='center' class ='FacetDataTextC'>LIBRE</td>";
$matrizb =$matrizb."<td align='center' class ='FacetDataTextC'><button name='btnreprog' width='10' onClick='nuevoper(".$idtp.",4,".$idga.",".$ida.")'> A </button></td></tr>";
}else{
while ($rowpn= mysql_fetch_array($resultperprognoc)){
$codigohora=$codigohora.$rowpn['idh'];
$idh=$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>";
$codprog=$rowpn['codprog'];
$valor = validareprog($anno, $mm, $dia, $idj, $ids, $idtp, $codprog, $idga, $ida, $idh);
//$codprog = (int)$codprog ;
//$matrizb =$matrizb."<td width='150' align='center' class ='FacetDataTextC'>".$rowpn['apepat'];
////////dm
$matrizb =$matrizb."<td width='150' align='center' ";
if($valor==5){$matrizb =$matrizb." class='FacetDataTextCDM'>";}else{$matrizb =$matrizb." class='FacetDataTextC'>";}
$matrizb =$matrizb.$rowpn['apepat'];
////
$he=0;
if($rowpn['thora']==1){
$matrizb =$matrizb." ( EXT ) ";
$he=1;
}
$matrizb =$matrizb."</td><td class ='FacetDataTextC'><img src='../img/S.jpg' width='17' height='17' value='$codprog' onClick=suspenderper(".$idtp.",".$idh.",".$idga.",".$ida.",".$he.",this.value,4);></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
}//fin tipo personal
}}//programacion de grupo area

$matriz =$matriz."</table>";
echo $matriz;
?>
</body>
</html>

