<?php
require('fpdf/fpdf.php');

date_default_timezone_set('UTC');
//var cadena = "reportehrsext.php?idj="+idj+"&ids="+ids+"&anno="+anno+"&mes="+mes+"&idtp="+idtp+"&contrato="+contrato+"&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;
}
//$idj=1;
//$ids=2;
//$idtp=1;
//$ugid='SCPHU4';
//$tipog ='D';
//echo $ffinal;

$diap0=substr($finicial, 0, 2);
//$mm=substr($finicial, 3, 2);
//$anno=substr($finicial, 6, 4);

$diapf=substr($ffinal, 0, 2);
//$mm=substr($ffinal, 3, 2);
//$anno=substr($ffinal, 6, 4);

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="";
$serv[] = array();
$nomserv[] = array();
while ($rows= mysql_fetch_array($resultserv)){
$a=$a+1;
$serv[$a]=$rows['cods'];
$nomserv[$a]=$rows['nomc'];
}
while ($rowj= mysql_fetch_array($resultdepa)){
$a=$a+1;
$serv[$a]=$rowj['cods'];
$nomserv[$a]=$rowj['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');


//datos del personal
/*
$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'];
}*/
//$idhora=$_GET['idhora'];
$mes = mktime( 0, 0, 0, $mm, 1, $anno ); 
$numeroDeDias = intval(date("t",$mes)); 


//analizando los espacios
$diasresta = $numeroDeDias-27;
$diasrestantes = $diasresta + 1;
$diascabecera = $diasresta + 16;
$cabeceramayor = $diascabecera + 16;


//parte 1 del cuerpo
$data = array();
$data[] = array('area'=>'UCI','hi'=>'7:00','hf'=>'13:00','nh'=>'6','tipo'=>'M');




$dato1cab = array();
//$dato1cab[] = array('RED ASISTENCIAL','dra'=>'INSTITUTO NACIONAL CARDIOVASCULAR','ca'=>'CENTRO ASISTENCIAL','rca'=>'INSTITUTO NACIONAL CARDIOVASCULAR');
//$dato1cab[] = array('ra'=>'AREA','dra'=>'AREA CLINICA QUIRURGICA','ca'=>'SERVICIO','rca'=>'HOSPITALIZACION UCI Y UCIN');

$dato1cab[] = array('RED ASISTENCIAL','INSTITUTO NACIONAL CARDIOVASCULAR','CENTRO ASISTENCIAL','INSTITUTO NACIONAL CARDIOVASCULAR');
$dato1cab[] = array('AREA / DPTO',$jefatura,'SERVICIO',$servicio);
$dato2cab[] = array('ESPECIALIDAD',$especialidad,'CARGO',$cargo,'NRO COLEGIATURA',$numcolegiatura);
$dato3cab[] = array('APELLIDOS Y NOMBRES',$nombre,'NRO DNI',$dni,'NRO ESPECIALIDAD',$numespecialidad);
$dato4cab[] = array('MES QUE INICIA',$arraymes[$mm-1]['mes']);
$dato5cab[] = array('TOTAL DE HORAS');
$dato6cab[] = array('AREAS / ACTIVIDADES','TURNO','N. HRS');

//titulos
$cabeceraho = 'HORAS ORDINARIAS';
$cabecerahe = 'HORAS EXTRAS';
$cabecerahoa = 'HORAS ORDINARIAS DE APOYO';
$cabecerahea = 'HORAS EXTRAS DE APOYO';
//parte 2 del cuerpo
$data2 = array(); 
$dj=1;
for ($i=1;$i<=$numeroDeDias;$i++){
$dj = $i-1;
$dia= date("w",mktime( 0, 0, 0, $mm, $i, $anno )); 
switch ($dia) {
    case 0:
        $data2[$dj]= 6;
        break;
    case 1:
    case 2:
    case 3:
    case 4:
    case 5:
    case 6:
        $data2[$dj]= '';
        break;
}
}




$header = array('AREA','HORARIO','TURNO','NH');

function cabeceradias($diap0,$diapf,$mm, $anno){
$headerdia = array(); 
$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[$j]= 'D';
		break;
    case 1:
        $headerdia[$j]= 'L';
		break;
    case 2:
        $headerdia[$j]= 'M';
		break;
	case 3:
        $headerdia[$j]= 'M';
		break;
	case 4:
        $headerdia[$j]= 'J';
		break;
	case 5:
        $headerdia[$j]= 'V';
		break;
	case 6:
        $headerdia[$j]= 'S';
		break;
}
$j=$j+1;
}
return $headerdia;
}

