<?php
session_start();

if (isset($_SESSION['tipousuario']))
		{if ($_SESSION['tipousuario']=='1')
		header ('location:menu_personal.php');
		}

if  (trim($_SESSION['nombre_validar'])=='') header ('location:index.php');
$accion=$_REQUEST["accion"];
$codigo=$_REQUEST["codigo"];
$cedula=$_REQUEST["cedula"];

///////////////////////////////////////////////// inicio del programa ////////////////////////////////////////////////////////////////
?>
<html>
	<head><script language='javascript' src='fscript.js'></script>
	<style type="text/css">
		#menuListHoriz li {
		  list-style: none;
		  display: inline;/* Es necesario para que los elementos de la lista se situen uno tras otro (horizontalidad) */
		}
		#menuListHoriz li a, #menuList1 li a:focus {
		  list-style: none;
		  background-color: #ffffff;
		  border: 1px solid #2E2E2E;
		  padding:12px;
		  text-decoration: none;
		  color: #000;
		  font-family: Verdana,Arial,Helvetica;
		  font-size: 12px;
		  font-weight:bold;
		}
		#menuListHoriz li a:hover {
		  color: #FF0000;
		  border: 2px solid #2E2E2E;
		  font-size: 12px;
		  font-weight:bold;
		}
	</style> 
	</head>
	<body>	
		<table border=0 cellpadding=0 cellspacing=0 align=center width='100%' style='margin:0 0 0 0px;position:absolute; top:0; left:0;'>
			<tr>
				<td background='images/alto_04.png' width='20%' valign=top>
					<img src="images/alto_01.png">
				</td>
				<td background='images/alto_04.png' width='60%' valign=top align=center>
					<img src="images/alto_02.png">
				</td>
				<td background='images/alto_04.png' width='20%' valign=top>
					<img src="images/alto_03.png">
				</td>
			</tr>	
		</table>	
		<br><br><br><br>
		<?
		$cadena=strtoupper($_SERVER['HTTP_USER_AGENT']);
		//if (strstr($cadena,'FOX')) echo "<br>";
		if (strstr($cadena,'NET')) echo "<br>";
		?>

		<center>
					<ul id="menuListHoriz">
					<?php
						echo "<li><a href='prestaciones_liquidacion_renuncia.php'  >Renuncia</a></li>";
						echo "<li><a href='prestaciones_liquidacion_justificado.php'  >Despido Justificado</a></li>";
						echo "<li><a href='prestaciones_liquidacion_injustificado.php' >Despido Injustificado</a></li>";
						echo "<li><a href='prestaciones_liquidacion_intereses.php' >C&aacute;lculo de Intereses</a></li>";
					?>	
					  <li><a href='menu_prestaciones.php' rel='nofollow'><font color=blue>Regresar</a></li></font>
					</ul> 
				</center>	
		<table border=0 cellpadding=0 cellspacing=0 width='100%'>
			<tr>
				<td width='50%'><font size=2 color='black' face='verdana'><a href='menu.php'>[MEN&Uacute; Principal]</a>--><a href='menu_prestaciones.php'>[Prestaciones]</a>-->[Liquidaciones]</font></td> 
				<td  width='50%' align='right'><font size=2 color='black' face='verdana'>Usuario: <b><?echo $_SESSION['nombre_validar'];?></b></font></td> 
			</tr>
		</table>	
<?
echo "<font face='verdana' size=2>";
include ("fphp.php");
$enlace=cargar_conexion();
$entra=0;
echo "<hr>";
echo "<font face='verdana' size='2' color='#6E6E6E'>
<b>Opciones:</b> &nbsp;&nbsp;<img border = 0 src='images/b_edit.png' alt='Modificar'>:Seleccionar Trabajador
</font><br><br>";
echo "<font face='verdana, arial, helvetica, sans-serif' size=4 >Liquidaci&oacute;n de Trabajadores (INJUSTIFICADO)</font><br>";

