<?php session_start();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=''; 
menu();
echo "<b>Opciones:</b> &nbsp;&nbsp;<img border = 0 src='img2/icono_01.png' alt='Modificar'>&nbsp;SEleccionar al Trabajador
&nbsp;&nbsp;<img border = 0 src='img2/libreta.png' alt='Modificar'>&nbsp;Modificar Cuotas
&nbsp;&nbsp;<img border = 0 src='img2/apuntes.png' alt='Constancia'>&nbsp;Modificar Saldo
&nbsp;&nbsp;<img border = 0 src='img2/icono_05.png' alt='Constancia'>&nbsp;Desactivar en este per&iacute;odo
&nbsp;&nbsp;<img border = 0 src='img2/reporte.png' alt='Contrato'>&nbsp;Imprimir Prestamo
&nbsp;&nbsp;<img border = 0 src='img2/icono_07.png' alt='Contrato'>&nbsp;Eliminar Prestamo";
if (substr($_SESSION['acceso_validar'],9,1)=='1') echo "&nbsp;&nbsp;<img border = 0 src='images/b_estavez.png' alt='Constante'>:<a href='calculo_carga.php'>Carga de Constantes</a>";
if (substr($_SESSION['acceso_validar'],9,1)=='1') echo "&nbsp;&nbsp;<img border = 0 src='images/b_reporte.png' alt='Carga x Trabajador'>:<a href='calculo_carga_trabajador.php'>Carga por Trabajador</a>";
echo "</font><hr>";
echo "<h2>Actualizaci&oacute;n de Cargas Autom&aacute;ticas</h2>";

// CARGAR REGISTRO DE NUMERO DE PRESTAMO
$consulta="Select * from empresa where id='".$_SESSION['id_validar']."' " ;
$resultado1 = mysqli_query($enlace,$consulta) or die('Verificaci&oacute; del C&oacute;digo fall&oacute;: ' . mysqli_error($enlace));
if ($num_rows == mysqli_num_rows($resultado1)) {
	echo "<br><br>Debe ingresar parametros de la Empresa...";
	exit();
}		

$accion=$_REQUEST["accion"];
$codigo=$_REQUEST["codigo"];
$cedula=$_REQUEST["cedula"];
$numero=$_REQUEST["numero"];

if ((!$_POST  ||  $_POST['busca']==1) && ($accion!=2) && ($accion!=5) && ($accion!=33)&& ($accion!=6)&& ($accion!=8) && ($accion!=4) && $_POST['nuevo']!=1 && $_POST['modifica']!=1 && $_POST['modificasaldo']!=1 && $_POST['graba']!=1){

/////////////////////////////////////////////////MOSTRAR SOLICITUD DE CEDULA ////////////////////////////////////////////////////////////////
	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>";
			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 = 'sso' ".($_POST['orden']=='sso'?'checked':'')."> Ficha";
		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' 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'] == "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'> 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 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'>".$row['sso']."</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='50' align='center'>".($row['status']=='A'?'<img src="img2/status_ver.png">':'<img src="img2/status_fal.png">')."</td>";
				echo "<td width='120' align=center>".$row['fechai']."</td>";
				
				echo "<td width='30' align='center'><a target='_blank' 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></form>";
	} else echo "<hr>"; 		
	mysqli_free_result($resultado);
}