//function dias 

function cabeceranumdias($diap0,$diapf,$mm,$anno){
$headernum = array(); 
$diap0 = $diap0 + 0;
$diapf = $diapf + 0;
$j = 0;
for ($i=$diap0;$i<=$diapf;$i++){
		$headernum[$j]= $i;
		$j=$j+1;
}
return $headernum;
}

/////
$header3 = array(); 
$pinta = array();
$pintap = array();
$pintast = array();
$pintat = array();
$ancho= array();
$alinea= array();
$j=1;
$suma= 0;
for ($i=1;$i<=$numeroDeDias;$i++){
$j = $i-1;
$dia= date("w",mktime( 0, 0, 0, $mm, $i, $anno )); 
switch ($dia) {
    case 0:
		//dias domingos
        $header3[$j]= $i;
		$pinta[$j]= array(254,249,152);
		$pintap[$j]= array(254,259,152);
		$pintast[$j]= array(254,259,152);
		$pintat[$j]= array(254,259,152);
		$ancho[$j]= 5.5;
		$suma = $suma + 5.5;
		$alinea[$j]= 'C';
        break;
    case 1:
    case 2:
    case 3:
    case 4:
    case 5:
    case 6:
        $header3[$j]= $i;
		$pinta[$j]= array(196,251,252);
		$pintap[$j]= array(255,255,255);
		$pintast[$j]= array(228,254,255);
		$pintat[$j]= array(255,238,254);
		$ancho[$j]= 5.5;
		$suma = $suma + 5.5;
		$alinea[$j]= 'C';
        break;
}
}
///17 longitudes del dia
$sumat = $suma + (15*5.5);


