<?php
require_once '../Conexion/conexion.php';
require_once '../Conexion/ConexionPDO.php';
setlocale(LC_ALL, "es_ES");
date_default_timezone_set("America/Bogota");
@session_start();
$id_factura = $_GET['id'];
$compania   = $_SESSION['compania'];
$con = new ConexionPDO();


$resul      = $con->Listar("SELECT gp_tipo_factura.prefijo,gp_factura.numero_factura FROM gp_factura 
LEFT JOIN gp_tipo_factura on gp_tipo_factura.id_unico = gp_factura.tipofactura where gp_factura.id_unico = " . $id_factura . "");
// $resul      = $mysqli->query($queryNFa);
$roww       = $resul;
$numeroDocumentoComercial   = $roww[0]['prefijo'] . $roww[0]['numero_factura'];


$resul      = $con->Listar("SELECT numeroidentificacion FROM gf_tercero where id_unico = $compania ");
// $resul      = $mysqli->query($query);
$roww       = $resul;
$companiaa  = $roww[0]['numeroidentificacion'];
$nit        = $companiaa;

$Mensaje    = '';


//-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
//token
$resultTkn = $con->Listar("SELECT token_fe FROM gf_tercero where id_unico='" . $compania . "'");
// $resultTkn = $mysqli->query($sqlTkn);
$rowTkn = $resultTkn;

$token = $rowTkn[0]["token_fe"];
if ($token == null) {
  Token($compania);
}




function Token($compania)
{
  global $con;
  $resl   = $con->Listar("SELECT usuario_fe,contrasena_fe FROM gf_tercero where id_unico='" . $compania . "' ");
  // $resl   = $mysqli->query($qury);
  $rowwu  = $resl;
  $usu    = $rowwu[0]['usuario_fe'];
  $contra = $rowwu[0]['contrasena_fe'];

  $usuario        = $usu;
  $contrasenia    = $contra;
  //API url:
  $url = 'https://csi.clarisa.co:8443/seguridad/rest/api/v1/login/';
  //JSON
  $data = array(
    'usuario' => $usuario,
    'contrasenia' => $contrasenia
  );

  //configuraciones del json
  $options = array(
    'http' => array(
      'header' => "Content-Type: application/json",
      'method' => 'POST',
      'content' => json_encode($data)
    )
  );

  //engine:
  $context = stream_context_create($options);
  $result = file_get_contents($url, false, $context);
  if ($result === FALSE) { /* Handle error */
  }
  //compiling  JSON
  $resultData = json_decode($result, TRUE);
  $re = $resultData["data"];
  $token = $re["token"];

  // $sqlEdiTknn = "UPDATE gf_tercero set token_fe ='" . $token . "' where id_unico='" . $compania . "'";
  // $resultTkn = $mysqli->query($sqlEdiTknn);
  $sql_cons = "UPDATE gf_tercero set token_fe =:token where id_unico= :id_unico";
  $sql_dato = array(
    array(":token_fe", $token),
    array(":id_unico", $compania),

  );
  $obj_resp = $con->InAcEl($sql_cons, $sql_dato);
  // if ($resultTkn == false) {
  if (empty($obj_resp)) {
    echo "ERROR";
  }
  return $token;
}


$Mensaje = pdf($token, $nit, $numeroDocumentoComercial);

if ($Mensaje == '') {
  $token = Token($compania);

  // $sqlEdiTknn = "UPDATE gf_tercero set token_fe ='" . $token . "' where id_unico='" . $compania . "'";
  // $resultTkn = $mysqli->query($sqlEdiTknn);

  $sqlEdiTknn = "UPDATE gf_tercero set token_fe =:token_fe where id_unico=:id_unico";

  $sql_dato = array(
    array(":token_fe", $token),
    array(":id_unico", $compania),

  );
  $obj_resp = $con->InAcEl($sql_cons, $sql_dato);

  $Mensaje = pdf($token, $nit, $numeroDocumentoComercial);
}


function pdf($token, $nit, $numeroDocumentoComercial)
{
  $curl = curl_init();

  curl_setopt_array($curl, [
    CURLOPT_PORT => "8443",
    CURLOPT_URL => "https://csi.clarisa.co/reportes/rest/api/v1/pdf/factura?nit=" . $nit . "&numeroFactura=" . $numeroDocumentoComercial . "",
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_ENCODING => "",
    CURLOPT_MAXREDIRS => 10,
    CURLOPT_TIMEOUT => 30,
    CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
    CURLOPT_CUSTOMREQUEST => "GET",
    CURLOPT_POSTFIELDS => "",
    CURLOPT_COOKIE => "JSESSIONID=T-BnFgT-56AicuIZjUdJwv5k6ZwZBOi_UAu0olV7.ip-172-31-9-34",
    CURLOPT_HTTPHEADER => [
      "Authorization:'" . $token . "'",
      "Content-Type: application/json"
    ],
  ]);

  $response   = curl_exec($curl);
  $err        = curl_error($curl);

  curl_close($curl);


  if ($err) {
    //echo "cURL Error #:" . $err;
    $solu3 = "cURL Error #:" . $err;
  } else {
    $solu = json_decode($response, true);
    $solu2 = $solu['data'];

    $Mensaje = $solu2;
  }
  return $Mensaje;
}
if ($Mensaje == '') {
  header('Location: ../informes/inf_factura_electronica.php?factura=' . base64_encode($id_factura) . '&t=' . $_REQUEST['t']);
} else {

  $archivo = fopen("datos.txt", "w+");
  $contenido = $Mensaje;
  fwrite($archivo, $contenido);


  $pdf_base64 = "datos.txt";

  $pdf_base64_handler = fopen($pdf_base64, 'r');
  $pdf_content = fread($pdf_base64_handler, filesize($pdf_base64));
  fclose($pdf_base64_handler);
  $pdf_decoded = base64_decode($pdf_content);
  $pdf = fopen('FE.pdf', 'w');
  fwrite($pdf, $pdf_decoded);
  fclose($pdf);


  header("Content-type: application/pdf");
  header("Content-Disposition: inline; filename=documento.pdf");
  readfile("FE.pdf");
}
