Skip to content

Instantly share code, notes, and snippets.

@carlosleonam
Created October 19, 2021 03:01
Show Gist options
  • Save carlosleonam/ba4f0c26dc80050c829b02d05ab07e9a to your computer and use it in GitHub Desktop.
Save carlosleonam/ba4f0c26dc80050c829b02d05ab07e9a to your computer and use it in GitHub Desktop.
Gerar Relatorio no Jasper (Form) (by @fernando Fernandes)
<?php
use Adianti\Control\TWindow;
class GeraRelatorioForm extends TWindow
{
protected $form; // formulário
private $valueField;
private $datagrid; // listing
private $pageNavigation;
private $loaded;
private $filter_criteria;
private static $database = 'database';
private static $activeRecord = 'Relatorios';
private static $primaryKey = 'id';
private static $formName = 'gerar_relatorio_form';
private $relatorioPath = '';
function __construct()
{
parent::__construct();
// creates the form
$this->form = new BootstrapFormBuilder(self::$formName);
$this->setSize(600, null);
$this->setTitle('Gerar Relatório');
$this->setProperty('class', 'window_modal');
// creates a DataGrid
$this->datagrid = new BootstrapDatagridWrapper(new TDataGrid);
$this->datagrid->style = 'width: 100%';
$this->datagrid->disableDefaultClick();
// creates the datagrid columns
$descricaoInput = new TDataGridColumn('descricaoInput', 'Descrição', 'left');
$valorInput = new TDataGridColumn('valorInput', 'Valor', 'left');
// add the columns to the DataGrid
$this->datagrid->addColumn($descricaoInput);
$this->datagrid->addColumn($valorInput);
// create the datagrid model
$this->datagrid->createModel();
$panel = new TPanelGroup();
$panel->{'widget'} = "bootstrapformbuilder";
$panel->add($this->datagrid)->style = 'overflow-x:auto';
$actionGerar = new TAction(array($this, 'gerarRelatorio'), ['static' => '1']);
$this->form->addFields([$panel]);
$btn_gerar_relatorio = $this->form->addAction('Gerar Relatório', $actionGerar, 'far:save');
$btn_gerar_relatorio->class .= 'btn btn-primary';
// wrap the page content using vertical box
$vbox = new TVBox;
$vbox->style = 'width: 100%';
$vbox->add($this->form);
TStyle::importFromFile('app/lib/include/table/styles.css');
TStyle::importFromFile('app/lib/include/window/style.css');
parent::add($vbox);
}
/**
* method onReload()
* Load the datagrid with the database objects
*/
function onShow($param = NULL)
{
try {
// open a transaction with database 'samples'
TTransaction::open(self::$database);
$this->datagrid->clear();
$this->form->delFields();
if (isset($param['id'])) {
$readerFormReport = Relatorios::find($param['id']);
}
foreach ($readerFormReport->getParametros() as $data) {
$object = new stdClass;
if (str_contains($data->tipo, 'texto')) {
$object->valorInput = new TEntry($data->parametro);
$object->valorInput->setValue($data->valor_padrao);
$object->descricaoInput = $data->parametro;
}
if (str_contains($data->tipo, 'Data')) {
$object->valorInput = new TDate($data->parametro);
// $object->valorInput->setDatabaseMask('yyyy-mm-dd');
// $object->valorInput->setMask('dd/mm/yyyy');
$object->valorInput->setValue(date('Y-m-d'));
$object->descricaoInput = $data->parametro;
}
if (str_contains($data->tipo, 'booleano')) {
$object->valorInput = new TCombo($data->parametro);
$object->valorInput->setValue($data->valor_padrao);
$object->valorInput->setSize('80%');
$object->valorInput->addItems(['S' => 'Sim', 'N' => 'Não']);
$object->descricaoInput = $data->parametro;
}
$this->datagrid->addItem($object);
$this->form->addField($object->valorInput); // important!
}
//Helper add id into form
$id = new stdClass;
$id->valorInput = new THidden('id');
$id->valorInput->setValue($param['id']);
$linhaHidden = $this->datagrid->addItem($id);
$linhaHidden->style = "display: none;";
$this->form->addField($id->valorInput);
//Helper add path into form
$path = new stdClass;
$path->valorInput = new THidden('path');
$path->valorInput->setValue($this->relatorioPath);
$linhaHidden = $this->datagrid->addItem($path);
$linhaHidden->style = "display: none;";
$this->form->addField($path->valorInput);
// close the transaction
TTransaction::close();
$this->loaded = true;
} catch (Exception $e) // in case of exception
{
// shows the exception error message
new TMessage('error', $e->getMessage());
// undo all pending operations
TTransaction::rollback();
}
}
public function getQueryString($param, $paramDb)
{
$queryString = "?";
foreach ($param as $key => $val) {
foreach ($paramDb as $data) {
if ($key == $data->parametro) {
$queryString = $queryString . $key . "=" . $val . "&";
}
}
}
return $queryString;
}
public function gerarRelatorio($param)
{
try {
TTransaction::open(self::$database);
if (isset($param['id'])) {
$report = Relatorios::find($param['id']);
}
$criteria = new TCriteria;
$criteria->add(new TFilter('id', 'IN', [5, 6, 7]));
$jasperServer = Preferencias::getObjects($criteria);
$queryString = $this->getQueryString($param, $report->getParametros());
$url = $jasperServer[0]->valor . $report->relatorio . '.pdf' . $queryString;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_TIMEOUT, 90000); //timeout after 30 seconds
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($ch, CURLOPT_USERPWD, strval($jasperServer[1]->valor) . ':' . strval($jasperServer[2]->valor));
$data = curl_exec($ch);
$this->status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE); //get status code
curl_close($ch);
if ($this->status_code != 200) {
$xml = simplexml_load_string(strval($data));
var_dump($xml);
$exception = new Exception("Erro $this->status_code. $xml->errorCode: $xml->message.");
$exception->errorCode = $xml->errorCode;
$exception->errorMessage = $xml->message;
throw $exception;
}
$uniqName = uniqid();
$fp_patchW = '/var/www/html/scsweb/app/output/' . $uniqName . '.pdf';
$fp_patchR = 'app/output/' . $uniqName . '.pdf';
$fp = fopen($fp_patchW, 'w');
fwrite($fp, $data);
fclose($fp);
$window = TWindow::create($report->titulo, 0.9, 0.9);
$embed = new TElement('embed');
$embed->src = $fp_patchR;
$embed->style = 'width:100%;height:95%';
$window->add($embed);
$window->show();
TTransaction::close();
} catch (Exception $e) // in case of exception
{
// shows the exception error message
new TMessage('error', $e->getMessage());
// undo all pending operations
TTransaction::rollback();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment