<?php include "../index/conexion.inc" ?>
<?php include "../index/validacion.php" ?>
<?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
	{
		// VARIABLES
		$cV01 = $_POST['desde'];	/* DESDE */
		$cV02 = $_POST['hasta'];	/* HASTA */
		// PARAMETROS PARA EXPORTAR
		header("Pragma: public");
		header("Expires: 0");
		$filename = "Facturas Tracking.xls";
		header("Content-type: application/x-msdownload");
		header("Content-Disposition: attachment; filename=$filename");
		header("Pragma: no-cache");
		header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
		echo "
			<table>
				<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>";
					// CONSULTA SQL
					$sql = "EXECUTE JOIN_REPORTE_FV_TRACKING '".$cV01."' , '".$cV02."'";
					$pa1 = array();
					$op1 = array("scrollable" => SQLSRV_CURSOR_KEYSET);
					// Lanzar la consulta
					//$re1 = sqlsrv_query($conexion, $sql, $pa1, $op1);
					$re1 = sqlsrv_prepare( $conexion, $sql, array());
					if( sqlsrv_execute( $re1 ) === false ) {
			
						die( print_r( sqlsrv_errors(), true));
					}
					while($fila = sqlsrv_fetch_array($re1))
					{
						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>
		";
	}
	// Desconexion
	sqlsrv_close($conexion);
?>