<?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);
	$_SESSION['per_validar']=mostrar_fecha($row['desde']).' al '.mostrar_fecha($row['hasta'])." (Nom:".$row['nomina'].")";
} else $_SESSION['per_validar']=''; 
menu();

///////////////////////////////////////////////// siguiente trabajador  ////////////////////////////////////////////////////////////////
if ($_REQUEST['accion']==9 ) {
	if ($_REQUEST['gerencia'] != "") $query1 .= " and (gerencia='".$_REQUEST['gerencia']."') ";
	if ($_REQUEST['orden'] == "fechai") $query1 .= " order by fechai";
	else if ($_REQUEST['orden'] == "nom") $query1 .= " order by nombres";
	else if ($_REQUEST['orden'] == "ape") $query1 .= " order by apellidos";
	else $query1 .= " order by codigo"; 
	$entro=0;
	$query="select * from personal where id='".$_SESSION['id_validar']."' and (status='A' or status='C') $query1";
	$consulta = mysqli_query($enlace,$query) or die ($query. " ".mysqli_error($enlace));
	while ($row=mysqli_fetch_array($consulta)) {
		if ($row['codigo']==$_REQUEST['cedula']) $entro=1;
		if ($entro==2) {
			$cedula = $row['codigo'];
			$_REQUEST['cedula'] = $row['codigo'];
			$entro=0;
		}
		if ($entro==1) $entro=2;
	} 	
}

$accion=$_REQUEST["accion"]==''?$_POST["accionx"]:$_REQUEST["accion"];
$codigo=$_REQUEST["codigo"];
$cedula=$_REQUEST["cedula"]==''?$_POST["cedula"]:$_REQUEST["cedula"];
$constante=$_REQUEST["constante"];
$ordena=$_REQUEST["ordena"];
$nomina=$_REQUEST["nomina"];
$monto=$_REQUEST["monto"];

if ($_REQUEST['orden']!='') {
	$_POST['orden']=$_REQUEST['orden'];
	$_POST['gerencia']=$_REQUEST['gerencia'];
	$_POST['busca']='1';
}
echo "<b>Opciones:</b> ";

if ($accion==3 || $accion==9) echo "&nbsp;&nbsp;
      <a href='" . $_SERVER['PHP_SELF'] . "'>
          <img border='0' src='img2/regresa.png' alt='Regresar'>
          Otro Trabajador
      </a>";
if (substr($_SESSION['acceso_validar'],9,1)=='1') echo "&nbsp;&nbsp;
<a href='calculo_carga.php'><img border = 0 src='img2/icono_agregar.png' alt='carga_conceptos'>
	Carga por Conceptos
	</a>";

if (substr($_SESSION['acceso_validar'],10,1)=='1') echo "&nbsp;&nbsp;
	<a href='calculo_prestamo.php'>
	<img border = 0 src='img2/carga_prestamo2.png' alt='cargar_prestamo'>Carga de Prestamos
	</a>";

if ($accion==3 || $accion==9) echo "&nbsp;&nbsp;
      <a href='" . $_SERVER['PHP_SELF']."?accion=9&cedula=".$cedula."&orden=".$_POST['orden']."&gerencia=".$_POST['gerencia']
	   . "'>
          <img border='0' src='img2/next.png' alt='Siguiente'>
          Siguiente Trabajador
      </a>";
echo "<hr>";


echo "<h2><center>CARGA DE CONCEPTOS POR TRABAJADOR</center></h2>";


