<?php include "../index/conexion.inc" ?>
<?php include "../index/validacion.php" ?>
<?php
	$cV01 = $_POST['v1'];	/* DESDE */
	$cV02 = $_POST['v2'];	/* HASTA */
?>
<?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
	{
		// CONSULTA SQL
		$sql = "DECLARE @DESDE DATE = '".$cV01."' \n"
			  ."DECLARE @HASTA DATE = '".$cV02."' \n"
			  ."SELECT C.NUMSERIE, C.NUMPEDIDO, C.SUPEDIDO, C.FECHAPEDIDO, SUM(D.CANTIDADDEVUELTA) AS UNIDADESTOTAL, \n"
			  ."(SELECT TOP(1) CODALMACEN FROM PEDVENTALIN AS AL WHERE AL.NUMSERIE= C.NUMSERIE AND AL.NUMPEDIDO = C.NUMPEDIDO) AS BODEGA,  \n"
			  ."(SELECT TOP(1) C2.NOMBRECLIENTE FROM CLIENTES AS C2 WHERE C2.CODCLIENTE = C.CODCLIENTE) AS DESTINO,  \n" 
			  ."D.ICG \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"
			  ."WHERE (D.MODULO in (22,220)) AND (CONVERT(DATE,C.FECHAPEDIDO,3) >= @DESDE AND CONVERT(DATE,C.FECHAPEDIDO,3) <= @HASTA)  \n"
			  ."GROUP BY C.NUMSERIE, C.NUMPEDIDO, C.SUPEDIDO, C.FECHAPEDIDO, D.ICG,C.CODCLIENTE ";
		$pa1 = array();
		$op1 = array("scrollable" => SQLSRV_CURSOR_KEYSET);
		// Lanzar la consulta
		$re1 = sqlsrv_query($conexion, $sql, $pa1, $op1);
		if (sqlsrv_num_rows($re1) != 0)
		{
			echo "
			<div class='x_panel'>
				<div class='x_title'>
					<h3>Traslados preparados</h3>
					<div class='clearfix'></div>
				</div>
				</br>
				</br>
				<div class='table-responsive'>
					<table id='example' class='table table-striped table-bordered' style='width:100%'>
						<thead>
							<tr>
								<th></th>
								<th>FECHA PEDIDO</th>
								<th>SERIE</th>
								<th>PEDIDO</th>
								<th>SUPEDIDO</th>
								<th>UNIDADES TOTAL</th>
								<th>BODEGA</th>
								<th>DESTINO</th>
								<th>CARRITO</th>
							</tr>
						</thead>
						<tbody";
						while($fila = sqlsrv_fetch_array($re1))
						{
							$cV01 = $fila['NUMSERIE'];
							$cV02 = $fila['NUMPEDIDO'];
							$cV03 = $fila['SUPEDIDO'];
							$cV04 = $fila['BODEGA'];							
							$cV05 = $fila['ICG'];
							$cV06 = trim($fila['DESTINO']);
							echo "
								<tr>
									<td>
										<button class='btn btn-sm btn-dark' type='button' data-placement='top' onclick='popup01(\"".$cV01."\",\"".$cV02."\",\"".$cV03."\",\"".$cV04."\",\"".$cV05."\",\"".$cV06."\")' data-toggle='tooltip' data-original-title='Detalle'><i class='fa fa-list-alt'></i></button>
									</td>
									<td>".$fila['FECHAPEDIDO']->format('Y-m-d h:m:s')."</td>
									<td>".$fila['NUMSERIE']."</td>
									<td>".$fila['NUMPEDIDO']."</td>
									<td>".$fila['SUPEDIDO']."</td>
									<td>".$fila['UNIDADESTOTAL']."</td>
									<td>".$fila['BODEGA']."</td>
									<td>".$cV06."</td>
									<td>".$fila['ICG']."</td>
								</tr>
							";
						}
						echo "
						</tbody>
					</table>
				</div>
			</div>
			";
		}
		else
		{
			echo "
			<div class='alert alert-warning alert-dismissible fade in' role='alert'>
			<button type='button' class='close' data-dismiss='alert' aria-label='Close'><span aria-hidden='true'>×</span>
			</button>
				Registros <strong>NO</strong> localizados.
			</div>
			";
		}
		sqlsrv_free_stmt($re1);
	}
	sqlsrv_close($conexion);
?>
<script>
	$(document).ready(function() {
		$('#example').DataTable();
	} );
</script>