<?php
session_start();

include "prph/coneccion_adm.php";
	$cruc= trim($_POST['ruc']);
	$_SESSION['web_basedato_ruc']=$cruc;
	$usuario = trim($_POST['usuario']);
	$contrasena=trim($_POST['contrasena']);
	
	
	$cadena_sql = "set search_path to 'ADMIN';  select ccod_ruc from fe_registrotributario_tl where ccod_ruc = '$cruc' and bestado=true;";
	$resultado = pg_query($conexiondb_adm, $cadena_sql);
	if (pg_num_rows($resultado) == 0)
	{
		echo "<center><b>El Ruc indicado no existe o no esta registrado en nuestra base de dato </b></center>";
		
	}
	else
	{
	 $bdd = "run4clou_bd$cruc";
	$cadena_sql = "set search_path to 'ADMIN'; select ccod_ruc, cemail, cbasedato, cpassword, case when btodos=false then '0' else '1' end naccesogral, crazon_social from fe_clientes_tl where  ccod_ruc= '$usuario' and bestado = true and cbasedato = '".$bdd."';";
	
	$resultado = pg_query($conexiondb_adm, $cadena_sql);
	//echo $cadena_sql;
	echo  pg_last_error($conexiondb_adm);	
	if (pg_num_rows($resultado)>0)
	{
		$cruccliente_bd = trim(pg_fetch_result($resultado,0,"ccod_ruc"));
		$cmail_bd = trim(pg_fetch_result($resultado,0,"cemail"));
		$cbasedato_bd =trim(pg_fetch_result($resultado,0,"cbasedato"));
		$cpassword_bd =trim(pg_fetch_result($resultado,0,"cpassword"));
		$naccesogral_bd = trim(pg_fetch_result($resultado,0,"naccesogral"));
		$crazon_social_bd = trim(pg_fetch_result($resultado,0,"crazon_social"));
		

		if ($cbasedato_bd==$bdd  and $cruccliente_bd and  $usuario and $cpassword_bd == md5($contrasena)  )
		{
			//echo "OK";
			$_SESSION['web_cnitfe'] = $cruccliente_bd;
			$_SESSION['web_nombre_empresafe'] = $crazon_social_bd;
			
			
		}
		else
		{
			echo "<center><b>Datos incorrectos (verifique Ruc, Usuario, Contraseña) </b></center>";
			return false;
		}
			
	}
	
	include "prph/coneccionfe.php";
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="es">
<?php

$month = date('m');
$year = date('Y');
$day = date("d", mktime(0,0,0, $month+1, 0, $year));

$primerdiames= date('d/m/Y');
$primerdiames= date('d/m/Y', mktime(0,0,0, $month, 1, $year));
$utlimodiames =	date('d/m/Y', mktime(0,0,0, $month, $day, $year));

?>
<head>
<title>Documentos electr&oacute;nicos</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" type="text/css" media="screen" href="css/main.css" />
<link rel="stylesheet" href="css/w3.css">

<script type="text/javascript" src="js/funcionesjs.js"></script>  


</head>

<body id="vbody" >
<div class ="contenedor">
	<div   style="float:left;width:99%;"  >
	<input type="hidden" name="usuario" id="usuario" value="<?php echo $usuario; ?>"/>
	<div style="float:left;">
	 <label > Fecha: Desde </label>
	</div >

	<div style="width:150px;float:left;margin-left:10px;">
	  <input name="txtfecha_ini" type="text" placeholder="Fecha " id ="txtfechaemisiondesde" class="w3-input" maxlength="10" style="width:110px;height:32px;"  value="<?php echo $primerdiames?>"  />
	</div >

	<div style="float:left;">
	 <label >Hasta </label>
	</div >

	<div style="width:150px;float:left;margin-left:10px;">
	  <input name="txtfecha_fin" type="text" placeholder="Fecha " id ="txtfechaemisionhasta" class="w3-input" maxlength="10" style="width:110px;height:32px;"  value="<?php echo $utlimodiames?>"  />
	</div >
	
	 <div style="width:110px;float:left;">
	  <button type="submit" name="submit" class="w3-btn w3-blue" id ="btnupdatefe"  onClick="f_carga_datosfe();">Actualizar</button>
	 </div >

	<div style="width:35%;float:right;font-size:10px;margin-top:25px;" align="right">  
	<?php
		$web_login_ruccliente_fe = "";
		if (isset ($_SESSION['web_cnitfe']))
		{		
			echo "<a > " .$_SESSION['web_cnitfe']. "</a> ";
			echo "<a >" .$_SESSION['web_nombre_empresafe']. "</a>  ";
			echo " <a style='text-decoration: underline' href='index.php'> CERRAR SESI&OacuteN</a>  ";
		}
		//include('../cperun/cpe_sendone.php');
	?>

	</div>
	</div>
	
	<div id="felectronicasd" style="float:left;width:100%;"  >
	  
	</div>

</div >



<?php

if (!isset ($_SESSION['web_basedato_ruc']))
{

  //echo " <script language='javascript1.1' type='text/javascript'> window.location.href='../index.php'; </script>"; 
  
  echo "iniciando";
}
else
{
	
	echo " <script language='javascript1.1' type='text/javascript'> f_carga_datosfe(); </script>"; 
}

	}
?>


</body>
</html>