<html>
<head>
<link rel="stylesheet" type="text/css" href="styles/Style.css">
<script language="JavaScript" type="text/javascript" src="ajax.js"></script>
</head><body>
<?php
//require_once("class/class.conectar.php");
require_once("class/class.diaferiado.php");
include_once("validaprogramacionmes.php");


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

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


//armar el encabezado de las fechas
$mes = mktime( 0, 0, 0, $mm, 1, $anno ); 
$numeroDeDias = intval(date("t",$mes)); 

$cabcal="<table>";
$cabcal=$cabcal."<tr><td rowspan='2' class='facetfilaTD' width='100'>AREA</td><td rowspan='2' class='facetfilaTD'>HORARIO</td><td rowspan='2' class='facetfilaTD'>TURNO</td><td rowspan='2' class='facetfilaTD'>NH</td>";
for ($i=1;$i<=$numeroDeDias;$i++){
$dia= date("w",mktime( 0, 0, 0, $mm, $i, $anno )); 
$fechacal = $anno.'-'.$mm.'-'.$i;
//dia feriado
$diaft=0;
$Objdf = new DiaFeriado();
$res_df=array();
$res_df=$Objdf->buscar_dia_feriado($conex,$fechacal);

if(count($res_df)>0){
$diaft=$res_df[0]['tipo'];
}

switch ($dia) {
    case 0:
        $cabcal=$cabcal."<td width='20' align='center' class='facetdataTDR'><b>DOM</b></td>";
        break;
    case 1:
        $cabcal=$cabcal."<td width='20' align='center'";if($diaft==1){ $cabcal=$cabcal." class='facetdataTDR'><b>LUN</b></td>";}elseif($diaft==2){$cabcal=$cabcal." class='facetdataTDNL'><b>LUN</b></td>";}else{ $cabcal=$cabcal." class='facetdataTD'>LUN</td>"; }
		break;
    case 2:
        $cabcal=$cabcal."<td width='20' align='center'";if($diaft==1){ $cabcal=$cabcal." class='facetdataTDR'><b>MAR</b></td>";}elseif($diaft==2){$cabcal=$cabcal." class='facetdataTDNL'><b>MAR</b></td>";}else{ $cabcal=$cabcal." class='facetdataTD'>MAR</td>"; }
        break;
	case 3:
        $cabcal=$cabcal."<td width='20' align='center'";if($diaft==1){ $cabcal=$cabcal." class='facetdataTDR'><b>MIE</b></td>";}elseif($diaft==2){$cabcal=$cabcal." class='facetdataTDNL'><b>MIE</b></td>";}else{ $cabcal=$cabcal." class='facetdataTD'>MIE</td>"; }
        break;
	case 4:
        $cabcal=$cabcal."<td width='20' align='center'";if($diaft==1){ $cabcal=$cabcal." class='facetdataTDR'><b>JUE</b></td>";}elseif($diaft==2){$cabcal=$cabcal." class='facetdataTDNL'><b>JUE</b></td>";}else{ $cabcal=$cabcal." class='facetdataTD'>JUE</td>"; }
        break;
	case 5:
        $cabcal=$cabcal."<td width='20' align='center'";if($diaft==1){ $cabcal=$cabcal." class='facetdataTDR'><b>VIE</b></td>";}elseif($diaft==2){$cabcal=$cabcal." class='facetdataTDNL'><b>VIE</b></td>";}else{ $cabcal=$cabcal." class='facetdataTD'>VIE</td>"; }
        break;
	case 6:
        $cabcal=$cabcal."<td width='20' align='center'";if($diaft==1){ $cabcal=$cabcal." class='facetdataTDR'><b>SAB</b></td>";}elseif($diaft==2){$cabcal=$cabcal." class='facetdataTDNL'><b>SAB</b></td>";}else{ $cabcal=$cabcal." class='facetdataTD'>SAB</td>"; }
        break;
}}
$cabcal=$cabcal."<td rowspan='2' class='facetfilaTD'><b>EDIT</b></td></tr><tr>";

