<?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 = "SELECT PC.NUMSERIE, PC.NUMPEDIDO, PC.SUPEDIDO, PC.FECHAPEDIDO, SUM(PL.UNIDADESTOTAL) AS UNIDADESTOTAL, MAX(PL.CODALMACEN) CODALMACEN, \n"
			  ."PC.IDESTADO, E.ESTADO \n"
			  ."FROM \n"
			  ."PEDVENTACAB AS PC \n"
			  ."INNER JOIN PEDVENTALIN AS PL ON (PC.NUMSERIE = PL.NUMSERIE) AND (PC.NUMPEDIDO = PL.NUMPEDIDO) \n"
			  ."INNER JOIN ESTADOSTIPODOC AS E ON (PC.IDESTADO = E.ID) \n"
			  ."WHERE (PC.FECHAPEDIDO>='".$cV01."') AND (PC.FECHAPEDIDO<='".$cV02."') AND (PC.TIPODOC = 22) \n"
			  ."GROUP BY PC.NUMSERIE, PC.NUMPEDIDO, PC.SUPEDIDO, PC.FECHAPEDIDO, PC.IDESTADO, E.ESTADO \n"
			  ."ORDER BY PC.FECHAPEDIDO ASC";
		$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>Asignación de traslados</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='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>SUPEDIDO</th>
								<th>FECHA</th>
								<th>TOTAL UNIDADES</th>
								<th>ALMACEN</th>
								<th>IDESTADO</th>
								<th>ESTADO</th>
							</tr>
						</thead>
						<tbody>";
						while($fila = sqlsrv_fetch_array($re1))
						{
							$cV01 = $fila['NUMSERIE'];
							$cV02 = $fila['NUMPEDIDO'];
							$cV03 = $fila['ESTADO'];
							if ($fila['IDESTADO'] == -1)
								$cV04 = "danger";
							else
								$cV04 = "dark";
							echo "
								<tr>
									<td>
										<button class='btn btn-sm btn-dark' type='button' data-placement='top' onclick=popup01('$cV01','$cV02') 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['SUPEDIDO']."</td>
									<td>".$fila['FECHAPEDIDO']->format('Y-m-d h:m:s')."</td>
									<td>".$fila['UNIDADESTOTAL']."</td>
									<td>".$fila['CODALMACEN']."</td>
									<td>".$fila['IDESTADO']."</td>
									<td>
										<button class='btn btn-sm btn-".$cV04."' type='button' data-placement='top' onclick=popup99('$cV01','$cV02','$cV03') data-toggle='tooltip' data-original-title='Detalle'>".$fila['ESTADO']."</button>
									</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>
<script>
	var mipopup;
	function popup99(d01, d02, d03)
	{
		v01 = d01;
		v02 = d02;
		v03 = d03;
		posicion_x=(screen.width/2)-(600/2);
		posicion_y=(screen.height/2)-(400/2)-50;
		mipopup = window.open("reporte_11_est.php?id="+v01+"&id2="+v02+"&id3="+v03, "estados", "width=600, Height=400, menubar=0, toolbar=0,directories=0,scrollbars=no,resizable=no,left="+posicion_x+",top="+posicion_y+"");
		mipopup.focus()
	}
</script>
