<?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');
$accion=$_REQUEST["accion"];
$codigo=$_REQUEST["codigo"];
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']=''; 
if ($_POST['accion']==3 ) {
///////////////////////////////////////////////// IMPRIMIR REGISTROS ////////////////////////////////////////////////////////////////
	$consulta=mysqli_query($enlace,"select * from empresa where id='".$_SESSION['id_validar']."' ");
	if (mysqli_num_rows($consulta)!=0){
			$row=mysqli_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(0,0,'Fecha: '.hoy(),0,1,'L'); $this->Ln(5);
	    $this->Cell(140);$this->Cell(0,0,'Usuario: '.$usuario,0,1,'L'); $this->Ln(5);

		$this->Ln(10);
		$this->SetFont('arial','B',14);
		$this->Cell(0,0,'Reporte General de Constantes',0,1,'C');$this->Ln(5);
	    $header=array('Item','Codigo','Detalle','Unidad','Fijo');
	    $this->SetFillColor(255,0,0);
	    $this->SetTextColor(255);
	    $this->SetDrawColor(128,0,0);
	    $this->SetLineWidth(.3);
	    $this->SetFont('','');
	    //Cabecera
	    $w=array(15,30,60,60,15);
		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=mysqli_query($enlace,'SELECT * from constante');
	$linea=0;
	$color=0;
	while($row=mysqli_fetch_array($consulta)) {
		$linea++;
		$pdf->cell(15,5,$linea,0,0,'C',$color);
		$pdf->cell(30,5,$row['codigo'],0,0,'C',$color);
		$pdf->cell(60,5,$row['detalle'],0,0,'',$color);
		$pdf->cell(60,5,$row['unidad'],0,0,'',$color);
		$pdf->cell(15,5,$row['fijo'],0,1,'C',$color);
		$color=!$color;
	}
	$pdf->Output();
}
else {
///////////////////////////////////////////////// INICIO DEL PROGRAMA ////////////////////////////////////////////////////////////////
menu();

$entra=0;
echo "<b>Opciones:</b> &nbsp;&nbsp;<img border = 0 src='images/b_elimina.png' alt='Elimina'>:Eliminar Constante	
&nbsp;&nbsp;<img border = 0 src='images/b_edit.png' alt='Modificar'>:Modificar Constante
</font><hr>";
echo "<h2>Actualizaci&oacute;n de Constantes</h2>";

echo "<font face='verdana' size=2>";
///////////////////////////////////////////////// AGREGAR REGISTROS  ////////////////////////////////////////////////////////////////
	if ($_POST['graba']==1 ) {
		$consulta="Select * from constante where id='".$_SESSION['id_validar']."' and codigo = '".$_POST['codigo']."'" ;
		$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) {
			$consulta="insert into constante (id,codigo,detalle,unidad,fijo) values ('".$_SESSION['id_validar']."','".$_POST['codigo']."',ucase('".$_POST['detalle']."'),ucase('".$_POST['unidad']."'),'".$_POST['fijo']."')";
			$resultado = mysqli_query($enlace,$consulta) or die('Verificaci&oacute; del C&oacute;digo fall&oacute;: ' . mysqli_error($enlace));
		} 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 constante set detalle=ucase('".$_POST['detalle']."'),unidad=ucase('".$_POST['unidad']."'),fijo = ucase('".$_POST['fijo']."') where id='".$_SESSION['id_validar']."' and codigo='".$_POST['codigo']."' " ;
		$resultado = mysqli_query($enlace,$consulta) or die('Verificaci&oacute; del C&oacute;digo fall&oacute;: ' . mysqli_error($enlace));
	}
///////////////////////////////////////////////// MOSTRAR ENTRADA DE DATOS  ////////////////////////////////////////////////////////////////
	if ((!$_POST || $accion==2 || $_POST['graba']==1|| $_POST['modifica']==1) && $accion!=3){
		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='500' align='center'>";
		echo "<tr>";
			echo "<td> C&oacute;digo:</td>";
			echo "<td> <input name='codigo' size = '4' maxlength = '4' onkeyup='verificar_codigo(this.form)'></td>";
		echo "</tr>";
		echo "<tr>";
			echo "<td> Detalle (Descripci&oacute;n):</td>";
			echo "<td> <input name='detalle' size = '30' maxlength = '50'></td>";
		echo "</tr>";
		echo "<tr>";
			echo "<td> Cuenta Contable:</td>";
			echo "<td> <input name='unidad' size = '30' maxlength = '50'></td>";
		echo "</tr>";
		echo "<tr>";
			echo "<td> Tipo Fijo?:</td>";
			echo "<td colspan = 2>";
			echo "	<input type = 'radio' name = 'fijo' VALUE = '1' checked> SI";
			echo "	<input type = 'radio' name = 'fijo' VALUE = '2'> NO";
		echo "</tr>";
		echo "</table>";
		echo "	<script language='JavaScript' type='text/JavaScript'>";
		echo "		document.entrada.codigo.focus()";
		echo "	</script>";
		echo "	<center><input name='procesar' type = 'submit' value = 'Grabar Registro' disabled></center>";
		echo "	</form>"; 
	}	
///////////////////////////////////////////////// ELIMINAR REGISTROS ////////////////////////////////////////////////////////////////
	if ($accion==2) { 
		$consulta="delete from constante where id='".$_SESSION['id_validar']."' and codigo='".$codigo."' ";
		$resultado = mysqli_query($enlace,$consulta) or die('Verificaci&oacute; del C&oacute;digo fall&oacute;: ' . mysqli_error($enlace));
	}
///////////////////////////////////////////////// MODIFICAR REGISTROS ////////////////////////////////////////////////////////////////
	if ($accion==3) {
		$consulta="Select * from constante where id='".$_SESSION['id_validar']."' and codigo = '".$codigo."'" ;
		$resultado = mysqli_query($enlace,$consulta) or die('Verificaci&oacute; del C&oacute;digo fall&oacute;: ' . mysqli_error($enlace));
		$num_rows = mysqli_num_rows($resultado);
// CARGAR REGISTRO SI NO EXISTE
		if ($num_rows != 0) {
				$row=mysqli_fetch_array($resultado);
				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='500' align='center'>";
				echo "<tr>";
					echo "<td> C&oacute;digo:</td>";
					echo "<td> <input name='codigo' size = '4' maxlength = '4' value='".$row[0]."' readonly></td>";
				echo "</tr>";
				echo "<tr>";
					echo "<td> Detalle (Descripci&oacute;n):</td>";
					echo "<td> <input name='detalle' size = '30' maxlength = '50' value='".$row[1]."'></td>";
				echo "</tr>";
				echo "<tr>";
					echo "<td> Unidad:</td>";
					echo "<td> <input name='unidad' size = '30' maxlength = '50' value='".$row[2]."'></td>";
				echo "</tr>";
				echo "<tr>";
					echo "<td> Tipo Fijo?</td>";
					echo "<td colspan = 2>";
					if ($row[3]==1) {
						echo "	<input type = 'radio' name = 'fijo' VALUE = '1' checked> SI";
						echo "	<input type = 'radio' name = 'fijo' VALUE = '2'> NO";
					} else {	
						echo "	<input type = 'radio' name = 'fijo' VALUE = '1'> SI";
						echo "	<input type = 'radio' name = 'fijo' VALUE = '2' checked> NO";
					}
				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 = 'master_constante.php'>";
				echo "<td><input type = 'submit' value = 'Cancelar'></td>";
				echo "</form></table><br>"; 				
			}
	}
///////////////////////////////////////////////// MOSTRAR TABLA DE REGISTROS ////////////////////////////////////////////////////////////////
	$consulta="Select * from constante where id='".$_SESSION['id_validar']."' order by codigo, detalle";
	$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 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 ali=center><font face='verdana' size=2>Unidad</font></th>";
			echo "<th ali=center><font face='verdana' size=2>Tipo</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 = mysqli_fetch_array($resultado);
				if ($color) $tcolor="#BFE5FF";
				else $tcolor="#F4FEFF";
				$color=!$color;
				echo '<tr bgcolor="'.$tcolor.'" >';
				echo '<td width="100" align="center">'.$row[0].'</td>';
				echo '<td width="400">'.$row[1].'</td>';
				echo '<td width="100">'.$row[2].'</td>';
				if ($row[3]==1) echo '<td  align=center width="100">ES FIJO</td>';
				else echo '<td  align=center width="100"><b>--</b></td>';
				$codigo = $row['0'];
				if (substr($_SESSION['acceso_validar'],17,1)==0) 
				echo "<td width='60' align='center'></td>";
				else echo "<td width='60' align='center'><a onclick='return validar_elimina()' href='".$_SERVER['PHP_SELF']."?accion=2&codigo=".$codigo."'><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."'><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 = 'accion' value = '3'>";
			echo "<center><input type = 'submit' value = 'Imprimir Resultado'></center></form>";
	} else echo "<hr>"; 		
	mysqli_free_result($resultado);
	mysqli_close($enlace);
	echo "	<script language='JavaScript' type='text/JavaScript'>";
	echo "		document.entrada.codigo.focus()";
	echo "	</script>";
    echo "</body>";
	echo "</html>";
}
?>