//los numeros de la cabecera
for ($i=1;$i<=$numeroDeDias;$i++){
$dia= date("w",mktime( 0, 0, 0, $mm, $i, $anno )); 
$fechacal = $anno.'-'.$mm.'-'.$i;
//dia feriado
$diaft=0;
$Objdf = new DiaFeriado();
$res_df=array();
$res_df=$Objdf->buscar_dia_feriado($conex,$fechacal);

if(count($res_df)>0){
$diaft=$res_df[0]['tipo'];
}


switch ($dia) {
    case 0:
        $cabcal=$cabcal."<td align='center' class='facetfilaTD'><b>$i</b></td>";
        break;
    case 1:
    case 2:
    case 3:
    case 4:
    case 5:
    case 6:
        $cabcal=$cabcal."<td align='center' ";if($diaft==1){ $cabcal=$cabcal." class='facetfilaTD'><b>$i</b></td>";}elseif($diaft==2){$cabcal=$cabcal." class='facetfilaTD'><b>$i</b></td>";}else{ $cabcal=$cabcal." class='facetfilaTD'>$i</td>"; }
        break;
}}
$cabcal=$cabcal."</tr>";

echo $cabcal;

$Objaa = new Area();
$res_a_p=array();
$res_a_p=$Objaa->listar_area_prog_all($conex,$anno,$mm,$idj,$ids,$idtp,$idga);

