<?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
	{
		// CONSULTA SQL
		$cV00 = $_POST['v1'];
		$sql = "DECLARE @BODEGA NVARCHAR(4) = '".$cV00."'  EXECUTE JOIN_REPORTE_STOCK_POR_BODEGA @BODEGA  \n"
			  ."SELECT  A.BODEGA, A.CAJAMASTER,A.UBICACION,A.MODELO, A.DESCRIPCION,  A.CODARTICULO, A.TALLA, A.COLOR, A.CODBARRAS, \n"
			 ." UNIDADES - ISNULL(SALIDAS,0) STOCK, A.CODUBICACION \n"
			  ."FROM  \n"
			  ."( SELECT  B.BODEGA,  A.CODIGOBARRAS CAJAMASTER, A.CODUBICACION UBICACION, A.CODARTICULO, AR.REFPROVEEDOR MODELO, AR.DESCRIPCION, \n"
			  ."	A.TALLA, A.COLOR, ARL.CODBARRAS, A.CANTIDADARTICULOS AS UNIDADES, B.CODUBICACION \n"
			  ."	FROM JOIN_CAJAMASTER_2 AS A  \n"
			  ."	LEFT JOIN JOIN_UBICACIONES AS B ON CONVERT(VARCHAR, A.CODUBICACION) = CONVERT(VARCHAR, B.IDUBICACION) \n"
			  ."	LEFT JOIN ARTICULOS AS AR ON (A.CODARTICULO = AR.CODARTICULO) \n"
			 ." 	LEFT JOIN ARTICULOSLIN AS ARL ON (A.CODARTICULO = ARL.CODARTICULO) AND (A.TALLA = ARL.TALLA COLLATE Latin1_General_CS_AI) AND (A.COLOR = ARl.COLOR COLLATE Latin1_General_CS_AI) \n"
			  ."	WHERE (B.BODEGA = @BODEGA) AND A.CODIGOBARRAS <>'0000'\n"
			 ." ) AS A LEFT JOIN \n"
			 ." ( SELECT CAJAMASTER, CODIGOARTICULO, TALLA,COLOR, SUM(SALIDAS) SALIDAS \n"
			 ." 	FROM JOIN_REPORTE_STOCK_BOG_TB AS AA WHERE BODEGA = @BODEGA \n"
			."	AND CAJAMASTER <>'0000'\n"
				."GROUP BY CAJAMASTER, CODIGOARTICULO, TALLA,COLOR\n"
			  .") AS B ON (A.CODARTICULO = B.CODIGOARTICULO) AND (A.TALLA = B.TALLA) \n"
			 ." AND (A.COLOR = B.COLOR) AND (A.CAJAMASTER = B.CAJAMASTER) WHERE \n"
			  ."(UNIDADES - ISNULL(SALIDAS,0) <>0) \n"
		."		UNION ALL\n"
			."   SELECT  A.BODEGA, A.CAJAMASTER,A.UBICACION,A.MODELO, A.DESCRIPCION,  A.CODARTICULO, A.TALLA, A.COLOR, A.CODBARRAS, \n"
			 ." ( UNIDADES - ISNULL(SALIDAS,0))- ISNULL(C.REST,0) STOCK, A.CODUBICACION  \n"
			 ." FROM \n" 
			 ." ( \n"
			 ." 	SELECT  B.BODEGA,  A.CODIGOBARRAS CAJAMASTER, A.CODUBICACION UBICACION, A.CODARTICULO, AR.REFPROVEEDOR MODELO, AR.DESCRIPCION, \n"
			  ."	A.TALLA, A.COLOR, ARL.CODBARRAS, A.CANTIDADARTICULOS AS UNIDADES, B.CODUBICACION \n"
			  	."FROM JOIN_CAJAMASTER_2 AS A  \n"
			  ."	LEFT JOIN JOIN_UBICACIONES AS B ON CONVERT(VARCHAR, A.CODUBICACION) = CONVERT(VARCHAR, B.IDUBICACION) \n"
			  ."	LEFT JOIN ARTICULOS AS AR ON (A.CODARTICULO = AR.CODARTICULO) \n"
			  ."	LEFT JOIN ARTICULOSLIN AS ARL ON (A.CODARTICULO = ARL.CODARTICULO) AND (A.TALLA = ARL.TALLA COLLATE Latin1_General_CS_AI) AND (A.COLOR = ARl.COLOR COLLATE Latin1_General_CS_AI) \n"
			  ."	WHERE (B.BODEGA = @BODEGA) AND A.CODIGOBARRAS ='0000'\n"
			  .") AS A LEFT JOIN \n"
			  ."( SELECT CAJAMASTER,UBICACION, CODIGOARTICULO, TALLA,COLOR, SUM(SALIDAS) SALIDAS \n"
			  	."FROM JOIN_REPORTE_STOCK_BOG_UB AS AA WHERE BODEGA = @BODEGA \n"
				."GROUP BY CAJAMASTER,UBICACION, CODIGOARTICULO, TALLA,COLOR\n"
			 ." ) AS B ON (A.CODARTICULO = B.CODIGOARTICULO) AND (A.TALLA = B.TALLA) \n"
			 ." AND (A.COLOR = B.COLOR) AND (A.CAJAMASTER = B.CAJAMASTER)\n"
			 ." AND  A.UBICACION = B.UBICACION\n"
			 ." LEFT JOIN \n"
			  ." (SELECT '0000' CAJAMASTER , A.CODARTICULO, A.TALLA,A.COLOR, A.CODUBICACION, SUM(ISNULL(A.UNIDADES,0)) REST \n"
             ." FROm JOIN_AUX_PICKING_FACTURAS AS A INNER JOIN JOIN_UBICACIONES AS B ON A.CODUBICACION = B.IDUBICACION \n"
			 ." WHERE  CONVERT(DATE,A.FECHA,3)>=( SELECT TOP(1) ULTIMAREG FROM JOIN_ULTIMAREG WHERE CODALMACEN = @BODEGA ) \n"
			."	GROUP BY A.CODARTICULO, A.TALLA,A.COLOR, A.CODUBICACION) AS C ON  \n"
			."	(A.CODARTICULO = C.CODARTICULO) AND (A.TALLA = C.TALLA)  \n"
		   ."   AND (A.COLOR = C.COLOR) AND (A.CAJAMASTER = C.CAJAMASTER) \n"
			."  AND  A.UBICACION = C.CODUBICACION \n"
			."  WHERE \n"
			 ." (UNIDADES - ISNULL(SALIDAS,0) <>0)  ";
		$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>STOCK BODEGA No. ".$cV00."</h3>
					<div class='clearfix'></div>
					<button type='submit' class='btn btn-success'>
						<span class='glyphicon glyphicon-export' aria-hidden='true'></span> Exportar a excel
					</button>
				</div>
				<div class='table-responsive'>
					<table id='example' class='table table-striped table-bordered' style='width:100%'>
						<thead>
							<tr>
								<th>ARTICULO</th>
								<th>DESCRIPCION</th>
								<th>TALLA</th>
								<th>COLOR</th>
								<th>CODBARRAS</th>
								<th>MODELO</th>
								<th>STOCK</th>
								<th>CAJA MASTER</th>
								<th>ID UBICACION</th>
								<th>UBICACION</th>
							</tr>
						</thead>
						<tbody>";
						while($fila = sqlsrv_fetch_array($re1))
						{
							echo "
								<tr>
									<td>".$fila['CODARTICULO']."</td>
									<td>".$fila['DESCRIPCION']."</td>
									<td>".$fila['TALLA']."</td>
									<td>".$fila['COLOR']."</td>
									<td>".$fila['CODBARRAS']."</td>
									<td>".$fila['MODELO']."</td>
									<td>".$fila['STOCK']."</td>
									<td>".$fila['CAJAMASTER']."</td>
									<td>".$fila['UBICACION']."</td>
									<td>".$fila['CODUBICACION']."</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>