<?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 preparados.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>FECHA</th>
						<th>CODIGO DE BARRAS</th>
						<th>ARTICULO</th>
						<th>DESCRIPCION</th>
						<th>TALLA</th>
						<th>COLOR</th>
						<th>UNIDADES</th>
						<th>UBICAICON</th>
						<th>CAJA MASTER</th>
						<th>CARRITO</th>
					</tr>
				</thead>
				<tbody>";
					// CONSULTA SQL
					$cV01 = $_GET['id'];
					$cV02 = $_GET['id2'];
					$cV03 = $_GET['id3'];
					$cV04 = $_GET['id4'];
					$cV05 = $_GET['id5'];
					// CONSULTA SQL
					$sql = "DECLARE @SERIE NVARCHAR(10) = '".$cV01."' \n"
						  ."DECLARE @NUMERO INT = ".$cV02." \n"
						  ."DECLARE @SUPEDIDO NVARCHAR(30) = '".$cV03."' \n"
						  ."DECLARE @BODEGA NVARCHAR(5) = '".$cV04."' \n"
						  ."DECLARE @CARRITO NVARCHAR(10) = '".$cV05."' \n"
						  ."SELECT C.NUMSERIE, C.NUMPEDIDO, C.SUPEDIDO, D.FECHA, D.CODIGOARTICULO, A.DESCRIPCION, D.TALLA, D.COLOR, D.CANTIDADDEVUELTA, D.UBICACION, D.CAJAMASTER, D.ICG AS CARRITO, \n"
						  ."@BODEGA CODALMACEN, N.CODBARRAS, A.REFPROVEEDOR, D.IDDEVOLUCION  \n"
						  ."FROM JOIN_DEVOLUCIONES AS D   \n"
						  ."LEFT JOIN PEDVENTACAB AS C ON (C.TIPODOC = CASE WHEN   D.MODULO = 220 THEN 22 ELSE D.MODULO END) AND (C.SUPEDIDO = D.DOCUMENTO COLLATE Modern_Spanish_CI_AS)   \n"
						  ."LEFT JOIN ARTICULOS AS A ON (D.CODIGOARTICULO = A.CODARTICULO)   \n"
						  ."LEFT JOIN ARTICULOSLIN AS N ON (D.CODIGOARTICULO = N.CODARTICULO) AND (D.TALLA = N.TALLA COLLATE Modern_Spanish_CI_AS) AND (D.COLOR = N.COLOR COLLATE Modern_Spanish_CI_AS )  \n"
						  ."WHERE (MODULO IN(22,220)) AND (CONVERT(VARCHAR, C.SUPEDIDO) = CONVERT(VARCHAR, @SUPEDIDO)) AND (D.ICG = @CARRITO)  \n"
						  ."ORDER BY D.FECHA 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['FECHA']->format('Y-m-d h:m:s')."</td>
							<td>".$fila['CODBARRAS']."</td>
							<td>".$fila['CODIGOARTICULO']."</td>
							<td>".$fila['DESCRIPCION']."</td>
							<td>".$fila['TALLA']."</td>
							<td>".$fila['COLOR']."</td>
							<td>".$fila['CANTIDADDEVUELTA']."</td>
							<td>".$fila['UBICACION']."</td>
							<td>".$fila['CAJAMASTER']."</td>
							<td>".$fila['CARRITO']."</td>
							</tr>
						";
					}
				echo "
				</tbody>
			</table>
		";
	}
	// Desconexion
	sqlsrv_close($conexion);
?>