<?php include "../index/conexion.inc" ?>
<?php include "../index/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
	{
		// PARAMETROS PARA EXPORTAR
		header("Pragma: public");
		header("Expires: 0");
		$filename = "Traslados recibidos.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>ARTICULO</th>
						<th>REFERENCIA</th>
						<th>DESCRIPCION</th>
						<th>TALLA</th>
						<th>COLOR</th>
						<th>UNIDADES</th>
						<th>UNIDADES RECIBIDAS</th>
						<th>CARRITO</th>
					</tr>
				</thead>
				<tbody>";
					$cV01 = $_GET['id3'];
					$cV02 = $_GET['id4'];
					// CONSULTA SQL
					$sql = "SELECT TR.IDDEVOLUCION, TR.DOCUMENTO, A.CODARTICULO, A.REFPROVEEDOR, A.DESCRIPCION, TR.TALLA, TR.COLOR, TR.CANTIDADDEVUELTA AS UNIDADES, \n"
						  ."ISNULL(TR.RECIBODESTINO,0) AS UNIDADES_RECIBIDAS, TR.ICG \n"
						  ."FROM JOIN_DEVOLUCIONES AS TR LEFT JOIN ARTICULOS AS A ON TR.CODIGOARTICULO = A.CODARTICULO \n"
						  ."WHERE (TR.MODULO = 22) AND (ISNULL(TR.RECIBODESTINO, '') <> '') AND (TR.DOCUMENTO = '".$cV01."') AND (TR.ICG = ".$cV02.")";
					$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['CODARTICULO']."</td>
								<td>".$fila['REFPROVEEDOR']."</td>
								<td>".$fila['DESCRIPCION']."</td>
								<td>".$fila['TALLA']."</td>
								<td>".$fila['COLOR']."</td>
								<td>".$fila['UNIDADES']."</td>
								<td>".$fila['UNIDADES_RECIBIDAS']."</td>
								<td>".$fila['ICG']."</td>
							</tr>
						";
					}
				echo "
				</tbody>
			</table>
		";
	}
	// Desconexion
	sqlsrv_close($conexion);
?>