<?php
		
		include('../conexion.php');
		
		session_start();
		 
		if($_SESSION['tipo_usu']=='a' or $_SESSION['tipo_usu']=='u'  or $_SESSION['tipo_usu']=='c'){
		}else{
			header('location:acceso_denegado.php');
		}


 $usu=$_SESSION['username'];
 
$permodlim = $_SESSION['permodlim']; 
$permodpla = $_SESSION['permodpla']; 

		
$consulta = "SELECT * FROM usuarios WHERE usu='$usu'";
$resultado = $mysqli->query($consulta);
$row = $resultado->fetch_array();
 $nomusu = $row["usu"];


$hoy=date("d/m/Y");
$hora=date("H:i:s");



$accion=$_POST["accion"];
if (!isset($accion)) { $accion=$_GET["accion"]; }

$dsccli=strtoupper($_POST["ANombre"]);
$dscfia1=strtoupper($_POST["dscfia1"]);
$dscfia2=strtoupper($_POST["dscfia2"]);

$dsccli = str_replace("á","A",$dsccli);
$dsccli = str_replace("é","E",$dsccli);
$dsccli = str_replace("í","I",$dsccli);
$dsccli = str_replace("ó","O",$dsccli);
$dsccli = str_replace("ú","U",$dsccli);
$dsccli = str_replace("ñ","Ñ",$dsccli);

$dscfia1 = str_replace("á","A",$dscfia1);
$dscfia1 = str_replace("é","E",$dscfia1);
$dscfia1 = str_replace("í","I",$dscfia1);
$dscfia1 = str_replace("ó","O",$dscfia1);
$dscfia1 = str_replace("ú","U",$dscfia1);
$dscfia1 = str_replace("ñ","Ñ",$dscfia1);

$dscfia2 = str_replace("á","A",$dscfia2);
$dscfia2 = str_replace("é","E",$dscfia2);
$dscfia2 = str_replace("í","I",$dscfia2);
$dscfia2 = str_replace("ó","O",$dscfia2);
$dscfia2 = str_replace("ú","U",$dscfia2);
$dscfia2 = str_replace("ñ","Ñ",$dscfia2);


$percon=strtoupper($_POST["Apercon"]);
$corcli=strtolower($_POST["Acorreo"]);
$corcli2=strtolower($_POST["Acorreo2"]);
$cedtip=$_POST["cedtip"];
$cedcli=$_POST["ACedula"];
$cedfia1=$_POST["cedfia1"];
$cedfia2=$_POST["cedfia2"];
$cdgpais=$_POST["cdgpais"];
$numtel=$_POST["Atelefono"];
$numtel2=$_POST["Atelefono2"];
$numtel = str_replace ("-","",$numtel);

$descli=$_POST["descuento"];
$cliexe=$_POST["ATipo"];

$tippre=$_POST["combocategoria"];
$cdgtar=$_POST["combotarifa"];
$cdgprovin=$_POST["cbx_provincia"]; 
$cdgcanton=$_POST["cbx_canton"]; 
$cdgdistri=$_POST["cbx_distrito"]; 
$cdgbarrio=$_POST["cbx_barrio"]; 
$dircli=strtoupper($_POST["ADireccion"]);

$numdocexo=strtoupper($_POST["numdocexo"]);
$fecemiexo=$_POST["fecemiexo"];
$fecvenexo=$_POST["fecvenexo"];
$porimpexo=$_POST["porimpexo"];


$fecemiexo = implode("-", array_reverse(explode("-", $fecemiexo))); 
$fecvenexo = implode("-", array_reverse(explode("-", $fecvenexo ))); 


	if ($fecemiexo == ""){
	
$fecemiexo = "";
$fecvenexo = "";

	 
	 } else {

		
$fecemiexo = substr($fecemiexo,0,11);
$fecvenexo = substr($fecvenexo,0,11);

$fecemiexo = $fecemiexo."T00:00:00";
$fecvenexo = $fecvenexo."T00:00:00";
 
 }

$gesven=$_POST["gesven"];

if (!isset($_POST["chkver"])) { $chkver=0; } else  {  $chkver=$_POST["chkver"]; } 
if (!isset($_POST["chksug"])) { $chksug=0; } else  {  $chksug=$_POST["chksug"]; } 
if (!isset($_POST["clitributa"])) { $clitributa=0; } else  {  $clitributa=$_POST["clitributa"]; } 
if (!isset($_POST["tarexoagro"])) { $tarexoagro=0; } else  {  $tarexoagro=$_POST["tarexoagro"]; } 




	$consulta = "SELECT MAX(CDGCLI) AS CDGCLI FROM web_0301 WHERE CDGCLI != 9999 ";
					$resultado = $mysqli->query($consulta);
					$row = $resultado->fetch_assoc();
					$id=$row["CDGCLI"];
					$cdgcli= $id + 1 ;	
					