///////////////////////////////////////////////// graba registros  ////////////////////////////////////////////////////////////////
if ($_POST['graba']==1 ) {
	if ($_POST['mon']=='' || $_POST['mon']<=0) $men='Falta completar Datos..';
	else {
		$fing = substr($_POST['fec'],4,4).'-'.substr($_POST['fec'],2,2).'-'.substr($_POST['fec'],0,2);		
		$query = "insert into personal_sueldo  ( codigo, fec, sueldo) values(ucase(trim('".$_POST['cedula']."')), ucase(trim('".$fing."')), '".$_POST['mon']."') ";
		$consulta = mysql_query ($query) or die ("no se pudo ingresar los datos...".mysql_error().$query);
		$consulta="select * from personal_sueldo where codigo='".$cedula."' order by fec desc limit 1";
		$resultado = mysql_query($consulta) or die($consulta . mysql_error());
		$num_rows = mysql_num_rows($resultado);
		if ($num_rows!=0) {
			$row=mysql_fetch_array($resultado);
			$sue=$row['sueldo'];
			$consulta="update personal set smes=$sue where codigo='".$cedula."'";
			$resultado = mysql_query($consulta) or die($consulta . mysql_error());
		}
	}		
	$accion=3;
	$cedula=$_POST['cedula'];
}


////////////////////////////////////////////  eliminar item
if ($accion==2) {
		$consulta="delete from personal_sueldo where id='".$codigo."'";
		$resultado = mysql_query( $consulta)  or die($consulta . mysql_error());
		$men="Registro Eliminado..";
		$accion=3;
}
	
