<?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=mysql_query("select * from periodo where id='".$_SESSION['id_validar']."' order by nomina,periodo desc limit 1");
if (mysql_num_rows($consulta)!=0){
	$row=mysql_fetch_array($consulta);
	$per=mostrar_fecha($row['desde']).' al '.mostrar_fecha($row['hasta'])." (Nom:".$row['nomina'].")";
} else $per=''; 
$accion=$_REQUEST["accion"];
$codigo=$_REQUEST["codigo"];
$tabla=$_REQUEST["tabla"];
$detalle=$_REQUEST["detalle"];
if ($_POST['imprime']==1 ) {
///////////////////////////////////////////////// IMPRIMIR REGISTROS ////////////////////////////////////////////////////////////////
	require('fphp.php');
	$enlace=cargar_conexion();
	$consulta=mysql_query("select * from empresa where id='".$_SESSION['id_validar']."' ");
	if (mysql_num_rows($consulta)!=0){
			$row=mysql_fetch_array($consulta);
			$_POST['pie']=$row['pie'];
			$_POST['encabeza']=$row['encabeza'];
			$_POST['empresa']=(substr($_POST['nomina'],6,100)==""?$_SESSION['empresa_validar']:substr($_POST['nomina'],6,100)) ;
	}   
	require('fpdf.php');

	class PDF extends FPDF {
	function Header()
	{
	    $this->Image($_SESSION['logo_validar'],10,10,20,20);
	    $this->SetFont('arial','',12);
		$this->Ln(2);
	    $this->Cell(0,0,$_POST['empresa'],0,1,'R'); $this->Ln(5);
		$this->Cell(0,0,$_POST['encabeza'],0,1,'R');$this->Ln(5);
		$this->SetFont('arial','',10);
	    $this->Cell(140);$this->Cell(40,0,'Fecha: '.hoy(),0,1,'L'); $this->Ln(5);
	    $this->Cell(140);$this->Cell(40,0,'Usuario: '.$usuario,0,1,'L'); $this->Ln(5);

		$this->Ln(10);
		$this->SetFont('arial','B',14);
		$this->Cell(0,0,'Reporte General de Tablas',0,1,'C');$this->Ln(5);
	    $header=array('Item','Codigo','Tabla','Detalle');
	    $this->SetFillColor(255,0,0);
	    $this->SetTextColor(255);
	    $this->SetDrawColor(128,0,0);
	    $this->SetLineWidth(.3);
	    $this->SetFont('','');
	    //Cabecera
	    $w=array(15,30,40,100);
		for($i=0;$i<count($header);$i++)
	        $this->Cell($w[$i],5,$header[$i],1,0,'C',1);
	    $this->Ln();
	    //Restauración de colores y fuentes
	    $this->SetFillColor(224,235,255);
	    $this->SetTextColor(0);
	    $this->SetFont('');
	}


//Pie de página ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	function Footer()
	{
	$this->SetY(-15);
	$this->Cell(0,0,'',1,1,'C');
	$this->SetFont('Times','I',8);
	$this->ln(2);
	$this->Cell(20);$this->Cell(0,0,$_POST['pie'],0,1,'C');
	$this->Cell(0,10,'Página '.$this->PageNo().'/{nb}',0,0,'C');
	}
}
//Cuerpo ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	$pdf=new PDF();
	$pdf->AddPage();
	$pdf->AliasNbPages();
	$pdf->SetFont('Arial','',10);
	$pdf->SetFillColor(224,235,255);
	$pdf->SetTextColor(0);
	$consulta=mysql_query('SELECT * from tablas');
	$linea=0;
	$color=0;
	while($row=mysql_fetch_array($consulta)) {
		$linea++;
		$pdf->cell(15,5,$linea,0,0,'C',$color);
		$pdf->cell(30,5,$row['codigo'],0,0,'Z',$color);
		$pdf->cell(40,5,$row['tabla'],0,0,'',$color);
		$pdf->cell(100,5,$row['detalle'],0,1,'',$color);
		$color=!$color;
	}
	$pdf->Output();
}
else {

menu();
echo "<font face='verdana' size=2>";
$entra=0;
echo "<b>Opciones:</b> &nbsp;&nbsp;<img border = 0 src='images/b_elimina.png' alt='Elimina'>:Eliminar Estructura	
&nbsp;&nbsp;<img border = 0 src='images/b_edit.png' alt='Modificar'>:Modificar Estructura
</font><hr>";
echo "<h2>Actualizaci&oacute;n de Estructura</h2>";

///////////////////////////////////////////////// INICIO DEL PROGRAMA ////////////////////////////////////////////////////////////////
///////////////////////////////////////////////// AGREGAR REGISTROS  ////////////////////////////////////////////////////////////////
	if ($_POST['graba']==1 ) {
		$consulta="Select * from tablas where id='".$_SESSION['id_validar']."' and codigo = '".$_POST['codigo']."' and tabla=ucase('".$_POST['tabla']."')" ;
		$resultado = mysql_query($consulta) or die('Verificaci&oacute; del C&oacute;digo fall&oacute;: ' . mysql_error());
		$num_rows = mysql_num_rows($resultado);
		if ($num_rows == 0) {
			$consulta="insert into tablas (id,codigo,tabla,detalle) values ('".$_SESSION['id_validar']."', '".$_POST['codigo']."',ucase('".$_POST['tabla']."'),ucase('".$_POST['detalle']."'))";
			$resultado = mysql_query($consulta) or die('Verificaci&oacute; del C&oacute;digo fall&oacute;: ' . mysql_error());
		} else $entra=1;
	}
    if ($entra) echo "<font face='Verdana, Arial, Helvetica, sans-serif' color ='#FF0000'size=3> (Registro Existe)</font>";
///////////////////////////////////////////////// ACTUALIZAR REGISTROS  ////////////////////////////////////////////////////////////////
	if ($_POST['modifica']==1 ) {
		$consulta="update tablas set detalle=ucase('".$_POST['detalle']."') where id='".$_SESSION['id_validar']."' and codigo = '".$_POST['codigo']."' and tabla=ucase('".$_POST['tabla']."')" ;
		$resultado = mysql_query($consulta) or die('Verificaci&oacute; del C&oacute;digo fall&oacute;: ' . mysql_error());
	}
///////////////////////////////////////////////// MOSTRAR ENTRADA DE DATOS  ////////////////////////////////////////////////////////////////
	if (!$_POST && $accion!=3 && $accion!=2 && $_POST['busca']!=1 && $_POST['modifica']!=1 && $_POST['graba']!=1){
		echo "<form name = 'entrada' action = '".$_SERVER['PHP_SELF']."' method = 'POST'  enctype='multipart/form-data' onSubmit='return validar_codigo(this)'>";
		echo "<input type = 'hidden' name = 'busca' value = '1'>";
		echo "<table cellpadding=2 cellspacing=0 width='400' align='center'>";
		echo "<tr>";
			echo "<td> <font face='Verdana, Arial, Helvetica, sans-serif' size=2 >Tabla Activa:</font></td>";
			echo "<td> <font face='Verdana, Arial, Helvetica, sans-serif' size=2 >";
			echo "<select name='tabla'>";
			if ($_POST && $_POST['tabla']=="Cargo") echo "<option selected>Cargo";
		    else echo "<option>Cargo";
			if ($_POST && $_POST['tabla']=="Departamento") echo "<option selected>Departamento";
		    else echo "<option>Departamento";
			if ($_POST && $_POST['tabla']=="Empresa") echo "<option selected>Empresa";
		    else echo "<option>Empresa";
			echo "</td> </font>";
		echo "<td><input name = 'procesar' type = 'submit' value = 'Buscar Registro'></td>";
		echo "</tr>";
		echo "</table>";
		echo "	<script language='JavaScript' type='text/JavaScript'>";
		echo "		document.entrada.tabla.focus()";
		echo "	</script>";
		echo "	</form>"; 
}		

///////////////////////////////////////////////// ELIMINAR REGISTROS ////////////////////////////////////////////////////////////////
	if ($accion==2) { 
		$consulta="delete from tablas where id='".$_SESSION['id_validar']."' and codigo = '".$codigo."' and tabla=ucase('".$tabla."')" ;
		$resultado = mysql_query($consulta) or die('Verificaci&oacute; del C&oacute;digo fall&oacute;: ' . mysql_error());
	}
	
///////////////////////////////////////////////// MODIFICAR REGISTROS ////////////////////////////////////////////////////////////////
	if ($accion==3) {
		if ($_POST['tabla']!="") $tabla=$_POST['tabla'];
		echo "<form name = 'entrada' action = '".$_SERVER['PHP_SELF']."' method = 'POST'  enctype='multipart/form-data' onSubmit='return validar_codigo(this)'>";
		echo "<input type = 'hidden' name = 'modifica' value = '1'>";
		echo "<table cellpadding=2 cellspacing=0 width='600' align='center'>";
		echo "<tr>";
			echo "<td> <font face='Verdana, Arial, Helvetica, sans-serif' size=2 >Tabla Activa:</font></td>";
			echo "<td> <input name='tabla' size = '30' maxlength = '30' readonly value='".$tabla."' ></td>";
		echo "<tr>";
			echo "<td> <font face='Verdana, Arial, Helvetica, sans-serif' size=2 >C&oacute;digo:</font></td>";
			echo "<td> <input name='codigo' size = '4' maxlength = '4' readonly value='".$codigo."' ></td>";
		echo "</tr>";
		echo "<tr>";
			echo "<td> <font face='Verdana, Arial, Helvetica, sans-serif' size=2 >Detalle (Descripci&oacute;n):</font></td>";
			echo "<td> <input name='detalle' size = '30' maxlength = '50' value='".$detalle."'></td>";
		echo "</tr>";
		echo "</table>";
		echo "<table border=0  align = 'center'><tr>";
		echo "<td><input type = 'submit' value = 'Grabar Modificaci&oacute;n'></td>";
		echo "</form>"; 
		echo "<form action = '".$_SERVER['PHP_SELF']."' method = 'POST' >";
		echo "<input type = 'hidden' name = 'busca' value = '1'>";
		echo "<td><input type = 'submit' value = 'Cancelar'><input type='hidden' name='tabla' size = '30' maxlength = '30' value='".$tabla."' ></td>";
		echo "</form></table><br>"; 				
	}
	
///////////////////////////////////////////////// ACTUALIZAR REGISTROS  ////////////////////////////////////////////////////////////////
	if ($_POST['busca']==1 || $_POST['graba']==1 || $accion==2 || $_POST['modifica']==1 ) {
		if ($_POST['tabla']!="") $tabla=$_POST['tabla'];
		echo "<form name = 'entrada' action = '".$_SERVER['PHP_SELF']."' method = 'POST'  enctype='multipart/form-data' onSubmit='return validar_codigo(this)'>";
		echo "<input type = 'hidden' name = 'graba' value = '1'>";
		echo "<table cellpadding=2 cellspacing=0 width='600' align='center'>";
		echo "<tr>";
			echo "<td> <font face='Verdana, Arial, Helvetica, sans-serif' size=2 >Tabla Activa:</font></td>";
			echo "<td> <input name='tabla' size = '30' maxlength = '30' readonly value='".$tabla."' ></td>";
		echo "<tr>";
			echo "<td> <font face='Verdana, Arial, Helvetica, sans-serif' size=2 >C&oacute;digo:</font></td>";
			echo "<td> <input name='codigo' size = '4' maxlength = '4' onkeyup='verificar_codigo(this.form)'></td>";
		echo "</tr>";
		echo "<tr>";
			echo "<td> <font face='Verdana, Arial, Helvetica, sans-serif' size=2 >Detalle (Descripci&oacute;n):</font></td>";
			echo "<td> <input name='detalle' size = '30' maxlength = '50'></td>";
		echo "</tr>";
		echo "</table>";

		echo "<table border=0  align = 'center'><tr>";
		echo "<td><input name = 'procesar' type = 'submit' value = 'Grabar Registro Nuevo' disabled></td>";
		echo "</form>"; 
		echo "<form action = '".$_SERVER['PHP_SELF']."'>";
		echo "<td><input type = 'submit' value = 'Seleccionar otra Tabla'></td>";
		echo "</form></table><br>"; 				
		echo "<script language='JavaScript' type='text/JavaScript'>";
		echo "		document.entrada.codigo.focus()";
		echo "</script>";
		///////////////////////////////////////////////// MOSTRAR TABLA DE REGISTROS ////////////////////////////////////////////////////////////////
		$consulta="Select * from tablas where id='".$_SESSION['id_validar']."' and tabla=ucase('".$tabla."') order by codigo,detalle";
		$resultado = mysql_query($consulta) or die($consulta . mysql_error());
		$num_rows = mysql_num_rows($resultado);
		if ($num_rows != 0){
				echo "<table border=1 cellpadding=2 cellspacing=0 width='600' align='center'>";
				echo "<tr BGcolor='#00BFFF' >";
				echo "<th ali=center><font face='verdana' size=2>C&oacute;digo</font></th> ";
				echo "<th ali=center><font face='verdana' size=2>Detalle</font></th>";
				echo "<th colspan=2 ali=center><font face='verdana'  size=2>Acci&oacute;n</font></th></tr>";
				for ($cont=0;$cont<$num_rows;$cont++) {
					$row = mysql_fetch_array($resultado);
					echo '<tr bgcolor="#cdcdcd" >';
					echo '<td width="100" align="center"><font face="Verdana, Arial, Helvetica, sans-serif" size=2 >'.$row['codigo'].'</font></td>';
					echo '<td width="400"><font face="Verdana, Arial, Helvetica, sans-serif" size=2 >'.$row['detalle'].'</font></td>';
					$codigo = $row['codigo'];
					$detalle = $row['detalle'];
					$tabla = $row['tabla'];
					echo "<td width='60' align='center'><a onclick='return validar_elimina()' href='".$_SERVER['PHP_SELF']."?accion=2&codigo=".$codigo."&tabla=".$tabla."'><img  border = 0  src='images/b_elimina.png' alt='ELIMINAR éste registro' ></a></td>";
					echo "<td width='60' align='center'><a href='".$_SERVER['PHP_SELF']."?accion=3&codigo=".$codigo."&tabla=".$tabla."&detalle=".$detalle."'><img border = 0 src='images/b_edit.png' alt='MODIFICAR éste registro'></a></td>";
					echo '</tr>';
				}
				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>"; 		
		mysql_free_result($resultado);
		mysql_close($enlace);	
}	

    echo "</body>";
	echo "</html>";
}
?>