<?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 = "EXECUTE JOIN_INV_REP_PEDIDOS  '".$cV01."' ,'".$cV02."'";
		$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>Pedidos general</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></th>
								<th>SERIE</th>
								<th>PEDIDO</th>
								<th>FECHA PEDIDO</th>
								<th>SERIE FAC</th>
								<th>FACTURA</th>
								<th>FECHA FAC</th>
								<th>ID.ESTADO</th>
								<th>ESTADO</th>
								<th>VENDEDOR</th>
								<th>COD.CLIENTE</th>
								<th>CLIENTE</th>
								<th>IDPICKING</th>
								<th>GUIA</th>
								<th>ESTADO ADICIONAL</th>
							</tr>
						</thead>
						<tbody>";
						while($fila = sqlsrv_fetch_array($re1))
						{
							$cV01 = $fila['NUMSERIE'];
							$cV02 = $fila['NUMPEDIDO'];
							$cV03 = $fila['IDESTADO'];
							$cV04 = "default";
							if ($fila['IDESTADO'] == -1)
								$cV04 = "danger";
							if ($fila['IDESTADO'] == 2)
								$cV04 = "warning";
							if ($fila['IDESTADO'] == 3)
								$cV04 = "info";
							if ($fila['IDESTADO'] == 4)
								$cV04 = "success";
							$cV05 = $fila['ESTADO_AD'];
							$cV06 = str_replace(" ","&nbsp;", $fila['ESTADO']);
							$cV07 = $fila['IDPICKING'];
							echo "
								<tr class='".$cV04."'>
									<td>
										<button class='btn btn-sm btn-dark' type='button' data-placement='top' onclick=popup01('$cV01','$cV02','$cV03') data-toggle='tooltip' data-original-title='Detalle'><i class='fa fa-list-alt'></i></button>
									</td>
									<td>".$fila['NUMSERIE']."</td>
									<td>".$fila['NUMPEDIDO']."</td>
									<td>".$fila['FECHAPEDIDO']."</td>
									<td>".$fila['SERIEFAC']."</td>
									<td>".$fila['FACTURA']."</td>
									<td>".$fila['FECHAFAC']."</td>
									<td>".$fila['IDESTADO']."</td>";
									if ($_SESSION["mEmpleado"] == 5)
									{
										echo "
											<td>
												<button class='btn btn-sm btn-dark' type='button' data-placement='top' onclick=popup03('$cV01','$cV02','$cV06') data-toggle='tooltip' data-original-title='Cambio de estado'>".$fila['ESTADO']."</button>
											</td>
										";
									}
									else
									{
										echo "
											<td>".$fila['ESTADO']."</td>
										";
									}
									echo "
									<td>".$fila['NOMVENDEDOR']."</td>
									<td>".$fila['CODCLIENTE']."</td>
									<td>".$fila['NOMBRECLIENTE']."</td>";
									if ($_SESSION["mEmpleado"] == 5)
									{
										if ($fila['IDPICKING'] != '')
										{
											echo"
											<td>
												<button class='btn btn-sm btn-dark' type='button' data-placement='top' onclick=popup04('$cV01','$cV02','$cV07') data-toggle='tooltip' data-original-title='Cambio de ID PICKING'>".$fila['IDPICKING']."</button>
											</td>
											";
										}
										else
										{
											echo"
											<td>
												<button class='btn btn-sm btn-success' type='button' data-placement='top' onclick=popup04('$cV01','$cV02','$cV07') data-toggle='tooltip' data-original-title='Cambio de ID PICKING'>Agregar</button>
											</td>
											";
										}
									}
									else
									{
										echo "
											<td>".$fila['IDPICKING']."</td>
										";
									}
									echo "
									<td>".$fila['GUIA']."</td>";
									if ($_SESSION["mEmpleado"] == 5)
									{
										if ($fila['IDESTADO'] == '3')
										{
											echo"
											<td>
												<button class='btn btn-sm btn-dark' type='button' data-placement='top' onclick=popup02('$cV01','$cV02','$cV05') data-toggle='tooltip' data-original-title='Cambio de estado'>".$fila['ESTADO_AD']."</button>
											</td>
											";
										}
										else
										{
											echo "
											<td>".$fila['ESTADO_AD']."</td>
											";
										}
									}
									else
									{
										echo "
										<td>".$fila['ESTADO_AD']."</td>
										";
									}
									echo "
								</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>