<?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();
$accion=$_REQUEST["accion"];
$cedula=$_REQUEST["cedula"];
$detalle=$_REQUEST["detalle"];
$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=''; 
menu();
set_time_limit(3000); 

///////////////////////////////////////////////// grabar vacaciones ////////////////////////////////////////////////////////////////
if ($_POST['grabar']==1) {
	$fecha1 = crear_fecha($_POST['fecha1']);
	$fecha2 = crear_fecha($_POST['fecha2']);
	if ($_POST['numero']=='') $_POST['numero']=0;
	$consulta="insert into ficha_laboral (id, cedula,empresa,puesto,detalle,numero,pais,fdesde,fhasta)  values ('".$_SESSION['id_validar']."',";
	$consulta .="'".$_POST['cedula']."','".$_POST['empresa']."','".$_POST['puesto']."','".$_POST['detalle']."','".$_POST['numero']."','".$_POST['pais']."','".$fecha1."','".$fecha2."')";
	$resultado = mysqli_query($enlace,$consulta) or die('verificaci&oacute; del c&oacute;digo fall&oacute;: ' . mysqli_error($enlace));
}
///////////////////////////////////////////////// grabar vacaciones ////////////////////////////////////////////////////////////////
if ($accion==2) {
	$consulta="delete from ficha_laboral where id='".$_SESSION['id_validar']."' and cedula='".$cedula."' and empresa='".$detalle."'";
	$resultado = mysqli_query($enlace,$consulta) or die('verificaci&oacute; del c&oacute;digo fall&oacute;: ' . mysqli_error($enlace));
}

