<?php

    session_start();

    if(!isset($_SESSION['user_login_status']) && $_SESSION['user_login_status'] != 1)
    exit;

    include("../../config/db.php");
    include("../../config/conexion.php");
    include("../../funciones.php");
    require_once("../../modelos/factura.php");
    require_once("../../modelos/factura_detalle.php");
    require_once("../../classes/ZipHelper.php");
    require_once(__DIR__.'/../../pdf/html2pdf.class.php');

    $facturas = $_POST['facturas'];

    $upload_directory = __DIR__.'/../../pdf/documentos/facturas_pdf/';

    // Obtener Factura Encabezado
   $factura = new factura();
   $data = $factura->ObtenerFactura($facturas[0]);
   $data = $data[0];

   // Obtener Factura Detalles
   $facturadetalle = new factura_detalle();
   $datad = $facturadetalle->ObtenerFacturaDetalles($facturas[0]);

   try
   {
        ob_start();
        include(__DIR__.'/../../pdf/documentos/res/plantilla_factura_html.php');
        $content = ob_get_clean();

        $file_name = 'B'.($data->numero_factura).'.pdf';

        // init HTML2PDF
        $html2pdf = new HTML2PDF('P', 'LETTER', 'es', true, 'UTF-8', array(0, 0, 0, 0));
        // convert
        $html2pdf->writeHTML($content);
        // send the PDF
        $html2pdf->Output($upload_directory.$file_name,'F');

        // Response
        $zippath = 'pdf/documentos/facturas_pdf/'.$file_name;

        $response = array("zippath" => $zippath, "opcion" => 1, "numero_factura" => $data->numero_factura);

        echo json_encode($response);
   }
   catch(HTML2PDF_exception $e){
        echo 'ERRRR :: '.$e->getMessage().''; die();
   } 


