<?php include "../index/conexion.inc" ?>
<?php include "../index/validacion.php" ?>
<?php
	$cV01 = $_POST['v1'];	/* DESDE */
	$cV02 = $_POST['v2'];	/* HASTA */
	$cV03 = $_POST['v3'];	/* BODEGA */
?>
<?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 = "SELECT A.IDLINEA, AR.CODARTICULO, AR.REFPROVEEDOR, ISNULL(AL1.TALLA, ISNULL(AL2.TALLA, AL3.TALLA)) AS TALLA, \n"
			  ."ISNULL(AL1.COLOR, ISNULL(AL2.COLOR, AL3.COLOR)) AS COLOR, A.CODBARRAS, A.BODEGA, B.NOMBREALMACEN, A.UNIDADES, A.FECHAALTA, \n"
			  ."ISNULL(V.CODVENDEDOR, 0) AS CODVENDEDOR, V.NOMVENDEDOR AS VENDEDOR, A.CAJAMASTER, A.UBICACION, UB.IDZONA, UB.PASILLO, UB.LADO, \n"
			  ."P.CODPROVEEDOR, A.GONDOLA, A.DOCUMENTO, 1 AS ORD \n"
			  ."FROM JOIN_INVENTARIO_FISICO AS A \n"
			  ."LEFT JOIN ALMACEN AS B ON (A.BODEGA = B.CODALMACEN COLLATE Latin1_General_CS_AI) \n"
			  ."LEFT JOIN ARTICULOSLIN AS AL1 ON (A.CODBARRAS = AL1.CODBARRAS COLLATE Latin1_General_CS_AI) \n"
			  ."LEFT JOIN ARTICULOSLIN AS AL2 ON (A.CODBARRAS = AL2.CODBARRAS2 COLLATE Latin1_General_CS_AI) \n"
			  ."LEFT JOIN ARTICULOSLIN AS AL3 ON (A.CODBARRAS = AL3.CODBARRAS3 COLLATE Latin1_General_CS_AI) \n"
			  ."LEFT JOIN ARTICULOS AS AR ON (AR.CODARTICULO = AL1.CODARTICULO OR AR.CODARTICULO = AL2.CODARTICULO OR AR.CODARTICULO = AL3.CODARTICULO) \n"
			  ."LEFT JOIN JOIN_UBICACIONES AS UB ON (CONVERT(VARCHAR, A.UBICACION) = CONVERT(VARCHAR, UB.IDUBICACION)) \n"
			  ."LEFT JOIN VENDEDORES AS V ON (A.USUARIO = V.CODVENDEDOR) \n"
			  ."LEFT JOIN REFERENCIASPROV AS P ON (AR.REFPROVEEDOR = P.REFPROVEEDOR) \n"
			  ."WHERE (CONVERT(DATE, A.FECHAALTA) >= '".$cV01."') AND (CONVERT(DATE, A.FECHAALTA) <= '".$cV02."') AND (A.BODEGA = '".$cV03."') \n"
			  ."ORDER BY A.FECHAALTA DESC";
		$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>Inventario Fisico</h3>
					<div class='clearfix'></div>					
				</div>
				<button type='submit' class='btn btn-success'>
					<span class='glyphicon glyphicon-export' aria-hidden='true'></span> Exportar a Excel
				</button>
				<div class='col-md-6 col-sm-6 col-xs-12'>
					<input class='form-control' id='myInput' type='text' placeholder='Buscar...'>
				</div>
				<div class='table-responsive'>
					<table class='table table-striped jambo_table bulk_action'>
						<thead>
							<tr>
								<th>LINEA</th>
								<th>DOCUMENTO</th>
								<th>GONDOLA</th>
								<th>COD.ARTICULO</th>
								<th>PROVEEDOR</th>
								<th>TALLA</th>
								<th>COLOR</th>
								<th>COD.BARRAS</th>
								<th>BODEGA</th>
								<th>ALMACEN</th>
								<th>UNIDADES</th>
								<th>FECHA</th>
								<th>COD.VENDEDOR</th>
								<th>VENDEDOR</th>
								<th>CAJA MASTER</th>
								<th>UBICACION</th>
								<th>ZONA</th>
								<th>PASILLO</th>
								<th>LADO</th>
							</tr>
						</thead>";
						while($fila = sqlsrv_fetch_array($re1))
						{
							echo "
								<tbody id='myTable'>
									<tr>
										<td>".$fila['IDLINEA']."</td>
										<td>".$fila['DOCUMENTO']."</td>
										<td>".$fila['GONDOLA']."</td>
										<td>".$fila['CODARTICULO']."</td>
										<td>".$fila['REFPROVEEDOR']."</td>
										<td>".$fila['TALLA']."</td>
										<td>".$fila['COLOR']."</td>
										<td>".$fila['CODBARRAS']."</td>
										<td>".$fila['BODEGA']."</td>
										<td>".$fila['NOMBREALMACEN']."</td>
										<td>".$fila['UNIDADES']."</td>
										<td>".$fila['FECHAALTA']->format('Y-m-d h:m:s')."</td>
										<td>".$fila['CODVENDEDOR']."</td>
										<td>".$fila['VENDEDOR']."</td>
										<td>".$fila['CAJAMASTER']."</td>
										<td>".$fila['UBICACION']."</td>
										<td>".$fila['IDZONA']."</td>
										<td>".$fila['PASILLO']."</td>
										<td>".$fila['LADO']."</td>
									</tr>
								</tbody>
							";
						}
					echo "
					</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(){
	  $("#myInput").on("keyup", function() {
		var value = $(this).val().toLowerCase();
		$("#myTable tr").filter(function() {
		  $(this).toggle($(this).text().toLowerCase().indexOf(value) > -1)
		});
	  });
	});
</script>