<?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'];
		$cV01 = $_POST['v2'];
		$cV02 = $_POST['v3'];
		$cV03 = $_POST['v4'];
		$sql = "SELECT C.BODEGA, A.NOMBREALMACEN, C.GONDOLA, C.DOCUMENTO, SUM(C.UNIDADES) AS UNIDADES, '' CAJAMASTER, CONVERT(DATE,C.FECHAALTA,3) FECHAALTA,CASE WHEN C.IDESTADO =1 THEN 'ANULADO' ELSE  ( CASE WHEN C.IDESTADO =2 THEN 'APROBADO' ELSE  'SIN ESTADO' END) END AS NESTADO ,C.IDESTADO, \n"
		." ISNULL(SUM( IF2.UNIDADES),0) UNIDADES2,ISNULL(SUM( IF3.UNIDADES),0) UNIDADES3, \n"
		."case  WHEN SUM(IF2.UNIDADES) IS NULL then 'NA' \n"
		."when (SUM(C.UNIDADES) - ISNULL (SUM(IF2.UNIDADES),0)) <> 0 then'Si' \n"
		."when (SUM(C.UNIDADES) - ISNULL (SUM(IF2.UNIDADES),0)) = 0 then'No' \n"
		."end Diferencia_1vs2\n"
		.",case WHEN SUM(IF3.UNIDADES) IS NULL then 'NA'\n"
		."when (SUM(C.UNIDADES) - ISNULL (SUM(IF3.UNIDADES),0)) <> 0 then'Si' \n"
		."when (SUM(C.UNIDADES) - ISNULL (SUM(IF3.UNIDADES),0)) = 0 then'No' \n"
		."end Diferencia_1vs3 \n"
			  ."FROM JOIN_INVENTARIO_FISICO AS C \n"
			  ."LEFT JOIN ALMACEN AS A ON (C.BODEGA = A.CODALMACEN COLLATE Latin1_General_CS_AI) \n"
			  ."LEFT JOIN join_inventario_fisico_rev IF2 ON C.CODBARRAS = IF2.CODBARRAS AND C.GONDOLA = IF2.GONDOLA\n"
			  ."AND C.CAJAMASTER = IF2.CAJAMASTER AND IF2.REV=1\n"
			  ."LEFT JOIN join_inventario_fisico_rev IF3 ON C.CODBARRAS = IF3.CODBARRAS AND C.GONDOLA = IF3.GONDOLA\n"
			  ."AND C.CAJAMASTER = IF3.CAJAMASTER AND IF3.REV=2\n"
			  ."WHERE (C.BODEGA = '".$cV01."') AND (CONVERT(DATE, C.FECHAALTA) >= '".$cV02."' AND CONVERT(DATE, C.FECHAALTA) <= '".$cV03."') \n"
			  ."GROUP BY C.BODEGA, A.NOMBREALMACEN, C.GONDOLA, C.DOCUMENTO,  CONVERT(DATE,C.FECHAALTA,3), C.IDESTADO \n"
			  ."ORDER BY C.DOCUMENTO 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>Documento | Fiscalizacion de inventario</h3>
					<div class='clearfix'></div>					
				</div>
				<div class='col-md-3'>
					<input class='form-control' id='myInput' type='text' placeholder='Buscar...'>
				</div>
				<button type='submit' class='btn btn-danger'>
					<span class='fa fa-file-pdf-o' aria-hidden='true'></span> Documento fiscalización
				</button>
				<button type='button' class='btn btn-success' onclick='excel06();'>
					<span class='glyphicon glyphicon-export' aria-hidden='true'></span> Exportar a Excel
				</button>
				<div class='table-responsive'>
					<table class='table table-striped jambo_table bulk_action'>
						<thead>
							<tr>";
								if ($cV00 == '1')
								{
									echo "
									<th>
										<input type='checkbox' id='check-all' class='flat'>
									</th>
									<th>TOMA FISICA</th>
									<th>NUEVA TOMA</th>
									<th>DOCUMENTO</th>
									<th>GONDOLA</th>
									<th>BODEGA</th>
									<th>ALMACEN</th>
									<th>TOTAL UNIDADES</th>
									<th>FECHA</th>
									<th>NESTADO</th>
									<th>ESTADO</th>
									<th>UNIDADES2</th>
									<th>UNIDADES3</th>
									<th>Diferencia_1vs2</th>
									<th>Diferencia_1vs3</th>
									";
								}
								else
								{
									echo "
									<th>
										<input type='checkbox' id='check-all' class='flat'>
									</th>
									<th>TOMA FISICA</th>
									<th>NUEVA TOMA</th>
									<th>CAJA MASTER</th>
									<th>DOCUMENTO</th>
									<th>GONDOLA</th>
									<th>BODEGA</th>
									<th>ALMACEN</th>
									<th>TOTAL UNIDADES</th>
									<th>FECHA</th>
									<th>NESTADO</th>
									<th>ESTADO</th>
									<th>UNIDADES2</th>
									<th>UNIDADES3</th>
									<th>Diferencia_1vs2</th>
									<th>Diferencia_1vs3</th>
									";
								}
							echo "
							</tr>
						</thead>
						<tbody id='myTable'>";
						while($fila = sqlsrv_fetch_array($re1))
						{
							$cV01 = $fila['DOCUMENTO'];
							$cV90 = $fila['BODEGA'];
							$cV91 = $fila['GONDOLA'];
							if ($cV00 == '1')
							{
								echo "
									<tr>
										<td class='a-center'>
											<input type='checkbox' class='flat' name='lista[]' value='".$cV01."'>
										</td>
										<td>
											<button class='btn btn-sm btn-dark' type='button' data-placement='top' onclick='popup01($cV01)'><i class='fa fa-edit'></i></button>
										</td>";
										if ($fila['IDESTADO'] == 0)
										{
											echo "
											<td>
												<button class='btn btn-sm btn-success' type='button' data-placement='top' onclick=popup03('$cV01','$cV90','$cV91')>
													<span class='glyphicon glyphicon-file' aria-hidden='true'></span>
												</button>
											</td>";
										}
										else
										{
											echo "<td></td>";
										}
										echo "
										<td>".$fila['DOCUMENTO']."</td>
										<td>".$fila['GONDOLA']."</td>
										<td>".$fila['BODEGA']."</td>
										<td>".$fila['NOMBREALMACEN']."</td>
										<td>".$fila['UNIDADES']."</td>
										
										";
										
										if ($fila['IDESTADO'] == 0)
										{
											echo "
											<td>
												<button class='btn btn-sm btn-dark' type='button' data-placement='top' onclick=popup05('$cV01')>".$fila['FECHAALTA']->format('Y-m-d h:m:s')."</button>
											</td>
											<td>
												<button class='btn btn-sm btn-dark' type='button' data-placement='top' onclick=popup04('$cV01')>CAMBIO DE ESTADO</button>
											</td>
											<td></td>
											
											";
										}
										else
										{
											echo "
											
											<td>".$fila['FECHAALTA']->format('Y-m-d h:m:s')."</td>
											<td>".$fila['NESTADO']."</td>
											<td>Bloqueado</td>
											
											
											";
										}
										echo "<td>".$fila['UNIDADES2']."</td>";
										echo "<td>".$fila['UNIDADES3']."</td>";
										echo "<td>".$fila['Diferencia_1vs2']."</td>";
										echo "<td>".$fila['Diferencia_1vs3']."</td>";
										
										
									
								    
									//	<td>".$fila['Diferencia_1vs2']."</td>";
									//	<td>".$fila['Diferencia_1vs3']."</td>";
										echo "
									</tr>
								";
							}
							else
							{
								echo "
									<tr>
										<td class='a-center'>
											<input type='checkbox' class='flat' name='lista[]' value='".$cV01."'>
										</td>
										<td>
											<button class='btn btn-sm btn-dark' type='button' data-placement='top' onclick=popup01('$cV01')><i class='fa fa-edit'></i></button>
										</td>
										<td>
											<button type='button' class='btn btn-success' data-toggle='modal' data-target='.bd-ingreso-modal-xl' onclick='popup02($cV01)'>
												<span class='glyphicon glyphicon-file' aria-hidden='true'></span>
											</button>
										</td>
										<td>".$fila['CAJAMASTER']."</td>
										<td>".$fila['DOCUMENTO']."</td>
										<td>".$fila['GONDOLA']."</td>
										<td>".$fila['BODEGA']."</td>
										<td>".$fila['NOMBREALMACEN']."</td>
										<td>".$fila['UNIDADES']."</td>
										<td>".$fila['NESTADO']."</td>
										<td>".$fila['FECHAALTA']->format('Y-m-d h:m:s')."</td>";
										if ($fila['IDESTADO'] == 0)
										{
											echo "
											<td>
												<button class='btn btn-sm btn-dark' type='button' data-placement='top' onclick=popup04('$cV01')>CAMBIAR</button>
											</td>
											";
										}
										else
										{
											echo "
											<td>Bloqueado</td>
											";
										}
										echo "
										<td>".$fila['UNIDADES2']."</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>
	// Script para seleccion o Deseleccionar chekBox
	$("#check-all").on("click", function() {
	  $(".flat").prop("checked", this.checked);
	});
	// if all checkbox are selected, check the selectall checkbox and viceversa
	$(".flat").on("click", function() {
	  if ($(".flat").length == $(".flat:checked").length) {
		$("#check-all").prop("checked", true);
	  } else {
		$("#check-all").prop("checked", false);
	  }
	});
</script>
<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>