<?php

	session_start();
	if (!isset($_SESSION['user_login_status']) AND $_SESSION['user_login_status'] != 1) {
        header("location: ../../login.php");
		exit;
    }
	
	
	/* Connect To Database*/
	include("../../config/db.php");
	include("../../config/conexion.php");
	$session_id= session_id();
	$id_despacho= intval($_GET['id_despacho']);
	$sql_count=mysqli_query($con,"select * from despachos where id_despacho='".$id_despacho."'");
	$count=mysqli_num_rows($sql_count);
	if ($count==0)
	{
	echo "<script>alert('Despacho no encontrada')</script>";
	echo "<script>window.close();</script>";
	exit;
	}

	
	require_once(dirname(__FILE__).'/../html2pdf.class.php');
		
	//Variables por GET
	$sql_despacho=mysqli_query($con,"select * from despachos where id_despacho='".$id_despacho."'");
	$rw_despacho=mysqli_fetch_array($sql_despacho);
	$numero_despacho=$rw_despacho['numero_despacho'];
	$fecha_despacho=$rw_despacho['fecha'];
	$tienda_destino=$rw_despacho['destino'];
	$tienda_origen=$rw_despacho['usuario'];
	$condiciones=$rw_despacho['estatus'];
	
	$consulta=mysqli_query($con,"select * from despachos where numero_despacho='$numero_despacho'");
	if(mysqli_num_rows($consulta)>0)
	{
	$dat=mysqli_fetch_array($consulta);	
	$responsable=$dat['responsable'];
	$cedula=$dat['cedula'];
	$destino=$dat['destino'];
	$tipo_vehiculo=$dat['tipo_vehiculo'];
	$marca_vehiculo=$dat['marca_vehiculo'];
	$placa_vehiculo=$dat['placa_vehiculo'];
	$fecha_despacho=$dat['fecha'];

	
    // get the HTML
     ob_start();
     include(dirname('__FILE__').'/res/despacho_html.php');
    $content = ob_get_clean();

    try
    {
        // init HTML2PDF
        $html2pdf = new HTML2PDF('P', 'LETTER', 'es', true, 'UTF-8', array(0, 0, 0, 0));
        // display the full page
        $html2pdf->pdf->SetDisplayMode('fullpage');
        // convert
        $html2pdf->writeHTML($content, isset($_GET['vuehtml']));
        // send the PDF
        $html2pdf->Output('despacho.pdf');
    }
    catch(HTML2PDF_exception $e)
	{
        echo $e;
        exit;
	}
	}
	else
	{
	echo "<script>alert('No se puede emitir la nota de entrega. No se han cargado los datos del despacho');
	windows.location.href='despachos.php'
	</script>";
	echo "<script>window.close();</script>";
	}
	
	