class PDF extends FPDF
{


//function FancyTable($cabeza,$data,$y)
function colordia()
{
    //Colores, ancho de línea y fuente en negrita
    $this->SetFillColor(255,0,0);
    $this->SetTextColor(255);
    $this->SetDrawColor(128,0,0);
    $this->SetLineWidth(.3);
    $this->SetFont('','B');
}	


function ImprimirTabla($data,$x, $y, $h, $w,$alig)
{
    $this->SetFillColor(224,200,255);
    $this->SetTextColor(0);
    $this->SetDrawColor(128,0,0);
    $this->SetLineWidth(.3);
    $this->SetFont('','B');
	$this->SetXY($x,$y);
    //Datos
	$dib=false;
   	for($i=0;$i<count($data);$i++)
    {
	for($j=0;$j<count($w);$j++)
		{
        $this->Cell($w[$j],$h,$data[$i][$j],1,0,$alig[$j],$dib);
		$dib = !$dib;
        }
		$y = $y + $h;
		$this->SetXY($x,$y);
		
    }

}

function ImprimirTablaTitulo($data,$x, $y, $h, $w,$alig,$dib)
{
    $this->SetFillColor(224,200,255);
    $this->SetTextColor(0);
    $this->SetDrawColor(128,0,0);
    $this->SetLineWidth(.3);
    $this->SetFont('','B');
	$this->SetXY($x,$y);
    //Datos
	  	
	
        $this->Cell($w,$h,$data,1,0,$alig,$dib);
		//$dib = !$dib;
    
		//$this->SetXY($x,$y);
		
    
}

function ImprimirTablaSinColor($data,$x, $y, $h, $w,$alig)
{
    $this->SetFillColor(224,200,255);
    $this->SetTextColor(0);
    $this->SetDrawColor(128,0,0);
    $this->SetLineWidth(.3);
    $this->SetFont('','B');
	$this->SetXY($x,$y);
    //Datos
	$dib=false;
   	for($i=0;$i<count($data);$i++)
    {
	for($j=0;$j<count($w);$j++)
		{
        $this->Cell($w[$j],$h,$data[$i][$j],1,0,$alig[$j],$dib);
		//$dib = !$dib;
        }
		$y = $y + $h;
		$this->SetXY($x,$y);
		
    }

}

function ImprimirTablaSinColorLineal($data,$x, $y, $h, $w,$alig)
{
    $this->SetFillColor(224,200,255);
    $this->SetTextColor(0);
    $this->SetDrawColor(128,0,0);
    $this->SetLineWidth(.3);
    $this->SetFont('','B');
	$this->SetXY($x,$y);
    //Datos
	$dib=false;
   	for($i=0;$i<count($data);$i++)
    {
	        $this->Cell($w[$i],$h,$data[$i],1,0,$alig[$i],$dib);
		//$dib = !$dib;
    }
		//$y = $y + $h;
		//$this->SetXY($x,$y);
		
    //}

}

function ImprimirTablaSinColorFirma($data,$x, $y, $h, $w,$alig,$dibuja)
{
    $this->SetFillColor(224,200,255);
    $this->SetTextColor(0);
    $this->SetDrawColor(128,0,0);
    $this->SetLineWidth(.3);
    $this->SetFont('','B');
	$this->SetXY($x,$y);
    //Datos
	$dib=false;
   	for($i=0;$i<count($data);$i++)
    {
	        $this->Cell($w[$i],$h,$data[$i],$dibuja[$i],0,$alig[$i],$dib);
		//$dib = !$dib;
    }
		//$y = $y + $h;
		//$this->SetXY($x,$y);
		
    //}

}


function ImprimirTablaSinColorTitulo($data,$x, $y, $h, $w,$alig,$dibuja)
{
    $this->SetFillColor(224,200,255);
    $this->SetTextColor(0);
    $this->SetDrawColor(128,0,0);
    $this->SetLineWidth(.3);
    $this->SetFont('','B');
	$this->SetXY($x,$y);
    //Datos
	$dib=false;
   	//for($i=0;$i<count($data);$i++)
    //{
	        $this->Cell($w,$h,$data,$dibuja,0,$alig,$dib);
		//$dib = !$dib;
//    }
		//$y = $y + $h;
		//$this->SetXY($x,$y);
		
    //}

}


function ImprimirTablaHorario($data,$x, $y, $h, $w,$alig)
{
    $this->SetFillColor(224,200,255);
    $this->SetTextColor(0);
    $this->SetDrawColor(128,0,0);
    $this->SetLineWidth(.3);
    $this->SetFont('','B');
	$this->SetXY($x,$y);
    //Datos
	$dib=false;
   		for($j=0;$j<count($w);$j++)
		{
        $this->Cell($w[$j],$h,$data[$j],1,0,$alig,$dib);
		//$dib = !$dib;
		$x = $x + $w[$j];
		$this->SetXY($x,$y);
        }
		
		
    

}

function ImprimirTablaFechas($data,$x, $y, $h, $w,$alig)
{
    $this->SetFillColor(224,200,255);
    $this->SetTextColor(0);
    $this->SetDrawColor(128,0,0);
    $this->SetLineWidth(.3);
    $this->SetFont('','B');
	$this->SetXY($x,$y);
    //Datos
	$dib=false;
   		for($j=0;$j<count($data);$j++)
		{
        $this->Cell($w,$h,$data[$j],1,0,$alig,$dib);
		//$dib = !$dib;
		$x = $x + $w;
		$this->SetXY($x,$y);
        }
		
		
    

}

function ImprimirTablaPersonal($data,$x, $y, $h, $w,$alig)
{
    $this->SetFillColor(224,200,255);
    $this->SetTextColor(0);
    $this->SetDrawColor(128,0,0);
    $this->SetLineWidth(.3);
    $this->SetFont('','B');
	$this->SetXY($x,$y);
    //Datos
	$dib=false;
   		for($j=0;$j<count($w);$j++)
		{
        $this->Cell($w[$j],$h,$data[$j],1,0,$alig[$j],$dib);
		//$dib = !$dib;
		$x = $x + $w[$j];
		$this->SetXY($x,$y);
        }
		
		
    

}

function ImprimirTablaJustificada($data,$x, $y, $h, $w,$alig,$d)
{
    $this->SetFillColor(224,200,255);
    $this->SetTextColor(0);
    $this->SetDrawColor(128,0,0);
    $this->SetLineWidth(.3);
    $this->SetFont('','B');
	$this->SetXY($x,$y);
    //Datos
	$dib=false;
	$h=$h/$d;
   	
		$this->MultiCell($w,$h,$data,1,$alig,$dib);
		
    

}

function ImprimirTablaArea($data,$x, $y, $h, $w,$alig,$d,$horario)
{
    $this->SetFillColor(224,200,255);
    $this->SetTextColor(0);
    $this->SetDrawColor(128,0,0);
    $this->SetLineWidth(.3);
    $this->SetFont('','B');
	$this->SetXY($x,$y);
    //Datos
	$dib=false;
	//$h=($h/$d)*$horario;
	//$h = $h*$horario;
		$this->Cell($w,$h,$data,1,0,$alig,$dib);
		//$this->MultiCell($w,$h,$data,1,$alig,$dib);
		//$dib = !$dib;
        //$y = $y + $h;
		//$this->SetXY($x,$y);

}

function ImprimirDias($data,$x, $y, $h, $w,$alig,$pint)
{
    
    $this->SetTextColor(42,20,172);
    $this->SetDrawColor(128,0,0);
    $this->SetLineWidth(.3);
    $this->SetFont('','B');
	$this->SetXY($x,$y);
    //Datos
   	for($i=0;$i<count($data);$i++)
    {
	$this->SetFillColor($pint[$i][0],$pint[$i][1],$pint[$i][2]);
	
        $this->Cell($w[$i],$h,$data[$i],1,0,$alig[$i],true);
        //$y = $y + $h;
	}

}

function ImprimirDiasHoras($data,$x, $y, $h, $w,$alig,$pint)
{
    
    $this->SetTextColor(42,20,172);
    $this->SetDrawColor(128,0,0);
    $this->SetLineWidth(.3);
    $this->SetFont('','B');
	$this->SetXY($x,$y);
    //Datos
   	for($i=0;$i<count($data);$i++)
    {
	$this->SetFillColor($pint[$i][0],$pint[$i][1],$pint[$i][2]);
	
        $this->Cell($w[$i],$h,$data[$i],1,0,$alig[$i],true);
        //$x = $x + $w[$i];
		//$this->SetXY($x,$y);
	}

}

}


