/**
  * 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();
         $format = $formdata->output_type;
         $tickets = null;
         if ($formdata->cliente_id > 0) {
             $cliente = Pessoa::getPessoasEntidade($formdata->cliente_id);
             $retorno = Ticket::getTicketsCliente($cliente);
             $tickets = implode(",", $retorno);
         }
         $total = Atividade::retornaTotalAtividadesColaborador($formdata->colaborador_id, $formdata->mes_atividade, $formdata->ano_atividade, $tickets);
         if ($total) {
             $widths = array(25, 350, 70, 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('header', 'Times', '16', 'B', '#4A5590', '#C0D3E9');
             $tr->addStyle('footer', 'Times', '12', 'BI', '#4A5590', '#C0D3E9');
             $titulo = 'TODOS COLABORADORES';
             if ($formdata->colaborador_id > 0) {
                 $colaborador = new Pessoa($formdata->colaborador_id);
                 $titulo = utf8_decode($colaborador->pessoa_nome);
                 $totalPonto = $this->retornaPonto($formdata->colaborador_id, $formdata->mes_atividade);
                 $criteria = new TCriteria();
                 $criteria->add(new TFilter("mes", "=", $formdata->mes_atividade));
                 $criteria->add(new TFilter("ano", "=", $formdata->ano_atividade));
                 $criteria->add(new TFilter("colaborador_id", "=", $formdata->colaborador_id));
                 $repo = new TRepository('CargaHoraria');
                 $cargaHoraria = $repo->load($criteria);
                 foreach ($cargaHoraria as $carga) {
                     $horario = $carga->horario;
                 }
             } else {
                 //calcular todos
                 $criteria = new TCriteria();
                 $criteria->add(new TFilter("mes", "=", $formdata->mes_atividade));
                 $criteria->add(new TFilter("ano", "=", $formdata->ano_atividade));
                 $repo = new TRepository('CargaHoraria');
                 $cargaHoraria = $repo->load($criteria);
                 foreach ($cargaHoraria as $carga) {
                     $horario += $this->string->time_to_sec($carga->horario);
                 }
                 $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("usuario", "is not "));
                 $repo = new TRepository('Pessoa');
                 $pessoas = $repo->load($criteria);
                 foreach ($pessoas as $pessoa) {
                     $totalPonto += $this->string->time_to_sec($this->retornaPonto($pessoa->pessoa_codigo, $formdata->mes_atividade));
                 }
                 $horario = $this->string->sec_to_time($horario);
                 $totalPonto = $this->string->sec_to_time($totalPonto);
             }
             // report description
             $tr->addRow();
             $tr->addCell('', 'center', 'title');
             $tr->addCell($titulo, 'center', 'title');
             $tr->addCell("{$this->string->array_meses()[$formdata->mes_atividade]}-{$formdata->ano_atividade}", 'center', 'title', 2);
             // add a header row
             $tr->addRow();
             $tr->addCell('', 'center', 'header');
             $tr->addCell('Indicadores', 'center', 'header');
             $tr->addCell('Horas', 'center', 'header');
             $tr->addCell('%', 'center', 'header');
             // data rows
             $style = 'datai';
             $tr->addRow();
             $tr->addCell('', 'center', $style);
             $tr->addCell(utf8_decode('Carga horária mensal:'), 'left', $style);
             $tr->addCell($this->string->retira_segundos($horario), 'right', $style);
             $tr->addCell('100%', 'right', $style);
             $tr->addRow();
             $tr->addCell('', 'center', $style);
             $tr->addCell(utf8_decode('Horas ponto total:'), 'left', $style);
             $tr->addCell($this->string->retira_segundos($totalPonto), 'right', $style);
             $tr->addCell(round($this->string->time_to_sec($totalPonto) * 100 / $this->string->time_to_sec($horario)) . '%', 'right', $style);
             $cri = new TCriteria();
             if ($formdata->colaborador_id > 0) {
                 $cri->add(new TFilter("colaborador_id", "=", $formdata->colaborador_id));
             }
             $cri->add(new TFilter("ticket_id", "IN", array(328, 514)));
             $cri->add(new TFilter("extract('month' from data_atividade)", "=", $formdata->mes_atividade));
             $cri->add(new TFilter("extract('year' from data_atividade)", "=", $formdata->ano_atividade));
             $repo = new TRepository('Atividade');
             $ausencias = $repo->count($cri);
             if ($ausencias) {
                 $horas = $repo->load($cri);
                 foreach ($horas as $h) {
                     $tempo += $this->string->time_to_sec($h->hora_fim) - $this->string->time_to_sec($h->hora_inicio);
                 }
                 $pontoUtil = $this->string->time_to_sec($totalPonto) - $tempo;
                 $tr->addRow();
                 $tr->addCell('', 'center', $style);
                 $tr->addCell(utf8_decode('Horas ponto útil:'), 'left', $style);
                 $tr->addCell($this->string->retira_segundos($this->string->sec_to_time($pontoUtil)), 'right', $style);
                 $tr->addCell(round($pontoUtil * 100 / $this->string->time_to_sec($horario)) . '%', 'right', $style);
                 $tr->addRow();
                 $tr->addCell('', 'center', 'datap');
                 $tr->addCell(utf8_decode('Horas atividade (indicador total):'), 'left', 'datap');
                 $tr->addCell($this->string->retira_segundos($total), 'right', 'datap');
                 $tr->addCell(round($this->string->time_to_sec($total) * 100 / $this->string->time_to_sec($totalPonto)) . '%', 'right', 'datap');
                 $tr->addRow();
                 $tr->addCell('', 'center', 'datap');
                 $tr->addCell(utf8_decode('Horas atividade (indicador útil):'), 'left', 'datap');
                 $tr->addCell($this->string->retira_segundos($total), 'right', 'datap');
                 $tr->addCell(round($this->string->time_to_sec($total) * 100 / $pontoUtil) . '%', 'right', 'datap');
             } else {
                 $tr->addRow();
                 $tr->addCell('', 'center', $style);
                 $tr->addCell(utf8_decode('Horas atividade:'), 'left', $style);
                 $tr->addCell($this->string->retira_segundos($total), 'right', $style);
                 $tr->addCell(round($this->string->time_to_sec($total) * 100 / $this->string->time_to_sec($totalPonto)) . '%', 'right', $style);
             }
             // division row
             $tr->addRow();
             $tr->addCell($break, 'center', 'datai', 4);
             //ATESTADOS MEDICOS
             $objects = Atividade::retornaAtestadosMedicos($formdata->colaborador_id, $formdata->mes_atividade, $formdata->ano_atividade);
             // add a header row
             $tr->addRow();
             $tr->addCell('', 'center', 'header');
             $tr->addCell(utf8_decode('Ausências'), 'center', 'header');
             $tr->addCell('Horas', 'center', 'header');
             $tr->addCell('', 'center', 'header');
             // controls the background filling
             $colour = FALSE;
             $seq = 1;
             // data rows
             foreach ($objects as $row) {
                 $style = $colour ? 'datap' : 'datai';
                 $ticket = new Ticket($row['ticket_id']);
                 $tr->addRow();
                 $tr->addCell($seq, 'center', $style);
                 $tr->addCell(utf8_decode($ticket->titulo), 'left', $style);
                 $tr->addCell($this->string->retira_segundos($row['total']), 'right', $style);
                 $tr->addCell(round($this->string->time_to_sec($row['total']) * 100 / $this->string->time_to_sec($totalPonto)) . '%', 'right', $style);
                 $seq++;
                 $colour = !$colour;
             }
             // division row
             $tr->addRow();
             $tr->addCell($break, 'center', 'datai', 4);
             $objects = Atividade::retornaAtividadesColaborador($formdata->colaborador_id, $formdata->mes_atividade, $formdata->ano_atividade, $tickets);
             // add a header row
             $tr->addRow();
             $tr->addCell('', 'center', 'header');
             $tr->addCell('Tipo Atividades', 'center', 'header');
             $tr->addCell('Horas', 'center', 'header');
             $tr->addCell('%', 'center', 'header');
             // controls the background filling
             $colour = FALSE;
             $seq = 1;
             // data rows
             foreach ($objects as $row) {
                 $style = $colour ? 'datap' : 'datai';
                 $tr->addRow();
                 $tr->addCell($seq, 'center', $style);
                 $tr->addCell(utf8_decode($row['nome']), 'left', $style);
                 $tr->addCell($this->string->retira_segundos($row['total']), 'right', $style);
                 $tr->addCell(round($this->string->time_to_sec($row['total']) / $this->string->time_to_sec($total) * 100) . '%', 'right', $style);
                 $seq++;
                 $colour = !$colour;
             }
             // footer row
             $tr->addRow();
             $tr->addCell('', 'right', 'footer');
             $tr->addCell('Total:', 'left', 'footer');
             $tr->addCell($this->string->retira_segundos($total), 'right', 'footer');
             $tr->addCell('100%', 'right', 'footer');
             // division row
             $tr->addRow();
             $tr->addCell($break, 'center', 'datai', 4);
             $objects = Atividade::retornaAtividadesSistemaColaborador($formdata->colaborador_id, $formdata->mes_atividade, $formdata->ano_atividade, $tickets);
             // add a header row
             $tr->addRow();
             $tr->addCell('', 'center', 'header');
             $tr->addCell('Por Sistema', 'center', 'header');
             $tr->addCell('Horas', 'center', 'header');
             $tr->addCell('%', 'center', 'header');
             // controls the background filling
             $colour = FALSE;
             $seq = 1;
             // data rows
             foreach ($objects as $row) {
                 $style = $colour ? 'datap' : 'datai';
                 $tr->addRow();
                 $tr->addCell($seq, 'center', $style);
                 $tr->addCell(utf8_decode($row['nome']), 'left', $style);
                 $tr->addCell($this->string->retira_segundos($row['total']), 'right', $style);
                 $tr->addCell(round($this->string->time_to_sec($row['total']) / $this->string->time_to_sec($total) * 100) . '%', 'right', $style);
                 $seq++;
                 $colour = !$colour;
             }
             // footer row
             $tr->addRow();
             $tr->addCell('', 'right', 'footer');
             $tr->addCell('Total:', 'left', 'footer');
             $tr->addCell($this->string->retira_segundos($total), 'right', 'footer');
             $tr->addCell('100%', 'right', 'footer');
             // division row
             $tr->addRow();
             $tr->addCell($break, 'center', 'datai', 4);
             $objects = Atividade::retornaAtividadesClienteColaborador($formdata->colaborador_id, $formdata->mes_atividade, $formdata->ano_atividade, $tickets);
             foreach ($objects as $row) {
                 $cliente = new Pessoa($row['solicitante_id']);
                 if ($cliente->origem == 1) {
                     $ind = $cliente->codigo_cadastro_origem;
                 } else {
                     $ind = 999;
                 }
                 $array[$ind] += $this->string->time_to_sec($row['total']);
             }
             ksort($array);
             // add a header row
             $tr->addRow();
             $tr->addCell('', 'center', 'header');
             $tr->addCell('Por Cliente', 'center', 'header');
             $tr->addCell('Horas', 'center', 'header');
             $tr->addCell('%', 'center', 'header');
             // controls the background filling
             $colour = FALSE;
             $seq = 1;
             // data rows
             foreach ($array as $key => $value) {
                 if ($key < 999) {
                     $etd = new Entidade($key);
                     $nome = $key . ' - ' . $etd->entnomfan;
                 } else {
                     $nome = $key . ' - ECS';
                 }
                 $style = $colour ? 'datap' : 'datai';
                 $tr->addRow();
                 $tr->addCell($seq, 'center', $style);
                 $tr->addCell(utf8_decode($nome), 'left', $style);
                 $tr->addCell($this->string->retira_segundos($this->string->sec_to_time($value)), 'right', $style);
                 $tr->addCell(round($value / $this->string->time_to_sec($total) * 100) . '%', 'right', $style);
                 $seq++;
                 $colour = !$colour;
             }
             // footer row
             $tr->addRow();
             $tr->addCell('', 'right', 'footer');
             $tr->addCell('Total:', 'left', 'footer');
             $tr->addCell($this->string->retira_segundos($total), 'right', 'footer');
             $tr->addCell('100%', 'right', 'footer');
             // division row
             $tr->addRow();
             $tr->addCell($break, 'center', 'datai', 4);
             // footer row
             $tr->addRow();
             $tr->addCell(date('d/m/Y H:i:s'), 'center', 'footer', 4);
             // stores the file
             $var = rand(0, 1000);
             if (!file_exists("app/output/Atividade{$var}_{$formdata->mes_atividade}_{$formdata->colaborador_id}.{$format}") or is_writable("app/output/Atividade.{$format}")) {
                 $tr->save("app/output/Atividade{$var}_{$formdata->mes_atividade}_{$formdata->colaborador_id}.{$format}");
             } else {
                 throw new Exception(_t('Permission denied') . ': ' . "app/output/Atividade_{$formdata->mes_atividade}_{$formdata->colaborador_id}.{$format}");
             }
             // open the report file
             parent::openFile("app/output/Atividade{$var}_{$formdata->mes_atividade}_{$formdata->colaborador_id}.{$format}");
             // shows the success message
             new TMessage('info', 'Relatorio gerado. Por favor, habilite popups no navegador (somente para web).');
         } else {
             new TMessage('error', 'Sem atividade no periodo cadastrado!');
         }
         // 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();
     }
 }