<?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";
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;(2)<a href='master_concepto_nomina.php'>Desact/N&oacute;mina</a>";
echo "&nbsp;&nbsp;(3)<a href='master_concepto_periodo.php'>Desact/Per&iacute;odo</a>";
echo "</font><hr>";
echo "<h2>Desactivar Conceptos por Condici&oacute;n</h2>";
$accion=$_REQUEST["accion"];
$concepto=$_REQUEST["concepto"];
$cedula=$_REQUEST["cedula"];
$codigo=$_REQUEST["codigo"];

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']."'  enctype='multipart/form-data' onSubmit='return validar_valores(this)'>
				<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></td></tr></table>";

		// P E D I R   VALORES    P A R A   G R A B A R
		echo "<br>";
		echo "<table width = '500' align = 'center'>";
		echo "<input type = 'hidden' name = 'graba' value = '1'>";
		echo "<tr>	<td align = 'right'>Valor 1: </td><td><input type='text' name='valor1' size='20' maxlength='50'></td></tr>";
		echo "<tr>	<td align = 'right'>Condicion:</td><td><select name='condicion'><option selected>=<option>>=<option><=<option><><option>><option><</select></td></tr>";
		echo "<tr>	<td align = 'right'>Valor 2: </td><td><input type='text' name='valor2' size='20' maxlength='50'></td></tr>";
		echo "<tr>	<td></td><td><input type='submit' value='Grabar'></td></tr>";			
		echo "</table></form>";
		echo "	<script language='JavaScript' type='text/JavaScript'>";
		echo "		ENTRADA.valor1.focus()";
		echo "	</script>";

		// ELIMINAR REGISTROS
		if ($accion == 3) {
			$query = "delete from concepto_condicion where id='".$_SESSION['id_validar']."' and codigo = '".substr($concepto,0,4)."'  limit 1"; 
			$consulta = mysqli_query($enlace,$query) or die ($query);
		}

		//AGREGAR REGISTROS
		if ($_POST['graba'] == '1') {
					$query = "select * from concepto_condicion 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...");
					$resultado2 = mysqli_num_rows($consulta);
					
						$query = "insert into concepto_condicion (id,codigo,valor1,valor2,condicion) values ('".$_SESSION['id_validar']."',";
						$query .= "ucase('".$_POST['CONCEPTO']."'), ";
						$query .= "ucase('".$_POST['valor1']."'), ";
						$query .= "ucase('".$_POST['valor2']."'), ";
						$query .= "ucase('".$_POST['condicion']."'))";
						$consulta = mysqli_query($enlace,$query) or die ($query);
					
					/*
					else {
						$query = "update concepto_condicion set ";
						$query .= "valor1=ucase('".$_POST['valor1']."'), ";
						$query .= "valor2=ucase('".$_POST['valor2']."'), ";
						$query .= "condicion='".$_POST['condicion']."' where id='".$_SESSION['id_validar']."' and codigo='".substr($_POST['CONCEPTO'],0,4)."' ";
						$consulta = mysqli_query($enlace,$query) or die ($query);
					}
					*/
		}			

		//M O S T R A R   I N F O R M A C I O N
		$query = "select * from concepto_condicion 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);
		echo "<table width = '400' border align = 'center'>";
		echo "	<tr bgcolor = '#00BFFF'>";
		echo "		<th align = center><font face='verdana' size=2> C&oacute;digo </font></th>";
		echo "		<th ><font face='verdana' size=2> Valor1 </font></th>";
		echo "		<th align = center><font face='verdana' size=2> Condici&oacute;n </font></th>";
		echo "		<th ><font face='verdana' size=2> Valor2 </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['codigo']."</td>";
				echo "<td>".$row['valor1']."</td>";
				echo "<td align='center'>".$row['condicion']."</td>";
				echo "<td>".$row['valor2']."</td>";
				echo "<td align='center'>";
				$concepto = $row['codigo'];
				echo "<a href = '".$_SERVER['PHP_SELF']."?accion=3&concepto=".$concepto."'> Eliminar </a></td></tr>";
			}
			mysqli_free_result($consulta);
		}
		echo "</table>";
}
	
echo "</font></body>";
echo "</html>";
  