///////////////////////////////////////////////// mostrar entrada de datos  ////////////////////////////////////////////////////////////////
if ((!$_POST || $accion==2 || $_POST['graba']==1 || $_POST['busca']==1 || $_POST['modifica']==1 ) && $accion!=3 && $accion!=9 ) {
	echo "<form name = 'entrada' action = '".$_SERVER['PHP_SELF']."' method = 'POST'  enctype='multipart/form-data'>";
	echo "<input type = 'hidden' name = 'busca' value = '1'>";
		echo "<table cellpadding=2 cellspacing=0 width='500' align='center'>";
		echo "<tr>";
			echo "<td> C&eacute;dula:</td>";
			echo "<td> <input name='ced' size = '30' maxlength = '8'></td>";
		echo "</tr>";
		echo "<tr>";
			echo "<td> Nombres:</td>";
			echo "<td> <input name='nom' size = '30' maxlength = '50'></td>";
		echo "</tr>";
		echo "<tr>";
			echo "<td> Apellidos:</td>";
			echo "<td> <input name='ape' size = '30' maxlength = '3'></td>";
		echo "</tr>";
		echo "<tr>";
			echo "<td> Ordenado por:</td>";
			echo "<td colspan = 3>";
			echo "	<input type = 'radio' name = 'orden' value = 'ced' checked> cedula";
			echo "	<input type = 'radio' name = 'orden' value = 'nom'> nombre";
			echo "	<input type = 'radio' name = 'orden' value = 'ape'> apellido";
			echo "	<input type = 'radio' name = 'orden' value = 'sso'> Ficha";
		echo "</tr>";
		echo "<tr>";
			echo "<td> </td>";
			echo "<td colspan = 3><input type = 'submit' value = 'Buscar Registros'></td>";
		echo "</tr>";
		echo "</table></form>";
	echo "<script language='javascript' type='text/javascript'>document.entrada.ced.focus();</script>";
}

	
///////////////////////////////////////////////// ingresar remuneracion ////////////////////////////////////////////////////////////////
if ($accion==3) {
	$consulta="select * from personal where codigo='".$cedula."' ";
	$resultado = mysql_query($consulta) or die('verificaci&oacute; del c&oacute;digo fall&oacute;: ' . mysql_error());
	$num_rows = mysql_num_rows($resultado);
	if ($num_rows!=0) {
				$row=mysql_fetch_array($resultado);
				echo "<br><center><font color=red>Datos del Trabajador</font></center>";
				echo "<table align ='center'>";
					echo "<tr>";
						echo "<td align = 'right'> C&eacute;dula: </td>";
						echo "<td align = 'left'> <input type = 'text' name = 'cedula1' size = '30' maxlength = '8' readonly value = '".$row['codigo']."'> </td>";
					echo "</tr>";
					echo "<tr>";
						echo "<td align = 'right'> Nombres: </td>";
						echo "<td align = 'left'> <input type = 'text' name = 'nombres' size = '30' maxlength = '50' value = '".$row['nombres']."' > </td>";
					echo "</tr>";
					echo "<tr>";
						echo "<td align = 'right'> Apellidos: </td>";
						echo "<td align = 'left'> <input type = 'text' name = 'apellidos' size = '30' maxlength = '50' value = '".$row['apellidos']."' >  </td>";
					echo "</tr>";
					echo "<tr>";
						echo "<td align = 'right' > Sueldo Mensual Bs.: </td>";
						echo "<td align = 'left' colspan = 3 ><input type = 'text' name = 'smes' size = '30' maxlength = '10' value = '".$row['smes']."'>  </td>";
					echo "</tr>";					
				echo "</table>";

				echo "<br><center><font color=red>Datos a Ingresar</font></center>";
				?>	
				<form name = 'entrada' action = '<? echo $_SERVER['PHP_SELF']; ?>' method = 'POST'>
					<input type = 'hidden' name = 'graba' value = '1'>
					<input type = 'hidden' name = 'cedula' value = '<?echo $row['codigo'];?>'>
					<table align='center'>
						<tr>
							<td align='right'>Fecha (DDMMAAAA)?</b></td>
							<td ><input type = 'text' name='fec' size='20' maxlength='8'>
							</td>
						</tr>
						<tr>
							<td align='right'>Nuevo Sueldo?</td>
							<td ><input type = 'text' name='mon' size='20' >
							</td>
						</tr>
						<tr>
							<td colspan=2>&nbsp;</td>
						</tr>	
					</table>
					<center><font size=3 face='Verdana' color='red'><b><?echo $men; ?></b></font></center>					
				<?  
				echo "<table width = '20%' align ='center'>";
				echo "<tr><td><input type = 'submit' value = 'Grabar Nuevo Sueldo?'></td></form>";
				echo "<script language='javascript' type='text/javascript'>	document.entrada.fec.focus(); </script>";
				echo "<form name = 'regresar' method = 'POST' action = '".$_SERVER['PHP_SELF']."'>";
				echo "<td><input type = 'submit' value = 'Regresar al Inicio'></td></tr>";
				echo "</table></form>";
				
				$consulta="select * from personal_sueldo where codigo='".$cedula."' order by fec";
				$resultado = mysql_query($consulta) or die($consulta . mysql_error());
				$num_rows = mysql_num_rows($resultado);
				if ($num_rows != 0){
					$total=0;$linea=0;
					echo "<br><table border=1 cellpadding=2 cellspacing=0 align='center'>";
					echo "<tr bgcolor='#00BFFF' >";
					echo "<th ali = center><font face='verdana' size=2> #</font></th>";
					echo "<th ali = center><font face='verdana' size=2> Fecha</font></th>";
					echo "<th ali = center><font face='verdana' size=2> Sueldo</font></th>";
					echo "<th ali = center><font face='verdana' size=2> Elegir </font></th></tr>";
						for ($cont=0;$cont<$num_rows;$cont++) {
							$linea++;
							$color=!$color;
							if ($color) echo '<tr bgcolor="#f6f6f6" >';
							else echo '<tr bgcolor="#D8D8D8">';
							$row = mysql_fetch_array($resultado);
							echo "<td width='50'>".$linea."</td>";
							echo "<td width='100'>".$row['fec']."</td>";
							echo "<td width='100' align='right'>".number_format($row['sueldo'],2)."</td>";
							echo "<td width='30' align='center'><a onclick='return validar_elimina()' href='".$_SERVER['PHP_SELF']."?accion=2&cedula=".$cedula."&codigo=".$row['id']."'><img border = 0 src='images/b_elimina.png' alt='Eliminar'></a></td>";
							echo '</tr>';
						}
					echo '</tr></table></form>';
				}  		
				mysql_free_result($resultado);	
				mysql_close($enlace); 
				
	}
}

