<!DOCTYPE html>
<html lang="es">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
  <link rel="stylesheet" type="text/css" href="../styles/Style.css">
    <script language="JavaScript" type="text/javascript" src="ajax.js"></script>
  <title>REGISTRO DE LA PRODUCCION</title>
  <script language="JavaScript">
  
    
    
  </script>
</head>
<body>



<?php

date_default_timezone_set('UTC');
//var cadena = "reportehrsext.php?idj="+idj+"&ids="+ids+"&anno="+anno+"&mes="+mes+"&idtp="+idtp+"&contrato="+contrato+"&periodo=1";
//reporteproduccionmensual.php?idj=1&ids=2&anno=2019&mes=1&idtp=1&contrato=1&periodo=1
$idj=$_GET['idj'];
$ids=$_GET['ids'];
$anno=$_GET['anno'];
$mm=$_GET['mes'];
$idtp=$_GET['idtp'];
$contrato=$_GET['contrato'];
$periodo=$_GET['periodo'];



if($periodo==1){
$finicial = '01/'.$mm.'/'.$anno;
$ffinal = '15/'.$mm.'/'.$anno;
}
if($periodo==2){
$mmes = mktime( 0, 0, 0, $mm, 1, $anno ); 
$ndias = intval(date("t",$mmes)); 
$finicial = '16/'.$mm.'/'.$anno;
$ffinal = $ndias.'/'.$mm.'/'.$anno;
}

$diap0=substr($finicial, 0, 2)+0;
$diapf=substr($ffinal, 0, 2)+0;

//seleccionando el grupo ocupacional

if($idtp==1){
$idgo = 9;
}
if($idtp==2){
$idgo = 31;
}

if($idj==3){
$idj = 1;
}

echo "<input type='hidden' id='anno' name='anno' value='".$anno."'>";
echo "<input type='hidden' id='mes' name='mes' value='".$mm."'>";
echo "<input type='hidden' id='idj' name='idj' value='".$idj."'>";
echo "<input type='hidden' id='ids' name='ids' value='".$ids."'>";


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

//armando grupo de servicios y sus dependencias


$a=0;
$cad="";
$serv[] = array();
$nomserv[] = array();
$sqldepa="select j_id as codj, 0 as cods ,j_abrev as nom, j_desc as nomc from tb_jefatura where j_id='$idj'";
$resultdepa = mysqli_query($con, $sqldepa);
while ($rowj= mysqli_fetch_array($resultdepa)){
$a=$a+1;
$serv[$a]=$rowj['cods'];
$nomserv[$a]=$rowj['nomc'];
}

$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'";
$resultserv = mysqli_query($con, $sqlserv);
while ($rows= mysqli_fetch_array($resultserv)){
$a=$a+1;
$serv[$a]=$rows['cods'];
$nomserv[$a]=$rows['nomc'];
}

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

$mes = mktime( 0, 0, 0, $mm, 1, $anno ); 

if($periodo=='1'){
$numeroDeDias = 15;	
}elseif($periodo=='2'){
$numeroDeDias = intval(date("t",$mes)); 	
}

/*
//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 = '';
}

*/

//descripcion del tipo del personal
$sqltp = "select go_desc as descp from tb_grupoocupacional where go_id='$idgo'";
//echo "Consulta: ".$sqltp;
$resulttp = mysqli_query($con, $sqltp);
while ($rowtp= mysqli_fetch_array($resulttp)){
$descpersonal = $rowtp['descp']; 
}

if($contrato==1){
	$cadcont = "(1,2)";
	$cabecerahe = 'HORAS EXTRAS';
    $cabecerahea = 'HORAS EXTRAS DE APOYO';
}
if($contrato==2){
	$cadcont = "(3)";
	$cabecerahe = 'HORAS RPCT';
    $cabecerahea = 'HORAS RPCT DE APOYO';
}

