<?php

require_once __DIR__ . './../../../ComponentCode/PhpSpreadsheet_1.10/autoload.php'; 

use PhpOffice\PhpSpreadsheet\IOFactory;
use PhpOffice\PhpSpreadsheet\Shared\Date;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
use PhpOffice\PhpSpreadsheet\NamedRange;
use PhpOffice\PhpSpreadsheet\RichText\RichText;
use PhpOffice\PhpSpreadsheet\Style\Color;
use PhpOffice\PhpSpreadsheet\Style\NumberFormat;

//  Template processor instance creation

$reader = new \PhpOffice\PhpSpreadsheet\Reader\Xlsx();
// $reader->setIncludeCharts(true);
// $reader->setReadFilter();
// $reader->setReadEmptyCells(true);

$template_workflow = $_SESSION['InformeEXCEL'];

$base = __DIR__;

$folder = $_SESSION['config'][array_search('DOCUMENT_ROOT_WF2', array_column($_SESSION['config'], 'name'))][value];

$spreadsheet = $reader->load($base. '/'.$folder . $template_workflow);

// Recuperar datos 

// global $conn;

$VidProcess = $_SESSION['idProcess'] ; 
$VsuperCodeStatus = $_SESSION['superCodeStatus'];
$Vlanguage = $_SESSION['language'];
$VidExpedient = $_SESSION['idExpedient'];

// $Viduser=$_SESSION['iduser'];
$sql="
SELECT
expedient.idexpedient,
expedient.process_idprocess,
expedient.ct_status_idct_status,
concat (status.catalogNum,' - ',status.catalogCode) Name_status,
expedient.codeExpedient,
expedient.title,
expedient.observations,
expedient.assignedUser,
ifnull(user1.NameAndSurname,'') Name_assignedUser,
expedient.creationDate,
expedient.creationUser,
ifnull(user2.NameAndSurname,'') Name_creationUser,
expedient.lastUpdateDate,
expedient.lastUpdateUser,
ifnull(user3.NameAndSurname,'') Name_lastUpdateUser,
expedient.finishDate,
expedient.ex04_advanceAmount
FROM expedient
join (
SELECT
s.idct_status,
s.catalogNum,
n.catalogCode
FROM ct_status s
join (
SELECT
s.idsuper_catalog,
s.superCode,
c.language,
c.catalogNum,
c.catalogCode,
c.description,
c.groupNum
FROM catalog AS c
INNER JOIN super_catalog AS s ON c.super_catalog_idsuper_catalog = s.idsuper_catalog
  ) n on ( s.catalogNum = n.catalogNum )
where process_idprocess = $VidProcess and  n.superCode = '$VsuperCodeStatus' and n.language = '$Vlanguage') status
    on (status.idct_status = expedient.ct_status_idct_status )
left join user user1 on (user1.iduser = expedient.assignedUser)
left join user user2 on (user2.login = expedient.creationUser)
left join user user3 on (user3.login = expedient.lastUpdateUser)
where expedient.idexpedient = $VidExpedient
";
$resql=db_query($sql,$conn);
$data=db_fetch_array($resql);

// Variables on different parts of document
$spreadsheet->setActiveSheetIndex(0);

$DateList=now();
$spreadsheet->getActiveSheet()->setCellValue('D2', Date::PHPToExcel($DateList));
$spreadsheet->getActiveSheet()->setCellValue('B2', $data['codeExpedient']);

$spreadsheet->getActiveSheet()->setCellValue('C4', $data['title']);

$richText = new RichText();
$richText->createText($data['observations']); // Field type Memo
$spreadsheet->getActiveSheet()->getCell('C5')->setValue($richText);
$spreadsheet->getActiveSheet()->getStyle('C5')->getAlignment()->setWrapText(true);

$spreadsheet->getActiveSheet()->setCellValue('C6', $data['ex04_advanceAmount']);
$spreadsheet->getActiveSheet()->setCellValue('C7', $data['Name_status']);
$spreadsheet->getActiveSheet()->setCellValue('C8', $data['Name_assignedUser']);
$spreadsheet->getActiveSheet()->setCellValue('C9', Date::PHPToExcel($data['creationDate']));
$spreadsheet->getActiveSheet()->setCellValue('C10', $data['Name_creationUser']);
$spreadsheet->getActiveSheet()->setCellValue('C11', Date::PHPToExcel($data['finishDate']));
$spreadsheet->getActiveSheet()->setCellValue('C12', Date::PHPToExcel($data['lastUpdateDate']));
$spreadsheet->getActiveSheet()->setCellValue('C13', $data['Name_lastUpdateUser']);


// -------------------- v foot to save the new Excel document ------------------
$temp_file = tempnam(sys_get_temp_dir(), 'Excel');
// Save EXCEL
$writer = new Xlsx($spreadsheet);
$writer->setPreCalculateFormulas(false); //Disable formula validation
$writer->save($temp_file);

// $ temp_file must contain the resulting file
?>
