<?php
  $Position   = '../../../';
  require_once($Position.'config/config.php');
  require_once($Position.'config/conexion.php');
  require_once($Position.'config/user.php');
  require_once($Position.'funciones/acces.php');
  require_once($Position.'funciones/logout.php');

  if( $_GET['Id'] ){

    $Id = $_GET['Id'];

    //Factura
    $Query = "
      SELECT
        fac.Id,
        fac.Xml
      FROM Facturas fac
      WHERE fac.Id = '$Id'
        AND fac.Fk_Cuenta = '$Cuenta[Id]'
    ";
    $Result = mysqli_query($mysqli, $Query);
    $Fac = mysqli_fetch_assoc($Result);
    if( $Fac[Id] ){

      //Decode pdf content
      $decoded = base64_decode($Fac[Xml]);
      $file = 'factura.xml';
      file_put_contents($file, $decoded);

      if (file_exists($file)) {
          header('Content-Description: File Transfer');
          header('Content-Type: application/octet-stream');
          header('Content-Disposition: attachment; filename="'.basename($file).'"');
          header('Expires: 0');
          header('Cache-Control: must-revalidate');
          header('Pragma: public');
          header('Content-Length: ' . filesize($file));
          readfile($file);
          exit;
      }
    }
  }
?>