function cabeceradias($diap0,$diapf,$mm, $anno){
$headerdia = ''; 
$j = 0;
$diap0 = $diap0 + 0;
$diapf = $diapf + 0;
for ($i=$diap0;$i<=$diapf;$i++){
$dia= date("w",mktime( 0, 0, 0, $mm, $i, $anno )); 
switch ($dia) {
    case 0:
        $headerdia= $headerdia."<td class='FacetFieldCaptionTL' width='20' align= 'center'>D</td>";
		break;
    case 1:
        $headerdia= $headerdia."<td class='FacetFieldCaptionTL' width='20' align= 'center'>L</td>";
		break;
    case 2:
        $headerdia= $headerdia."<td class='FacetFieldCaptionTL' width='20' align= 'center'>M</td>";
		break;
	case 3:
        $headerdia= $headerdia."<td class='FacetFieldCaptionTL' width='20' align= 'center'>M</td>";
		break;
	case 4:
        $headerdia= $headerdia."<td class='FacetFieldCaptionTL' width='20' align= 'center'>J</td>";
		break;
	case 5:
        $headerdia= $headerdia."<td class='FacetFieldCaptionTL' width='20' align= 'center'>V</td>";
		break;
	case 6:
        $headerdia= $headerdia."<td class='FacetFieldCaptionTL' width='20' align= 'center'>S</td>";
		break;
}
$j=$j+1;
}
return $headerdia;
}

$tabla = "<table border=1 align='center'>";
$tabla = $tabla."<tr><td rowspan='2' class='FacetFieldCaptionTL' align='center'>DNI</td><td rowspan='2' class='FacetFieldCaptionTL' align='center'>COD. PLANILLA</td><td rowspan='2' class='FacetFieldCaptionTL' align='center'>APELLIDOS Y NOMBRES</td><td rowspan='2' align='center' class='FacetFieldCaptionTL' >TIPO</td>";

for ($a=$diap0;$a<= $diapf;$a++){
	$tablacnum = $tablacnum."<td class='FacetFieldCaptionTL' width='20' align= 'center'>".$a."</td>";	
}

$tabla = $tabla.$tablacnum."<td rowspan='2' class='FacetFieldCaptionTL' align='center'>TOTAL</td><td rowspan='2' class='FacetFieldCaptionTL' align='center'>DETALLE</td></tr>";
$tabla = $tabla.cabeceradias($diap0,$diapf,$mm, $anno);


