<?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.TIPODOC, T.DESCRIPCION AS DOCUMENTO, PC.IDESTADO, PC.NUMSERIE, PC.NUMALBARAN, PC.SUALBARAN, PC.FECHAALBARAN, P.CODPROVEEDOR, PL.CODALMACEN, \n"
			  ."P.NOMPROVEEDOR, PL.CODARTICULO, PL.REFERENCIA, PL.TALLA, PL.COLOR, PL.UNIDADESTOTAL \n"
			  ."FROM ALBCOMPRACAB AS PC \n"
			  ."INNER JOIN ALBCOMPRALIN AS PL ON (PC.NUMSERIE = PL.NUMSERIE) AND (PC.NUMALBARAN= PL.NUMALBARAN) \n"
			  ."LEFT JOIN PROVEEDORES AS P ON (PC.CODPROVEEDOR = P.CODPROVEEDOR) \n"
			  ."LEFT JOIN TIPOSDOC As T ON (PC.TIPODOC = T.TIPODOC) \n"
			  ."WHERE PC.TIPODOC IN (4,21) AND (CONVERT(DATE,FECHAALBARAN,3)>='".$cV01."' AND  CONVERT(DATE,FECHAALBARAN,3)<='".$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>Ingreso documentos de compra</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>TIPO DOC</th>
								<th>DOCUMENTO</th>
								<th>IDESTADO</th>
								<th>SERIE</th>
								<th>ALBARAN</th>
								<th>SUALBARAN</th>
								<th>FECHA ALBARAN</th>
								<th>PROVEEDOR</th>
								<th>COD.ALMACEN</th>
								<th>COD.PROVEEDOR</th>
								<th>COD.ARTICULO</th>
								<th>REFERENCIA</th>
								<th>TALLA</th>
								<th>COLOR</th>
								<th>UNIDADES</th>
							</tr>
						</thead>
						<tbody>";
						while($fila = sqlsrv_fetch_array($re1))
						{
							echo "
								<tr>
									<td>".$fila['TIPODOC']."</td>
									<td>".$fila['DOCUMENTO']."</td>
									<td>".$fila['IDESTADO']."</td>
									<td>".$fila['NUMSERIE']."</td>
									<td>".$fila['NUMALBARAN']."</td>
									<td>".$fila['SUALBARAN']."</td>
									<td>".$fila['FECHAALBARAN']->format('Y-m-d h:m:s')."</td>
									<td>".$fila['CODPROVEEDOR']."</td>
									<td>".$fila['CODALMACEN']."</td>
									<td>".$fila['NOMPROVEEDOR']."</td>
									<td>".$fila['CODARTICULO']."</td>
									<td>".$fila['REFERENCIA']."</td>
									<td>".$fila['TALLA']."</td>
									<td>".$fila['COLOR']."</td>
									<td>".$fila['UNIDADESTOTAL']."</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>