<?php include "conexion.inc" ?>
<?php include "validacion.php" ?>
<?php
	// conexión
	$servername = $svr_servidor;
	$conectioninfo = array("Database"=>$svr_base, "UID"=>$svr_usuario, "PWD"=>$svr_contrasena, "CharacterSet"=>"UTF-8");
	$conexion = sqlsrv_connect($servername, $conectioninfo);
	if ($conexion == false)
	{
		$mensaje = "Existe un error al intentar conectarse con el servidor...";
	}
	else
	{
		// VARIABLES
		$cV01 = $_POST['seleccion'];	/* TIPO DE LOG */
		$cV02 = "";
		// PARAMETROS PARA EXPORTAR
		header("Pragma: public");
		header("Expires: 0");
		$filename = "Documento_log.xls";
		header("Content-type: application/x-msdownload");
		header("Content-Disposition: attachment; filename=$filename");
		header("Pragma: no-cache");
		header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
		echo "
			<table>
				<thead>
					<tr>
						<th>IdError</th>
						<th>errCodError</th>
						<th>errDesError</th>
						<th>errFecha</th>
						<th>errCardCode</th>
						<th>errNumDocICG</th>
						<th>errItemCode</th>
						<th>errModulo</th>
						<th>errEstadoError</th>
						<th>errFechaCorreccion</th>
						<th>errAccion</th>
					</tr>
				</thead>
				<tbody>";
					// CONSULTA SQL
					if ($cV01 == 'ALL')
					{
						$sql = "SELECT * \n"
							  ."FROM tblError \n"
							  ."WHERE (errModulo <> 'OCRD') OR (errModulo <> 'OITM') OR (errModulo <> 'OINV') OR (errModulo <> 'ORIN') OR (errModulo <> 'ORCR') \n"
							  ."ORDER BY IdError DESC";
					}
					else
					{
						$sql = "SELECT * \n"
							  ."FROM tblError \n"
							  ."WHERE (errModulo = '".$cV01."') \n"
							  ."ORDER BY IdError DESC";
					}
					$pa1 = array();
					$op1 = array("scrollable" => SQLSRV_CURSOR_KEYSET);
					// Lanzar la consulta
					$re1 = sqlsrv_query($conexion, $sql, $pa1, $op1);
					while($fila = sqlsrv_fetch_array($re1))
					{
						echo "
							<tr>
							<td>".$fila['idError']."</td>
							<td>".$fila['errCodError']."</td>
							<td>".$fila['errDesError']."</td>
							<td></td>
							<td>".$fila['errCardCode']."</td>
							<td>".$fila['errNumDocICG']."</td>
							<td>".$fila['errItemCode']."</td>
							<td>".$fila['errModulo']."</td>
							<td>".$fila['errEstadoError']."</td>
							<td></td>
							<td>".$fila['errAccion']."</td>
							</tr>
						";
					}
				echo "
				</tbody>
			</table>
		";
	}
	// Desconexion
	sqlsrv_close($conexion);
?>