/////////////////////////////////////////////////MOSTRAR SOLICITUD DE CEDULA ////////////////////////////////////////////////////////////////
if ( (!$_POST || $_POST['busca']==1 ) && ($accion!=5) && ($accion!=2) && ($_POST['grabar']!=1)){
	echo "<b>Opciones:</b> &nbsp;&nbsp;<img border = 0 src='img2/icono_01.png' alt='Seleccionar'>&nbsp;Seleccionar Trabajador";
	echo "<hr><h2>Actualizaci&oacute;n de Ficha Laboral del Trabajador</h2>";	
	
	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' size = '30' maxlength = '8'></td>";
	echo "</tr>";
	echo "<tr>";
		echo "<td align=right> 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'> Operador";
	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' value = 'Buscar Registros'></td>";
	echo "</tr></table></form>"; 

	$query1 = "";
	if ($_POST['cedula'] != "") {
		$query1 = " and (codigo like '%".$_POST['cedula']."%' or nombres like '%".$_POST['cedula']."%' or apellidos like '%".$_POST['cedula']."%') ";
	}
	if ($_POST['orden'] == "sso") $query2 = " order by sso";
	else if ($_POST['orden'] == "nom") $query2 = " order by nombres";
	else if ($_POST['orden'] == "ape") $query2 = " order by apellidos";
	else $query2 = " order by codigo"; 

	$consulta="select * from personal where id='".$_SESSION['id_validar']."' and status<>'I' ".$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 bgcolor='#43bced' height='35'>";
			echo "<th align = center><font color='white'> Foto </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 = left><font color='white'> Ficha </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 = 4 align = center><font color='white'> Acci&oacute;n </font></th>";
			for ($cont=0;$cont<$num_rows;$cont++) {
				$row = mysqli_fetch_array($resultado);
				$cedula = $row['codigo'];
				if ($color) {
					$tcolor="#dfdfd0";
					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'><a href='".$_SERVER['PHP_SELF']."?accion=5&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>";
				echo "<td width='100'>".$row['sso']."</td>";
				echo "<td width='50' align='center'>".($row['status']=='A'?'<img src="img2/status_ver.png">':'<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=5&cedula=".$cedula."'><img border = 0 src='img2/icono_01.png' alt='Seleccionar éste registro'></a></td>";
				
				echo '</tr>';
				$color=!$color;
			}
			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);
}	

///////////////////////////////////////////////// MOSTRAR DATA A INGRESAR ////////////////////////////////////////////////////////////////
if ($accion==2 || $accion==5 || $_POST['grabar']==1) {
	include ("calendario/calendario.php");
	echo "<b>Opciones:</b> &nbsp;&nbsp;<img border = 0 src='images/b_regresa.png' alt='Regresar'>:<a href='".$_SERVER['PHP_SELF']."'>Regresar</a>";
	echo "<hr><h2>Actualizaci&oacute;n de Ficha Laboral del Trabajador</h2>";	

	echo "<form name = 'fentrada' action = '".$_SERVER['PHP_SELF']."' method = 'POST'  enctype='multipart/form-data'>";
	echo "<input type = 'hidden' name = 'grabar' value = '1'>";

	$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 "<table width='70%' rules=none border=0 cellpadding=2 cellspacing=0 align='center' style='border-width: 1px; border-color: 75263C;border-style: solid;'>";

		//=================================================================================================///
		echo "<tr bgcolor='#43bced' height='25'>";
		echo "<td colspan=2 align = center><font color='white'> Datos Personales </font></td>";
		echo "</tr>";
		
		
		echo "<tr><td width = '20%' align = 'right'> C&eacute;dula: </td>";
		echo "<td width = '36%' align = 'left'> <input type = 'text' name = 'cedula' size = '30' maxlength = '8' value = '".$row['codigo']."' readonly> * </td>";
		echo "</tr>";

		echo "<tr><td width = '15%' align = 'right'> Nombres: </td>";
		echo "<td width = '29%' align = 'left'> <input type = 'text' name = 'nombres' size = '30' maxlength = '50' value = '".trim($row['nombres'])." ".$row['nombres1']."' readonly> * </td>";
		echo "</tr>";

		echo "<tr><td align = 'right'> Apellidos: </td>";
		echo "<td align = 'left'> <input type = 'text' name = 'apellidos' size = '30' maxlength = '50' value = '".trim($row['apellidos'])." ".$row['apellidos1']."' readonly> * </td>";
		echo "</tr>";

		echo "<tr><td align = 'right' > Fecha de Ingreso: </td>";
		echo "<td align = 'left' > <input type = 'text' name = 'fec' size = '10' maxlength = '50' value = '".$row['fechai']."' readonly></td>";
		echo "</tr>";

		//=================================================================================================///
		echo "<tr bgcolor='#43bced' height='25'>";
		echo "<td colspan=2 align = center><font color='white'> Datos Laborales </font></td>";
		echo "</tr>";
		
		echo "<tr><td align = 'right' > Empresa? </td>";
		echo "<td align = 'left' > <input type = 'text' name = 'empresa' size = '100' maxlength = '100' ></td>";
		echo "</tr>";
		echo "<tr><td align = 'right' > Puesto? </td>";
		echo "<td align = 'left' > <input type = 'text' name = 'puesto' size = '50' maxlength = '50' ></td>";
		echo "</tr>";
		echo "<tr><td align = 'right' > Detalle? </td>";
		echo "<td align = 'left' > <input type = 'text' name = 'detalle' size = '100' maxlength = '200' ></td>";
		echo "</tr>";
		echo "<tr><td align = 'right' > Pais/Ciudad? </td>";
		echo "<td align = 'left' > <input type = 'text' name = 'pais' size = '50' maxlength = '50' ></td>";
		echo "</tr>";
		echo "<tr><td align = 'right' > Nro. de Persona a Cargos? </td>";
		echo "<td align = 'left' > <input type = 'text' name = 'numero' size = '10' maxlength = '10' ></td>";
		echo "</tr>";

		echo "<tr><td align = 'right' > Fecha Desde (ddmmaaaa): </td>";
		echo "<td align = 'left'>";
		escribe_formulario_fecha_vacio("fecha1","fentrada");
		echo "</td></tr>";

		echo "<tr><td align = 'right' > Fecha Hasta (ddmmaaaa): </td>";
		echo "<td align = 'left'>";
		escribe_formulario_fecha_vacio("fecha2","fentrada");
		echo "</td></tr>";

		echo "<tr><td align = 'right' > </td>";
		echo "<td align = 'left'>";
		echo "<input type = 'submit' value = 'Grabar Registro al Trabajador'>";
		echo "</td></tr>";
		
		echo "</tr>";
		echo "</table>";

	}
	echo "	<script language='javascript' type='text/javascript'>";
	echo "		document.fentrada.curso.focus()";
	echo "	</script>";
	echo "</form>"; 

	if ($cedula!='') {
		$consulta="select * from ficha_laboral where id='".$_SESSION['id_validar']."' and cedula= '".$cedula."' order by fhasta desc";
		$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 bgcolor='#43bced' height='35'>";
			echo "<th align = left><font color='white'> Fecha </font></th>";
			echo "<th align = left><font color='white'> Empresa </font></th>";
			echo "<th align = left><font color='white'> Puesto </font></th>";
			echo "<th align = left><font color='white'> Detalle </font></th>";
			echo "<th align = left><font color='white'> Pais/Ciudad </font></th>";
			echo "<th align = center><font color='white'> Personas a Cargo </font></th>";
			echo "<th colspan =  align = center><font color='white'> ? </font></th>";
			for ($cont=0;$cont<$num_rows;$cont++) {
				$row = mysqli_fetch_array($resultado);
				$cedula = $row['cedula'];
				if ($color) {
					$tcolor="#dfdfd0";
				} else { 
					$tcolor="white";
				}	
				echo '<tr bgcolor='.$tcolor.' >';
				echo "<td width='200'>".$row['fdesde']." al ".$row['fhasta']."</td>";
				echo "<td width='200'>".$row['empresa']."</td>";
				echo "<td width='200'>".$row['puesto']."</td>";
				echo "<td width='200'>".$row['detalle']."</td>";
				echo "<td width='200'>".$row['pais']."</td>";
				echo "<td width='100' align='center'>".number_format($row['numero'],0)."</td>";

				if (substr($_SESSION['acceso_validar'],18,1)=='1')
				echo "<td width='30' align='center'><a onclick='return validar_elimina()' href='".$_SERVER['PHP_SELF']."?accion=2&cedula=".$cedula."&detalle=".$row['empresa']."'><img  border = 0  src='img2/icono_07.png'alt='eliminar éste registro' ></a></td>";
				else echo "<td width='30' align='center'>&nbsp;</td>";
				
				echo '</tr>';
				$color=!$color;
			}
			echo '</table>';
		}  		
		mysqli_free_result($resultado);
	}		
echo "<br><br><br><br></body>";
echo "</html>";
}

mysqli_close($enlace);