////////////////////////////////////////// busca por filtros en tabla de registros ////////////////////////////////////////////////////////////////
if ( (!$_POST || $_POST['busca']==1 || $_POST['graba']==1 || $_POST['modifica']==1) && $accion!=3 && $accion!=9) {
	
	$query1 = "";
	if ($_POST['ced'] != "" || $_POST['nom'] != "" || $_POST['ape'] != "") {
		if ($_POST['ced'] != "") $query1 = " where codigo like '%".$_POST['ced']."%' ";
		if ($_POST['nom'] != "") {
			if ($query1 != "") $query1 = $query1." and nombres like '%".$_POST['nom']."%' ";
			else $query1 = " where nombres like '%".$_POST['nom']."%' ";
		}
		if ($_POST['ape'] != "") {
			if ($query1 != "") $query1 = $query1." and apellidos like '%".$_POST['ape']."%' ";
			else $query1 = " where apellidos like '%".$_POST['ape']."%' ";
		}
	}
	if ($_POST['orden'] == "ced") $query2 = " order by codigo";
	else if ($_POST['orden'] == "nom") $query2 = " order by nombres";
	else if ($_POST['orden'] == "ape") $query2 = " order by apellidos";
	else $query2 = " order by sso"; 
	
	$consulta="select * from personal ".$query1.$query2;
	$resultado = mysql_query($consulta) or die('verificaci&oacute; del c&oacute;digo fall&oacute;: ' . mysql_error());
	$num_rows = mysql_num_rows($resultado);
	if ($num_rows != 0){
			echo "<table border=1 cellpadding=2 cellspacing=0 width='800' align='center'>";
			echo "<tr bgcolor='#00BFFF' >";
			echo "<th align = center><font face='verdana' size=2> C&eacute;dula </font></th>";
			echo "<th align = center><font face='verdana' size=2> Nombres </font></th>";
			echo "<th align = center><font face='verdana' size=2> Apellidos </font></th>";
			echo "<th align = center><font face='verdana' size=2> Ficha </font></th>";
			echo "<th align = center><font face='verdana' size=2> Sueldo </font></th>";
			echo "<th align = center><font face='verdana' size=2> Ingreso </font></th>";
			echo "<th align = center><font face='verdana' size=2> acci&oacute;n </font></th>";
			for ($cont=0;$cont<$num_rows;$cont++) {
				$row = mysql_fetch_array($resultado);
				echo '<tr bgcolor="#cdcdcd" >';
				echo "<td width='100'><font face='verdana' size=2>".$row['codigo']."</td>";
				echo "<td width='250'><font face='verdana' size=2>".$row['nombres']."</td>";
				echo "<td width='250'><font face='verdana' size=2>".$row['apellidos']."</td>";
				echo "<td width='50' align='center'><font face='verdana' size=2>".$row['sso']."</td>";
				echo "<td width='100' align='right'><font face='verdana' size=2>".number_format($row['smes'],2)."</td>";
				echo "<td width='120'><font face='verdana' size=2>".$row['fechai']."</td>";
				$cedula = $row['codigo'];
				echo "<td width='60' align='center'><a href='".$_SERVER['PHP_SELF']."?accion=3&cedula=".$cedula."'><img border = 0 src='images/b_edit.png' alt='modificar éste registro'></a></td>";
				echo '</tr>';
			}
			echo '</table>';
	} else echo "<hr>"; 		
	mysql_free_result($resultado);
	mysql_close($enlace);
}	
echo "</font>";
echo "</body>";
echo "</html>";
?>