<?php
session_start();if  (trim($_SESSION['nombre_validar'])=='') header ('location:index.php');
include ("fphp.php");
$accion=$_REQUEST['accion'];
$enlace=cargar_conexion();
$consulta=mysqli_query($enlace,"select * from periodo where id='".$_SESSION['id_validar']."' order by nomina,periodo desc limit 1");
if (mysqli_num_rows($consulta)!=0){
	$row=mysqli_fetch_array($consulta);
	$_SESSION['per_validar']=mostrar_fecha($row['desde']).' al '.mostrar_fecha($row['hasta'])." (Nom:".$row['nomina'].")";
} else $_SESSION['per_validar']='';
set_time_limit(3000); 

///////////////////////////////////////////////// inicio del programa ////////////////////////////////////////////////////////////////
if ($_POST['bandera']==11) {

	if($_POST['email'] == $_SESSION['email_validar'])
		{
			//echo " Aqui si entra";
		if ($_POST['pass1'] == $_POST['pass2'] && $_POST['pass1']!='')
			{
				//echo "Aqui tambi?n";

				$consulta=mysqli_query($enlace,"update personal set clave='".$_POST['pass1']."' where id='".$_SESSION['id_validar']."' and codigo='".$_SESSION['cedula_validar']."'");
				//$_SESSION['email_validar']=$_POST['email'];
				$mensaje='Password Actualizado...';

			}
			else $mensaje='La confirmaci&oacute;n del nuevo password no coincide, intente de nuevo...';


		}

		else $mensaje='El email ingresado no coincide con el registrado SAIREH';


}