if(count($res_a_p)>0){
for($iaa=0;$iaa<sizeof($res_a_p);$iaa++){
$ida=$res_a_p[$iaa]['ida'];
$area=$res_a_p[$iaa]['area'];
$Objhaa = new Horario();
$cantreg=$Objhaa->cont_horario_prog_area_all($conex,$anno,$mm,$idj,$ids,$idtp,$idga,$ida);
echo "<tr><td rowspan='".$cantreg."' class='llenarTablaGarea' width='120'>".$area."</td>";
$res_h_a = array();
$res_h_a =$Objhaa->lis_horario_prog_area_all($conex,$anno,$mm,$idj,$ids,$idtp,$idga,$ida);

//////////////////
for($iha=0;$iha<sizeof($res_h_a);$iha++){
$idh=$res_h_a[$iha]['idhora'];
$Objha = new Horario();
$res_hr = array();
$res_hr = $Objha->buscar_horario($conex,$idh);
for($ihs=0;$ihs<sizeof($res_hr);$ihs++){
$hora =$res_hr[$ihs]['hora'];
$turno = $res_hr[$ihs]['turno'];
$numhora = $res_hr[$ihs]['numhora'];
echo "<td class='llenarTablaGareadet' width='120' align='center'>$hora</td><td class='llenarTablaGareadet' align='center'>$turno</td><td align='center' class='llenarTablaGareadet'>$numhora</td>";
}

for ($i=1;$i<=$numeroDeDias;$i++){
$Objhp = new HoraProgramacion();
$res_hp=array();
$res_hp=$Objhp->listar_hrprog($conex,$anno,$mm,$idj,$ids,$idtp,$idh,$i,$idga,$ida);

$dia= date("w",mktime( 0, 0, 0, $mm, $i, $anno )); 
$fechacal = $anno.'-'.$mm.'-'.$i;
$diaft=0;

$Objdf = new DiaFeriado();
$res_df=array();
$res_df=$Objdf->buscar_dia_feriado($conex,$fechacal);
if(count($res_df)>0){
$diaft=$res_df[0]['tipo'];
}

if(count($res_hp)==0){
switch ($dia) {
    case 0: echo "<td width='20' class ='FacetDataText'> </td>"; break;
    case 1: 
    case 2:
    case 3:
    case 4:
    case 5:
    case 6:
		echo "<td align='center' width='20' ";if($diaft==1){ echo " class='FacetDataText'> </td>";}elseif($diaft==2){echo " class='FacetDataTextDNL'> </td>";}else{ echo " class='llenarTabla'> </td>"; }
        break;
}

}else{
for($ihp=0;$ihp<sizeof($res_hp);$ihp++){
$codprog = $res_hp[$ihp]['cod'];
$tipohora = $res_hp[$ihp]['tipo'];

// consulta de cruce de horario
$resalta="";
$resalta = validaprog($conex,$anno, $mm, $i, $idj, $ids, $idtp, $codprog, $idga, $ida, $idh,$tipohora);
$fecha = $anno.'-'.$mm.'-'.$i;	
//echo $fecha." - fechita";
//moestra programacion 
switch ($dia) {
   case 0: echo "<td width='20'"; if($resalta==2){ echo "bgcolor='green' ";}elseif($resalta==1){ echo "bgcolor='red' ";}elseif($resalta==3){ echo "bgcolor='#48A3F3' ";}elseif($resalta==4){ echo "bgcolor='litlegreen' ";}elseif($resalta==5){ echo "bgcolor='brown' ";}elseif($resalta==6){ echo "bgcolor='pink' ";}elseif($resalta==7){ echo "bgcolor='purple' ";}elseif($resalta==8){ echo "bgcolor='orange' ";} elseif($resalta==11){ echo "bgcolor='#CC66FF' ";} elseif($resalta==12){ echo "bgcolor='#99FF66' ";} elseif($resalta==9){ echo "bgcolor='#FF00CC' ";}elseif($resalta==10){ echo "bgcolor='#CCCC00' ";} else{ echo "class ='FacetDataText' ";} echo " align='center'><b>"; if($resalta==13){ $lectura = consulta_hrpermiso($conex,$codprog,$fecha,$idtp,$anno,$mm,$idj,$ids); echo  "<img src='' border=0 height='5' width='5' style='background-color:#FF0000;' alt='".$lectura."'>";} if($tipohora==1){ echo $codprog."e</td>";}else{ echo $codprog."</b></td>";} break;
    case 1: 
    case 2:
    case 3:
    case 4:
    case 5:
    case 6:
        echo "<td width='20'"; if($resalta==2){ echo "bgcolor='green' ";}elseif($resalta==1){ echo "bgcolor='red' ";}elseif($resalta==3){ echo "bgcolor='#48A3F3' ";}elseif($resalta==4){ echo "bgcolor='litlegreen' ";}elseif($resalta==5){ echo "bgcolor='brown' ";}elseif($resalta==6){ echo "bgcolor='pink' ";}elseif($resalta==7){ echo "bgcolor='purple' ";}elseif($resalta==8){ echo "bgcolor='orange' ";} elseif($resalta==11){ echo "bgcolor='#CC66FF' ";} elseif($resalta==12){ echo "bgcolor='#99FF66' ";} elseif($resalta==9){ echo "bgcolor='#FF00CC' ";} elseif($resalta==10){ echo "bgcolor='#CCCC00' ";} else{ if($diaft==1){ echo " class='FacetDataText' ";}elseif($diaft==2){echo " class='FacetDataTextDNL' ";}else{ echo " class ='llenarTabla' ";}} echo " align='center'><b>"; if($resalta==13){ $lectura = consulta_hrpermiso($conex,$codprog,$fecha,$idtp,$anno,$mm,$idj,$ids); echo "<img src='' border=0 height='6' width='6' style='background-color:#FF0000;' alt='".$lectura."'>";} if($tipohora==1){ echo $codprog."e</b></td>";}else{ echo $codprog."</b></td>";} break;
}
}

}}

echo "<td><input class='FacetButton' type='button' value='VER' onclick='editarturno(".$idj.",".$ids.",".$idga.",".$ida.",".$idh.",1)'"; if($save==1){ echo "disabled";} echo"/></td></tr>";
}

}}
echo "</table>";

?>
</body></html>