<?
session_start();

//RESTRICCI?N DE ACCESO A TRABAJADOR (ACCESO INDIVIDUAL)
if (isset($_SESSION['tipousuario']))
		{if ($_SESSION['tipousuario']=='1')
		header ('location:menu_personal.php');
		}

if  (trim($_SESSION['nombre_validar'])=='') header ('location:index.php');
include ("fphp.php");
$enlace=cargar_conexion();
$detalle=$_REQUEST["detalle"];
$pago=$_REQUEST["pago"];

		/////////////////////////////////////////////// imprimir PAYROLL
		$consulta=mysqli_query($enlace,"select * from empresa where id='".$_SESSION['id_validar']."' ");
		if (mysqli_num_rows($consulta)!=0){
			$row=mysqli_fetch_array($consulta);
			$_POST['empresal']=$_SESSION['empresa_validar'];
			$_POST['pie']=$row['pie'];
			$_POST['derecho']=$row['derecho'];
			$_POST['encabeza']=$row['encabeza'];
		}

		$query = "select * from utilidades where detalle='".$detalle."' and id='".$_SESSION['id_validar']."' ";
		$consulta = mysqli_query($enlace,$query) or die ("no se pudo realizar la consulta...");
		$row=mysqli_fetch_array($consulta);
		$_POST['subtitulo']=$row['detalle'];

		require('fpdf.php');
		class PDF extends FPDF {
		function Header()
		{
	    $this->Image($_SESSION['logo_validar'],10,10,30,30);
	    $this->SetFont('arial','B',11);
		$this->Ln(4);
	    $this->Cell(0,5,$_POST['empresal'],0,1,'R');
		$this->SetFont('arial','',7);
		$this->Cell(0,5,$_POST['encabeza'],0,1,'R');
		$this->Ln(5);
		$this->SetFont('arial','',9);
	    $this->Cell(0,5,'Fecha: '.hoy(),0,1,'R');
		$this->SetFont('arial','',7);
	    $this->Cell(0,5,'Hora: '.hora(),0,1,'R');
		$this->SetFont('arial','B',12);
		$this->Ln(5);
		$this->Cell(0,0,'Reporte de Pago de Bonificacion de Fin de Ano (Utilidades)',0,1,'C');$this->Ln(5);
		$this->SetFont('arial','',10);
		$this->Cell(0,0,utf8_decode($_POST['subtitulo']),0,1,'C');$this->Ln(5);
		$this->SetFont('arial','',10);

	    $header=array('Cod.','Cedula','Nombres y Apellidos','Tipo','Concepto','Cantidad','Calculo','Asig. Bs.','Ded. Bs.');
	    $w=array(10,15,35,10,45,20,20,20,20);

	    $this->SetFillColor(70,77,119);
			$this->SetTextColor(255);
			$this->SetDrawColor(255,255,255);
			$this->SetLineWidth(.5);
	    $this->SetFont('','');
		$this->SetFont('arial','B',8);
		for($i=0;$i<count($header);$i++)
        $this->Cell($w[$i],4,$header[$i],1,0,'C',1);
	    $this->Ln();
	    $this->SetFont('');
		}
		function Footer()
		{
		$this->SetY(-20);
		$this->Cell(0,0,'',1,1,'C');
		$this->SetFont('Times','I',8);
		$this->ln(2);
		$this->Cell(0,0,$_POST['pie'],0,1,'C');
		$this->Cell(0,10,'Usuario: '.$_SESSION['nombre_validar'].' - P?gina '.$this->PageNo().'/{nb} - Derechos: '.$_POST['derecho'],0,0,'C');
		}
		}
		//Cuerpo ///////////////////////////////
		$pdf=new PDF($orientacion='P',$unit='mm',$format='letter');
		$pdf->AddPage();
		$pdf->AliasNbPages();
		$pdf->SetFont('Arial','',7);
		$pdf->SetFillColor(238,239,243);
		$pdf->SetTextColor(0);

		$query = "select * from utilidades where detalle='".$detalle."' and id='".$_SESSION['id_validar']."' and fecha='".$pago."' order by cedula, concepto asc, concepto";
		$consulta = mysqli_query($enlace,$query) or die ("no se pudo realizar la consulta...");
		$linea=0;
		$color=0;
		$tasig=0;
		$tdedu=0;
		$bandera=0;
		$ttasig=0; $ttdedu=0;$tced='';
		while($row=mysqli_fetch_array($consulta)) {
			$ttasig+= (($row['concepto']=='8017' || $row['concepto']=='8117')?$row['monto']:0);
			$ttdedu+= (($row['concepto'] =='8018' || $row['concepto'] == '8019' || $row['concepto'] == '8118' || $row['concepto'] == '8119')?$row['monto']:0);

			$tnom="";$tcedx='';
			if ($tced!=$row['cedula']) {
				if ($linea>0) {
					$pdf->Cell(195,0,'',1,1,'C'); $pdf->ln(1);
					$pdf->cell(155,5,'Sub Total por Trabajador.....Bs.',0,0,'R');
					$pdf->cell(20,5,number_format($tasig,2) ,0,0,'R');
					$pdf->cell(20,5,number_format($tdedu,2) ,0,1,'R');
					$pdf->cell(155,5,'NETO UTILIDADES A PAGAR.....Bs.',0,0,'R');
					$pdf->cell(40,5,number_format($tasig-$tdedu,2) ,0,1,'R');
					$pdf->ln(5);
					$pdf->Cell(195,0,'',1,1,'C'); $pdf->ln(1);
					$color=0;
					$tasig=0;
					$tdedu=0;
				}
				$tced=$row['cedula'];
				$tcedx=$row['cedula'];
				$consul1=mysqli_query($enlace,"select nombres,apellidos,sso from personal where id='".$_SESSION['id_validar']."' and codigo='".$tced."'" ) or die($consul1.mysqli_error($enlace));
				if (mysqli_num_rows($consul1)<>0) {
					$row1=mysqli_fetch_array($consul1);
					//$tnom=trim($row1['sso'])." ".trim($row1['nombres']).',  '.trim($row1['apellidos']);
					$tnom=trim($row1['nombres']).',  '.trim($row1['apellidos']);
				}
			}

			$tasig+= (($row['tipo']=='A' && $row['concepto']!='8020' && $row['concepto']!='8120')?$row['monto']:0);
			$tdedu+= (($row['tipo']=='D' && $row['concepto']!='8021' && $row['concepto']!='8121') ?$row['monto']:0);
			$linea++;
			$pdf->cell(10,5,$row['concepto'],0,0,'C',$color);
			$pdf->cell(15,5,$tcedx,0,0,'',$color);
			$pdf->cell(35,5,$tnom,0,0,'',$color);
			$pdf->cell(10,5,$row['tipo'],0,0,'C',$color);
			$pdf->cell(45,5,substr($row['desconcepto'],0,40),0,0,'L',$color);
			$pdf->cell(20,5,number_format($row['cantidad'],2),0,0,'R',$color);
			$pdf->cell(20,5,($row['tipo']=='R'?number_format($row['monto'],2):''),0,0,'R',$color);
			$pdf->cell(20,5,($row['tipo']=='A'?number_format($row['monto'],2):''),0,0,'R',$color);
			$pdf->cell(20,5,($row['tipo']=='D'?number_format($row['monto'],2):''),0,1,'R',$color);
			$color=!$color;
		}
		$pdf->Cell(195,0,'',1,1,'C'); $pdf->ln(1);
		$pdf->cell(155,5,'Sub Total por Trabajador.....Bs.',0,0,'R');
		$pdf->cell(20,5,number_format($tasig,2) ,0,0,'R');
		$pdf->cell(20,5,number_format($tdedu,2) ,0,1,'R');
		$pdf->cell(155,5,'NETO UTILIDADES A PAGAR.....Bs.',0,0,'R');
		$pdf->cell(40,5,number_format($tasig-$tdedu,2) ,0,1,'R');

		$pdf->Cell(195,0,'',1,1,'C'); $pdf->ln(1);
		$pdf->cell(155,5,'Totales Generales',0,0,'R');
		$pdf->cell(20,5,number_format($ttasig,2) ,0,0,'R');
		$pdf->cell(20,5,number_format($ttdedu,2) ,0,1,'R');
		$pdf->cell(155,5,'NETO TOTAL GENERAL A PAGAR.....Bs.',0,0,'R');
		$pdf->cell(40,5,number_format($ttasig-$ttdedu,2) ,0,1,'R');
		$pdf->Cell(195,0,'',1,1,'C'); $pdf->ln(1);
		$pdf->Cell(195,0,'',1,1,'C');
		$pdf->Output();
		mysqli_close($enlace);
?>
