<?php 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();
$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);
	$per=mostrar_fecha($row['desde']).' al '.mostrar_fecha($row['hasta'])." (Nom:".$row['nomina'].")";
} else $per=''; 
if ($_POST['imprime']==1 ) {
///////////////////////////////////////////////// IMPRIMIR REGISTROS ////////////////////////////////////////////////////////////////
	$consulta=mysqli_query($enlace,"select * from empresa where id='".$_SESSION['id_validar']."' ");
	if (mysqli_num_rows($consulta)!=0){
			$row=mysqli_fetch_array($consulta);
			$_POST['pie']=$row['pie'];
			$_POST['encabeza']=$row['encabeza'];
			$_POST['empresa']=(substr($_POST['nomina'],6,100)==""?$_SESSION['empresa_validar']:substr($_POST['nomina'],6,100)) ;
	}   
	require('fpdf.php');
	class PDF extends FPDF {
	function Header()
	{
	    $this->Image($_SESSION['logo_validar'],10,10,20,20);
	    $this->SetFont('arial','',12);
		$this->Ln(2);
	    $this->Cell(0,0,$_POST['empresa'],0,1,'R'); $this->Ln(5);
		$this->Cell(0,0,$_POST['encabeza'],0,1,'R');$this->Ln(5);
		$this->SetFont('arial','',10);
	    $this->Cell(140);$this->Cell(40,0,'Fecha: '.hoy(),0,1,'L'); $this->Ln(5);
	    $this->Cell(140);$this->Cell(40,0,'Usuario: '.$_SESSION['nombe_validar'],0,1,'L'); $this->Ln(5);

		$this->Ln(10);
		$this->SetFont('arial','B',14); 
		$this->Cell(0,0,'Reporte General de Nóminas',0,1,'C');$this->Ln(5);
	    $header=array('Item','Código','Detalle','Tipo');
	    $this->SetFillColor(255,0,0);
	    $this->SetTextColor(255);
	    $this->SetDrawColor(128,0,0);
	    $this->SetLineWidth(.3);
	    $this->SetFont('','');
	    //Cabecera
	    $w=array(15,25,100,40);
		for($i=0;$i<count($header);$i++)
	        $this->Cell($w[$i],5,$header[$i],1,0,'C',1);
	    $this->Ln();
	    //Restauración de colores y fuentes
	    $this->SetFillColor(224,235,255);
	    $this->SetTextColor(0);
	    $this->SetFont('');
	}
	
//Pie de página ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	function Footer()
	{
	$this->SetY(-15);
	$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,'Página '.$this->PageNo().'/{nb}',0,0,'C');
	}
}
//Cuerpo ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////	
	$pdf=new PDF();
	$pdf->AddPage();
	$pdf->AliasNbPages();
	$pdf->SetFont('Arial','',10);
	$pdf->SetFillColor(224,235,255);
	$pdf->SetTextColor(0);
	$consulta=mysqli_query($enlace,'select * from nomina');
	$linea=0;
	$color=0;
	while($row=mysqli_fetch_array($consulta)) {
		$linea++;
		$pdf->cell(15,5,$linea,0,0,'C',$color);
		$pdf->cell(25,5,$row['codigo'],0,0,'C',$color);
		$pdf->cell(100,5,$row['detalle'],0,0,'',$color);
		if ($row['tipo']==1) $ntipo='Semanal';
		if ($row['tipo']==2) $ntipo='Quincenal';
		if ($row['tipo']==3) $ntipo='Mensual';
		$pdf->cell(40,5,$ntipo,0,1,'',$color);
		$color=!$color;
	}
	$pdf->Output();
}else {
?>
<html>
	<head><script language='javascript' src='fscript.js'></script>
	<link rel='stylesheet' type='text/css' href='styles.css' />
	</head>
	<body>	
		<?
		menu();
		?>
		<br><table border=0 cellpadding=0 cellspacing=0 width='100%'>
			<tr>
				<td  width='100%' align='left'><font size=2 color='black' face='verdana'>Empresa: <b><?echo $_SESSION['empresa_validar'];?></b>  - Per&iacute;odo: <b><font color=red><?echo $per;?></font></b>  -  Usuario: <b><?echo $_SESSION['nombre_validar'];?></b></font></td> 
			</tr>
		</table>	
<?		
echo "<b>Opciones:</b> &nbsp;&nbsp;<img border = 0 src='images/b_elimina.png' alt='Elimina'>:Eliminar Usuario	
&nbsp;&nbsp;<img border = 0 src='images/b_edit.png' alt='Modificar'>:Modificar Usuario
</font><hr>";
echo "<h2>Actualizaci&oacute;n de N&oacute;mina</h2>";

$accion=$_REQUEST["accion"];
$codigo=$_REQUEST["codigo"];
$periodo=$_REQUEST["periodo"];
$detalle=$_REQUEST["detalle"];
$cuenta=$_REQUEST["cuenta"];
$nomina=$_REQUEST["nomina"];
echo "<font face='verdana' size=2>";

///////////////////////////////////////////////// INICIO DEL PROGRAMA ////////////////////////////////////////////////////////////////
	$entra=0;
///////////////////////////////////////////////// AGREGAR REGISTROS  ////////////////////////////////////////////////////////////////
	if ($_POST['graba']==1 ) {
		$consulta="Select * from nomina where id='".$_SESSION['id_validar']."' and codigo = '".$_POST['codigo']."'" ;
		$resultado = mysqli_query($enlace,$consulta) or die('Verificaci&oacute; del C&oacute;digo fall&oacute;: ' . mysqli_error($enlace));
		$num_rows = mysqli_num_rows($resultado);
		if ($num_rows == 0) {
			$consulta="insert into nomina (id,codigo,detalle,periodo,cuenta,tipo) values ('".$_SESSION['id_validar']."','".$_POST['codigo']."',ucase('".$_POST['detalle']."'),'".$_POST['periodo']."','".$_POST['cuenta']."','".$_POST['tipo']."')";
			$resultado = mysqli_query($enlace,$consulta) or die('Verificaci&oacute; del C&oacute;digo fall&oacute;: ' . mysqli_error($enlace));
		} else $entra=1;
	}
    if ($entra) echo "<font face='Verdana, Arial, Helvetica, sans-serif' color ='#FF0000'size=3> (Registro Existe)</font>";
///////////////////////////////////////////////// ACTUALIZAR REGISTROS  ////////////////////////////////////////////////////////////////
	if ($_POST['modifica']==1 ) {
		$consulta="update nomina set detalle=ucase('".$_POST['detalle']."'),periodo='".$_POST['periodo']."',cuenta = '".$_POST['cuenta']."', tipo='".$_POST['tipo']."' where id='".$_SESSION['id_validar']."' and codigo='".$_POST['codigo']."' " ;
		$resultado = mysqli_query($enlace,$consulta) or die('Verificaci&oacute; del C&oacute;digo fall&oacute;: ' . mysqli_error($enlace));
	}
///////////////////////////////////////////////// MOSTRAR ENTRADA DE DATOS  ////////////////////////////////////////////////////////////////
	if ((!$_POST || $accion==2 || $_POST['graba']==1 || $_POST['modifica']==1) && $accion!=3){
		echo "<form name = 'entrada' action = '".$_SERVER['PHP_SELF']."' method = 'POST'  enctype='multipart/form-data' onSubmit='return validar_codigo_detalle_periodo(this)'>";
		echo "<input type = 'hidden' name = 'graba' value = '1'>";
		echo "<table cellpadding=2 cellspacing=0 align='center'>";
		echo "<tr>";
			echo "<td> C&oacute;digo:</td>";
			echo "<td> <input name='codigo' size = '3' maxlength = '3' onkeyup='verificar_nomina(this.form)'></td>";
		echo "</tr>";
		echo "<tr>";
			echo "<td> Detalle (Descripci&oacute;n):</td>";
			echo "<td> <input name='detalle' size = '30' maxlength = '50'></td>";
		echo "</tr>";
		echo "<tr>";
			echo "<td> Per&iacute;odo Inicial:</td>";
			echo "<td> <input name='periodo' size = '3' maxlength = '3' onkeyup='verificar_periodo(this.form)'></td>";
		echo "</tr>";
		echo "<tr>";
			echo "<td> Cuenta Contable:</td>";
			echo "<td> <input name='cuenta' size = '30' maxlength = '50'></td>";
		echo "</tr>";
		echo "<tr>";
			echo "<td> Tipo de N&oacute;mina:</td>";
			echo "<td >";
			echo "	<input type = 'radio' name = 'tipo' VALUE = '1' checked> Semanal";
			echo "	<input type = 'radio' name = 'tipo' VALUE = '2'> Quincenal";
			echo "	<input type = 'radio' name = 'tipo' VALUE = '3'> Mensual";
		echo "</tr>";
		echo "</table>";
		echo "	<center><input name='procesar' type = 'submit' value = 'Grabar Registro' disabled></center>";
		echo "	</form>"; 
		echo "	<script language='JavaScript' type='text/JavaScript'>";
		echo "		document.entrada.codigo.focus()";
		echo "	</script>";
	}	
///////////////////////////////////////////////// ELIMINAR REGISTROS ////////////////////////////////////////////////////////////////
	if ($accion==2) { 
		$consulta="delete from nomina where id='".$_SESSION['id_validar']."' and codigo='".$codigo."' ";
		$resultado = mysqli_query($enlace,$consulta) or die('Verificaci&oacute; del C&oacute;digo fall&oacute;: ' . mysqli_error($enlace));
	}
///////////////////////////////////////////////// MODIFICAR REGISTROS ////////////////////////////////////////////////////////////////
	if ($accion==3) {
		$consulta="Select * from nomina where id='".$_SESSION['id_validar']."' and codigo = '".$codigo."'" ;
		$resultado = mysqli_query($enlace,$consulta) or die('Verificaci&oacute; del C&oacute;digo fall&oacute;: ' . mysqli_error($enlace));
		$num_rows = mysqli_num_rows($resultado);
// CARGAR REGISTRO SI NO EXISTE
		if ($num_rows != 0) {
				$row=mysqli_fetch_array($resultado);
				echo "<form name = 'entrada' action = '".$_SERVER['PHP_SELF']."' method = 'POST'  enctype='multipart/form-data' onSubmit='return validar_codigo_detalle_periodo(this)'>";
				echo "<input type = 'hidden' name = 'modifica' value = '1'>";
				echo "<table cellpadding=2 cellspacing=0 align='center'>";
				echo "<tr>";
					echo "<td witdht=200> C&oacute;digo:</td>";
					echo "<td> <input name='codigo' size = '3' maxlength = '3' value='".$row[0]."' readonly></td>";
				echo "</tr>";
				echo "<tr>";
					echo "<td> Detalle (Descripci&oacute;n):</td>";
					echo "<td> <input name='detalle' size = '30' maxlength = '50' value='".$row[1]."'></td>";
				echo "</tr>";
				echo "<tr>";
					echo "<td> Per&iacute;odo Inicial:</td>";
					echo "<td> <input name='periodo' size = '30' maxlength = '3' value='".$row[2]."' onkeyup='verificar_periodo(this.form)'></td>";
				echo "</tr>";
				echo "<tr>";
					echo "<td> Cuenta Contable:</td>";
					echo "<td> <input name='cuenta' size = '30' maxlength = '50' value='".$row[3]."'></td>";
				echo "</tr>";
				echo "<tr>";
					echo "<td> Tipo de N&oacute;mina:</td>";
					echo "<td >";
					if ($row[4]==1) echo "	<input type = 'radio' name = 'tipo' VALUE = '1' checked> Semanal";
					else echo "	<input type = 'radio' name = 'tipo' VALUE = '1'> Semanal";
					if ($row[4]==2) echo "	<input type = 'radio' name = 'tipo' VALUE = '2' checked> Quincenal";
					else echo "	<input type = 'radio' name = 'tipo' VALUE = '2'> Quincenal";
					if ($row[4]==3) echo "	<input type = 'radio' name = 'tipo' VALUE = '3' checked> Mensual";
					else echo "	<input type = 'radio' name = 'tipo' VALUE = '3'> Mensual";
				echo "</tr>";
				echo "</table>";
				echo "<table border=0  align = 'center'><tr>";
				echo "<td><input type = 'submit' value = 'Grabar Modificaci&oacute;n'></td>";
				echo "</form>"; 
				echo "<form action = '".$_SERVER['PHP_SELF']."'>";
				echo "<td><input type = 'submit' value = 'Cancelar'></td>";
				echo "</form></table><br>"; 				
			}
	}
///////////////////////////////////////////////// MOSTRAR TABLA DE REGISTROS ////////////////////////////////////////////////////////////////
	$consulta="Select * from nomina where id='".$_SESSION['id_validar']."' order by codigo, detalle";
	$resultado = mysqli_query($enlace,$consulta) or die('Verificaci&oacute; del C&oacute;digo fall&oacute;: ' . mysqli_error($enlace));
	$num_rows = mysqli_num_rows($resultado);
	if ($num_rows != 0){
			echo "<table border=1 cellpadding=2 cellspacing=0 align='center'>";
			echo "<tr BGcolor='#00BFFF' >";
			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>Per&iacute;odo</font></th>";
			echo "<th ali=center><font face='verdana' size=2>Tipo</font></th>";
			echo "<th colspan=2 ali=center><font face='verdana' size=2>Acci&oacute;n</font></th></tr>";
			for ($cont=0;$cont<$num_rows;$cont++) {
				$row = mysqli_fetch_array($resultado);
				if ($color) $tcolor="#BFE5FF";
				else $tcolor="#F4FEFF";
				$color=!$color;
				echo '<tr bgcolor="'.$tcolor.'" >';
				echo '<td width="100" align="center">'.$row[0].'</td>';
				echo '<td width="300">'.$row[1].'</td>';
				echo '<td  align=center width="100">'.$row[2].'</td>';
				echo '<td  align=center width="100">'.$row[4].'</td>';
				$codigo = $row['0'];
				$detalle = $row['1'];
				$periodo = $row['2'];
				$cuenta = $row['3'];
				$tipo = $row['4'];
				if (substr($_SESSION['acceso_validar'],17,1)==0) 
				echo "<td width='60' align='center'></td>";
				else echo "<td width='60' align='center'><a onclick='return validar_elimina()' href='".$_SERVER['PHP_SELF']."?accion=2&codigo=".$codigo."'><img  border = 0  src='images/b_elimina.png' alt='ELIMINAR éste registro' ></a></td>";
				echo "<td width='60' align='center'><a href='".$_SERVER['PHP_SELF']."?accion=3&codigo=".$codigo."&detalle=".$detalle."&periodo=".$periodo."&cuenta=".$cuenta."&tipo=".$tipo."'><img border = 0 src='images/b_edit.png' alt='MODIFICAR éste registro'></a></td>";
				echo '</tr>';
			}
			echo '</table>';
			echo "<form name='imprimir' method='POST' target ='_blank' action='".$_SERVER['PHP_SELF']."' enctype='multipart/form-data'>";
			echo "  <input type = 'hidden' name = 'imprime' value = '1'>";
			echo "<center><input type = 'submit' value = 'Imprimir Resultado'></center></form>";
	} else echo "<hr>"; 		
	mysqli_free_result($resultado);
	mysqli_close($enlace);
	echo "	<script language='JavaScript' type='text/JavaScript'>";
	echo "		document.entrada.codigo.focus()";
	echo "	</script>";
    echo "</font></body>";
	echo "</html>";
}
?>