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

date_default_timezone_set('UTC');
$idj=$_GET['idj'];
$ids=$_GET['ids'];
$idtp=$_GET['idtp'];
$anno=$_GET['anno'];
$mm=$_GET['mes'];
//$ugid='SCPHU4';

include("../bd/sql_variables.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
$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 = mysqli_query($con,$sqldepa);
$resultserv = mysqli_query($con,$sqlserv);
$a=0;
$cad="";
$serv[] = array();
$nomserv[] = array();
while ($rows= mysqli_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= mysqli_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'];
}

/*
//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=b.c_id) as cargo, (select esp_desc from tb_especialidad where ua_idespec=b.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 = mysqli_query($con,$sqlper);
while ($row= mysqli_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)); 


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

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

$header2 = array(); 
$j = 1;
for ($i=1;$i<=$numeroDeDias;$i++){
$j = $i-1;
$dia= date("w",mktime( 0, 0, 0, $mm, $i, $anno )); 
switch ($dia) {
    case 0:
        $header2[$j]= 'DOM';
		break;
    case 1:
        $header2[$j]= 'LUN';
        break;
    case 2:
        $header2[$j]= 'MAR';
        break;
	case 3:
        $header2[$j]= 'MIE';
        break;
	case 4:
        $header2[$j]= 'JUE';
        break;
	case 5:
        $header2[$j]= 'VIE';
        break;
	case 6:
        $header2[$j]= 'SAB';
        break;
}
}

$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 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;
   	for($i=0;$i<count($data);$i++)
    {
	for($j=0;$j<count($w);$j++)
		{
		$this->MultiCell($w[$j],$h,$data[$i][$j],1,$alig[$j],$dib);
		$dib = !$dib;
        }
		$y = $y + $h;
		$this->SetXY($x,$y);
		
    }

}

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

}

}

$texto="FORMATO UNICO DE LA PROGRAMACION DEL PERSONAL - ESSALUD";

//print_r($header2);

$alturaX = 25;
$ha = 4;

$pdf=new PDF();
//$pdf->FPDF('L');
$pdf->__construct('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);


$sqldiv = "select b.ga_id as id, (select ga_desc from tb_garea where ga_id=a.ga_id) as area from tb_areaservicio a inner join tb_area b on a.a_id=b.a_id and a.ga_id=b.ga_id where a.s_id='$ids' and a.j_id='$idj' and a.tp_id='$idtp' group by b.ga_id order by b.ga_id";
$resultdiv = mysqli_query($con,$sqldiv);

if ($resultdiv == false){
echo 'PROVOCO UN ERROR EN LA CONSULTA 1111';
}else{

while ($rowd= mysqli_fetch_array($resultdiv)){
$idga=$rowd['id'];

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

//$sqla = "select a.ga_id as idga, a.a_id as ida, b.a_desc as adesc, b.a_tipo as atipo  from tb_areaservicio a inner join tb_area b on a.ga_id=b.ga_id and a.a_id=b.a_id where a.s_id='$ids' and a.j_id='$idj' and a.ga_id='$idga'";
$sqlar = "select b.ga_id as id, (select ga_desc from tb_garea where ga_id=a.ga_id) as garea from tb_areaservicio a inner join tb_area b on a.a_id=b.a_id and a.ga_id=b.ga_id where a.s_id='$ids' and a.j_id='$idj' and a.ga_id='$idga' and a.tp_id='$idtp' group by b.ga_id order by b.ga_id";
//echo $sqlar."<br>";
// busca el nombre del area
$resultar = mysqli_query($con,$sqlar);
//echo $sqlar."<br>";
if ($resultar == false){
echo 'PROVOCO UN ERROR EN LA CONSULTA 2';
}else{
while ($rowar= mysqli_fetch_array($resultar)){
$garea = $rowar['garea'];
}}
///////////////////////////////////////
//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);
while ($rows= mysqli_fetch_array($resultserv)){
$nomservicio = $rows['nom']; 
}

///NUEVA HOJA
$pdf->AddPage();
$alturaX=25;
$ha=4;
$pdf->SetTextColor(0);
$pdf->SetFont('Helvetica','B',10);
$pdf->Image('../img/logoincor.jpg', 10, 10, 45, 16,'jpg');
$pdf->Image('../img/logoessalud.jpg', 243, 12, 40, 11,'jpg');
$alturaX = $alturaX+ $ha;
$sqlservicio = "select tp_desc from tb_tipoperson t where tp_id='$idtp'";
$results = mysqli_query($con,$sqlservicio);
while ($rowar= mysqli_fetch_array($results)){
$tpdesc = $rowar['tp_desc'];
}
$texto="FORMATO UNICO DE LA PROGRAMACION DEL PERSONAL ASISTENCIAL MENSUAL DEL PROFESIONAL DE LA SALUD: ".$tpdesc;
$pdf->ImprimirTablaSinColorTitulo($texto,20,$alturaX,$ha,$sumat,'C',0);
//$pdf->Text(30,$alturaX,$texto);
$alturaX = $alturaX+ 2*$ha;
$pdf->SetFont('Arial','',5.6);

$wt1 = array($sumat*0.12,$sumat*0.1,$sumat*0.06,$sumat*0.1,$sumat*0.34,$sumat*0.06,$sumat*0.08,$sumat*0.1);
$dibu1 = array(1,1,0,1,1,0,1,1);
$dibu2 = array(1,1,0,1,1,0,1,1);
$alin = array('L','L','','L','L','','L','L');
$encabezado1 = array('RED ASISTENCIAL','INCOR','','DPTO / AREA',$nomjefatura,'','AÑO',$anno);
$encabezado2 = array('CENTRO ASISTENCIAL','INCOR','','SERVICIO',$nomservicio,'','MES',$arraymes[$mm-1]['mes']);
$pdf->ImprimirTablaSinColorFirma($encabezado1,25,$alturaX,$ha,$wt1,$alin,$dibu1);
$alturaX = $alturaX+ $ha;
$pdf->ImprimirTablaSinColorFirma($encabezado2,25,$alturaX,$ha,$wt1,$alin,$dibu2);
$alturaX = $alturaX+ 2*$ha;
$ha=5;
$pdf->ImprimirTablaSinColor($dato6cab,25,$alturaX,2*$ha,$w6, $alig6);

$pdf->SetFont('Arial','',5.5);
$pdf->ImprimirDias($header2,$posicionx,$alturaX,$ha,$ancho,$alinea,$pinta);
$alturaX = $alturaX + $ha;
$pdf->ImprimirDias($header3,$posicionx,$alturaX,$ha,$ancho,$alinea,$pinta);
$alturaX = $alturaX + $ha;
$pdf->SetFont('Arial','',6);
///////////////////////////////////////
$hst = $sumat*0.12 + $sumat*0.166 + $suma;
$pdf->ImprimirTablaTitulo($garea,25,$alturaX,$ha,$hst,'L',true);
$alturaX = $alturaX + $ha;
//echo "<center><table><tr><td align='center'><font color='red'><b>".$garea."</b></font></td><td><input type='button' id='but".$idga."' value='Ocultar' onCLick='mostrardiv(".$idga.");'></td></tr></table>";
////////////////primer div para el contenido de las areas

//echo "<div id='gadet".$idga."' style='display: ;'>";

//include("cargagarea.php");

////////CARGA AREA

//pregunta por las areas
$sqlarea="select distinct(a_id) as ida from tb_areaservicio where ga_id='$idga' and tp_id='$idtp' and j_id='$idj' and s_id='$ids' order by ida";
$resulta = mysqli_query($con,$sqlarea);
//echo $sqlarea."<br>echo<br>";
if(mysqli_num_rows($resulta)!=0){
$conta=0;
while ($row= mysqli_fetch_array($resulta)){
$ida=$row['ida'];
$sqlcantturno= "select a.a_id as codarea, (select a_desc from tb_area where a_id=a.a_id and ga_id=a.ga_id) as area, ";
$sqlcantturno= $sqlcantturno." count(distinct(a.h_id)) as idhora from areahora a inner join tb_areaservicio b on a.s_id=b.s_id ";
$sqlcantturno= $sqlcantturno." and a.j_id=b.j_id and a.a_id=b.a_id and a.ga_id=b.ga_id where a.j_id='$idj' and a.s_id='$ids' ";
$sqlcantturno= $sqlcantturno." and a.a_id='$ida' and a.ga_id='$idga' and a.ah_estado='1' and b.tp_id='$idtp' group by a.a_id order by codarea";
$resultcantturno = mysqli_query($con,$sqlcantturno);
//echo $sqlcantturno."<br><br>";
if(($alturaX + 2*$ha)>173){

///NUEVA HOJA
$pdf->AddPage();
$alturaX=25;
$ha=4;
$pdf->SetTextColor(0);
$pdf->SetFont('Helvetica','B',10);
$pdf->Image('../img/logoincor.jpg', 10, 10, 45, 16,'jpg');
$pdf->Image('../img/logoessalud.jpg', 243, 12, 40, 11,'jpg');
$alturaX = $alturaX+ $ha;
$sqlservicio = "select tp_desc from tb_tipoperson t where tp_id='$idtp'";
$results = mysqli_query($con,$sqlservicio);
while ($rowar= mysqli_fetch_array($results)){
$tpdesc = $rowar['tp_desc'];
}
$texto="FORMATO UNICO DE LA PROGRAMACION DEL PERSONAL ASISTENCIAL MENSUAL DEL PROFESIONAL DE LA SALUD: ".$tpdesc;
$pdf->ImprimirTablaSinColorTitulo($texto,20,$alturaX,$ha,$sumat,'C',0);
//$pdf->Text(30,$alturaX,$texto);
$alturaX = $alturaX+ 2*$ha;
$pdf->SetFont('Arial','',5.6);

$wt1 = array($sumat*0.12,$sumat*0.1,$sumat*0.06,$sumat*0.1,$sumat*0.34,$sumat*0.06,$sumat*0.08,$sumat*0.1);
$dibu1 = array(1,1,0,1,1,0,1,1);
$dibu2 = array(1,1,0,1,1,0,1,1);
$alin = array('L','L','','L','L','','L','L');
$encabezado1 = array('RED ASISTENCIAL','INCOR','','DPTO / AREA',$nomjefatura,'','AÑO',$anno);
$encabezado2 = array('CENTRO ASISTENCIAL','INCOR','','SERVICIO',$nomservicio,'','MES',$arraymes[$mm-1]['mes']);
$pdf->ImprimirTablaSinColorFirma($encabezado1,25,$alturaX,$ha,$wt1,$alin,$dibu1);
$alturaX = $alturaX+ $ha;
$pdf->ImprimirTablaSinColorFirma($encabezado2,25,$alturaX,$ha,$wt1,$alin,$dibu2);
$alturaX = $alturaX+ 2*$ha;
$ha=5;
$pdf->ImprimirTablaSinColor($dato6cab,25,$alturaX,2*$ha,$w6, $alig6);

$pdf->SetFont('Arial','',5.5);
$pdf->ImprimirDias($header2,$posicionx,$alturaX,$ha,$ancho,$alinea,$pinta);
$alturaX = $alturaX + $ha;
$pdf->ImprimirDias($header3,$posicionx,$alturaX,$ha,$ancho,$alinea,$pinta);
$alturaX = $alturaX + $ha;
$pdf->SetFont('Arial','',6);
///////////////////////////////////////
$hst = $sumat*0.12 + $sumat*0.166 + $suma;
$pdf->ImprimirTablaTitulo($garea,25,$alturaX,$ha,$hst,'L',true);
$alturaX = $alturaX + $ha;
$conta=0;
}
while ($rownt= mysqli_fetch_array($resultcantturno)){


						$area= $rownt['area'];
						$cantreg = $rownt['idhora'];
						$hp = $ha*$cantreg;
						$wa = $sumat*0.12;
						$pdf->ImprimirTablaArea($area,25,$alturaX,$hp,$wa,'C',2,$cantreg);

//pregunta por el horario
$sqlhora=" select distinct(a.h_id) as idhora, b.h_horaturn as horaturn, (select o_idturn from tb_ordena where o_turn=b.h_horaturn) as tt ";
$sqlhora=$sqlhora." from areahora a inner join tb_horario b on a.h_id = b.h_id inner join tb_areaservicio c on a.s_id=c.s_id ";
$sqlhora=$sqlhora." and a.j_id=c.j_id and a.a_id=c.a_id and a.ga_id=c.ga_id where a.j_id='$idj' and a.s_id='$ids' ";
$sqlhora=$sqlhora." and c.tp_id='$idtp' and a.a_id='$ida' and a.ga_id='$idga' order by tt ";
$resulthora=mysqli_query($con,$sqlhora);
//echo $sqlhora."<br><br>";
$alturaXH = $alturaX;
$alturaXT = $alturaX;
while ($rowh= mysqli_fetch_array($resulthora)){
$idh=$rowh['idhora'];
//pregunta por el turno
$sqlturno = "select h_id, concat(date_format(h_horaini,'%k:%i %p'),' - ',date_format(h_horafin,'%k:%i %p')) as hora, h_horaturn as turno, h_horanum as numhora from tb_horario where h_id='$idh'";
$resultturno = mysqli_query($con,$sqlturno);
//echo $sqlturno."<br><br>";
while ($rowtt= mysqli_fetch_array($resultturno)){
						$horario = array($rowtt['hora'], $rowtt['turno'],$rowtt['numhora']);
						$wh= array($sumat*0.095, $sumat*0.025, $sumat*0.0455);
						$pdf->ImprimirTablaHorario($horario,25+$wa,$alturaXH ,$ha,$wh,'C');	
						$alturaXH = $alturaXH + $ha;
}

$diaprog = array();
for ($i=1;$i<=$numeroDeDias;$i++){
$dia= date("w",mktime( 0, 0, 0, $mm, $i, $anno )); 
switch ($dia) {
    case 0: $diaprog[$i-1]=" "; break;
    case 1: 
    case 2:
    case 3:
    case 4:
    case 5:
    case 6:
        $diaprog[$i-1]=" "; break;
}

}

$pdf->ImprimirDiasHoras($diaprog,$posicionx,$alturaXT,$ha,$ancho,$alinea,$pintap);
$alturaXT = $alturaXT + $ha;
}
$alturaX = $alturaX + $ha*$cantreg;

}
$conta=$conta + 1;
}//termina de consultar el area
}



}
$alturaX = $alturaX + $ha;
}

////////LISTADO DEL PERSONAL
//pregunta por el personal

$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', 150, 12, 40, 11,'jpg');
$texto = "CONSOLIDADOS DE HORAS PROGRAMADOS DEL PERSONAL ASISTENCIAL";
$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($tpdesc,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;
$fecha = $arraymes[$mm-1]['mes']." - ".$anno;
$pdf->ImprimirTablaSinColorTitulo($fecha,25,$ini,$ha,$sumat*0.65,'C',0);
$ini = $ini + 1.5*$ha;
$pdf->SetFont('Arial','B',6);
					    $subtitulo = array('Nro','CODIGO','APELLIDOS Y NOMBRES','HO','HE','TOTAL','OBSERVACIONES');
						$wh= array($sumat*0.03, $sumat*0.04, $sumat*0.23,$sumat*0.04,$sumat*0.04,$sumat*0.04,$sumat*0.30);
						$pdf->ImprimirTablaHorario($subtitulo,15,$ini,$ha,$wh,'C');	
						$ini = $ini + $ha;
						//////////////////////////
						
//pregunta por el personal
$sqlperprog = "select * from tb_usutrab b where b.j_id='$idj' and b.tp_id='$idtp' and b.ut_idserva='$ids' and b.uap_anno='$anno' and b.uap_mes ='$mm'";
$resultperprog = mysqli_query($con,$sqlperprog);
$factor =0;
if(mysqli_num_rows($resultperprog)==0){
$sqlper1=" select a.ug_id as cod, a.ua_codprog as codigo, CONCAT(a.ua_apepat,' ',a.ua_apemat,' ',a.ua_nom) as nombre, ";
$sqlper1= $sqlper1." b.s_id as idsa from tb_usudep 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.s_id='$ids' order by nombre ";
$factor=1;
}else{
$sqlper1="select a.ug_id as cod, b.ut_codprog as codigo, CONCAT(a.ua_apepat,' ',a.ua_apemat,' ',a.ua_nom) as nombre, ";
$sqlper1= $sqlper1." b.ut_idserva as idsa, b.ut_obs as obs1 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";
}
//Echo $sqlper1."<br>";
//horas por los servicios
$a=1;
$b=1;
$sumaho=0;
$sumahe=0;
$resultper = mysqli_query($con,$sqlper1);
while ($rowp= mysqli_fetch_array($resultper)){
$ugid = $rowp['cod'];
$codprog = $rowp['codigo'];
$nombre = $rowp['nombre'];
$obsp = "";
if($factor==1){
$askcas = "select * from tb_usudep a inner join tb_tipocont b on a.tc_idcont = b.tc_idcont where  a.ug_id='$ugid' and b.tc_abrev in ('CAS')";
//echo $askcas."<br>";
$resultcas = mysqli_query($con,$askcas);
//pregunta por ultimo mes del anno
if($mm==1){
$mmp=12;
$annop = $anno-1;
}else{
$mmp=$mm-1;
$annop = $anno;
}
//plazo nombrado y indeterminado
if(mysqli_num_rows($resultcas)==0){
$askvacac="select * from tb_vacaciones where ug_id ='$ugid' and v_estado=1 and month(v_fechaini)='$mmp' and year(v_fechaini)='$annop' and month(v_fechafin)='$mmp' and year(v_fechafin)='$annop' and v_numdias='30'";
$resultvacac = mysqli_query($con,$askvacac);
if(mysqli_num_rows($resultvacac)!=0){
$askhrsprog = "select sum(b.h_horanum) as nrohrs from tb_horaprog".$anno." a inner join tb_horario b on a.h_id=b.h_id where a.ug_id ='$ugid' and a.uap_anno='$annop' and a.uap_mes='$mmp' and a.tp_id='$idtp' and a.hp_idserva='$ids' and a.j_id='$idj' and a.hp_tipohora='0'";
$resulthrsprog = mysqli_query($con,$askhrsprog);
while ($rowpp= mysqli_fetch_array($resulthrsprog)){
$hrsdemas= $rowpp['nrohrs'];
if($hrsdemas>0){
$obsp = $hrsdemas." HRS PROGRAMADAS EN ".$arraymes[$mmp-1]['mes']." ".$annop;
}
}
}
}else{
//plazo cas
$askvacac="select * from tb_vacaciones where ug_id ='$ugid' and v_estado=1 and month(v_fechaini)='$mmp' and year(v_fechaini)='$annop' and month(v_fechafin)='$mmp' and year(v_fechafin)='$annop' and v_numdias='15'";
$resultvacac = mysqli_query($con,$askvacac);
if(mysqli_num_rows($resultvacac)!=0){
$askhrsprog = "select sum(b.h_horanum) as nrohrs from tb_horaprog".$anno." a inner join tb_horario b on a.h_id=b.h_id where a.ug_id ='$ugid' and a.uap_anno='$annop' and a.uap_mes='$mmp' and a.tp_id='$idtp' and a.hp_idserva='$ids' and a.j_id='$idj' and a.hp_tipohora='0'";
$resulthrsprog = mysqli_query($con,$askhrsprog);
while ($rowpp= mysqli_fetch_array($resulthrsprog)){
$horasprog= $rowpp['nrohrs'];
$hrsdemas = $horasprog - 100;
if($hrsdemas>0){
$obsp = $hrsdemas." HRS PROGRAMADAS EN ".$arraymes[$mmp-1]['mes']." ".$annop;
}
}
}
}
//pregunta  por las vacaciones del mes en programacion
//editar comentario
		$sqlaskvacie="select v_id as id, concat(date_format(v_fechaini,'%d/%m/%Y'),' AL ', date_format(v_fechafin,'%d/%m/%Y')) as fecha from tb_vacaciones where ((year(v_fechaini)='$anno' and month(v_fechaini)='$mm') or (year(v_fechafin)='$anno' and month(v_fechafin)='$mm')) and v_estado='0' and ug_id='$ugid'";
		//echo $sqlaskvacie."<br>";
		$resultsql3e = mysqli_query($con,$sqlaskvacie);
		$comentario="";
		$comentario1="";
		$a=0;
		if(mysqli_num_rows($resultsql3e)!=0){
		while ($rowa= mysqli_fetch_array($resultsql3e)){
		if($a>0){
		$comentario1 = " Y DEL ".$comentario1;
		}else{
		$comentario1 = $comentario1.$rowa['fecha'];
		}
		$a=$a+1;
		}
		$comentario = " VACACIONES DEL ".$comentario1;
		}
		
//descanso medico
			$sqlaskdm="select dm_id as id, concat(date_format(dm_fechaini,'%d/%m/%Y'),' AL ', date_format(dm_fechafin,'%d/%m/%Y')) as fecha from tb_descmed where ((year(dm_fechaini)='$anno' and month(dm_fechaini)='$mm') or (year(dm_fechafin)='$anno' and month(dm_fechafin)='$mm')) and ug_id='$ugid'";
					$resultdm = mysqli_query($con,$sqlaskdm);
					$comentario2="";
					$a=0;
					if(mysqli_num_rows($resultdm)!=0){
					while ($rowdm= mysqli_fetch_array($resultdm)){
					if($a>0){
					$comentario2 = $comentario2." Y DEL ";
					}
					$comentario2 = $comentario2.$rowdm['fecha'];
					$a=$a+1;
					}
					$comentario = $comentario." DESCANSO MEDICO DEL ".$comentario2;
					}
	// onomastico
	$sqlono = "select day(ua_fechanac) as onomastico from tb_usuasist where ug_id='$ugid' and month(ua_fechanac)='$mm'";
					$resultono = mysqli_query($con,$sqlono);
					$comentario3="";
					if(mysqli_num_rows($resultono)!=0){
					while ($rowono= mysqli_fetch_array($resultono)){
					$comentario3 = $comentario3.$rowono['onomastico'];
					}
					$comentario = $comentario." ONOMASTICO EL DIA ".$comentario3;
					}
					$obsp =$obsp." ".$comentario;
}else{
//si ya hay progrmacion grabada
$obsp = $rowp['obs1'];
}

//suma de horas del personal por el servicio

			if($b>40){
			$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', 150, 12, 40, 11,'jpg');
			$texto = "CONSOLIDADOS DE HORAS PROGRAMADOS DEL PERSONAL ASISTENCIAL";
			$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($tpdesc,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;
			$fecha = $arraymes[$mm-1]['mes']." - ".$anno;
			$pdf->ImprimirTablaSinColorTitulo($fecha,25,$ini,$ha,$sumat*0.65,'C',0);
			$ini = $ini + 1.5*$ha;
			$pdf->SetFont('Arial','B',6);
							$subtitulo = array('Nro','CODIGO','APELLIDOS Y NOMBRES','HO','HE','TOTAL','OBSERVACIONES');
						$wh= array($sumat*0.03, $sumat*0.04, $sumat*0.23,$sumat*0.04,$sumat*0.04,$sumat*0.04,$sumat*0.30);
						$pdf->ImprimirTablaHorario($subtitulo,15,$ini,$ha,$wh,'C');	
						$ini = $ini + $ha;
			$personallista = array($a,$codprog,$nombre,0,0,0,$obsp);
			$b=1;
			}else{
			$personallista = array($a,$codprog,$nombre,0,0,0,$obsp);
			$alin = array('C','C','L','C','C','C','C');
			$pdf->ImprimirTablaPersonal($personallista,15,$ini,$ha,$wh,$alin);
			$ini = $ini + $ha;	
			}
			
	$a = $a+1;
	$b = $b + 1;
}
					
						
						////////////////////////
						
						$subtitulo = array('TOTAL',$sumaho,$sumahe,$sumaho+$sumahe,'');
						$wh= array($sumat*0.30,$sumat*0.04,$sumat*0.04,$sumat*0.04,$sumat*0.30);
						$pdf->ImprimirTablaHorario($subtitulo,15,$ini,$ha,$wh,'C');	
						$ini = $ini + $ha;
$pdf->Output();

?>