///////////////////////////////////////////////// inicio del programa ////////////////////////////////////////////////////////////////
if (!$_POST || $accion==11 || $_POST['bandera']==11) {
		menu();




		$entra=0;
		echo "<b>Opciones:</b> ";
		echo "&nbsp;&nbsp;<a href='".$_SERVER['PHP_SELF']."?accion=11'><img border = 0 src='img2/icono_01.png' alt='Modificar Clave'>&nbsp;Modificar Clave</a>";
		echo "<H3><center><font color=red>".$mensaje."</font></center></H3>";

		//echo "&nbsp;&nbsp;<a target='_blank' href='prestaciones_informe_consultar.php?accion=2&cedula=".$_SESSION['cedula_validar']."'><img border = 0 src='img2/apuntes.png' alt='Consultar Prestaciones'>&nbsp;Informaci&oacute;n de Prestaciones</a>";
		echo "<hr><h2>REPORTE INDIVIDUAL DE PAGOS DE N&Oacute;MINAS Y RECIBOS DE PAGO</h2>";



if ($accion==11) {

		$consulta1 = "SELECT clave, mensaje FROM personal WHERE id='".$_SESSION['id_validar']."' and codigo='".$_SESSION['cedula_validar']."' ";
	$resultado1 = mysqli_query($enlace,$consulta1);
	$row1 = mysqli_fetch_array($resultado1);
	$clave = $row1['clave'];
	$_SESSION['email_validar'] = $row1['mensaje'];

	//echo $clave;
	//echo $_SESSION['email_validar'];
			echo " <br><br><form name='entrada' method='POST' action='".$_SERVER['PHP_SELF']."' enctype='multipart/form-data'>
			<input type = 'hidden' name = 'bandera' value = '11'>
			<table align='center'>

				<tr>
					<td></td>
					<td colspan=2 align='right'>Email Actual:</font></td>
					<td colspan=2 align=''><input name='email' size = '50' maxlength = '50'></font></td>
					<td></td>
				</tr>
				<tr>
					<td></td>
					<td colspan=2 align='right'>Nuevo Password:</font></td>
					<td colspan=2 align=''><input name='pass1' size = '20' maxlength = '20'></font></td>
					<td></td>
				</tr>
				<tr>
					<td></td>
					<td colspan=2 align='right'>Confirme Nuevo Password:</font></td>
					<td colspan=2 align=''><input name='pass2' size = '20' maxlength = '20'></font></td>
					<td></td>
				</tr>
				<tr>
					<td></td>
					<td colspan=2 align='right'></td>
					<td colspan=2 align=''><input name='procesar' type = 'submit' value = 'Cambiar Password'></font></td>
					<td></td>
				</tr>
			</tr>
			</table>
			<script language='JavaScript' type='text/JavaScript'>
				document.entrada.pass1.focus()
			</script>
			</form>";
		}

		echo " <form name='entrada' action='".$_SERVER['PHP_SELF']."' method='POST' target='_blank' enctype='multipart/form-data'>
				<input type='hidden' name='usuario' value = '".$_SESSION['nombre_validar']."'>
				<input type='hidden' name='forma' value = '1'>
				<table align='center'>";

		echo "  <tr><td align='righta'>Tipo Reporte:</td>
				<td align='left'>
					<select style='width:300px;' name='treporte'>
						<option value='3' selected>Pago de N&oacute;mina (Pantalla)
						<option value='4'>Recibos de Pagos (Imprimir)
					</select>
				</td>
		</tr>";

		$consulta = mysqli_query($enlace,"select * from periodo where id='".$_SESSION['id_validar']."' and nomina='".$_SESSION['nomina_validar']."' order by nomina,periodo DESC") or die ("select * from periodo order by nomina,periodo DESC");
		$numr = mysqli_num_rows($consulta);
		echo "<tr>";
			echo "<td align = 'right'> Per&iacute;odo / N&oacute;mina: </font></td>";
			echo "<td align = 'left'> <select style='width:300px;' name = 'periodo1'>";
			for ($x=1;$x<=$numr;$x++) {
				$row1 = mysqli_fetch_array($consulta);
				if ($x!=1) {
					echo "<option>".$row1['periodo']." - Nomina:".$row1['nomina']." - ".$row1['desde']." al ".$row1['hasta'];
				}
			}
			echo "</select></td>";
		echo "</tr>";

		echo "<tr><td colspan='4' align='center'>";
			echo "<input name='ver' type='image' src='img2/boton_buscar.jpg' value='Procesar Reporte'>";
		echo "</td></tr>";
		echo "</table></form>";



} else {
///////////////////////////////////////////////// cuando esta recibiendo datos  ////////////////////////////////////////////////////////////////

		if ($_POST['treporte']==1) $_POST['titulo']='Resumen General de Conceptos';
		if ($_POST['treporte']==2) $_POST['titulo']='Detalle Individual de N&oacute;mina';
		if ($_POST['treporte']==3) $_POST['titulo']='Pago de N&oacute;mina General';
		if ($_POST['treporte']==4) $_POST['titulo']='Recibo de Pago';
		if ($_POST['treporte']==5) $_POST['titulo']='Detalle de Conceptos';
		if ($_POST['treporte']==6) $_POST['titulo']='Detalle de Deducciones/Asignaciones Automáticas';
		if ($_POST['treporte']==7) $_POST['titulo']='Carta Bancaria';
		if ($_POST['treporte']==8) $_POST['titulo']='Pago de N?mina (Sabana)';

		$payroll="z_".trim($_SESSION['usuario_validar']);
		$consulta=mysqli_query($enlace,"create table if not exists `".$payroll."` (
			  `cedula` varchar(8) NOT NULL,
			  `concepto` varchar(4) NOT NULL,
			  `desconcepto` varchar(50) NOT NULL,
			  `tipo` varchar(1) NOT NULL,
			  `monto` decimal(18,2) NOT NULL,
			  `saldo` decimal(18,2) NOT NULL,
			  `cantidad` decimal(18,2) NOT NULL,
			  `nomina` varchar(3) NOT NULL,
			  `desnomina` varchar(50) NOT NULL,
			  `periodo` varchar(3) NOT NULL,
			  `desperiodo` varchar(50) NOT NULL
		) ENGINE=MyISAM DEFAULT CHARSET=latin1;");

		$consulta=mysqli_query($enlace,"delete from $payroll");
		$consulta=mysqli_query($enlace,"insert into $payroll (cedula,concepto,desconcepto,tipo,monto,saldo,cantidad,nomina,desnomina,periodo,desperiodo) select cedula,concepto,desconcepto,tipo,monto,saldo,cantidad,nomina,desnomina,periodo,desperiodo from historico where id='".$_SESSION['id_validar']."' and nomina='".substr($_POST['periodo1'],13,3)."' and periodo ='".substr($_POST['periodo1'],0,3)."' and cedula='".$_SESSION['cedula_validar']."'   group by historico.cedula,historico.concepto  ") or die ("insert into historico select * from payroll :".mysqli_error($enlace));
		$nomina='';$periodo='';$desnomina='';$desperiodo='';
		$consulta=mysqli_query($enlace,"select * from $payroll"  );

		$row=mysqli_fetch_array($consulta);
		$nomina=$row['nomina'];
		$desnomina=$row['desnomina'];
		$periodo=$row['periodo'];
		$desperiodo=$row['desperiodo'];

		/////////////////////////////////////////////// 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']=$row['nombre'];
			$_POST['pie']=$row['pie'];
			$_POST['derecho']=$row['derecho'];
			$_POST['encabeza']=$row['encabeza'];
			$_POST['empresa']=$desnomina;
		}
		$_POST['desnomina']=$desnomina;
		$_POST['nomina']=substr($_POST['periodo1'],13,3);
		$_POST['periodo']=$periodo.' - '.$desperiodo;

		if ($_POST['treporte']==3) {
			$consulta=mysqli_query($enlace,"select * from personal,$payroll where personal.id='".$_SESSION['id_validar']."' and $payroll.cedula=personal.codigo and $payroll.cedula='".$_SESSION['cedula_validar']."' AND substr($payroll.concepto,1,1)!=5 AND substr($payroll.concepto,1,1)!=6 AND substr($payroll.concepto,1,1)!=4 AND concepto NOT IN(2000,2001,2002,2003,2004,2005,2006,2007,2010,2012) order by $payroll.concepto, $payroll.desconcepto  "  );
		} else {
			$consulta  ="select $payroll.*,personal.nombres,personal.apellidos,personal.sso,personal.cuenta,personal.cargo, personal.fechai ";
			$consulta .="from $payroll,personal where personal.id='".$_SESSION['id_validar']."' and $payroll.cedula=personal.codigo and $payroll.cedula='".$_SESSION['cedula_validar']."' AND substr($payroll.concepto,1,1)!=5 AND substr($payroll.concepto,1,1)!=6 AND substr($payroll.concepto,1,1)!=4 AND concepto NOT IN(2000,2001,2002,2003,2004,2005,2006,2007,2010,2012)";
			$consulta .="order by personal.sso,$payroll.cedula,$payroll.concepto, $payroll.desconcepto";
			$consulta  =mysqli_query($enlace,$consulta) or die (mysqli_error($enlace));
		}

		//--------------------------------------------------------------------------------------------------------------------------------- REPORTE
		//--------------------------------------------------------------------------------------------------------------------------------- PRENOMINA PAYROLL
		if ($_POST['treporte']==3) {
				echo "<center>";
				echo "<font face='Trebuchet MS' color='black' size='5'>";
				echo "<br>".$_POST['empresal'];

				echo "<font face='Trebuchet MS' color='black' size='3'>";
				echo "<br>".$_POST['encabeza'];
				if ($_POST['cierre']==0) echo '<br>Reporte de NOMINA';
				else echo '<br>CIERRE AUDITADO';

				echo "<font face='Trebuchet MS' color='black' size='3' >";
				echo "<br>".$_POST['periodo'];

				echo "<font  color='black' size='6' style='font-family:Trebuchet MS; text-transform:uppercase; font-weight:bold;'>";
				echo "<br><br>".($_POST['titulo']==''?'Pago de N?mina General':$_POST['titulo']);
				echo "</center></font>";

				echo "<table border=0 cellpadding=2 cellspacing=2 align='center' style='font-family:Trebuchet MS;'>";


				$linea=0;
				$color=0;
				$tasig=0;
				$tdeduc=0;
				$bandera=0;
				$ttasig=0; $ttdeduc=0;
				while($row=mysqli_fetch_array($consulta)) {
					$ttasig+=($row['tipo']=='A'?$row['monto']:0);
					$ttdeduc+=($row['tipo']=='D'?$row['monto']:0);
					if ($bandera==0) {
						$bandera=1;
						$tcedula=$row['cedula'];
						$tcargo=$row['cargo'];
						$concar=mysqli_query($enlace,"select * from tablas where tabla='CARGO' and codigo='".$tcargo."' and id='".$_SESSION['id_validar']."' "  );
						while($row1=mysqli_fetch_array($concar))
							$tcargo=$row1['detalle'];
						$tnombre=trim($row['nombres']).' '.trim($row['apellidos']).' - '.$tcargo.($row['cuenta']==''?'':' - cuenta bancaria: '.$row['cuenta']);

						//echo "<tr><td height='50'  style='font-family:Trebuchet MS; text-transform:uppercase; font-weight:bold;'>".$tcedula."</td>";
						echo "<tr><td colspan=6 height='50' align='center'  style='font-family:Trebuchet MS; text-transform:uppercase; font-weight:bold;'>CEDULA: ".$tcedula." - ".$tnombre."</td></tr>";
						$color=0;

						echo "<tr BGcolor='#464D77' style='color:#FFF; font-family:Trebuchet MS;'>";
						echo "<th ali=center><font face='verdana' size=2>C&oacute;digo</font></th> ";
						echo "<th ali=center><font face='verdana' size=2>Detalle</font></th>";
						echo "<th ali=center><font face='verdana' size=2>Cantidad</font></th>";
						echo "<th ali=center><font face='verdana' size=2>Asignaci&oacute;n</font></th>";
						echo "<th ali=center><font face='verdana' size=2>Deducci&oacute;n</font></th>";
						echo "<th ali=center><font face='verdana' size=2>Saldo</font></th>";
						echo "</tr>";
					}
					if ($tcedula==$row['cedula']) {
						$tasig+=($row['tipo']=='A'?$row['monto']:0);
						$tdeduc+=($row['tipo']=='D'?$row['monto']:0);
						$tasigx=($row['tipo']=='A'?$row['monto']:0);
						$tdeducx=($row['tipo']=='D'?$row['monto']:0);
						$tnombre=trim($row['nombres']).',  '.trim($row['apellidos']);
						$tconcepto=$row['concepto'];
						$tdetalle=$row['desconcepto'];
						$tsaldo=$row['saldo'];
						$tcantidad=$row['cantidad'];
						$tcedula=$row['cedula'];




						if ($color) echo '<tr bgcolor="#FCFCFC" >';
						else echo '<tr bgcolor="#EEEFF3">';
						$color=!$color;

						echo "<td width='10'><font  color='black' size='3'>".$tconcepto."</font></td>";
						echo "<td width='300'><font  color='black' size='3'>".$tdetalle."</font></td>";
						echo "<td width='120' align='right'><font  color='black' size='2'><b>".number_format($tcantidad,2)."</font></b></td>";
						echo "<td width='120' align='right'><font  color='#84BE45' size='3'>".number_format($tasigx,2)."</font></td>";
						echo "<td width='120' align='right'><font  color='#E47470' size='3'>".number_format($tdeducx,2)."</font></td>";
						echo "<td width='120' align='right'><font  color='black' size='3'>".($tconcepto=="DEDU"?number_format($tsaldo,2):"")."</font></td>";
						echo "</tr>";
					}
					else {

						echo "<tr BGcolor='#464D77' style='color:#FFF; font-family:Trebuchet MS;' >";
						echo "<th ali=center colspan=3><font  size=3>Sub-Totales Bs.</font></th> ";
						echo "<td width='120' align='right'><font   size='3'><b>".number_format($tasig,2)."</font></b></td>";
						echo "<td width='120' align='right'><font   size='3'>".number_format($tdeduc,2)."</font></td>";
						echo "<td width='120' align='right'><font   size='3'>".number_format($tasig-$tdeduc,2)."</font></td>";
						echo "</tr>";

						$tasig=($row['tipo']=='A'?$row['monto']:0);
						$tdeduc=($row['tipo']=='D'?$row['monto']:0);
						$tasigx=($row['tipo']=='A'?$row['monto']:0);
						$tdeducx=($row['tipo']=='D'?$row['monto']:0);
						$tconcepto=$row['concepto'];
						$tdetalle=$row['desconcepto'];
						$tcantidad=$row['cantidad'];
						$tcedula=$row['cedula'];
						$tcargo=$row['cargo'];
						$concar=mysqli_query($enlace,"select * from tablas where tabla='CARGO' and codigo='".$tcargo."' and id='".$_SESSION['id_validar']."' "  );
						while($row1=mysqli_fetch_array($concar))
							$tcargo=$row1['detalle'];
						$tnombre=trim($row['sso'])." ".trim($row['nombres']).',  '.trim($row['apellidos']).' - '.$tcargo.($row['cuenta']==''?'':' - cuenta bancaria: '.$row['cuenta']);

						echo "<tr><td colspan=1>".$tcedula."</td>";
						echo "<td colspan=5>".$tnombre."</td></tr>";
						$color=0;

						if ($color) echo '<tr bgcolor="#FCFCFC" >';
						else echo '<tr bgcolor="#EEEFF3">';
						$color=!$color;

						echo "<td width='10'><font  color='black' size='3'>".$tconcepto."</font></td>";
						echo "<td width='300'><font  color='black' size='3'>".$tdetalle."</font></td>";
						echo "<td width='120' align='right'><font  color='black' size='2'><b>".number_format($tcantidad,2)."</font></b></td>";
						echo "<td width='120' align='right'><font  color='#84BE45' size='3'>".number_format($tasigx,2)."</font></td>";
						echo "<td width='120' align='right'><font  color='#E47470' size='3'>".number_format($tdeducx,2)."</font></td>";
						echo "<td width='120' align='right'><font  color='black' size='3'>".($tconcepto=="DEDU"?number_format($tsaldo,2):"")."</font></td>";
						echo "</tr>";
					}
				}
				echo "<tr BGcolor='#464D77' style='color:#FFF; font-family:Trebuchet MS;' >";
				echo "<th ali=center colspan=3><font  size=3>Totales Bs.</font></th> ";
				echo "<td width='120' align='right'><font  size='3'><b>".number_format($ttasig,2)."</font></b></td>";
				echo "<td width='120' align='right'><font   size='3'>".number_format($ttdeduc,2)."</font></td>";
				echo "<td width='120' align='right'><font  size='3'>".number_format($ttasig-$ttdeduc,2)."</font></td>";
				echo "</tr>";

		} // fin de reporte 3 payroll

		//--------------------------------------------------------------------------------------------------------------------------------- REPORTE
		//--------------------------------------------------------------------------------------------------------------------------------- RECIBOS DE PAGOS
		if ($_POST['treporte']==4) {
			require('fpdf.php');
			class PDF extends FPDF {
			function Header()
			{
			}
			function Footer()
			{
			$this->SetY(-20);
			}
			}


			if ($_POST['forma']==1)	{
					//Cuerpo //////////////////////////////////////////////
						$pdf=new PDF($orientation='P',$unit='mm',$format='letter');
						$pdf->AddPage();
						$pdf->AliasNbPages();
						$pdf->SetFont('helvetica','',10);
						$linea=0;
						$color=0;
						$tasig=0;
						$tdeduc=0;
						$bandera=0;$salto=1;
						$ttasig=0; $ttdeduc=0;
						$tlinea=0;
						while($row=mysqli_fetch_array($consulta)) {
							$ttasig+=($row['tipo']=='A'?$row['monto']:0);
							$ttdeduc+=($row['tipo']=='D'?$row['monto']:0);
							if ($bandera==0) {
								//===================================== inicio de cabecera
								$pdf->SetFont('arial','',12);
								$pdf->Cell(100,5,$_SESSION['empresa_validar'],0,0,'L');
								$pdf->SetFont('arial','',10);
								$pdf->Cell(100,5,'Fecha: '.hoy(),0,1,'R');
								$pdf->Cell(100,5,$_POST['encabeza'],0,0,'L');
								$pdf->Cell(100,5,'Nómina: '.$_POST['empresa'],0,1,'R');
								$pdf->SetFont('arial','B',12);
								$pdf->Cell(0,5,($_POST['titulo']==''?'Recibo de Pago':$_POST['titulo']),0,1,'C');
								$pdf->Cell(0,5,$_POST['subtitulo'],0,1,'C');
								if ($_POST['gerencia']!="") $pdf->Cell(130,5,'Gerencia: '.$_POST['gerencia'],0,1,'L');
								if ($_POST['division']!="") $pdf->Cell(130,5,'Divisi?n: '.$_POST['division'],0,1,'L');
								if ($_POST['departamento']!="") $pdf->Cell(130,5,'Departamento: '.$_POST['departamento'],0,1,'L');
								//===================================== final de cabecera
								$bandera=1;
								$tcedula=$row['cedula'];
								$tfechai=$row['fechai'];
								$tcargo=$row['cargo'];
								$concar=mysqli_query($enlace,"select * from tablas where tabla='CARGO' and codigo='".$tcargo."' and id='".$_SESSION['id_validar']."' "  );
								while($row1=mysqli_fetch_array($concar)) $tcargo=$row1['detalle'];
								$tnombre=trim($row['nombres']).',  '.trim($row['apellidos']).' - '.$tcargo;
								$pdf->ln(5);
								$pdf->SetFont('helvetica','',12);
								$pdf->cell(1);$pdf->cell(1,5,"[".$tcedula."] - ".$tnombre,0,1,'');
								$pdf->SetFont('helvetica','',10);
								$header=array('Cod.','Detalle','Cantidad','Asignación(BS)','Deducción(BS)','Saldo(BS)');
								$w=array(10,70,20,30,30,30);
								for($i=0;$i<count($header);$i++) $pdf->Cell($w[$i],4,$header[$i],1,0,'C',0);
								$pdf->Ln();
								$pdf->SetFont('');
							}
							if ($tcedula==$row['cedula']) {
								$tasig+=($row['tipo']=='A'?$row['monto']:0);
								$tdeduc+=($row['tipo']=='D'?$row['monto']:0);
								$tasigx=($row['tipo']=='A'?$row['monto']:0);
								$tdeducx=($row['tipo']=='D'?$row['monto']:0);
								$tnombre=trim($row['nombres']).',  '.trim($row['apellidos']);
								$tconcepto=$row['concepto'];
								$tdetalle=$row['desconcepto'];
								$tfechai=$row['fechai'];
								$tsaldo=$row['saldo'];
								$tcantidad=$row['cantidad'];
								$tcedula=$row['cedula'];
								$pdf->cell(10,5,$tconcepto,0,0,'',$color);
								$pdf->cell(60,5,$tdetalle,0,0,'',$color);
								$pdf->SetFont('helvetica','',12);
								$pdf->cell(30,5,number_format($tcantidad,2),0,0,'R',$color);
								$pdf->cell(30,5,number_format($tasigx,2),0,0,'R',$color);
								$pdf->cell(30,5,number_format($tdeducx,2),0,0,'R',$color);
								$pdf->cell(30,5,($tconcepto=="DEDU"?number_format($tsaldo,2):""),0,1,'R',$color);
								$pdf->SetFont('helvetica','',10);
								$tlinea++;
							} else {
								$pdf->Cell(190,0,'',1,1,'C'); $pdf->ln(1);
								$pdf->cell(100,5,'Sub-Total',0,0,'R');
								$pdf->SetFont('helvetica','',12);
								$pdf->cell(30,5,number_format($tasig,2),0,0,'R');
								$pdf->cell(30,5,number_format($tdeduc,2),0,0,'R');
								$pdf->cell(30,5,number_format($tasig-$tdeduc,2),0,1,'R');
								$pdf->Cell(190,0,'',1,1,'C'); $pdf->ln(1);
								$pdf->SetFont('helvetica','',10);
								$pdf->cell(100,5,'Total a Cobrar:',0,0,'R');
								$pdf->SetFont('helvetica','',12);
								$pdf->cell(90,5,number_format($tasig-$tdeduc,2) ,0,1,'R');
								$pdf->SetFont('helvetica','',10);
								$pdf->ln(10);
								$pdf->cell(190,5,'-----------------------------------------------',0,0,'R');
								$pdf->ln(3);
								$pdf->cell(185,5,'Firma / Cedula de Recibido',0,0,'R');
								$pdf->ln(7);
								$pdf->SetFont('helvetica','',12);
								$pdf->Cell(0,0,'Nomina: '.$_POST['nomina']." - ".'Periodo: '.$_POST['periodo'],0,1,'L');$pdf->ln(5);
								$pdf->SetFont('helvetica','',10);
								$pdf->Cell(0,0,'Fecha de Ingreso: '.$tfechai,0,1,'L');

								if ($salto==1) {
									for ($t=0; $t<=10-$tlinea;$t++) $pdf->ln(5);
									$tlinea=1;
									$pdf->SetFont('helvetica','',12);
									$pdf->Ln(2);
									//===================================== inicio de cabecera
									$pdf->SetFont('arial','',12);
									$pdf->Cell(100,5,$_POST['empresal'],0,0,'L');
									$pdf->SetFont('arial','',10);
									$pdf->Cell(100,5,'Fecha: '.hoy(),0,1,'R');
									$pdf->Cell(100,5,$_POST['encabeza'],0,0,'L');
									$pdf->Cell(100,5,'N?mina: '.$_POST['empresa'],0,1,'R');
									$pdf->SetFont('arial','B',12);
									$pdf->Cell(0,5,($_POST['titulo']==''?'Recibos de Pagos':$_POST['titulo']),0,1,'C');
									$pdf->Cell(0,5,$_POST['subtitulo'],0,1,'C');
									if ($_POST['gerencia']!="") $pdf->Cell(130,5,'Gerencia: '.$_POST['gerencia'],0,1,'L');
									if ($_POST['division']!="") $pdf->Cell(130,5,'Divisi?n: '.$_POST['division'],0,1,'L');
									if ($_POST['departamento']!="") $pdf->Cell(130,5,'Departamento: '.$_POST['departamento'],0,1,'L');
									//===================================== final de cabecera
									$salto=0;
								} else {
									$pdf->AddPage();
									//===================================== inicio de cabecera
									$pdf->SetFont('arial','',12);
									$pdf->Cell(100,5,$_POST['empresal'],0,0,'L');
									$pdf->SetFont('arial','',10);
									$pdf->Cell(100,5,'Fecha: '.hoy(),0,1,'R');
									$pdf->Cell(100,5,$_POST['encabeza'],0,0,'L');
									$pdf->Cell(100,5,'N?mina: '.$_POST['empresa'],0,1,'R');
									$pdf->SetFont('arial','B',12);
									$pdf->Cell(0,5,($_POST['titulo']==''?'Recibos de Pagos':$_POST['titulo']),0,1,'C');
									$pdf->Cell(0,5,$_POST['subtitulo'],0,1,'C');
									if ($_POST['gerencia']!="") $pdf->Cell(130,5,'Gerencia: '.$_POST['gerencia'],0,1,'L');
									if ($_POST['division']!="") $pdf->Cell(130,5,'Divisi?n: '.$_POST['division'],0,1,'L');
									if ($_POST['departamento']!="") $pdf->Cell(130,5,'Departamento: '.$_POST['departamento'],0,1,'L');
									//===================================== final de cabecera
									$tlinea=1;
									$salto=1;
								}
								$tasig=($row['tipo']=='A'?$row['monto']:0);
								$tdeduc=($row['tipo']=='D'?$row['monto']:0);
								$tasigx=($row['tipo']=='A'?$row['monto']:0);
								$tdeducx=($row['tipo']=='D'?$row['monto']:0);
								$tconcepto=$row['concepto'];
								$tdetalle=$row['desconcepto'];
								$tcantidad=$row['cantidad'];
								$tfechai=$row['fechai'];
								$tcedula=$row['cedula'];
								$tcargo=$row['cargo'];
								$concar=mysqli_query($enlace,"select * from tablas where tabla='CARGO' and codigo='".$tcargo."' and id='".$_SESSION['id_validar']."' "  );
								while($row1=mysqli_fetch_array($concar))
									$tcargo=$row1['detalle'];
								$tnombre=trim($row['sso'])." ".trim($row['nombres']).',  '.trim($row['apellidos']).' - '.$tcargo;
								$pdf->ln(5);
								$pdf->SetFont('helvetica','',12);
								$pdf->cell(1);$pdf->cell(1,5,"[".$tcedula."] - ".$tnombre,0,1,'');
								$pdf->SetFont('helvetica','',10);
								for($i=0;$i<count($header);$i++)
									$pdf->Cell($w[$i],4,$header[$i],1,0,'C',0);
								$pdf->Ln(5);
								$pdf->cell(10,5,$tconcepto,0,0,'',$color);
								$pdf->cell(60,5,$tdetalle,0,0,'',$color);

								$pdf->SetFont('helvetica','',12);
								$pdf->cell(30,5,number_format($tcantidad,2),0,0,'R',$color);
								$pdf->cell(30,5,number_format($tasigx,2),0,0,'R',$color);
								$pdf->cell(30,5,number_format($tdeducx,2),0,0,'R',$color);
								$pdf->cell(30,5,($tconcepto=="DEDU"?number_format($tsaldo,2):""),0,1,'R',$color);
								$pdf->SetFont('helvetica','',10);
							}
						}
						$pdf->Cell(190,0,'',1,1,'C'); $pdf->ln(1);
						$pdf->cell(100,5,'Sub-Total',0,0,'R');
						$pdf->SetFont('helvetica','',12);
						$pdf->cell(30,5,number_format($tasig,2),0,0,'R');
						$pdf->cell(30,5,number_format($tdeduc,2),0,0,'R');
						$pdf->cell(30,5,number_format($tasig-$tdeduc,2),0,1,'R');
						$pdf->SetFont('helvetica','',10);
						$pdf->Cell(190,0,'',1,1,'C');
						$pdf->cell(100,5,'Total a Cobrar:',0,0,'R');
						$pdf->SetFont('helvetica','',12);
						$pdf->cell(90,5,number_format($tasig-$tdeduc,2) ,0,1,'R');
						$pdf->SetFont('helvetica','',10);
						$pdf->ln(10);
						$pdf->cell(190,5,'-----------------------------------------------',0,0,'R');
						$pdf->ln(3);
						$pdf->cell(185,5,'Firma / Cedula de Recibido',0,0,'R');
						$pdf->ln(7);
						$pdf->SetFont('helvetica','',12);
						$pdf->Cell(0,0,'Nomina: '.$_POST['nomina']." - ".'Periodo: '.$_POST['periodo'],0,1,'L');$pdf->ln(5);
						$pdf->SetFont('helvetica','',10);
						$pdf->Cell(0,0,'Fecha de Ingreso: '.$tfechai,0,1,'L');

			} else {

					//Cuerpo //////////////////////////////////////////////
					$pdf=new PDF($orientation='L',$unit='mm',$format='letter');
					$pdf->AddPage();
					$pdf->AliasNbPages();
					$pdf->SetFont('Arial','',8);
					$pdf->SetFillColor(224,235,255);
					$pdf->SetTextColor(0);
					//$consulta=mysqli_query($enlace,"select * from personal,$payroll where $payroll.cedula=personal.codigo order by personal.".$_POST['ordena'].", $payroll.concepto , $payroll.desconcepto"  );
					$linea=0;
					$color=0;
					$tasig=0;
					$tdeduc=0;
					$bandera=0;
					$ttasig=0; $ttdeduc=0;
					$lineaxrecibo=0;
					$partedelrecibo=0;
					while($row=mysqli_fetch_array($consulta)) {
						$ttasig+=($row['tipo']=='A'?$row['monto']:0);
						$ttdeduc+=($row['tipo']=='D'?$row['monto']:0);
						if ($bandera==0) {

							//===================================== inicio de cabecera linea 1
							$pdf->SetFont('arial','',12);
							$pdf->Cell(60,5,$_POST['empresal'],0,0,'L');
							$pdf->SetFont('arial','',10);
							$pdf->Cell(60,5,'Fecha: '.hoy(),0,0,'R');
							$pdf->Cell(20);
							$pdf->SetFont('arial','',12);
							$pdf->Cell(60,5,$_POST['empresal'],0,0,'L');
							$pdf->SetFont('arial','',10);
							$pdf->Cell(60,5,'Fecha: '.hoy(),0,1,'R');

							//===================================== inicio de cabecera linea 2
							$pdf->SetFont('arial','',12);
							$pdf->Cell(60,5,$_POST['encabeza'],0,0,'L');
							$pdf->SetFont('arial','',10);
							$pdf->Cell(60,5,'N?mina: '.$_POST['empresa'],0,0,'R');
							$pdf->Cell(20);
							$pdf->SetFont('arial','',12);
							$pdf->Cell(60,5,$_POST['encabeza'],0,0,'L');
							$pdf->SetFont('arial','',10);
							$pdf->Cell(60,5,'N?mina: '.$_POST['empresa'],0,1,'R');

							//===================================== inicio de cabecera linea 3
							$pdf->SetFont('arial','B',12);
							$pdf->Cell(130,5,($_POST['titulo']==''?'Recibos de Pagos':$_POST['titulo']),0,0,'C');
							$pdf->Cell(130,5,($_POST['titulo']==''?'Recibos de Pagos':$_POST['titulo']),0,1,'C');
							$pdf->Cell(130,5,$_POST['subtitulo'],0,0,'C');
							$pdf->Cell(130,5,$_POST['subtitulo'],0,1,'C');

							//===================================== inicio de cabecera linea 4
							if ($_POST['gerencia']!="") {
								$pdf->Cell(130,5,'Gerencia: '.$_POST['gerencia'],0,0,'L');
								$pdf->Cell(130,5,'Gerencia: '.$_POST['gerencia'],0,1,'L');
							}
							if ($_POST['division']!="") {
								$pdf->Cell(130,5,'Divisi?n: '.$_POST['division'],0,0,'L');
								$pdf->Cell(130,5,'Divisi?n: '.$_POST['division'],0,1,'L');
							}
							if ($_POST['departamento']!="") {
								$pdf->Cell(130,5,'Departamento: '.$_POST['departamento'],0,0,'L');
								$pdf->Cell(130,5,'Departamento: '.$_POST['departamento'],0,1,'L');
							}
							//===================================== final de cabecera

							$bandera=1;
							$tcedula=$row['cedula'];
							$tcargo=$row['cargo'];
							$concar=mysqli_query($enlace,"select * from tablas where tabla='CARGO' and codigo='".$tcargo."' and id='".$_SESSION['id_validar']."' "  );
							while($row1=mysqli_fetch_array($concar))
								$tcargo=$row1['detalle'];
							$tnombre=trim($row['sso'])." ".trim($row['nombres']).',  '.trim($row['apellidos']).' - '.$tcargo.($row['cuenta']==''?'':' - cuenta bancaria: '.$row['cuenta']);

							//===================================== inicio de cabecera linea 5
							$pdf->SetFont('Arial','',10);
							$pdf->ln(5);
							$pdf->cell(130,5,"[".$tcedula."] - ".$tnombre,0,0,'');
							$pdf->Cell(10);
							$pdf->cell(130,5,"[".$tcedula."] - ".$tnombre,0,1,'');

							$pdf->SetFont('Arial','',8);
							$header=array('Cod.','Descripci?n','Cantidad','Asignaci?n','Deducci?n','Saldo');
							$pdf->SetDrawColor(128,0,0);
							$pdf->SetLineWidth(.3);
							$pdf->SetFont('','');
							$pdf->SetFont('arial','B',7);
							$w=array(10,50,15,15,15,15);
							for($i=0;$i<count($header);$i++)
								$pdf->Cell($w[$i],4,$header[$i],1,0,'C',1);
							$pdf->Cell(20);
							for($i=0;$i<count($header);$i++)
								$pdf->Cell($w[$i],4,$header[$i],1,0,'C',1);
							$pdf->Ln();
							$pdf->SetFont('');
						}

						if ($tcedula==$row['cedula']) {
							$tasig+=($row['tipo']=='A'?$row['monto']:0);
							$tdeduc+=($row['tipo']=='D'?$row['monto']:0);
							$tasigx=($row['tipo']=='A'?$row['monto']:0);
							$tdeducx=($row['tipo']=='D'?$row['monto']:0);
							$tnombre=trim($row['nombres']).',  '.trim($row['apellidos']);
							$tconcepto=$row['concepto'];
							$tdetalle=$row['desconcepto'];
							$tsaldo=$row['saldo'];
							$tcantidad=$row['cantidad'];
							$tcedula=$row['cedula'];
							$pdf->cell(10,5,$tconcepto,0,0,'',$color);
							$pdf->cell(50,5,$tdetalle,0,0,'',$color);
							$pdf->cell(15,5,number_format($tcantidad,2),0,0,'R',$color);
							$pdf->cell(15,5,number_format($tasigx,2),0,0,'R',$color);
							$pdf->cell(15,5,number_format($tdeducx,2),0,0,'R',$color);
							$pdf->cell(15,5,($tconcepto=="DEDU"?number_format($tsaldo,2):""),0,0,'R',$color);
							$pdf->Cell(20);
							$pdf->cell(10,5,$tconcepto,0,0,'',$color);
							$pdf->cell(50,5,$tdetalle,0,0,'',$color);
							$pdf->cell(15,5,number_format($tcantidad,2),0,0,'R',$color);
							$pdf->cell(15,5,number_format($tasigx,2),0,0,'R',$color);
							$pdf->cell(15,5,number_format($tdeducx,2),0,0,'R',$color);
							$pdf->cell(15,5,($tconcepto=="DEDU"?number_format($tsaldo,2):""),0,1,'R',$color);
						}
						else {
							$pdf->Cell(120,0,'',1,0,'C');
							$pdf->Cell(20);
							$pdf->Cell(120,0,'',1,1,'C'); $pdf->ln(1);

							$pdf->cell(75,5,'Sub-Total',0,0,'R');
							$pdf->cell(15,5,number_format($tasig,2),0,0,'R');
							$pdf->cell(15,5,number_format($tdeduc,2),0,0,'R');
							$pdf->cell(15,5,number_format($tasig-$tdeduc,2),0,0,'R');
							$pdf->Cell(20);
							$pdf->cell(75,5,'Sub-Total',0,0,'R');
							$pdf->cell(15,5,number_format($tasig,2),0,0,'R');
							$pdf->cell(15,5,number_format($tdeduc,2),0,0,'R');
							$pdf->cell(15,5,number_format($tasig-$tdeduc,2),0,1,'R');

							$pdf->Cell(120,0,'',1,0,'C');
							$pdf->Cell(20);
							$pdf->Cell(120,0,'',1,1,'C'); $pdf->ln(1);

							$pdf->cell(75,5,'Total a Cobrar:',0,0,'R');
							$pdf->cell(45,5,number_format($tasig-$tdeduc,2) ,0,0,'R');
							$pdf->Cell(20);
							$pdf->cell(75,5,'Total a Cobrar:',0,0,'R');
							$pdf->cell(45,5,number_format($tasig-$tdeduc,2) ,0,1,'R');

							$pdf->ln(10);
							$pdf->cell(120,5,'-----------------------------------------------',0,0,'R');
							$pdf->Cell(20);
							$pdf->cell(120,5,'-----------------------------------------------',0,1,'R');
							$pdf->cell(120,5,'Firma / Cedula de Recibido',0,0,'R');
							$pdf->Cell(20);
							$pdf->cell(120,5,'Firma / Cedula de Recibido',0,1,'R');

							$pdf->Cell(130,5,'Nomina: '.$_POST['nomina']." - ".'Periodo: '.$_POST['periodo'],0,0,'L');
							$pdf->Cell(10);
							$pdf->Cell(130,5,'Nomina: '.$_POST['nomina']." - ".'Periodo: '.$_POST['periodo'],0,1,'L');

							$pdf->AddPage();

							$tasig=($row['tipo']=='A'?$row['monto']:0);
							$tdeduc=($row['tipo']=='D'?$row['monto']:0);
							$tasigx=($row['tipo']=='A'?$row['monto']:0);
							$tdeducx=($row['tipo']=='D'?$row['monto']:0);
							$tconcepto=$row['concepto'];
							$tdetalle=$row['desconcepto'];
							$tcantidad=$row['cantidad'];
							$tcedula=$row['cedula'];
							$tcargo=$row['cargo'];

							//===================================== inicio de cabecera linea 1
							$pdf->SetFont('arial','',12);
							$pdf->Cell(60,5,$_POST['empresal'],0,0,'L');
							$pdf->SetFont('arial','',10);
							$pdf->Cell(60,5,'Fecha: '.hoy(),0,0,'R');
							$pdf->Cell(20);
							$pdf->SetFont('arial','',12);
							$pdf->Cell(60,5,$_POST['empresal'],0,0,'L');
							$pdf->SetFont('arial','',10);
							$pdf->Cell(60,5,'Fecha: '.hoy(),0,1,'R');

							//===================================== inicio de cabecera linea 2
							$pdf->SetFont('arial','',12);
							$pdf->Cell(60,5,$_POST['encabeza'],0,0,'L');
							$pdf->SetFont('arial','',10);
							$pdf->Cell(60,5,'N?mina: '.$_POST['empresa'],0,0,'R');
							$pdf->Cell(20);
							$pdf->SetFont('arial','',12);
							$pdf->Cell(60,5,$_POST['encabeza'],0,0,'L');
							$pdf->SetFont('arial','',10);
							$pdf->Cell(60,5,'N?mina: '.$_POST['empresa'],0,1,'R');

							//===================================== inicio de cabecera linea 3
							$pdf->SetFont('arial','B',12);
							$pdf->Cell(130,5,($_POST['titulo']==''?'Recibos de Pagos':$_POST['titulo']),0,0,'C');
							$pdf->Cell(130,5,($_POST['titulo']==''?'Recibos de Pagos':$_POST['titulo']),0,1,'C');
							$pdf->Cell(130,5,$_POST['subtitulo'],0,0,'C');
							$pdf->Cell(130,5,$_POST['subtitulo'],0,1,'C');

							//===================================== inicio de cabecera linea 4
							if ($_POST['gerencia']!="") {
								$pdf->Cell(130,5,'Gerencia: '.$_POST['gerencia'],0,0,'L');
								$pdf->Cell(130,5,'Gerencia: '.$_POST['gerencia'],0,1,'L');
							}
							if ($_POST['division']!="") {
								$pdf->Cell(130,5,'Divisi?n: '.$_POST['division'],0,0,'L');
								$pdf->Cell(130,5,'Divisi?n: '.$_POST['division'],0,1,'L');
							}
							if ($_POST['departamento']!="") {
								$pdf->Cell(130,5,'Departamento: '.$_POST['departamento'],0,0,'L');
								$pdf->Cell(130,5,'Departamento: '.$_POST['departamento'],0,1,'L');
							}
							//===================================== final de cabecera

							$bandera=1;
							$tcedula=$row['cedula'];
							$tcargo=$row['cargo'];
							$concar=mysqli_query($enlace,"select * from tablas where tabla='CARGO' and codigo='".$tcargo."' and id='".$_SESSION['id_validar']."' "  );
							while($row1=mysqli_fetch_array($concar))
								$tcargo=$row1['detalle'];
							$tnombre=trim($row['sso'])." ".trim($row['nombres']).',  '.trim($row['apellidos']).' - '.$tcargo.($row['cuenta']==''?'':' - cuenta bancaria: '.$row['cuenta']);

							//===================================== inicio de cabecera linea 5
							$pdf->SetFont('Arial','',10);
							$pdf->ln(5);
							$pdf->cell(130,5,"[".$tcedula."] - ".$tnombre,0,0,'');
							$pdf->Cell(10);
							$pdf->cell(130,5,"[".$tcedula."] - ".$tnombre,0,1,'');

							$pdf->SetFont('Arial','',8);
							$pdf->SetDrawColor(128,0,0);
							$pdf->SetLineWidth(.3);
							$pdf->SetFont('','');
							$pdf->SetFont('arial','B',7);
							$w=array(10,50,15,15,15,15);
							for($i=0;$i<count($header);$i++)
								$pdf->Cell($w[$i],4,$header[$i],1,0,'C',1);
							$pdf->Cell(20);
							for($i=0;$i<count($header);$i++)
								$pdf->Cell($w[$i],4,$header[$i],1,0,'C',1);
							$pdf->Ln();
							$pdf->SetFont('');
							$pdf->cell(10,5,$tconcepto,0,0,'',$color);
							$pdf->cell(50,5,$tdetalle,0,0,'',$color);
							$pdf->cell(15,5,number_format($tcantidad,2),0,0,'R',$color);
							$pdf->cell(15,5,number_format($tasigx,2),0,0,'R',$color);
							$pdf->cell(15,5,number_format($tdeducx,2),0,0,'R',$color);
							$pdf->cell(15,5,($tconcepto=="DEDU"?number_format($tsaldo,2):""),0,0,'R',$color);
							$pdf->Cell(20);
							$pdf->cell(10,5,$tconcepto,0,0,'',$color);
							$pdf->cell(50,5,$tdetalle,0,0,'',$color);
							$pdf->cell(15,5,number_format($tcantidad,2),0,0,'R',$color);
							$pdf->cell(15,5,number_format($tasigx,2),0,0,'R',$color);
							$pdf->cell(15,5,number_format($tdeducx,2),0,0,'R',$color);
							$pdf->cell(15,5,($tconcepto=="DEDU"?number_format($tsaldo,2):""),0,1,'R',$color);
						}
						$color=!$color;
					}
					$pdf->Cell(120,0,'',1,0,'C');
					$pdf->Cell(20);
					$pdf->Cell(120,0,'',1,1,'C'); $pdf->ln(1);

					$pdf->cell(75,5,'Sub-Total',0,0,'R');
					$pdf->cell(15,5,number_format($tasig,2),0,0,'R');
					$pdf->cell(15,5,number_format($tdeduc,2),0,0,'R');
					$pdf->cell(15,5,number_format($tasig-$tdeduc,2),0,0,'R');
					$pdf->Cell(20);
					$pdf->cell(75,5,'Sub-Total',0,0,'R');
					$pdf->cell(15,5,number_format($tasig,2),0,0,'R');
					$pdf->cell(15,5,number_format($tdeduc,2),0,0,'R');
					$pdf->cell(15,5,number_format($tasig-$tdeduc,2),0,1,'R');

					$pdf->Cell(120,0,'',1,0,'C');
					$pdf->Cell(20);
					$pdf->Cell(120,0,'',1,1,'C'); $pdf->ln(1);
					$pdf->cell(75,5,'Total a Cobrar:',0,0,'R');
					$pdf->cell(45,5,number_format($tasig-$tdeduc,2) ,0,0,'R');
					$pdf->Cell(20);
					$pdf->cell(75,5,'Total a Cobrar:',0,0,'R');
					$pdf->cell(45,5,number_format($tasig-$tdeduc,2) ,0,1,'R');

					$pdf->ln(10);
					$pdf->cell(120,5,'-----------------------------------------------',0,0,'R');
					$pdf->Cell(20);
					$pdf->cell(120,5,'-----------------------------------------------',0,1,'R');
					$pdf->cell(120,5,'Firma / Cedula de Recibido',0,0,'R');
					$pdf->Cell(20);
					$pdf->cell(120,5,'Firma / Cedula de Recibido',0,1,'R');

					$pdf->Cell(130,5,'Nomina: '.$_POST['nomina']." - ".'Periodo: '.$_POST['periodo'],0,0,'L');
					$pdf->Cell(10);
					$pdf->Cell(130,5,'Nomina: '.$_POST['nomina']." - ".'Periodo: '.$_POST['periodo'],0,1,'L');
			}
			$pdf->Output();

		} // fin de reporte 4 recibos de pagos


} // fin del else de POST
?>