$alturaX = 25;
$ha = 4;

$pdf=new PDF();
$pdf->FPDF('L');

$posicionx = $sumat*0.12 + $sumat*0.1655 + 25;
$posicionx2 = ($sumat - ($posicionx + $suma)) - 14.75;
//altura


//creando cabecera 1
$a = 0.20*$suma;
$b= 0.30*$suma;
$c= 0.20*$suma;
$d = $suma - ($a+$b+$c);
$w = array($a,$b,$c,$d);
$alig = array('L','C','L','C');
//imprimir (datos, posc x, posc y, altura, ancho, alineacion)

$s = $sumat*0.2;
$x= $sumat*0.3;
$y= $sumat*0.2;
$z= $sumat*0.3;

$w1 = array($sumat*0.12,$sumat*0.32,$sumat*0.12,$sumat*0.44);
$w2 = array($sumat*0.12,$sumat*0.32,$sumat*0.12,$sumat*0.22,$sumat*0.10,$sumat*0.12);
$w3 = array($sumat*0.12,$sumat*0.44,$sumat*0.10,$sumat*0.12,$sumat*0.10,$sumat*0.12);
$w4 = array($sumat*0.12,$sumat*0.1655);
$wancho = $sumat - ($sumat*0.12 + $sumat*0.165 + $suma);
$w5 = array($wancho);
$posicionx = $sumat*0.12 + $sumat*0.1655 + 25;
$posicionx2 = ($sumat - ($posicionx + $suma)) - 14.75;

$alig6 = array('L','C','C');
$w6 = array($sumat*0.12,$sumat*0.12, $sumat*0.0455);

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

//descripcion del tipo del personal
$sqltp = "select tp_desc as descp from tb_tipoperson where tp_id='$idtp'";
$resulttp = mysql_query($sqltp);
while ($rowtp= mysql_fetch_array($resulttp)){
$descpersonal = $rowtp['descp']; 
}