///////////////////////////////////////////////// graba registros  ////////////////////////////////////////////////////////////////
if ($_POST['grabar']=='11' ) {

	$consulta=mysqli_query($enlace,"select * from periodo where id='".$_SESSION['id_validar']."' and nomina='".substr($_POST['nomina'],0,3)."' order by periodo DESC limit 1");
	if (mysqli_num_rows($consulta)!=0){
			$row=mysqli_fetch_array($consulta);
			if ($row['pre']==1) $nograbar=1;
			else $nograbar=0;
	} 
	if ($nograbar==0) {
	
		$query = "select * from constante where id='".$_SESSION['id_validar']."' order by codigo ";
		$consulta = mysqli_query($enlace,$query) or die ($query. " ".mysqli_error($enlace));
		while ($row = mysqli_fetch_array($consulta)) {
			
			$cedula='C'.trim($row['codigo']);
			if ($_POST[$cedula]!="") {
					
				$monto = $_POST[$cedula]+0;	
				$query = "select * from constante_individual where id='".$_SESSION['id_validar']."' and nomina='".substr($_POST['nomina'],0,3)."' and cedula = '".$_POST['cedula']."'  and codigo='".$row['codigo']."'";
				$cquery = mysqli_query($enlace,$query) or die ($query. " ".mysqli_error($enlace));
				if (mysqli_num_rows($cquery)!=0) {
					$row1 = mysqli_fetch_array($cquery);
					$query = "update constante_individual set monto='".$monto."' where id='".$_SESSION['id_validar']."' and nomina='".substr($_POST['nomina'],0,3)."' and cedula = '".$_POST['cedula']."'  and codigo='".$row['codigo']."'";
					$cquery = mysqli_query($enlace,$query) or die ($query. " ".mysqli_error($enlace));
				} else {
					$query = "insert into constante_individual (id, codigo,cedula,nomina,monto) values ('".$_SESSION['id_validar']."','".$row['codigo']."','".$_POST['cedula']."','".substr($_POST['nomina'],0,3)."','".$monto."')" ;
					$cquery = mysqli_query($enlace,$query) or die ($query. " ".mysqli_error($enlace));
				}
				//echo $query."<br/>";
				
			}	
		}
		$accion=3;	
		$_POST['codigo']=$_POST['cedula'];
		$_POST['cedula']='';
	}	
}		

/////////////////////////////////////////////////////////////////////////////////////////////////// MOSTRAR REGISTROS
if ($accion==3 || $accion==9) {
	if ($_POST['grabar']=='11') $cedula=$_POST['codigo'];
	$consulta="select * from personal where id='".$_SESSION['id_validar']."' and codigo='".$cedula."' ";
	$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) {
			$row=mysqli_fetch_array($resultado);
			
			echo "<form name = 'entrada2' action = '".$_SERVER['PHP_SELF']."'  method='POST' enctype='multipart/form-data'>";
			echo "<input type = 'hidden' name = 'grabar' value = '11'>";
			echo "<input type = 'hidden' name = 'orden' value = '".$_POST['orden']."'>";
			echo "<input type = 'hidden' name = 'accionx' value = '".$accion."'>";
			echo "<input type = 'hidden' name = 'gerencia' value = '".$_POST['gerencia']."'>";
			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 = 'cedula' 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']." ".$row['nombres1']."' > </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']." ".$row['apellidos1']."' >  </td>";
				echo "</tr>";
				echo "<tr>";
					echo "<td align = 'right' > Nomina: </td>";
					echo "<td align = 'left' colspan = 3 ><input type = 'text' name = 'nomina' size = '30' maxlength = '10' value = '".$row['nomina']."'>  </td>";
				echo "</tr>";					
			echo "</table>";

			echo "<br><center><font color=red>Conceptos a Ingresar</font></center>";

			echo "<table rules=none border=0 cellpadding=2 cellspacing=0 align='center'>";
			echo "<tr bgcolor='#43bced' height='35'>";
			echo "		<th align = center><font color=white> C&oacute;digo </font></th>";
			echo "		<th align = center><font color=white> Detalle </font></th>";
			echo "		<th align = center><font color=white> Monto Actual</font></th>";
			echo "		<th align = center><font color=white> Nuevo Monto? </font></th></tr>";
			
			$query2 = "select * from constante where id='".$_SESSION['id_validar']."' order by codigo ";
			$consulta2 = mysqli_query($enlace,$query2) or die ($query2. " ".mysqli_error($enlace));
			while ($row2 = mysqli_fetch_array($consulta2)) {

				if ($color) $tcolor="#CFE7DE";
				else $tcolor="white";
				echo '<tr bgcolor='.$tcolor.' >';
				
				echo "<td align='left'>".$row2['codigo']."</td>";
				echo "<td>".$row2['detalle']."</td>";

				$query = "select * from constante_individual where id='".$_SESSION['id_validar']."' and nomina='".$row['nomina']."' and codigo='".$row2['codigo']."' and cedula = '".$row['codigo']."'";
				$cquery = mysqli_query($enlace,$query) or die ($query. " ".mysqli_error($enlace));
				$monto=0;
				if (mysqli_num_rows($cquery)!=0) {
					$row1 = mysqli_fetch_array($cquery);
					$monto=$row1['monto'];
				}	
					
				echo "<td align='right'>".$monto."</td>";
				$cedula='C'.trim($row2['codigo']);
				
				echo "<td width='100'>
					<input style='color:black; font-size:1.5em; height: 30px;  width: 150px' name='".$cedula."' maxlength = '20'>
				</td>";				
				echo "</tr>";		
				$color=!$color;
				
			}
			mysqli_free_result($cquery);
			echo "</table>";
			echo "<center><input name = 'procesar' type='submit' class='boton' value='Cargar Nuevos Montos'></form></center>";
		}	
		exit();
}