///////////////////////////////////////////////// AGREGAR REGISTROS  ////////////////////////////////////////////////////////////////
if ($_POST['graba']==1 ) {
	$detallesuma= strtoupper(trim($_POST['sector'].' '.$_POST['detalle']));
	if ($_POST['minteres']=='') $_POST['minteres']=0;
	$suma=$_POST['minteres']+$_POST['monto'];

	$consulta="select numero from prestamo where id='".$_SESSION['id_validar']."' and cedula = '".$_POST['cedula']."' and saldo>0 and detalle = '".$detallesuma."'" ;
	$resultado = mysqli_query($enlace,$consulta) or die($consulta . mysqli_error($enlace));
	
	echo $consulta;
	exit();
	
	if (mysqli_num_rows($resultado)!=0) {
		$rowt = mysqli_fetch_array($resultado);
		$numero=$rowt['numero'];
		
		$consulta="update prestamo set monto = monto + ".$_POST['monto'].", minteres= minteres + ".$_POST['minteres'].", saldo= saldo + ".$suma."  where numero=".$numero." and id='".$_SESSION['id_validar']."' and cedula = '".$_POST['cedula']."' and saldo>0 and detalle = '".$detallesuma."' ";
		$resultado = mysqli_query($enlace,$consulta) or die($consulta . mysqli_error($enlace));

		$consulta="insert into prestamo (id, cedula, numero, fecha, tipo, detalle, monto, minteres, cuota, saldo, total) ";
		$consulta="values ('".$_SESSION['id_validar']."',ucase(trim('".$_POST['cedula']."')),".$_POST['numero'].",ucase(trim('".$_POST['fecha']."')),ucase(trim('".$_POST['tipo']."')),ucase(trim('".$detallesuma."')),".$_POST['monto'].",".$_POST['minteres'].",".$_POST['cuota'].",0,".$suma.")";
		$resultado = mysqli_query($enlace,$consulta) or die($consulta . mysqli_error($enlace));
		$resultado = mysqli_query($enlace,"update empresa set prestamo = ".$_POST['numero']." where id='".$_SESSION['id_validar']."' ") or die(mysqli_error($enlace));
		
	} else {	
		$consulta="insert into prestamo (id, cedula, numero, fecha, tipo, detalle, monto, minteres, cuota, saldo, total) ";
		$consulta="values ('".$_SESSION['id_validar']."',ucase(trim('".$_POST['cedula']."')),".$_POST['numero'].",ucase(trim('".$_POST['fecha']."')),ucase(trim('".$_POST['tipo']."')),ucase(trim('".$detallesuma."')),".$_POST['monto'].",".$_POST['minteres'].",".$_POST['cuota'].",".$suma.",".$suma.")";
		$resultado = mysqli_query($enlace,$consulta) or die($consulta . mysqli_error($enlace));
		$resultado = mysqli_query($enlace,"update empresa set prestamo = ".$_POST['numero']." where id='".$_SESSION['id_validar']."' ") or die(mysqli_error($enlace));
	}	
 }

///////////////////////////////////////////////// ACTUALIZAR REGISTROS  ////////////////////////////////////////////////////////////////
if ($_POST['modifica']==1 ) {
	if ($_POST['cuota']!="") {
		$consulta="update prestamo set cuota=".$_POST['cuota']." where id='".$_SESSION['id_validar']."' and cedula = '".$_POST['cedula']."' and numero='".$_POST['numero']."'" ;
		$resultado = mysqli_query($enlace,$consulta) or die($consulta . mysqli_error($enlace));
	}
}

///////////////////////////////////////////////// ACTUALIZAR REGISTROS  ////////////////////////////////////////////////////////////////
if ($accion==6 ) {
		$consulta="select interes from prestamo where id='".$_SESSION['id_validar']."' and cedula = '".$cedula."' and numero='".$numero."'" ;
		$resultado = mysqli_query($enlace,$consulta) or die($consulta . mysqli_error($enlace));
		if (mysqli_num_rows($resultado)!=0) {
			$rowt = mysqli_fetch_array($resultado);
			if ($rowt['interes']==0) $consulta="update prestamo set interes=1 where id='".$_SESSION['id_validar']."' and cedula='".$cedula."' and numero='".$numero."'" ;
			else $consulta="update prestamo set interes=0 where id='".$_SESSION['id_validar']."' and cedula='".$cedula."' and numero='".$numero."'" ;
			$resultado = mysqli_query($enlace,$consulta) or die($consulta . mysqli_error($enlace));
		}
}

///////////////////////////////////////////////// ELIMINAR REGISTROS  ////////////////////////////////////////////////////////////////
if ($accion==33 ) {
		$consulta="delete from prestamo where id='".$_SESSION['id_validar']."' and cedula = '".$cedula."' and numero='".$numero."'" ;
		$resultado = mysqli_query($enlace,$consulta) or die($consulta . mysqli_error($enlace));
}

///////////////////////////////////////////////// ACTUALIZAR REGISTROS  ////////////////////////////////////////////////////////////////
if ($_POST['modificasaldo']==1 ) {
	if ($_POST['saldo']!="") {
			$consulta="update prestamo set saldo=".$_POST['saldo']." where id='".$_SESSION['id_validar']."' and cedula = '".$_POST['cedula']."' and numero='".$_POST['numero']."'" ;
			$resultado = mysqli_query($enlace,$consulta) or die($consulta . mysqli_error($enlace));
	}
	if ($_POST['detalle']!="") {
			$consulta="update prestamo set detalle='".$_POST['detalle']."' where id='".$_SESSION['id_validar']."' and cedula = '".$_POST['cedula']."' and numero='".$_POST['numero']."'" ;
			$resultado = mysqli_query($enlace,$consulta) or die($consulta . mysqli_error($enlace));
	}
}

