<?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");
$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'];
}

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


$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('Nro','CODIGO','APELLIDOS Y NOMBRES');

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

$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('C','C','L');
$w6 = array($sumat*0.04,$sumat*0.05, $sumat*0.2);
$wtitulo = $sumat*0.04 + $sumat*0.05 + $sumat*0.2;



////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' group by b.ga_id order by b.ga_id";
//echo $sqlar."<br>";
// busca el nombre del area
$resultar = mysql_query($sqlar);
//echo $sqlar."<br>";
if ($resultar == false){
echo 'PROVOCO UN ERROR EN LA CONSULTA 2';
}else{
while ($rowar= mysql_fetch_array($resultar)){
$garea = $rowar['garea'];
}}
///////////////////////////////////////
//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']; 
}

///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 = mysql_query($sqlservicio);
while ($rowar= mysql_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,0,0);
$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,'','','');
$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;
$posicionx2 = ($sumat - ($posicionx + $suma)) - 14.75;
$pdf->ImprimirTablaSinColor($dato6cab,20,$alturaX,2*$ha,$w6, $alig6);
						$subtitulo = array('HO','HE','TOTAL');
						$wh= array($sumat*0.06,$sumat*0.06,$sumat*0.06);
						$pdf->ImprimirTablaSinColor($subtitulo,$posicionx2,$ini,$ha,$wh,'C');	
						$ini = $ini + $ha;
	
$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);
///////////////////////////////////////

//$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*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);

$pdf->Output();

?>