////////////////////////////////////////// busca por filtros en tabla de registros ////////////////////////////////////////////////////////////////
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 align=right> C&eacute;dula/Nombre/Apellido:</td>";
			echo "<td> <input name='cedula' id='cedula' size='30' maxlength='12' onkeyup='buscarEmpleado(this.value)'>
			<div id='sugerencias' style='background:white; border:0px solid #ccc; position:absolute; width:300px; z-index:1000;'></div></td>";
			
	echo "</tr>";
		echo "<tr>";
			echo "<td align=right> Ordenado por:</td>";
			echo "<td>";
			echo "	<input type = 'radio' name = 'orden' value = 'ced' ".($_POST['orden']=='ced'?'checked':'')."> cedula";
			echo "	<input type = 'radio' name = 'orden' value = 'nom' ".($_POST['orden']=='nom'?'checked':'')."> nombre";
			echo "	<input type = 'radio' name = 'orden' value = 'ape' ".($_POST['orden']=='ape'?'checked':'')."> apellido";
			echo "	<input type = 'radio' name = 'orden' value = 'fechai' ".($_POST['orden']=='fechai'?'checked':'')."> Fecha Ingreso";
		echo "</tr>";
		echo "<tr>";
			$consulta="Select * from tablas where tabla=ucase('GERENCIA') and id='".$_SESSION['id_validar']."' order by codigo,detalle";
			$consulta = mysqli_query($enlace,$consulta) or die ("no se pudo realizar la consulta...".$consulta.'='.mysqli_error($enlace));	
			$resultado = mysqli_num_rows($consulta);
			echo "<td align = 'right'> Gerencia: </td>";
			echo "<td align = 'left'> <select style='width:280px;' name = 'gerencia'><option>";
			if ($resultado != 0) {
				for ($i=0;$i<$resultado;$i++) {
					$row1 = mysqli_fetch_array($consulta);
					echo "<option width = '30%' value='".$row1['codigo']."' ".($_POST['gerencia']==$row1['codigo']?'selected':'').">".$row1['codigo']." - ".$row1['detalle'];
				}
			}	
			echo "</select>";
			echo "</td>";
		echo "</tr>";
	echo "</table>";
	echo "	<script language='javascript' type='text/javascript'>";
	echo "		document.entrada.cedula.focus()";
	echo "	</script>";
	echo "<table border=0  align = 'center'><tr>";
	echo "<td><input type = 'submit' class='boton' value = 'Buscar Registros'></td>";
	echo "</tr></table></form>"; 

	$query1 = "";
	if ($_POST['gerencia'] != "") {
		$query1 .= " and (gerencia='".$_POST['gerencia']."') ";
	}
	if ($_POST['cedula'] != "") {
		$query1 .= " and (codigo like '%".$_POST['cedula']."%' or nombres like '%".$_POST['cedula']."%' or apellidos like '%".$_POST['cedula']."%') ";
	}
	if ($_POST['orden'] == "fechai") $query2 = " order by fechai";
	else if ($_POST['orden'] == "nom") $query2 = " order by nombres";
	else if ($_POST['orden'] == "ape") $query2 = " order by apellidos";
	else $query2 = " order by codigo"; 
	
	if ($_SESSION['nom01_validar']!='') $query3 = " and nomina<>'".$_SESSION['nom01_validar']."' "; 

	$consulta="select * from personal where id='".$_SESSION['id_validar']."' and (status='A' or status='C') ".$query3.$query1.$query2;
	$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 rules=none border=0 cellpadding=2 cellspacing=0 align='center'>";
			echo "<tr class='encabezadoTabla'>";
			//echo "<th align = center><font color='white'> Foto </font></th>";
			//echo "<th align = left><font color='white'> Ficha </font></th>";
			echo "<th align = left><font color='white'> C&eacute;dula </font></th>";
			echo "<th align = left><font color='white'> Nombres </font></th>";
			echo "<th align = left><font color='white'> Apellidos </font></th>";
			echo "<th align = center><font color='white'> Status </font></th>";
			echo "<th align = center><font color='white'> Ingreso </font></th>";
			echo "<th colspan = 1 align = center><font color='white'> Acci&oacute;n </font></th>";
			echo '</tr>';
			for ($cont=0;$cont<$num_rows;$cont++) {
				$row = mysqli_fetch_array($resultado);
				$cedula = $row['codigo'];
				if ($color) {
					$tcolor="#CFE7DE";
					if (file_exists('fotos/'.$cedula.'.jpg')) $imagen='fotos/'.$cedula.'.jpg';
					else $imagen = 'img2/nohay2.jpg';
				} else { 
					$tcolor="white";
					if (file_exists('fotos/'.$cedula.'.jpg')) $imagen='fotos/'.$cedula.'.jpg';
					else $imagen = 'img2/nohay.jpg';
				}	
				echo '<tr bgcolor='.$tcolor.' >';
				//echo "<td align'center'><img src='".$imagen."' height='64' width='64'></td>";
				//echo "<td width='100'>".$row['fechai']."</td>";
				echo "<td width='100'><a name='".$cedula."' href='".$_SERVER['PHP_SELF']."?accion=3&cedula=".$cedula."'>".$row['codigo']."</a></td>";
				echo "<td width='250'>".trim($row['nombres'])." ".$row['nombres1']."</td>";
				echo "<td width='250'>".trim($row['apellidos'])." ".$row['apellidos1']."</td>";
				if ($row['status'] == 'A') {
						echo "<td width='50' align='center'><img src=img2/status_ver_doble_zaul.png></td>";
					} elseif ($row['status'] == 'C') {
						echo "<td width='50' align='center'><img src=img2/status_ver.png></td>";
					} elseif ($row['status'] == 'S') {
						echo "<td width='50' align='center'><img src=img2/status_ver_naranja.png></td>";
					}
				echo "<td width='120' align=center>".$row['fechai']."</td>";
				
				echo "<td width='30' align='center'><a href='".$_SERVER['PHP_SELF']."?accion=3&cedula=".$cedula."&orden=".$_POST['orden']."&gerencia=".$_POST['gerencia']."'><img border = 0 src='img2/icono_agregar.png' alt='Seleccionar éste registro'></a></td>";
				
				echo '</tr>';
				$color=!$color;
			}
			echo "</table></form>";
			echo "	<script language='javascript' type='text/javascript'>";
			echo "		document.entrada.".$cedula.".focus()";
			echo "	</script>";
	} else echo "<hr>"; 		
	mysqli_free_result($resultado);
	mysqli_close($enlace);