//listar servicios del departamento
//echo count($serv)."<br>";
//echo $serv[2]."<br>";
for($ii=1;$ii<count($serv);$ii++){
			$idsa = $serv[$ii];
			$sal = 0;
			$sumahe = 0;



//pregunta por el personal
//$sqlper1="select a.ug_id as cod, b.ut_codprog as codigo, a.ua_numplanilla as planilla, CONCAT(a.ua_apepat,' ',a.ua_apemat,' ',a.ua_nom) as nombre, ";
//$sqlper1= $sqlper1." b.ut_idserva as idsa from tb_usutrab b  inner join tb_usuasist a on a.ug_id= b.ug_id where b.j_id='$idj'";
//$sqlper1= $sqlper1." and b.tp_id='$idtp' and b.ut_idserva='$ids' and b.uap_anno='$anno' and b.uap_mes ='$mm' order by nombre";
$sqlper1="select distinct(a.ug_id) as cod, b.ut_codprog as codigo, a.ua_numplanilla as planilla, ";
$sqlper1= $sqlper1." CONCAT(a.ua_apepat,' ',a.ua_apemat,' ',a.ua_nom) as nombre, b.ut_idserva as idsa";
$sqlper1= $sqlper1." from tb_usutrab b inner join tb_usuasist a on a.ug_id= b.ug_id inner join tb_horaeje c on";
$sqlper1= $sqlper1." b.ut_codprog=c.ut_codprog and b.tp_id=c.tp_id and b.ug_id=c.ug_id and b.uap_anno=c.uap_anno";
$sqlper1= $sqlper1." and b.uap_mes=c.uap_mes and b.s_id=c.s_id and b.j_id=c.j_id and b.ut_idserva=c.he_idserva where b.j_id='$idj' and b.tp_id='$idtp'";
$sqlper1= $sqlper1." and c.he_idserva='$ids' and c.s_id='$idsa' and c.uap_anno='$anno' and c.uap_mes ='$mm' and (c.he_dia between '$diap0' and '$diapf') and c.he_tipohora = '1' order by nombre";

//horas por los servicios
$resultper = mysql_query($sqlper1);
if(mysql_num_rows($resultper)!=0){
/////creando listado
$pdf->AddPage('P');
$pdf->SetTextColor(0);
$sumat = 255;
$pdf->SetFont('Helvetica','B',10);
$pdf->Image('../img/logoincor.jpg', 10, 10, 45, 16,'jpg');
$pdf->Image('../img/logoessalud.jpg', 160, 12, 40, 11,'jpg');

////

//

$texto = "HORAS EXTRAS DEL ".$diap0." AL ".$diapf." DE ".$arraymes[$mm-1]['mes']." DEL ".$anno;

$ini = 25;
$pdf->ImprimirTablaSinColorTitulo($nomjefatura,25,$ini,$ha,$sumat*0.65,'C',0);
$ini = $ini + $ha;
$pdf->ImprimirTablaSinColorTitulo($nomservicio,25,$ini,$ha,$sumat*0.65,'C',0);
$ini = $ini + $ha;
$pdf->ImprimirTablaSinColorTitulo($descpersonal,25,$ini,$ha,$sumat*0.65,'C',0);
$ini = $ini + $ha*1.5;
$pdf->SetFont('Helvetica','B',9);
$pdf->ImprimirTablaSinColorTitulo($texto,25,$ini,$ha,$sumat*0.65,'C',0);
$ini = $ini + $ha;
$pdf->SetFont('Arial','B',7);
					    $subtitulo = array('COD PLANILLA','APELLIDOS Y NOMBRES');
						//$wh= array($sumat*0.04, $sumat*0.06, $sumat*0.25,$sumat*0.06,$sumat*0.06,$sumat*0.06,$sumat*0.20);
						$wh= array($sumat*0.08, $sumat*0.22);
						$pdf->ImprimirTablaHorario($subtitulo,15,$ini,$ha*2,$wh,'C');	
						//$ini = $ini + $ha;
						//////////////////////////
	
$cabeceranumarray = array();	
$cabeceradiasarray = array();
$anc = $sumat*0.1 + $sumat*0.2 + 15;
$cabeceranumarray = cabeceradias($diap0, $diapf, $mm, $anno);
$cabeceradiasarray = cabeceranumdias($diap0, $diapf, $mm, $anno);
//echo count($cabeceranumarray);
$wa = $anc/count($cabeceranumarray);
$total = "CANT. HRS EXTRAS";
$pdf->SetFont('Arial','B',5);
$pdf->ImprimirTablaJustificada($total,2*$anc,$ini,2*$ha,15,'C',2);
$pdf->SetFont('Arial','B',7);
$pdf->ImprimirTablaFechas($cabeceranumarray,$anc,$ini,$ha,$wa,'C');
$ini = $ini + $ha;
$pdf->ImprimirTablaFechas($cabeceradiasarray,$anc,$ini,$ha,$wa,'C');	
$ini = $ini + $ha;
///////

	while ($rowp= mysql_fetch_array($resultper)){
	$ugid = $rowp['cod'];
	$nombre = $rowp['nombre'];
	if(strlen($nombre)>=35){
	$nombre = substr($nombre,0,35);
	$arraydatos = array($rowp['planilla'],$nombre);
	}else{
	$arraydatos = array($rowp['planilla'],$nombre);
	}
	$alineacion = array('C','L');
	$pdf->ImprimirTablaPersonal($arraydatos,15, $ini, $ha, $wh,$alineacion);
    //$ini = $ini + $ha;
    //suma de horas del personal por el servicio
   //preguntando por el rango
	$i=0;
	$ng=0;
	$he=0;
	$guardiasarray = array();
	/////
	for($ddd=$diap0;$ddd<=$diapf;$ddd++){
	$horasa = 0;
	//pregunta por la extra de la noche previa al 16
	if($ddd==16){
			   $dddant=$ddd - 1;
			   $sqlhorasa="select a.he_dia, b.h_horaturn as turno, a.ut_codprog, ABS(TIME_FORMAT( Time(b.h_horafin) - Time(b.h_horaini) ,'%h')) as horas, a.ug_id, b.h_horaturn from tb_horaeje a inner join tb_horario b on a.h_id = b.h_id and ";
	           $sqlhorasa=$sqlhorasa." a.he_tipohora='1' and a.he_dia='$dddant' and a.uap_mes='$mm' and a.uap_anno='$anno' and a.ug_id='$ugid'";
	           $sqlhorasa=$sqlhorasa." and a.j_id='$idj' and a.s_id='$idsa' and a.he_idserva='$ids' and a.tp_id='$idtp'";
			   //echo $sqlhorasa."<br>";
			   $resulthorasa = mysql_query($sqlhorasa);
			   if(mysql_num_rows($resulthorasa)!=0){
			   while ($rowhsa= mysql_fetch_array($resulthorasa)){
			     $horasa = $rowhsa['horas'] + 0 ;
			     $turnoa = $rowhsa['turno'];
				  
				  //echo $turnoa."<br>";
				 if($turnoa =='N'){
				 $horasa = $horasa - 6;
				  }else{
				 $horasa=0;
				 
				 }
			   } }  
		} 
	if($ddd==1){
			   if($mm==1){
			   $annop=$anno - 1;
			   $mmp=12;
			   $dddant=31;
			   }else{
			   $mmp=$mm-1;
			   $mesp = mktime( 0, 0, 0, $mmp, 1, $anno ); 
			   $dddant = intval(date("t",$mesp));
			   }
			   
			   $sqlhorasa="select a.he_dia, b.h_horaturn as turno, a.ut_codprog, ABS(TIME_FORMAT( Time(b.h_horafin) - Time(b.h_horaini) ,'%h')) as horas, a.ug_id, b.h_horaturn from tb_horaeje a inner join tb_horario b on a.h_id = b.h_id and ";
	           $sqlhorasa=$sqlhorasa." a.he_tipohora='1' and a.he_dia='$dddant' and a.uap_mes='$mmp' and a.uap_anno='$anno' and a.ug_id='$ugid'";
	           $sqlhorasa=$sqlhorasa." and a.j_id='$idj' and a.s_id='$idsa' and a.he_idserva='$ids' and a.tp_id='$idtp'";
			   //echo $sqlhorasa."<br>";
			   $resulthorasa = mysql_query($sqlhorasa);
			   if(mysql_num_rows($resulthorasa)!=0){
			   while ($rowhsa= mysql_fetch_array($resulthorasa)){
			     $horasa = $rowhsa['horas'] + 0 ;
			     $turnoa = $rowhsa['turno'];
				  
				  //echo $turnoa."<br>";
				 if($turnoa =='N'){
				 $horasa = $horasa - 6;
				  }else{
				 $horasa=0;
				 
				 }
			   } }  
		} 
	//pregunta por las horas programadas opr el personal
	$sqlhoras="select a.he_dia, b.h_horaturn as turno, a.ut_codprog, ABS(TIME_FORMAT( Time(b.h_horafin) - Time(b.h_horaini) ,'%h')) as horas2, b.h_horanum as horas, a.ug_id, b.h_horaturn from tb_horaeje a inner join tb_horario b on a.h_id = b.h_id and ";
	$sqlhoras=$sqlhoras." a.he_tipohora='1' and a.he_dia='$ddd' and a.uap_mes='$mm' and a.uap_anno='$anno' and a.ug_id='$ugid'";
	$sqlhoras=$sqlhoras." and a.j_id='$idj' and a.s_id='$idsa' and a.he_idserva='$ids' and a.tp_id='$idtp'";
	//echo $sqlhoras."<br>";
	$resulthoras = mysql_query($sqlhoras);
		if(mysql_num_rows($resulthoras)==0){
				if($horasa>0){
				$guardiasarray[$i] = $horasa;
				$he = $he + $horasa;
				}else{
				$guardiasarray[$i] = '';
				}
				$i=$i+1;
				//$ddd=$ddd+1;
		}else{
		while ($rowhs= mysql_fetch_array($resulthoras)){
			$horas = $rowhs['horas'] + 0 ;
			$turno = $rowhs['turno'];
			//echo "personal"."<br>";
			//echo $ddd."<br>";
			//echo $i."<br>";
			//echo $turno."<br>";
			//echo $diapf."<br>";
			//echo $sqlhoras."<br>";
			
			if($turno=='N' && $horas>6){
			   if($ddd==$diapf){
			    $guardiasarray[$i]= $horas + $horasa - 6;
				$i=$i+1;
				//$ddd=$ddd+1;
				//echo $ddd."<br>";
			   //echo $i."<br>";
				$he = $he + $horas + $horasa - 6;
			   }else{
			   //echo $ddd."<br>";
			   //echo $i."<br>";
			    $variable = $horas + $horasa - 6;
			   	$guardiasarray[$i]= 6;
				$i=$i+1;
				$ddd=$ddd+1;
				$guardiasarray[$i]= $variable;
				$i=$i+1;
				$he = $he + $horas + $horasa;
				}
			}else{
			$guardiasarray[$i] = $horas + $horasa;
			$he = $he + $horas + $horasa;
			$i=$i+1;
			//$ddd=$ddd+1;
			}
		}
		}
		//$alin = array('C','C','L','C','C','C','C');
		
		//$a = $a+1;
	}
	$sumahe = $sumahe + $he;
	$pdf->ImprimirTablaFechas($guardiasarray,$anc,$ini,$ha,$wa,'C');
	$pdf->ImprimirTablaArea($he,$anc*2, $ini, $ha, 15,'C',2,1);
	$ini = $ini + $ha;
	//$pdf->ImprimirTablaPersonal($guardiasarray,15,$ini,$ha,$wh,'C');	
	//$ini = $ini + $ha;
	}
	$arrayfinal = array('TOTAL DE HORAS EXTRAS');
$alineacion = array('L');
//$anchito = $sumat*0.1 + $sumat*0.2 + $wa;
$anchito = array(($anc*2)-15);
$pdf->ImprimirTablaPersonal($arrayfinal,15, $ini, $ha, $anchito,$alineacion);
$pdf->ImprimirTablaArea($sumahe,$anc*2, $ini, $ha, 15,'C',2,1);

}
}
						
$pdf->Output();

?>
