/**
  * Class constructor
  * Creates the page
  */
 function __construct()
 {
     parent::__construct();
     $data['maria'] = array(1, 2, 3, 4, 5, 6, 7);
     $data['pedro'] = array(12, 3, 12, 4, 12, 4, 2);
     $data['joao'] = array(9, 8, 7, 6, 5, 4, 3);
     $chart = new TLineChart(new TPChartDesigner());
     $chart->setTitle('Chart title', NULL, NULL);
     $chart->setSize(640, 300);
     $chart->setXLabels(array('a', 'b', 'c', 'd', 'e', 'f', 'g'));
     $chart->setYLabel('Y axis label');
     $chart->setOutputPath('app/output/linechart.png');
     $chart->addData('maria', $data['maria']);
     $chart->addData('pedro', $data['pedro']);
     $chart->addData('joao', $data['joao']);
     $chart->generate();
     // wrap the page content using vertical box
     $vbox = new TVBox();
     $vbox->add(new TXMLBreadCrumb('menu.xml', __CLASS__));
     $vbox->add(new TImage('app/output/linechart.png'));
     parent::add($vbox);
 }
 /**
  * method onGenerate()
  * Executed whenever the user clicks at the generate button
  */
 function onGenerate()
 {
     try {
         // open a transaction with database 'atividade'
         TTransaction::open('atividade');
         // get the form data into an active record
         $formdata = $this->form->getData();
         $dataInicial = date('Y') . '-' . str_pad($formdata->mes_atividade, 2, 0, STR_PAD_LEFT) . '-01';
         $dataFinal = date('Y') . '-' . str_pad($formdata->mes_atividade, 2, 0, STR_PAD_LEFT) . '-' . cal_days_in_month(CAL_GREGORIAN, $formdata->mes_atividade, date('Y'));
         $dias = Atividade::retornaDiasAtividades($dataInicial, $dataFinal);
         $criteria = new TCriteria();
         $criteria->add(new TFilter("origem", "=", 1));
         $criteria->add(new TFilter("codigo_cadastro_origem", "=", 100));
         $criteria->add(new TFilter("ativo", "=", 1));
         $criteria->add(new TFilter("pessoa_codigo", "IN", "(SELECT colaborador_id FROM atividade WHERE data_atividade between '{$dataInicial}' and '{$dataFinal}')"));
         $newparam['order'] = 'pessoa_nome';
         $newparam['direction'] = 'asc';
         $criteria->setProperties($newparam);
         // order, offset
         $repo = new TRepository('Pessoa');
         $pessoas = $repo->load($criteria);
         $format = $formdata->output_type;
         if ($dias) {
             $widths = array(50);
             switch ($format) {
                 case 'html':
                     $tr = new TTableWriterHTML($widths);
                     $break = '<br />';
                     break;
                 case 'pdf':
                     $tr = new TTableWriterPDF($widths);
                     $break = '';
                     break;
                 case 'rtf':
                     if (!class_exists('PHPRtfLite_Autoloader')) {
                         PHPRtfLite::registerAutoloader();
                     }
                     $tr = new TTableWriterRTF($widths);
                     $break = '<br />';
                     break;
             }
             // create the document styles
             $tr->addStyle('title', 'Arial', '10', 'B', '#ffffff', '#6B6B6B');
             $tr->addStyle('datap', 'Arial', '10', '', '#000000', '#E5E5E5');
             $tr->addStyle('datai', 'Arial', '10', '', '#000000', '#ffffff');
             $tr->addStyle('totais', 'Arial', '10', '', '#000000', '#C0D3E9');
             $tr->addStyle('header', 'Times', '16', 'B', '#4A5590', '#C0D3E9');
             $tr->addStyle('footer', 'Times', '12', 'BI', '#4A5590', '#C0D3E9');
             // add a header row
             $tr->addRow();
             $tr->addCell(utf8_decode('Indicador de produtividade de: ') . strtoupper($this->string->array_meses()[$formdata->mes_atividade]), 'center', 'header', 33);
             $tr->addRow();
             $tr->addCell('Seq.', 'center', 'title');
             $tr->addCell('Nome', 'center', 'title');
             foreach ($dias as $dia) {
                 $tr->addCell(substr($dia['dias'], -2), 'center', 'title');
                 $arrayDias[substr($dia['dias'], -2)] = $dia['dias'];
             }
             $tr->addCell('TOTAL', 'center', 'title');
             // controls the background filling
             $colour = FALSE;
             // data rows
             $count = 1;
             foreach ($pessoas as $pessoa) {
                 $style = $colour ? 'datap' : 'datai';
                 $tr->addRow();
                 $tr->addCell($count, 'left', $style);
                 $tr->addCell(utf8_decode($pessoa->pessoa_nome), 'left', $style, 33);
                 $tr->addRow();
                 $tr->addCell('', 'left', $style);
                 $tr->addCell('Ponto', 'left', $style);
                 $totalPonto = null;
                 foreach ($arrayDias as $dia) {
                     $ponto = $this->retornaPonto($pessoa->pessoa_codigo, $dia);
                     $totalPonto += $this->string->time_to_sec($ponto);
                     $tr->addCell(substr($ponto, 0, -3), 'center', $style);
                     $arrayPonto[$dia] += $this->string->time_to_sec($ponto);
                 }
                 $arrayPonto['total'] += $totalPonto;
                 $tr->addCell(substr($this->string->sec_to_time($totalPonto), 0, -3), 'center', $style);
                 $tr->addRow();
                 $tr->addCell('', 'left', $style);
                 $tr->addCell('Atividades', 'left', $style);
                 $totalAtividades = null;
                 foreach ($arrayDias as $dia) {
                     $horas = Ponto::horaPreenchidas($dia, $pessoa->pessoa_codigo);
                     if (!$horas) {
                         $horas = '00:00:00';
                     }
                     $x = $this->retornaPonto($pessoa->pessoa_codigo, $dia);
                     if ($this->string->time_to_sec($x) > 0) {
                         $totalAtividades += $this->string->time_to_sec($horas);
                         $arrayAtividades[$dia] += $this->string->time_to_sec($horas);
                     }
                     $tr->addCell(substr($horas, 0, -3), 'center', $style);
                 }
                 $arrayAtividades['total'] += $totalAtividades;
                 $tr->addCell(substr($this->string->sec_to_time($totalAtividades), 0, -3), 'center', $style);
                 $tr->addRow();
                 $tr->addCell('', 'left', $style);
                 $tr->addCell('Produtividade', 'left', $style);
                 foreach ($arrayDias as $dia) {
                     $campo = $this->calculaPercentualProdutividade($pessoa->pessoa_codigo, $dia);
                     $tr->addCell($campo, 'center', $style);
                 }
                 $tr->addCell($this->calculaPercentualProdutividadeTotalColaborador($totalAtividades, $totalPonto), 'center', $style);
                 $tr->addRow();
                 $tr->addCell($break, 'left', $style, 33);
                 $count++;
                 $colour = !$colour;
             }
             // totais row
             $tr->addRow();
             $tr->addCell($count, 'left', 'totais');
             $tr->addCell('TOTAIS', 'left', 'totais', 33);
             $tr->addRow();
             $tr->addCell('', 'left', 'totais');
             $tr->addCell('Ponto', 'left', 'totais');
             foreach ($arrayDias as $dia) {
                 $tr->addCell(substr($this->string->sec_to_time($arrayPonto[$dia]), 0, -3), 'center', 'totais');
             }
             $tr->addCell(substr($this->string->sec_to_time($arrayPonto['total']), 0, -3), 'center', 'totais');
             $tr->addRow();
             $tr->addCell('', 'left', 'totais');
             $tr->addCell('Atividades', 'left', 'totais');
             foreach ($arrayDias as $dia) {
                 $tr->addCell(substr($this->string->sec_to_time($arrayAtividades[$dia]), 0, -3), 'center', 'totais');
             }
             $tr->addCell(substr($this->string->sec_to_time($arrayAtividades['total']), 0, -3), 'center', 'totais');
             $tr->addRow();
             $tr->addCell('', 'left', 'totais');
             $tr->addCell('Produtividade', 'left', 'totais');
             foreach ($arrayDias as $dia) {
                 $tr->addCell($this->calculaPercentualProdutividadeTotalColaborador($arrayAtividades[$dia], $arrayPonto[$dia]), 'center', 'totais');
             }
             $tr->addCell($this->calculaPercentualProdutividadeTotalColaborador($arrayAtividades['total'], $arrayPonto['total']), 'center', 'totais');
             // footer row
             $tr->addRow();
             $var = rand(0, 1000);
             $tr->addCell(date('d/m/Y H:i:s'), 'center', 'footer', 33);
             foreach ($arrayAtividades as $key => $value) {
                 if ($key != 'total') {
                     $yAtividades[substr($key, -2)] = $this->string->sec_to_time($value);
                     $xLabel[substr($key, -2)] = substr($key, -2);
                 }
             }
             foreach ($arrayPonto as $key => $value) {
                 if ($key != 'total') {
                     $yPonto[substr($key, -2)] = $this->string->sec_to_time($value);
                 }
             }
             ksort($yAtividades);
             ksort($yPonto);
             ksort($xLabel);
             $chart = new TLineChart(new TPChartDesigner());
             $chart->setTitle('Totais', NULL, NULL);
             $chart->setSize(1100, 600);
             $chart->setXLabels($xLabel);
             $chart->setYLabel('Horas');
             $chart->setOutputPath('app/output/linechart_{$var}.png');
             $chart->addData('Atividades', $yAtividades);
             $chart->addData('Ponto', $yPonto);
             $chart->generate();
             $tr->addRow();
             $tr->addCell(new TImage('app/output/linechart_{$var}.png'), 'center', 'datai', 33);
             // stores the file
             if (!file_exists("app/output/Ponto_{$var}.{$format}") or is_writable("app/output/Ponto_{$var}.{$format}")) {
                 $tr->save("app/output/Ponto_{$var}.{$format}");
             } else {
                 throw new Exception(_t('Permission denied') . ': ' . "app/output/Ponto_{$var}.{$format}");
             }
             // open the report file
             parent::openFile("app/output/Ponto_{$var}.{$format}");
             // shows the success message
             new TMessage('info', 'Relatorio gerado. Por favor, habilite popups no navegador (somente para web).');
         } else {
             new TMessage('error', 'No records found');
         }
         // fill the form with the active record data
         $this->form->setData($formdata);
         // close the transaction
         TTransaction::close();
     } catch (Exception $e) {
         // shows the exception error message
         new TMessage('error', '<b>Error</b> ' . $e->getMessage());
         // undo all pending operations
         TTransaction::rollback();
     }
 }