<?php
session_start();
$usuarioSession = $_SESSION['USUARIO'];
$grupo = $_SESSION['GRUPO'];
$contacto = $_SESSION['CONTACTO'];
$awa_session_id = $_SESSION['AWA_SESION_ID'];
include('../includes/conex.php');
include('../includes/eat.php');
include('../includes/historial.php');
include('../includes/esquemas.php');
if(!isset($usuarioSession) or $awa_session_id != $awa_id)
{
    eatCookies();
    header('Location:../index.php');
}
if(isset($_COOKIE['usuario']) AND $_COOKIE['usuario'] != $staticUsu )
{
    eatCookies();
    header('Loction:../index.php');
}
if(isset($_COOKIE['idioma']))
{
    include('../lg/LG_' . $_COOKIE['idioma'] . '.php');
}
else
{
    include('../lg/LG_SP.php');
}
$codigo = $_SESSION['CODIGO_COCHE'];
$documento = htmlspecialchars(mysqli_real_escape_string($con,$_GET['doc']));
$esquema = get_esquemas_y_esquemas_estructuras($con)[0];
$grupoe = get_esquemas_y_esquemas_estructuras($con)[1];
$sql = "SELECT E.NOMBRE, E.SUFIJO, E.PARTES
FROM BIBLIOTECA_ELEMENTOS E
WHERE CLAVE = '$documento'";
$res = $con->query($sql);
$fila = $res->fetch_array();
$tmp_doc = '../ofertas/tmp_pdf/' . $fila['NOMBRE'];
if(file_exists($tmp_doc))
{
    unlink($tmp_doc);
}
$create_doc = fopen($tmp_doc,'a');
$values = $fila['PARTES'];
for($i = 1; $i<= $values; $i++)
{
    $sqlPartes = "SELECT FICHERO
    FROM FICHEROS_" . $fila['SUFIJO'] . " 
    WHERE CLAVE = " . $documento . " 
    AND PARTE = $i
    AND VERSION = '0'";
    $resPartes = $con->query($sqlPartes);
    $filaPartes = $resPartes->fetch_array();
    $doc = fwrite($create_doc,$filaPartes['FICHERO']);
}
fclose($create_doc);
$filename = basename($fila['NOMBRE']);
header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-Disposition: attachment; filename=$filename");
header("Content-Type: application/zip");
header("Content-Transfer-Emcoding: binary");
readfile($tmp_doc);
unlink($tmp_doc);
historial($con,6,$codigo,$fila['NOMBRE']);
exit;
?>