///////////////////////////////////////////////// MOSTRAR DATA A MODIFICAR  ////////////////////////////////////////////////////////////////
if ($accion==2) {
	$consulta="select prestamo.*, personal.nombres, personal.apellidos, personal.sso from prestamo,personal where personal.id='".$_SESSION['id_validar']."' and prestamo.id='".$_SESSION['id_validar']."' and prestamo.cedula='".$cedula."' and prestamo.numero='".$numero."' and prestamo.cedula=personal.codigo";
	$resultado = mysqli_query($enlace,$consulta) or die($consulta . mysqli_error($enlace));
	if (mysqli_num_rows($resultado)!=0) {
				$row=mysqli_fetch_array($resultado);
				echo "-->".$row['nombres'];
				$nombre=$row['nombres'];
				$apellido=$row['apellidos'];
				echo "<form name = 'entrada' action = '".$_SERVER['PHP_SELF']."' method = 'POST'  enctype='multipart/form-data'>";
				echo "<input type = 'hidden' name = 'modifica' value = '1'>";
				echo "<table cellpadding=2 cellspacing=0 width='500' align='center'>";

				// CARGAR REGISTRO DE NUMERO DE PRESTAMO
				$consulta="Select * from empresa" ;
				$resultado1 = mysqli_query($enlace,$consulta) or die('Verificaci&oacute; del C&oacute;digo fall&oacute;: ' . mysqli_error($enlace));
				$data = mysqli_fetch_array($resultado1);
				$prestamo=$data['prestamo']+1;			
				
				echo "	<table width = '500' align ='center'>";
				echo "	<tr >";
				echo "     <td align = 'right'> C&eacute;dula: </td>";
				echo "     <td><input type = 'text' name = 'cedula' size = '30' maxlength = '8' readonly value = '".$row['cedula']."'></td>";
				echo "	</tr>";
				echo "	<tr >";
				echo "     <td align = 'right'> Nombres: </td>";
				echo "     <td><input type = 'text' name = 'nombre' size = '30' maxlength = '30' readonly value = '".$nombre."'></td>";
				echo "	</tr>";
				echo "	<tr >";
				echo "     <td align = 'right'> Apellidos: </td>";
				echo "     <td><input type = 'text' name = 'apellido' size = '30' maxlength = '30' readonly value = '".$apellido."'></td>";
				echo "	</tr>";
				echo "	<tr >";
				echo "     <td align = 'right'> Ficha: </td>";
				echo "     <td><input type = 'text' name = 'apellido' size = '30' maxlength = '30' readonly value = '".$row['sso']."'></td>";
				echo "	</tr>";
				echo "	<tr >";
				echo "     <td align = 'right'># Prestamo: </td>";
				echo "     <td><input type = 'text' name = 'numero' size = '30' maxlength = '10' readonly value = '".$row['numero']."'></td>";
				echo "	</tr>";
				echo "	<tr>";
				echo "  <td align = 'right'>Fecha :</td>";
				echo "	<td><b> <input type = 'text' name = 'fecha' size = '30' maxlength = '10' readonly value = '".$row['fecha']."'></b></td>";
				echo"	</tr>";
				echo "	<tr>";
				echo "   <td align = 'right'>Tipo: </td>";
				echo "   <td >";
				if ($row['tipo']=="D") echo "Deducci&oacute;n</td>";
				else echo "Asignaci&oacute;n</td>";
				echo "	</tr>";
				echo "	<tr >";
				echo "     <td align = 'right'> Detalle: </td>";
				echo "     <td > <input type = 'text' name = 'detalle' size = '30' maxlength = '50' readonly value = '".$row['detalle']."'></td>";
				echo "	</tr>";
				echo "	<tr >";
				echo "     <td align = 'right'> Monto del Prestamo: </td>";
				echo "     <td> <input type = 'text' name = 'deuda' size = '20' maxlength = '30'  readonly value = '".SPRINTF( "%30.2f" ,$row['total'])."'></td>";
				echo "	</tr>";
				echo "	<tr >";
				echo "     <td align = 'right'> Monto del Saldo: </td>";
				echo "     <td> <input type = 'text' name = 'saldo' size = '20' maxlength = '30'  readonly value = '".SPRINTF( "%30.2f" ,$row['saldo'])."'></td>";
				echo "	</tr>";
				echo "	<tr >";
				echo "     <td align = 'right'> Monto de la Cuota: </td>";
				echo "     <td> <input type = 'text' name = 'cuota1' size = '20' maxlength = '30'  value = '".$row['cuota']."' readonly></td>";
				echo "	</tr>";
				echo "	<tr >";
				echo "     <td align = 'right'> Nueva Cuota: </td>";
				echo "     <td> <input type = 'text' name = 'cuota' size = '20' maxlength = '30'></td>";
				echo "	</tr>";
				echo "	</table>";
				echo "<table border=0  align = 'center'><tr>";
				echo "<td><input type = 'submit' value = 'Grabar'></td>";
				echo "</form>"; 
				echo "<form action = '".$_SERVER['PHP_SELF']."'>";
				echo "<td><input type = 'submit' value = 'Cancelar'></td>";
				echo "</form>"; 
				echo "	<script language='JavaScript' type='text/JavaScript'>";
				echo "		document.entrada.cuota.focus()";
				echo "	</script>";
			    echo "</body>";
				echo "</html>";
		}
}