//PARA LA PRODUCCION
$sqlper1 = "select distinct(a.p_dni) as dni, d.rp_codplanilla as planilla,(select concat(p_apepat,' ',p_apemat,' ',p_nom) from tb_personal where p_dni=a.p_dni) as personal from tb_regproduccion a inner join tb_regpersonal b on a.p_dni=b.p_dni inner join tb_areaservicio c on  a.a_id = c.a_id inner join tb_regpersonal d on a.p_dni=d.p_dni where year(a.regp_fecha)=".$anno." and month(a.regp_fecha)= ".$mm." and day(a.regp_fecha) between ".$diap0." and ".$diapf." and c.j_id='".$idj."' and c.s_id='".$ids."'  and d.go_id=".$idgo." and d.tc_id in ".$cadcont ." group by dni order by personal";
//Echo $sqlper1."<br>";
//horas por los servicios
$ttnumhrsextras=0;
$ttcantprod=0;
$m=1;
$resultper = mysqli_query($con, $sqlper1);
if(mysqli_num_rows($resultper)!=0){
while ($rowper= mysqli_fetch_array($resultper)){
$dni= $rowper['dni'];	
$tabla=$tabla."<tr><td rowspan='2' class='FacetFieldCaptionON' align='center'>".$rowper['dni']."</td><td rowspan='2' class='FacetFieldCaptionON' align='center'>".$rowper['planilla']."</td><td rowspan='2' class='FacetFieldCaptionON'>".$rowper['personal']."</td>";
$tabdni ="<input type='hidden' id='dni".$m."' name='dni".$m."' value='".$dni."'><td rowspan='2'><input type='button' value='VER' onClick='verDetalleProd(".$m.")'></td>";
$m = $m +1;
$subtablae="";
$subtablap="";
$numhrsextras="";
$cantprod="";
for($i=$diap0;$i<=$diapf;$i++){
	
	$sqlaskhrs="SELECT sum(h_hrs) as hrseje FROM tb_regproduccion a inner join tb_horario b on a.h_id=b.h_id inner join tb_areaservicio c on a.a_id=c.a_id WHERE a.p_dni='".$dni."' and year(a.regp_fecha)='".$anno."' and  month(a.regp_fecha)='".$mm."' and  day(a.regp_fecha)='".$i."' and c.s_id='".$ids."' and c.j_id='".$idj."' GROUP by a.regp_fecha";
	//echo $sqlaskhrs."<br>";
	$resulthrs = mysqli_query($con, $sqlaskhrs);
    if(mysqli_num_rows($resulthrs)!=0){
	 while ($rowhrs= mysqli_fetch_array($resulthrs)){
	  $subtablae = $subtablae."<td class='FacetFieldCaptionTT' align='center'>".$rowhrs['hrseje']."</td>";
	  $numhrsextras=$numhrsextras+$rowhrs['hrseje'];
	 }
	 
	}else{
		$subtablae = $subtablae."<td class='FacetFieldCaptionTT' align='center'>-</td>";
	}
	
	$sqlaskprod = "SELECT count(b.pac_dni) as nropac FROM tb_regproduccion a inner join tb_atencionpac b on a.h_id=b.h_id and a.p_dni=b.p_dni and a.regp_fecha=b.regp_fecha inner join tb_areaservicio c on a.a_id=c.a_id  WHERE a.p_dni='".$dni."' and year(b.regp_fecha)='".$anno."' and month(b.regp_fecha)='".$mm."' and day(b.regp_fecha)='".$i."' and c.s_id='".$ids."' and c.j_id='".$idj."' GROUP by b.regp_fecha";
	//echo $sqlaskprod."<br>";
	$resultprod = mysqli_query($con, $sqlaskprod);
    if(mysqli_num_rows($resultprod)!=0){
	 while ($rowprod= mysqli_fetch_array($resultprod)){
	  $subtablap = $subtablap."<td class='FacetFieldCaptionON' align='center'>".$rowprod['nropac']."</td>";
	  $cantprod=$cantprod+$rowprod['nropac'];
	 }
	 
	}else{
		$subtablap = $subtablap."<td class='FacetFieldCaptionON' align='center'>-</td>";
	}	
	
	
}

$tabla=$tabla."<td class='FacetFieldCaptionTT' align='center'>NRO HRS</td>".$subtablae."<td class='FacetFieldCaptionTT' align='center'>".$numhrsextras."</td>".$tabdni."</tr>";
$tabla=$tabla."<td class='FacetFieldCaptionON' align='center'>PROD.</td>".$subtablap."<td class='FacetFieldCaptionON' align='center'>".$cantprod."</td></tr>";	

$tthrsextras = $tthrsextras  + $numhrsextras;
$ttcantprod = $ttcantprod  + $cantprod;

}

$tablatt = "</table><br><br><table align='center'>";
$tablatt = $tablatt."<tr><td class='FacetFieldCaptionTT' align='center'>TOTAL HRS REPORTADAS</td><td class='FacetFieldCaptionTT' align='center'>".$tthrsextras."</td></tr>";
$tablatt = $tablatt."<tr><td class='FacetFieldCaptionON' align='center'>PRODUCCION</td><td>".$ttcantprod."</td></tr>";
$tablatt = $tablatt."</table>";

echo $tabla.$tablatt ;

}else{
	echo "<center><b>NO SE REGISTRAR NINGUNA PRODUCCION</b></center>";
}

?>

</body>
</html>