<?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();

$entra=0;
echo "<b>Opciones:</b> &nbsp;&nbsp;<img border = 0 src='images/b_elimina.png' alt='Elimina'>:Eliminar Conceptos	
&nbsp;&nbsp;<img border = 0 src='images/b_edit.png' alt='Modificar'>:Modificar Conceptos";
echo "&nbsp;&nbsp;<img border = 0 src='images/b_estavez.png' alt='Concepto'><a href='master_concepto.php'> Conceptos</a>";
echo "&nbsp;&nbsp;(1)<a href='master_concepto_empleado.php'>Desact/Trabajador</a>";
echo "&nbsp;&nbsp;(3)<a href='master_concepto_periodo.php'>Desact/Per&iacute;odo</a>";
echo "&nbsp;&nbsp;(4)<a href='master_concepto_condicion.php'>Desact/Condici&oacute;n</a>";
echo "</font><hr>";
echo "<h2>Desactivar Conceptos por N&oacute;mina</h2>";
$accion=$_REQUEST["accion"];
$concepto=$_REQUEST["concepto"];
$cedula=$_REQUEST["cedula"];
$codigo=$_REQUEST["codigo"];
$nomina=$_REQUEST["nomina"];

echo "<font face='verdana' size=2>";
$query = "select * from concepto where id='".$_SESSION['id_validar']."' order by codigo";
$consulta = mysqli_query($enlace,$query) or die ("No se pudo consultar con la tabla...");
$resultado = mysqli_num_rows($consulta);
if ($resultado == 0) {
	echo "<table width = '50%'>";
	echo "<tr><td><h3>No se han ingresado CONCEPTOS...</h3></tr></td></table>";
} else {

// MUESTRA CONCEPTOS
	echo"	<table width = '500' align='center'>
			<form name = 'ENTRADA' method = 'POST' action = '".$_SERVER['PHP_SELF']."'>
			<tr> <td align = 'right'> Concepto: </td>
			<td align = 'left'> <select name = 'CONCEPTO'>";
			for ($i=0;$i<$resultado;$i++) {
				$row = mysqli_fetch_array($consulta);
				if ($_POST['CONCEPTO']!="") {
					if (substr($_POST['CONCEPTO'],0,4)==$row['codigo']) 
						echo "<option selected>".$row['codigo']." - ".$row['detalle'] ;
					else echo "<option>".$row['codigo']." - ".$row['detalle'] ;
				} else
					if ($concepto!="") {
						if (substr($concepto,0,4)==$row['codigo']) 
							echo "<option selected>".$row['codigo']." - ".$row['detalle'] ;
						else echo "<option>".$row['codigo']." - ".$row['detalle'] ;
					} else {
						echo "<option>".$row['codigo']." - ".$row['detalle'] ;				
					}
			}
	echo"   </select>&nbsp;&nbsp;&nbsp;&nbsp;
			<input type = 'submit' name =  'BUSCAR' value = '   Buscar   '>
			<input type = 'hidden' name = 'ACCION' value = '1'>
			</td></tr></form></table>";

// ELIMINAR REGISTROS
if ($accion == 3) {
				$query = "delete from concepto_nomina where id='".$_SESSION['id_validar']."' and codigo = '".substr($concepto,0,4)."' and nomina = '".substr($nomina,0,3)."' limit 1"; 
				$consulta = mysqli_query($enlace,$query) or die ("No se pudo consultar con la tabla...");
}

//AGREGAR REGISTROS
if ($_POST['ACCION'] == '2') {
			$query = "select * from concepto_nomina where id='".$_SESSION['id_validar']."' and codigo = '".substr($_POST['CONCEPTO'],0,4)."' and nomina = '".$_POST['nomina']."'";
			$consulta = mysqli_query($enlace,$query) or die ("No se pudo consultar con la tabla...");
			$resultado2 = mysqli_num_rows($consulta);
			if ($resultado2 == 0) {
				$query = "insert into concepto_nomina (id,codigo,nomina) values ('".$_SESSION['id_validar']."',";
				$codigo = "'".$_POST['CONCEPTO']."', ";
				$nomina = "'".$_POST['nomina']."')";
				$consulta = mysqli_query ($enlace, $query.$codigo.$nomina) or die ($query.$codigo.$nomina." ".mysqli_error($enlace));
			}
}			

// MUESTRA ENCABEZADO DE ENTRADA
if ($_POST['CONCEPTO'] != "" || $concepto != "") {
	if($concepto!="") $query = "select * from concepto where id='".$_SESSION['id_validar']."' and codigo = '".substr($concepto,0,4)."'";
	else $query = "select * from concepto where id='".$_SESSION['id_validar']."' and codigo = '".substr($_POST['CONCEPTO'],0,4)."'";
		$consulta = mysqli_query($enlace,$query) or die ("No se pudo consultar con la tabla...");
		$row = mysqli_fetch_array($consulta);
		$concepto = $row['codigo']." - ".$row['detalle'];
		// P E D I R   NOMINA    P A R A   G R A B A R
		
		echo "<br>";
		echo "<table width = '400' align = 'center'>";
		echo "<form name = 'ENTRADA1' action = '".$_SERVER['PHP_SELF']."' method = 'POST'  enctype='multipart/form-data' onSubmit='return validar_nomina(this)'>";
		echo "<input type = 'hidden' name = 'ACCION' value = '2'>";
		echo "<input type = 'hidden' name = 'CONCEPTO' value = '".$concepto."'>";

		$query = "select * from nomina where id='".$_SESSION['id_validar']."' order by codigo";
		$consulta = mysqli_query($enlace,$query) or die ("no se pudo realizar la consulta...");	
		$resultado = mysqli_num_rows($consulta);
		echo "<td align = 'right'> N&oacute;mina: </td>";
		echo "<td align = 'left'> <select name = 'nomina'>";
		if ($resultado != 0) {
			for ($i=0;$i<$resultado;$i++) {
				$row1 = mysqli_fetch_array($consulta);
				if ($row1['codigo']." - ".$row1['detalle']==$row['nomina']) echo "<option width = '30%' selected>".$row1['codigo']." - ".$row1['detalle'];
				else echo "<option width = '30%' value='".$row1['codigo']."'>".$row1['codigo']." - ".$row1['detalle'];
			}
		}	
		echo "</select></td>";
		echo "<td><input type = 'submit' name =  'BUSCAR' value = '   Grabar   '>";
		echo "</td></tr></form></table>";
		echo "	<script language='JavaScript' type='text/JavaScript'>";
		echo "		ENTRADA1.nomina.focus()";
		echo "	</script>";

		//M O S T R A R   I N F O R M A C I O N
		$query = "select * from concepto_nomina where id='".$_SESSION['id_validar']."' and codigo = '".substr($concepto,0,4)."'";
		$consulta = mysqli_query($enlace,$query) or die ("No se pudo consultar con la tabla...");
		$resultado = mysqli_num_rows($consulta);
		echo "<table width = '400' border align = 'center'>";
		echo "	<tr bgcolor = '#00BFFF'>";
		echo "		<th align = center><font face='verdana' size=2> N&oacute;mina </font></th>";
		echo "		<th align = center><font face='verdana' size=2> Acci&oacute;n </font></th><tr>";
		if ($resultado != 0) {
			while ($row = mysqli_fetch_array($consulta)) {
				echo "<tr bgcolor = '#cdcdcd'>";
				echo "<td>".$row['nomina']."</td>";
				echo "<td align='center'>";
				$nomina = $row['nomina'];
				echo "<a href = '".$_SERVER['PHP_SELF']."?accion=3&concepto=".$concepto."&nomina=".$nomina."'> Eliminar </a></td></tr>";
			}
			mysqli_free_result($consulta);
		}
		echo "</table>";
	}
}	
echo "</font></body>";
echo "</html>";
?>