?>

	<style>
	.boton {
		border-radius: 8px;      /* Bordes redondeados */
		padding: 8px 16px;       /* Más espacio interno */
		border: none;            /* Quita el borde feo por defecto */
		cursor: pointer;         /* Mano al pasar */
		transition: 0.2s ease;   /* Animación suave */
		}

		.boton:hover {
		transform: scale(1.03);  /* Efecto sutil al pasar el mouse */
		}

				/*Encabezado tablas */
	.encabezadoTabla {
    background: #43bced;
    height: 35px;
	}

	.encabezadoTabla th {
		padding: 8px 12px;
		text-align: left;
		color: white;
		font-weight: bold;
		font-size: 14px;
		letter-spacing: 0.5px;
		border-bottom: 2px solid #2fa3c9;
	}

	.encabezadoTabla th:nth-child(1),
	.encabezadoTabla th:nth-child(2),
	.encabezadoTabla th:nth-child(3),
	.encabezadoTabla th:nth-child(4),
	.encabezadoTabla th:nth-child(5),
	.encabezadoTabla th:nth-child(6)
	 {
		text-align: center;
	}
	/*hasta aquí Encabezado tablas */

	</style>

<!--Script JavaScript para hacer predictivo el cuadro de búsqueda de empleados -->
<script>
function buscarEmpleado(texto) {
    if (texto.length < 2) {
        document.getElementById("sugerencias").innerHTML = "";
        return;
    }

    var xhr = new XMLHttpRequest();
    xhr.open("POST", "buscar_empleado.php", true);
    xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");

    xhr.onreadystatechange = function() {
        if (xhr.readyState == 4 && xhr.status == 200) {
            document.getElementById("sugerencias").innerHTML = xhr.responseText;
        }
    };

    xhr.send("q=" + texto);
}
</script>

</font></body>
</html>	

