Melhorar código Pessoal eu gerei um relatório com mestre detalhe ficou muito bom e tá funcionando perfeito, porém eu gostaria que os amigos dessem uma olhada no meu código e visse se tem algo que eu possa melhorar no código e depois de pronto eu vou colocar como um artigo para ajudar outros amigos, em anexo como ficou o relatório, obrigado a todos que me ajudaram. ...
MO
Melhorar código  
Pessoal eu gerei um relatório com mestre detalhe ficou muito bom e tá funcionando perfeito, porém eu gostaria que os amigos dessem uma olhada no meu código e visse se tem algo que eu possa melhorar no código e depois de pronto eu vou colocar como um artigo para ajudar outros amigos, em anexo como ficou o relatório, obrigado a todos que me ajudaram.

 
  1. <?php
  2. class BensQueryReport extends TPage
  3. {
  4. private $form;
  5. public function __construct()
  6. {
  7. parent::__construct();
  8. $this->form = new BootstrapFormBuilder;
  9. $this->form->setFormTitle('Clientes');
  10. $combo_destino = new TDBUniqueSearch('destino_id', 'patrimonio', 'Destino', 'id', 'nome');
  11. $output = new TRadioGroup('output');
  12. $this->form->addFields( [new TLabel('Cidade')], [$combo_destino] );
  13. $this->form->addFields( [new TLabel('Formato')], [$output] );
  14. $output->setUseButton();
  15. $combo_destino->setMinLength(1);
  16. $output->addItems( ['html' => 'HTML', 'pdf' => 'PDF', 'rtf' => 'RTF', 'xls' => 'XLS'] );
  17. $output->setValue( 'pdf' );
  18. $output->setLayout('horizontal');
  19. $this->form->addAction('Gerar', new TAction([$this, 'onGenerate']), 'fa:download blue');
  20. $this->form->addActionLink('Voltar', new TAction(array('BensList','onReload')),'fa:table blue');
  21. parent::add( $this->form );
  22. }
  23. public function onGenerate($param)
  24. {
  25. try
  26. {
  27. $conn = TTransaction::open('patrimonio');
  28. $data = $this->form->getData();
  29. if (!$data->destino_id){
  30. $sql = "SELECT m.destino_id,d.nome, m.bens_id,b.num_tombamento, b.descricao, e.estadoprod, m.ativo FROM movimentacao m
  31. JOIN bens AS b ON m.bens_id = b.id
  32. JOIN destino AS d ON m.destino_id = d.id
  33. JOIN estadoprod as e ON b.estadoprod_id = e.id
  34. WHERE m.ativo = 'S' ORDER BY m.destino_id;";
  35. //echo'Entrou no primeiro....';
  36. $rows = TDatabase::getData( $conn, $sql, null );
  37. }else{
  38. $sql = "SELECT m.destino_id,d.nome, m.bens_id,b.num_tombamento, b.descricao, e.estadoprod, m.ativo FROM movimentacao m
  39. JOIN bens AS b ON m.bens_id = b.id
  40. JOIN destino AS d ON m.destino_id = d.id
  41. JOIN estadoprod as e ON b.estadoprod_id = e.id
  42. WHERE m.ativo = 'S' and m.destino_id = :destino_id ORDER BY m.destino_id;";
  43. //echo'Entrou no segundo....';
  44. $rows = TDatabase::getData( $conn, $sql, null, [ 'destino_id' => $data->destino_id ] );
  45. }
  46. if ($rows)
  47. {
  48. //$widths = [40, 200, 80, 120, 80];
  49. $widths = [800, null, null,null, null];
  50. switch ($data->output)
  51. {
  52. case 'html':
  53. $table = new TTableWriterHTML($widths);
  54. break;
  55. case 'pdf':
  56. $table = new TTableWriterPDF($widths);
  57. break;
  58. case 'rtf':
  59. $table = new TTableWriterRTF($widths);
  60. break;
  61. case 'xls':
  62. $table = new TTableWriterXLS($widths);
  63. break;
  64. }
  65. // id, nome, categoria, email, nascimento
  66. if (!empty($table))
  67. {
  68. $table->addStyle('header', 'Helvetica', '16', 'B', '#ffffff', '#4B5D8E');
  69. $table->addStyle('title', 'Helvetica', '10', 'B', '#ffffff', '#617FC3');
  70. $table->addStyle('datap', 'Helvetica', '10', 'B', '#000000', '#E3E3E3', 'LR');
  71. $table->addStyle('datai', 'Helvetica', '10', '', '#000000', '#ffffff', 'LR');
  72. $table->addStyle('footer', 'Helvetica', '10', '', '#2B2B2B', '#B4CAFF');
  73. }
  74. $table->setHeaderCallback( function($table) {
  75. $table->addRow();
  76. $table->addCell('Listagem de Bens Patrimoniais por Setor', 'center', 'header', 5);
  77. $table->addRow();
  78. $table->addCell('Setor', 'left', 'title');
  79. $table->addRow();
  80. $table->addCell('Bem Patrimonial', 'left', 'title');
  81. });
  82. $table->setFooterCallback( function ($table) {
  83. $table->addRow();
  84. $table->addCell(date('d/m/Y H:i:s'), 'right', 'footer', 5);
  85. });
  86. $colore = true;
  87. $previous_dest = null;
  88. foreach ($rows as $row)
  89. {
  90. //$style = $colore ? 'datap' : 'datai';
  91. if ($row['destino_id'] != $previous_dest) { // checa se destino se é diferente do anterior
  92. $style = 'datap';
  93. $table->addRow();
  94. $table->addCell( $row['destino_id'].' - '.$row['nome'], 'left', $style);
  95. $style = 'datai';
  96. }
  97. $table->addRow();
  98. $table->addCell( ' '.$row['num_tombamento'].' - '.$row['descricao'].' ('.$row['estadoprod'].')', 'left', $style);
  99. $previous_dest = $row['destino_id'];
  100. $colore = !$colore;
  101. }
  102. $output = 'app/output/tabular.'.$data->output;
  103. if (!file_exists($output) OR is_writable($output))
  104. {
  105. $table->save($output);
  106. parent::openFile($output);
  107. new TMessage('info', 'Relatório gerado com sucesso');
  108. }
  109. else
  110. {
  111. throw new Exception('Permissão negada: ' . $output);
  112. }
  113. return;
  114. }
  115. $this->form->setData($data);
  116. TTransaction::close();
  117. }
  118. catch (Exception $e)
  119. {
  120. new TMessage('error', $e->getMessage());
  121. }
  122. }
  123. function onshow()
  124. {
  125. parent::show();
  126. }
  127. }

Curso Dominando o Adianti Framework

O material mais completo de treinamento do Framework.
Curso em vídeo aulas + Livro completo + Códigos fontes do projeto ERPHouse.
Conteúdo Atualizado!


Dominando o Adianti Framework Quero me inscrever agora!

Comentários (0)