<?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 = "DECLARE @DESDE DATE = '".$cV01."' \n"
			  ."DECLARE @HASTA DATE = '".$cV02."' \n"
			  ."SELECT P.NUMSERIE, P.NUMPEDIDO, CONVERT(VARCHAR, P.FECHAPEDIDO, 105) AS FECHAPEDIDO, F.NUMSERIE AS SERIEFAC, F.NUMFACTURA AS FACTURA,  \n"
			  ."CONVERT(VARCHAR, F.FECHA, 105) AS FECHAFAC, P.IDESTADO, E.ESTADO, V.NOMVENDEDOR, C.CODCLIENTE, C.NOMBRECLIENTE, PL.IDPICKING, FL.GUIA, \n"
			  ."PL.ESTADO_AD \n"
			  ."FROM PEDVENTACAB AS P  \n"
			  ."INNER JOIN CLIENTES AS C ON (P.CODCLIENTE = C.CODCLIENTE) \n"
			  ."LEFT JOIN ALBVENTACAB AS A ON (P.SERIEALBARAN = A.NUMSERIE COLLATE Latin1_General_CS_AI) AND (P.NUMEROALBARAN = A.NUMALBARAN) \n"
			  ."LEFT JOIN FACTURASVENTA AS F ON (A.NUMSERIEFAC = F.NUMSERIE) AND (A.NUMFAC = F.NUMFACTURA) \n"
			  ."LEFT JOIN VENDEDORES AS V ON (P.CODVENDEDOR = V.CODVENDEDOR) \n"
			  ."LEFT JOIN ESTADOSTIPODOC AS E ON (P.TIPODOC = E.IDTIPODOC) AND  (P.IDESTADO = E.ID) \n"
			  ."LEFT JOIN PEDVENTACAMPOSLIBRES AS PL ON P.NUMSERIE = PL.NUMSERIE AND P.NUMPEDIDO = PL.NUMPEDIDO \n"
			  ."LEFT JOIN FACTURASVENTACAMPOSLIBRES AS FL ON F.NUMSERIE = FL.NUMSERIE AND F.NUMFACTURA = FL.NUMFACTURA \n"
			  ."WHERE (C.ESCLIENTEDELGRUPO = 0) AND (P.IDESTADO = 4) AND (CONVERT(DATE,P.FECHAPEDIDO,3) >= @DESDE AND CONVERT(DATE,P.FECHAPEDIDO,3) <= @HASTA) \n"
			  ."ORDER BY P.FECHAPEDIDO 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>Pedidos facturados</h3>
					<button type='submit' class='btn btn-success'>
						<span class='glyphicon glyphicon-export' aria-hidden='true'></span> Exportar a excel
					</button>
					<div class='clearfix'></div>
				</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 = "success";
							$cV05 = $fila['ESTADO_AD'];
							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>
									<td>".$fila['ESTADO']."</td>
									<td>".$fila['NOMVENDEDOR']."</td>
									<td>".$fila['CODCLIENTE']."</td>
									<td>".$fila['NOMBRECLIENTE']."</td>
									<td>".$fila['IDPICKING']."</td>
									<td>".$fila['GUIA']."</td>
									<td>".$fila['ESTADO_AD']."</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>