<?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_REPORTE_FV_TRACKING '".$cV01."' , '".$cV02."'";
		$pa1 = array();
		$op1 = array();
		// Lanzar la consulta
		//echo $sql;
		//$re1 = sqlsrv_query($conexion, $sql, $pa1, $op1);
		$re1 = sqlsrv_prepare( $conexion, $sql, array());
		
		if( sqlsrv_execute( $re1 ) === false ) {
			
			die( print_r( sqlsrv_errors(), true));
		}
		
		/*$q = "EXECUTE JOIN_REPORTE_E_S '2022-05-01' , '2022-05-13'";
		$r = sqlsrv_query($conexion, $q);
		$row = sqlsrv_fetch_array($r);
		var_dump($row);
		print_r(sqlsrv_errors(), true);*/
		//echo $re1;
		
		if (sqlsrv_execute($re1) === true)
		{
			echo "
			<div class='x_panel'>
				<div class='x_title'>
					<h3>Facturas Traking</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>ESTADO</th>
								<th>CODCLIENTE</th>
								<th>NOMBRECLIENTE</th>
								<th>NUMSERIE</th>
								<th>NUMFACTURA</th>
								<th>USR_FACTURA</th>
								<th>FECHA_FACTU</th>
								<th>USR_PICKING</th>
								<th>INI_PICKING</th>
								<th>FIN_PICKING</th>
								<th>USR_VALIDACION</th>
								<th>INI_VALIDA</th>
								<th>FIN_VALIDA</th>
								<th>USR_EMPACADOR</th>
								<th>INI_EMPACAD</th>
								<th>FIN_EMPACAD</th>
								<th>REFERENCIA</th>
								<th>TALLA</th>
								<th>COLOR</th>
								<th>UDS_FACTURA</th>
								<th>UDS_PICKING</th>
								<th>UDS_VALIDADAS</th>
								<th>GUIA</th>
								<th>FECHA_ENVIO</th>
								<th>USR_ENVIO</th>
								<th>COURIER</th>
							</tr>
						</thead>
						<tbody>";
						//echo "Hola Mundo";
						while($fila = sqlsrv_fetch_array($re1))
						{
							//print_r($fila);
							
							echo "
								<tr>
									<td>".$fila['ESTADO']."</td>
									<td>".$fila['CODCLIENTE']."</td>
									<td>".$fila['NOMBRECLIENTE']."</td>
									<td>".$fila['NUMSERIE']."</td>
									<td>".$fila['NUMFACTURA']."</td>
									<td>".$fila['USR_FACTURA']."</td>
									<td>".$fila['FECHA_FACTURA']->format('Y-m-d h:m:s')."</td>
									<td>".$fila['USR_PICKING']."</td>
									<td>".$fila['INICIO_PICKING']->format('Y-m-d h:m:s')."</td>
									<td>".$fila['FIN_PICKING']->format('Y-m-d h:m:s')."</td>
									<td>".$fila['USR_VALIDACION']."</td>
									<td>".$fila['INICIO_VALIDACION']->format('Y-m-d h:m:s')."</td>
									<td>".$fila['FIN_VALIDACION']->format('Y-m-d h:m:s')."</td>
									<td>".$fila['USR_EMPACADOR']."</td>
									<td>".$fila['INICIO_EMPACADO']->format('Y-m-d h:m:s')."</td>
									<td>".$fila['FIN_EMPACADO']->format('Y-m-d h:m:s')."</td>
									<td>".$fila['REFERENCIA']."</td>
									<td>".$fila['TALLA']."</td>
									<td>".$fila['COLOR']."</td>
									<td>".$fila['UDS_FACTURA']."</td>
									<td>".$fila['UDS_PICKING']."</td>
									<td>".$fila['UDS_VALIDADAS']."</td>
									<td>".$fila['GUIA']."</td>
									<td>".$fila['FECHA_ENVIO']->format('Y-m-d h:m:s')."</td>
									<td>".$fila['USR_ENVIO']."</td>
									<td>".$fila['COURIER']."</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>