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

date_default_timezone_set('UTC');
//var cadena = "reporteguardias.php?idj="+idj+"&ids="+ids+"&anno="+anno+"&mes="+mes+"&idtp="+idtp+"&contrato="+contrato+"&periodo=1&tipog=D";
//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'];
$tipog =$_GET['tipog'];

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';
//$finicial = '01/02/2011';
//$ffinal = '15/02/2011';

$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)){
$cad = $cad."<td width='10' class='facetfilaTD' colspan='4' align='center' >".$rows['nom']."</td>";
$a=$a+1;
$serv[$a]=$rows['cods'];
$nomserv[$a]=$rows['nomc'];
}
while ($rowj= mysql_fetch_array($resultdepa)){
$cad = $cad."<td width='10' class='facetfilaTD' colspan='4'  align='center' >".$rowj['nom']."</td>";
$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']; 
}

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

////
//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']; 
}
if($contrato==1){
$titulodepersonal= $descpersonal." ESSALUD";
}
if($contrato==2){
$titulodepersonal= $descpersonal." CAS";
}
//
if($tipog=='D'){
$texto = "GUARDIAS DIURNAS DEL ".$diap0." AL ".$diapf." DE ".$arraymes[$mm-1]['mes']." DEL ".$anno;
}
if($tipog=='N'){
$texto = "GUARDIAS NOCTURNAS 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($titulodepersonal,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',6);
					    $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.1, $sumat*0.2);
						$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 = "NRO GUARDIAS";
$pdf->ImprimirTablaJustificada($total,2*$anc,$ini,2*$ha,15,'C',2);
$pdf->ImprimirTablaFechas($cabeceranumarray,$anc,$ini,$ha,$wa,'C');
$ini = $ini + $ha;
$pdf->ImprimirTablaFechas($cabeceradiasarray,$anc,$ini,$ha,$wa,'C');	
$ini = $ini + $ha;
						//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";
if($contrato==1){
$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' and a.tc_idcont not in ('3') ";
$sqlper1= $sqlper1." and b.ut_idserva='$ids' and b.uap_anno='$anno' and b.uap_mes ='$mm' and c.h_id in (select h_id from tb_horario where h_horaturn='$tipog') and c.he_tipohora='0' order by nombre";
}
if($contrato==2){
$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' and a.tc_idcont in ('3') ";
$sqlper1= $sqlper1." and b.ut_idserva='$ids' and b.uap_anno='$anno' and b.uap_mes ='$mm' and c.h_id in (select h_id from tb_horario where h_horaturn='$tipog') and c.he_tipohora='0' order by nombre";
}
//Echo $sqlper1."<br>";
//horas por los servicios
//$a=1;
$resultper = mysql_query($sqlper1);
$guardiasarray = array();
while ($rowp= mysql_fetch_array($resultper)){
$ugid = $rowp['cod'];
$nombre = $rowp['nombre'];
	if(strlen($nombre)>=36){
	$nombre = substr($nombre,0,36);
	$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;
for($ddd=$diap0;$ddd<=$diapf;$ddd++){
if($tipog=='D'){
$sqlhoras="select a.he_dia, a.ut_codprog, 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 ";
$sqlhoras=$sqlhoras." b.h_horaturn='$tipog' and b.h_horanum='12' and a.he_tipohora='0' 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.he_idserva='$ids' and a.tp_id='$idtp'";
}else{
$sqlhoras="select a.he_dia, a.ut_codprog, TIME_FORMAT( Time(b.h_horaini) - Time(b.h_horafin) ,'%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 ";
$sqlhoras=$sqlhoras." b.h_horaturn='$tipog' and b.h_horanum='12' and a.he_tipohora='0' 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.he_idserva='$ids' and a.tp_id='$idtp'";
}
//echo $sqlhoras."<br>";
$resulthoras = mysql_query($sqlhoras);
	if(mysql_num_rows($resulthoras)==0){
			$guardiasarray[$i] = '';
			$i=$i+1;
	}else{
	while ($rowhs= mysql_fetch_array($resulthoras)){
		$horas = $rowhs['horas'];
		$guardiasarray[$i] = $horas;
		$ng = $ng + 1;
		$i=$i+1;
		
	}
	}
	//$alin = array('C','C','L','C','C','C','C');
	
	//$a = $a+1;
}
$pdf->ImprimirTablaFechas($guardiasarray,$anc,$ini,$ha,$wa,'C');
$pdf->ImprimirTablaArea($ng,$anc*2, $ini, $ha, 15,'C',2,1);
$ini = $ini + $ha;
	//$pdf->ImprimirTablaPersonal($guardiasarray,15,$ini,$ha,$wh,'C');	
	//$ini = $ini + $ha;					
}						
						////////////////////////
						
						//$subtitulo = array('TOTAL',$sumaho,$sumahe,$sumaho+$sumahe,);
						//$wh= array($sumat*0.35,$sumat*0.06,$sumat*0.06,$sumat*0.06,$sumat*0.20);
						//$pdf->ImprimirTablaHorario($subtitulo,15,$ini,$ha,$wh,'C');	
						//$ini = $ini + $ha;
$pdf->Output();

?>
