<?php
require __DIR__."/../../../ComponentCode/phpJasperXML_2.0.1/autoload.php";

use simitsdk\phpjasperxml\PHPJasperXML;

$filename = __DIR__."/../../".$_SESSION['InformeJRXML'];

// read parameters
$idProcess = $_SESSION['idProcess'];            // Paran type Integer
$tableStatus = $_SESSION['superCodeStatus'];  	// Param type String
$language = $_SESSION['language'];            	// Param type String

// Read record
$data = array();
$rs = DB::Query("
SELECT
expedient.idexpedient,
expedient.process_idprocess,
expedient.ct_status_idct_status,
concat (status.catalogNum,' - ',status.catalogCode) Name_status,
expedient.codeExpedient,
1000.00 Numero,
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 = $idProcess and  n.superCode = '$tableStatus' and n.language = '$language') 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)
ORDER by 3 asc, 1 asc
");
while( $row= $rs->fetchAssoc() )
{
$data[] = $row;
}
$config = [
    'driver'=>'array',
    'data'=> $data
];
$parameters = [];


$type = 'Pdf';
// $fileOutput= '';
$creator = 'FHumanes';
$author = 'fernandohumanes@gmail.com';
$title = $_SESSION['title_report'];
$keywords = 'PHPRunner';
$format_intl = 'es_ES';

$temp_file = tempnam(sys_get_temp_dir(), 'pdf');  // Create file temporal
$fileOutput = $temp_file;

$report = new PHPJasperXML();
$report->load_xml_file($filename)    
    ->setParameter($parameters)
    ->setDataSource($config)
    ->export($type,$fileOutput,$title,$creator,$author,$keywords,$format_intl);