///////////////////////////////////////////////// MOSTRAR DATA A MODIFICAR  SALDO///////////////////////////////////////////////////////////////
if ($accion==4) {
	$consulta="select * from prestamo,personal where prestamo.id='".$_SESSION['id_validar']."' and personal.id='".$_SESSION['id_validar']."' and prestamo.cedula='".$cedula."' and numero='".$numero."' and prestamo.cedula=personal.codigo";
	$resultado = mysqli_query($enlace,$consulta) or die($consulta . mysqli_error($enlace));
	if ($resultado!=0) {
				$row=mysqli_fetch_array($resultado);
				$nombre=$row['nombres'];
				$apellido=$row['apellidos'];
				echo "<form name = 'entrada' action = '".$_SERVER['PHP_SELF']."' method = 'POST'  enctype='multipart/form-data'>";
				echo "<input type = 'hidden' name = 'modificasaldo' value = '1'>";
				echo "<table cellpadding=2 cellspacing=0 width='500' align='center'>";
				// CARGAR REGISTRO DE NUMERO DE PRESTAMO
				$consulta="Select * from empresa" ;
				$resultado1 = mysqli_query($enlace,$consulta) or die('Verificaci&oacute; del C&oacute;digo fall&oacute;: ' . mysqli_error($enlace));
				$num_rows = mysqli_num_rows($resultado1);
				if ($num_rows == 0) {
					$consulta="insert into empresa values ('Demostracion','Demostracion','Demostracion',1)" ;
					$resultado1 = mysqli_query($enlace,$consulta) or die($consulta . mysqli_error($enlace));
					$prestamo=1;
				} else {
					$data = mysqli_fetch_array($resultado1);
					$prestamo=$data['prestamo']+1;			
				}		
				echo "	<table width = '500' align ='center'>";
				echo "	<tr >";
				echo "     <td align = 'right'> C&eacute;dula: </td>";
				echo "     <td><input type = 'text' name = 'cedula' size = '30' maxlength = '8' readonly value = '".$row['cedula']."'></td>";
				echo "	</tr>";
				echo "	<tr >";
				echo "     <td align = 'right'> Nombres: </td>";
				echo "     <td><input type = 'text' name = 'nombre' size = '30' maxlength = '30' readonly value = '".$nombre."'></td>";
				echo "	</tr>";
				echo "	<tr >";
				echo "     <td align = 'right'> Apellidos: </td>";
				echo "     <td><input type = 'text' name = 'apellido' size = '30' maxlength = '30' readonly value = '".$apellido."'></td>";
				echo "	</tr>";
				echo "	<tr >";
				echo "     <td align = 'right'> Ficha: </td>";
				echo "     <td><input type = 'text' name = 'apellido' size = '30' maxlength = '30' readonly value = '".$row['sso']."'></td>";
				echo "	</tr>";
				echo "	<tr >";
				echo "     <td align = 'right'># Prestamo: </td>";
				echo "     <td><input type = 'text' name = 'numero' size = '30' maxlength = '10' readonly value = '".$row['numero']."'></td>";
				echo "	</tr>";
				echo "	<tr>";
				echo "  <td align = 'right'>Fecha :</td>";
				echo "	<td><b> <input type = 'text' name = 'fecha' size = '30' maxlength = '10' readonly value = '".$row['fecha']."'></b></td>";
				echo"	</tr>";
				echo "	<tr>";
				echo "   <td align = 'right'>Tipo: </td>";
				echo "   <td >";
				if ($row['tipo']=="D") echo "Deducci&oacute;n</td>";
				else echo "Asignaci&oacute;n</td>";
				echo "	</tr>";
				echo "	<tr >";
				echo "     <td align = 'right'> Detalle: </td>";
				echo "     <td > <input type = 'text' name = 'detalle' size = '30' maxlength = '50' value = '".$row['detalle']."'></td>";
				echo "	</tr>";
				echo "	<tr >";
				echo "     <td align = 'right'> Monto del Prestamo: </td>";
				echo "     <td> <input type = 'text' name = 'deuda' size = '20' maxlength = '30'  readonly value = '".SPRINTF( "%30.2f" ,$row['total'])."'></td>";
				echo "	</tr>";
				echo "	<tr >";
				echo "     <td align = 'right'> Monto de la Cuota: </td>";
				echo "     <td> <input type = 'text' name = 'cuota' size = '20' maxlength = '30'  readonly value = '".SPRINTF( "%30.2f" ,$row['cuota'])."'></td>";
				echo "	</tr>";
				echo "	<tr >";
				echo "     <td align = 'right'> Monto del Saldo: </td>";
				echo "     <td> <input type = 'text' name = 'montosaldo' size = '20' maxlength = '30'  value = '".$row['saldo']."' readonly></td>";
				echo "	</tr>";
				echo "	<tr >";
				echo "     <td align = 'right'> Nuevo Saldo(Mayor): </td>";
				echo "     <td> <input type = 'text' name = 'saldo' size = '20' maxlength = '30' ></td>";
				echo "	</tr>";
				echo "	</table>";

				echo "<table border=0  align = 'center'><tr>";
				echo "<td><input type = 'submit' value = 'Grabar (Saldo o Detalle)'></td>";
				echo "</form>"; 
				echo "<form action = '".$_SERVER['PHP_SELF']."'>";
				echo "<td><input type = 'submit' value = 'Cancelar'></td>";
				echo "</form>"; 
				echo "	<script language='JavaScript' type='text/JavaScript'>";
				echo "		document.entrada.saldo.focus()";
				echo "	</script>";
			    echo "</body>";
				echo "</html>";
		}
}