$usuario=3 ;
$estatus="ACTIVO";
$fecha =date( "d-m-Y" );
$fecmod =$fecha;

if ($accion=="alta") {


$limcre=$_POST["ACredito"];
$limcre1=$_POST["ACredito"];
$limcre1 = str_replace (",","",$limcre1);
$cdgpla=$_POST["combocredito"]; //combocredito


	$query_operacion="INSERT INTO web_0301 (cdgcli, dsccli, dircli, cedcli, corcli,  corcli2,  numtel,numtel2, cliexe, limcre, tippre, cdgpla, estcli, chkver, percon , chksug , dscfia1, dscfia2, cedfia1, cedfia2, cdgprovin , cdgcanton, cdgdistri, cdgbarrio, cdgpais, cedtip, nomusu, clitributa , descli , numdocexo,  fecemiexo , fecvenexo , gesven, tarexoagro, cdgtar , porimpexo  ) VALUES ('$cdgcli', '$dsccli', '$dircli','$cedcli','$corcli'  ,'$corcli2'  ,'$numtel','$numtel2','$cliexe','$limcre1', '$tippre' , '$cdgpla' , '$estatus', '$chkver' , '$percon'  , '$chksug', '$dscfia1', '$dscfia2' , '$cedfia1', '$cedfia2' , '$cdgprovin' , '$cdgcanton', '$cdgdistri', '$cdgbarrio', '$cdgpais', '$cedtip' , '$nomusu' , '$clitributa' , '$descli'  , '$numdocexo'  , '$fecemiexo' , '$fecvenexo' , '$gesven'  , '$tarexoagro'   , '$cdgtar'  , '$porimpexo' )";					
	$mysqli->query($query_operacion);
    $mensaje="El cliente ha sido creado exitosamente"; 
	$cabecera1="Inicio >> clientes &gt;&gt; Nuevo Cliente ";
	$cabecera2="Modulo Nuevo Cliente ";

}

if ($accion=="modificar") {

	$estcli=$_POST["comboestatus"];
	$cdgcli=$_POST["cdgcli"];
	$permodlim = $_SESSION['permodlim']; // PERMISO DEL LIMITE DEL CREDITO
	$permodpla = $_SESSION['permodpla']; // PERMISO DEL PLAZO

		
			/*se busca lo que ya tenía guardado, sino tiene permisos*/
		$consulta = "SELECT * FROM web_0301 WHERE cdgcli='$cdgcli'";
		$resultado = $mysqli->query($consulta);
		$row = $resultado->fetch_array();
		$limcre1 = $row["LIMCRE"] ;
		$cdgpla = $row["CDGPLA"] ;


		if ($permodlim ==  1) {
		$limcre1=$_POST["ACredito"];
		$limcre1 = str_replace (",","",$limcre1);
		} 
		
		if ( $permodpla ==  1) {$cdgpla=$_POST["combocredito"]; }
 
   
   

  $query="UPDATE web_0301 SET dsccli='$dsccli', dircli='$dircli',cedcli='$cedcli' ,corcli2='$corcli2' ,corcli='$corcli',  numtel2='$numtel2',  numtel='$numtel', cliexe='$cliexe', limcre='$limcre1',  tippre='$tippre', cdgpla='$cdgpla' , estcli='$estcli' , chkver='$chkver' , percon='$percon', chksug='$chksug', dscfia1='$dscfia1', dscfia2='$dscfia2', cedfia1='$cedfia1', cedfia2='$cedfia2' , cdgprovin='$cdgprovin', cdgcanton='$cdgcanton', cdgdistri='$cdgdistri', cdgbarrio='$cdgbarrio', cedtip='$cedtip', cdgpais='$cdgpais', nomusu='$nomusu' , clitributa='$clitributa'  , descli='$descli' , gesven='$gesven' , numdocexo='$numdocexo', fecemiexo='$fecemiexo', fecvenexo='$fecvenexo' , tarexoagro='$tarexoagro' , cdgtar='$cdgtar' , porimpexo='$porimpexo'  WHERE cdgcli='$cdgcli'";
	$mysqli->query($query);
	
	    $mensaje="El cliente ha sido modificado exitosamente"; 
	$cabecera1="Inicio >> clientes &gt;&gt; Modificar Clientes ";
	$cabecera2="Modulo Modificar Clientes ";
}

