<?php
include_once '../ComponentCode/PHPWord_0.17/autoload.php';

// Template processor instance creation
$template_workflow = $_SESSION['InformeDOCX'];
$templateProcessor = new \PhpOffice\PhpWord\TemplateProcessor($template_workflow);

// -------------------- ^ header needed for Word templates ------------------

$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
$templateProcessor->setValue('Name_creationUser', $data['Name_creationUser']);  
$templateProcessor->setValue('ex04_advanceAmount ', number_format($data['ex04_advanceAmount'], 2, ',', '.'));

$today = date("Y-m-d H:i:s");

$templateProcessor->setValue('today_date', formatDate($today));

// -------------------- v foot to save new Word document ------------------
$temp_file = tempnam(sys_get_temp_dir(), 'PHPWord');
$templateProcessor->saveAS($temp_file);

// $ temp_file must contain the resulting file