///////////////////////////////////////////////// MOSTRAR DATA A INGRESAR ////////////////////////////////////////////////////////////////
if ($accion==5 || $_POST['modifica']==1 || $_POST['modificasaldo']==1 || $accion==6 || $accion==8 || $accion==33 || $_POST['graba']==1) {
		if ($accion==33 || $accion==5 || $_POST['modifica']==1 || $_POST['modificasaldo']==1 || $accion==6 || $_POST['graba']==1 || $accion==8 ) $consulta="select * from personal where codigo='".$cedula."' ";
		else $consulta="select * from personal where id='".$_SESSION['id_validar']."' and codigo='".$_POST['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);
		$row=mysqli_fetch_array($resultado);
		$cedula=$row['codigo'];
		$nombre=$row['nombres'];
		$apellido=$row['apellidos'];
		$nomina=trim($row['nomina']);
		$consulta3=mysqli_query($enlace,"select * from periodo where id='".$_SESSION['id_validar']."' and nomina='".$nomina."' order by periodo DESC limit 1");
		if (mysqli_num_rows($consulta3)!=0){
				$row3=mysqli_fetch_array($consulta3);
				if ($row3['pre']==1) $nograbar=1;
				else $nograbar=0;
		} 
		if ($nograbar==1)
			echo "<center><font color=red>".($nograbar==1?"No se puede Seleccionar al trabajador porque la Nómina esta en Pre-Cierre":"")."</font></center>";
		else {	
				echo "<form name = 'entrada' action = '".$_SERVER['PHP_SELF']."' method = 'POST'  enctype='multipart/form-data' onsubmit='return validar_prestamo(this)'>";
				echo "<input type = 'hidden' name = 'graba' value = '1'>";
				echo "<table cellpadding=2 cellspacing=0 width='500' align='center'>";
				$a=date ("Y")."-";
				$m=date ("n")."-";
				$d=date ("j");
				$Lfecha=$a.$m.$d;
				// CARGAR REGISTRO DE NUMERO DE PRESTAMO
				$consulta="Select * from empresa where id='".$_SESSION['id_validar']."'" ;
				$resultado1 = mysqli_query($enlace,$consulta) or die('Verificaci&oacute; del C&oacute;digo fall&oacute;: ' . mysqli_error($enlace));
				$num_rows = mysqli_num_rows($resultado1);
				if ($num_rows == 0) {
					$consulta="insert into empresa values ('Demostracion','Demostracion','Demostracion',1)" ;
					$resultado1 = mysqli_query($enlace,$consulta) or die($consulta . mysqli_error($enlace));
					$prestamo=1;
				} else {
					$data = mysqli_fetch_array($resultado1);
					$prestamo=$data['prestamo']+1;			
				}		
				echo "	<table width = '500' align ='center'>";
				echo "	<tr >";
				echo "     <td align = 'right'> C&eacute;dula: </td>";
				echo "     <td><input type = 'text' name = 'cedula' size = '30' maxlength = '8' readonly value = '".$cedula."'></td>";
				echo "	</tr>";
				echo "	<tr >";
				echo "     <td align = 'right'> Nombres: </td>";
				echo "     <td><input type = 'text' name = 'nombre' size = '30' maxlength = '30' readonly value = '".$nombre."'></td>";
				echo "	</tr>";
				echo "	<tr >";
				echo "     <td align = 'right'> Apellidos: </td>";
				echo "     <td><input type = 'text' name = 'apellido' size = '30' maxlength = '30' readonly value = '".$apellido."'></td>";
				echo "	</tr>";
				echo "	<tr >";
				echo "     <td align = 'right'> Ficha: </td>";
				echo "     <td><input type = 'text' name = 'apellido' size = '30' maxlength = '30' readonly value = '".$row['sso']."'></td>";
				echo "	</tr>";
				echo "	<tr >";
				echo "     <td align = 'right'># Prestamo: </td>";
				echo "     <td><input type = 'text' name = 'numero' size = '30' maxlength = '10' readonly value = '".$prestamo."'></td>";
				echo "	</tr>";
				echo "	<tr>";
				echo "  <td align = 'right'>Fecha :</td>";
				echo "	<td><b> <input type = 'text' name = 'fecha' size = '30' maxlength = '10' readonly value = '$Lfecha'></b></td>";
				echo "  </tr>";
				echo "  <tr>";
				echo "   <td align = 'right'>Tipo: </td>";
				echo "   <td > <select name = 'tipo'> <option> Deducci&oacute;n <option> Asignaci&oacute;n </select> </td>";
				echo "	</tr>";

				echo "   <tr>"; 
						cargar_select ("sector","Sector");
				echo "	</tr>";

				echo "	<tr >";
				echo "     <td align = 'right'> Detalle: </td>";
				echo "     <td > <input type = 'text' name = 'detalle' size = '30' maxlength = '50' value='PRESTAMO PERSONAL'></td>";
				echo "	</tr>";
				echo "	<tr >";
				echo "     <td align = 'right'> Monto Neto Bs.: </td>";
				echo "     <td > <input type = 'text' name = 'monto' size = '30' maxlength = '10'></td>";
				echo "	</tr>";
				echo "	<tr >";
				echo "     <td align = 'right'> Interes Bs.: </td>";
				echo "     <td > <input type = 'text' name = 'minteres' size = '30' maxlength = '10'></td>";
				echo "	</tr>";
				echo "	<tr >";
				echo "     <td align = 'right'> Monto de la Cuota: </td>";
				echo "     <td > <input type = 'text' name = 'cuota' size = '30' maxlength = '10'></td>";
				echo "	</tr>";
				echo "	</table>";
				echo "<table border=0  align = 'center'><tr>";
				echo "<td><input type = 'submit' value = 'Grabar'></td>";
				echo "</form>"; 
				echo "<td><input type='button' style='background:#C0D9D9;' value='Cerrar Ventana' name='cmdPrint2' onclick='window.close();'></td>";
				
				//echo "<form action = '".$_SERVER['PHP_SELF']."'>";
				//echo "<td><input type = 'submit' value = 'Cancelar / Buscar Otro'></td>";
				//echo "</form>"; 
				
				echo "	<script language='JavaScript' type='text/JavaScript'>";
				echo "		document.entrada.detalle.focus()";
				echo "	</script>";
				
				//$query="select * from prestamo where id='".$_SESSION['id_validar']."' and cedula='".$cedula."' and saldo>0 order by detalle,numero";
				$query="select * from prestamo where id='".$_SESSION['id_validar']."' and cedula='".$cedula."' order by numero";
				$consulta = mysqli_query($enlace,$query) or die ($query);
				$resultado = mysqli_num_rows($consulta);
				if ($resultado != 0) {

						echo "<table rules=none border=0 cellpadding=2 cellspacing=0 align='center'>";
						echo "<tr bgcolor='#43bced' height='35'>";
						echo "		<th ><font color=white> Numero </font></th>";
						echo "		<th ><font color=white> Tipo </font></th>";
						echo "		<th ><font color=white> Fecha </font></th>";
						echo "		<th ><font color=white> Detalle </font></th>";
						echo "		<th align = right><font color=white> Monto con Int. </font></th>";
						echo "		<th align = right><font color=white> Interes </font></th>";
						echo "		<th align = right><font color=white> Cuota </font></th>";
						echo "		<th align = right><font color=white> Saldo </font></th>";
						echo "		<th colspan=5 align = center><font  color=white> Acci&oacute;n </font></th><tr>";
				
						while ($row = mysqli_fetch_array($consulta)) {
							if ($color) $tcolor="#dfdfd0";
							else $tcolor="white";
							$color=!$color;
							echo '<tr bgcolor='.$tcolor.' >';
							echo "<td align='left'>".$row['numero']."</td>";
							echo "<td align='center'>".$row['tipo']."</td>";
							echo "<td width='150'align='center'>".$row['fecha']."</td>";
							echo "<td width='200'>".$row['detalle']."</td>";
							echo "<td align='right'>".SPRINTF( "%18.2f" ,$row['monto'])."</td>";
							echo "<td align='right'>".SPRINTF( "%18.2f" ,$row['minteres'])."</td>";
							if ($row['interes']!=0) echo "<td width='150' align='right'><font color='red'>".SPRINTF( "%18.2f" ,$row['cuota'])."</font></td>";
							else echo "<td  width='150' align='right'>".SPRINTF( "%18.2f" ,$row['cuota'])."</td>";
							echo "<td  width='150' align='right'><b>".SPRINTF( "%18.2f" ,$row['saldo'])."</b></td>";
							$cedula = $row['cedula'];
							$numero = $row['numero'];
							$saldo = $row['saldo'];
							echo "<td width='60' align='center'><a href='".$_SERVER['PHP_SELF']."?accion=2&cedula=".$cedula."&numero=".$numero."'><img border = 0 src='img2/libreta.png' alt='MODIFICAR la CUOTA'></a></td>";
							echo "<td width='60' align='center'><a href='".$_SERVER['PHP_SELF']."?accion=4&cedula=".$cedula."&numero=".$numero."&saldo=".$saldo."'><img border = 0 src='img2/apuntes.png' alt='MODIFICAR el SALDO o Detalle'></a></td>";
							echo "<td width='60' align='center'><a href='".$_SERVER['PHP_SELF']."?accion=6&cedula=".$cedula."&numero=".$numero."'><img border = 0 src='img2/icono_05.png' alt='DESACTIVAR en este Periodo'></a></td>";
							if ($row['tipo']=='D')		
								echo "<td width='60' align='center'><a target='_blank' href='calculo_prestamo_imprimir.php?cedula=".$cedula."&numero=".$numero."'><img border = 0 src='img2/reporte.png' alt='IMPRIMIR recibo'></a></td>";
								else echo "<td width='60' align='center'>&nbsp;</td>";
							if (substr($_SESSION['acceso_validar'],18,1)=='1')
								echo "<td  width='60' align='center'><a onclick='return validar_elimina()' href='".$_SERVER['PHP_SELF']."?accion=33&cedula=".$cedula."&numero=".$numero."'><img border = 0 src='img2/icono_07.png' alt='Eliminar Registro'></a></td>";
								else echo "<td width='60' align='center'>&nbsp;</td>";
						}
						echo "</table>";
				}
		}
		echo "<br><br><br></body>";
		echo "</html>";
	}
mysqli_close($enlace);
?>