if ($accion=="baja") {
	$cdgcli=$_GET["cdgcli"];
	$query="UPDATE web_0301 SET estcli='INACTIVO', nomusu='$nomb',  fecmod='$fecmod' WHERE cdgcli='$cdgcli'";
	$rs_query=mysql_query($query);
	if ($rs_query) { $mensaje="El cliente ha sido eliminado correctamente"; }
	$cabecera1="Inicio >> clientes &gt;&gt; Eliminar Cliente ";
	$cabecera2="Modulo Eliminar Cliente ";
	$query_mostrar="SELECT * FROM web_0301 WHERE cdgcli='$cdgcli'";
	$rs_mostrar=mysql_query($query_mostrar);
	$dsccli=mysql_result($rs_mostrar,0,"dsccli");
	$dircli=mysql_result($rs_mostrar,0,"dircli");
	$cedcli=mysql_result($rs_mostrar,0,"cedcli");
	$numtel=mysql_result($rs_mostrar,0,"numtel");
	$estcli=mysql_result($rs_mostrar,0,"estcli");

}



$consulta = "SELECT * FROM web_0301 WHERE cdgcli='$cdgcli'";
$resultado = $mysqli->query($consulta);
$row = $resultado->fetch_array();
  

		

?>

<html>
	<head>
		<title>Principal</title>
		<link href="../estilos/estilos.css" type="text/css" rel="stylesheet">
			<script type="text/javascript" src="../funciones/validar.js"></script>
		<script language="javascript">
		
		function aceptar() {
			location.href="index.php";
		}
		
		var cursor;
		if (document.all) {
		// Está utilizando EXPLORER
		cursor='hand';
		} else {
		// Está utilizando MOZILLA/NETSCAPE
		cursor='pointer';
		}
		
		
		
function nobackbutton(){	
   window.location.hash="no-back-button";	
   window.location.hash="Again-No-back-button" //chrome	
   window.onhashchange=function(){window.location.hash="no-back-button";}	
}
		
		
		</script>
	</head>
		<body  onLoad="nobackbutton();">
		<div id="">
			<div background="../Fondo/fondo.jpg" id="zonaContenido">
			  <div align="center">
				  
				  <div id="tituloForm" class="header"><?php echo $mensaje; ?> </div>
				 <p>&nbsp;</p>
		          <div id="frmBusqueda" class="contenidoCliente">
					<table class="fuente8" width="100%" cellspacing=0 cellpadding=3 border=0>
						<tr>
						  <td>&nbsp;</td>
						  <td>&nbsp;</td>
						  <td colspan="3">&nbsp;</td>
					  </tr>
						<tr>
						  <td>&nbsp;</td>
						  <td>&nbsp;</td>
						  <td>&nbsp;</td>
						  <td>&nbsp;</td>
						  <td>&nbsp;</td>
					  </tr>
						
						<tr>
						  <td><div align="left"></div></td>
						  <td><div align="left">Código </div></td>
						  <td width="26%"><div align="left"><?php echo $row["CDGCLI"] ?></div></td>
					      <td width="25%">&nbsp;</td>
					      <td>&nbsp;</td>
					  </tr>
						<tr>
						  <td width="2%"><div align="left"></div></td>
							<td width="10%"><div align="left">Nombre </div></td>
							<td colspan="2"><div align="left"><?php echo $row["DSCCLI"] ?></div>							  <div align="left"></div></td>
				             <?php   
					  							$chkver=$row["CHKVER"] ;

												if($chkver==1){?>
											                                                 
								  <td colspan="1"><input type="checkbox" name="chkver2" checked  readonly  disabled value="1" id="chkver3" >
												    Verificar Vencimiento												  </td>
												  <?php  }else{  ?>
											                                               
												  <td width="13%" colspan="1"><input type="checkbox" name="chkver2"  value="1"   disabled  readonly id="chkver3" >
					      Verificar Vencimiento</td>												
												  <?php  }  ?>
						</tr>
						<tr>
						  <td>&nbsp;</td>
						  <td>Contacto</td>
						  <td colspan="2"><div align="left"><?php echo $row["PERCON"]  ?></div></td>
			 <?php   
					  							$chksug=$row["chksug"] ;

												if($chksug==1){?>
												  <td colspan="1"><input type="checkbox" name="chksug" checked  readonly  disabled value="1" id="chksug" >
												    Muestra Sugerido</td>
												  <?php  }else{  ?>
												
												 <td colspan="1"><input type="checkbox" name="chksug"  value="1"   disabled  readonly id="chksug" >
												   Muestra Sugerido</td>
 												 <?php  }  ?>
					  </tr>
						<tr>
						  <td width="2%"><div align="left"></div></td>
							<td width="10%"><div align="left">Dirección </div></td>
						    <td colspan="2"><div align="left"><?php echo $row["DIRCLI"] ?></div></td>
							 <?php   
					  	$clitributa= $row["clitributa"] ;

						if($clitributa==1){?>
                          <td colspan="6"><input type="checkbox" name="clitributa" checked value="1" id="clitributa"readonly="" >
                            Cliente Tributa</td>
                          <?php  }else{  ?>
                          <td colspan="3"><input type="checkbox" name="clitributa"  value="1"  id="clitributa" readonly="" >
                           Cliente Tributa</td>
                          <?php  }  ?>
				            <td>&nbsp;</td>
						</tr>
						<tr>
						  <td>&nbsp;</td>
						  <td>Correo</td>
						  <td colspan="3"><?php echo $row["corcli"] ?></td>
					  </tr>
						<tr>
						  <td><div align="left"></div></td>
						  <td><div align="left">Estado</div></td>
						  <td><div align="left"><?php echo $row["ESTCLI"] ?></div></td>
					      <td>Plazo Crédito </td>
						  <td width="24%"><div align="left">
						    <div align="left"><?php echo $row["CDGPLA"] ?></div>
						  </div></td>
				      </tr>
					
					  <tr>
					    <td width="2%"><div align="left"></div></td>
							<td width="10%"><div align="left">Telefono</div></td>
							<td><div align="left"><?php echo $row["NUMTEL"] ?></div></td>
	                         <?php 
							        $tippre= $row["CLIEXE"];
                                                                $query_tippre = "SELECT * FROM tipocliente WHERE cdgtipcli='$tippre' ORDER BY cdgtipcli ASC";
                                                                $result = $mysqli->query($query_tippre);
                                                                $linea = $result->fetch_array();
	
						  ?>
							<td>Limite de Crédito </td>
						<td width="24%"><div align="left"><?php echo number_format($row["LIMCRE"],2,".",",")?></div></td>
		              </tr>
					
						<tr>
						  <td><div align="left"></div></td>
							<td><div align="left">Cédula </div></td>
							<td><div align="left"><?php echo $row["CEDCLI"] ?></div></td>
						    
							<?php

					echo $tipcli= $row["TIPPRE"];

					 $categoria = "SELECT * FROM categoria WHERE codcat= '$tipcli' ORDER BY codcat ASC";
                                          $tipo = $mysqli->query($categoria);
                                            $dato = $tipo->fetch_array();
		


					  ?>
							<td>Tipo Cliente </td>
							
							
							<td>
						  <div><?php echo $linea["dsctipcli"] ?></div>						  </td>
						</tr>
						
												<tr>
												  <td><div align="left"></div></td>
												  <td><div align="left">Saldo</div></td>
												  <td><div align="left"><?php echo number_format($row["SALACT"],2,".",",")?></div></td>
												  <td>Categoría</td>
												  <td><div align="left"><?php echo $dato["nomcat"] ?></div></td>
					  </tr>
												
												<tr>
												  <td>&nbsp;</td>
												  <td>Aval 1</td>
												  <td colspan="2"><div align="left"><?php echo $row["dscfia1"]?></div></td>
												  <td><?php echo $row["cedfia1"]?></td>
					  </tr>
												<tr>
												  <td>&nbsp;</td>
												  <td>Aval2</td>
												  <td colspan="2"><?php echo $row["dscfia2"]?></td>
												  <td><?php echo $row["cedfia2"]?></td>
					  </tr>
												<tr>
												  <td>&nbsp;</td>
												  <td>&nbsp;</td>
												  <td>&nbsp;</td>
												  <td>&nbsp;</td>
												  <td>&nbsp;</td>
					  </tr>
												<tr>
												  <td  align="center" colspan="5"><img src="../img/Undo_48x48-32.png" width="48" height="48" border="0" class="aCentro" title="Salir" onClick="aceptar()" onMouseOver="style.cursor=cursor">						      <div id="botonBusqueda"></div></td>
						</tr>
					</table>
			    </div>
			  </div>
		  </div>
		</div>
	</body>
</html>


