Pesquisar Período (Datas) no Relatório Tabular Caros amigos, solicito uma ajuda para pesquisa entre datas no Relatório Tabular. Tenho uma variável $data_lancamento e gostaria de pesquisar por exemplo tudo que foi lançado entre 01/03/2020 e 31/03/2020. Abaixo meu código. Os outros filtros estão funcionando, somente o da data que ainda não funciona. ...
PC
Pesquisar Período (Datas) no Relatório Tabular  
Caros amigos, solicito uma ajuda para pesquisa entre datas no Relatório Tabular. Tenho uma variável $data_lancamento e gostaria de pesquisar por exemplo tudo que foi lançado entre 01/03/2020 e 31/03/2020. Abaixo meu código. Os outros filtros estão funcionando, somente o da data que ainda não funciona.

 
  1. <?php
  2. class RelatorioUnidadeMensal extends TPage
  3. {
  4. private $form; // form
  5. /**
  6. * Class constructor
  7. * Creates the page and the registration form
  8. */
  9. function __construct()
  10. {
  11. parent::__construct();
  12. // creates the form
  13. $this->form = new BootstrapFormBuilder('form_RelatorioUnidadeMensal_report');
  14. $this->form->setFormTitle( 'Relatório Mensal da Unidade' );
  15. $data_lancamento = new TDate('data_lancamento');
  16. $data_vencimento = new TDate('data_vencimento');
  17. $tipo_conta_id = new TDBCombo('tipo_conta_id', 'gecon', 'TipoConta', 'id', 'descricao');
  18. $unidade_id = new TDBCombo('unidade_id', 'gecon', 'Unidade', 'id', 'numero');
  19. // create the form fields
  20. $this->form->addFields( [ new TLabel('Data inicio') ], [ $data_lancamento] ,
  21. [ new TLabel('Data Fim') ], [ $data_vencimento] );
  22. $this->form->addFields( [ new TLabel('Tipo Conta') ], [ $tipo_conta_id ] ,
  23. [ new TLabel('Unidade') ], [ $unidade_id]);
  24. //set Mask
  25. $data_lancamento->setMask('dd/mm/yyyy');
  26. $data_vencimento->setMask('dd/mm/yyyy');
  27. $output_type = new TRadioGroup('output_type');
  28. $this->form->addFields( [new TLabel('Mostrar em:')], [$output_type] );
  29. // define field properties
  30. $output_type->setUseButton();
  31. $options = ['html' =>'HTML', 'pdf' =>'PDF', 'rtf' =>'RTF', 'xls' =>'XLS'];
  32. $output_type->addItems($options);
  33. $output_type->setValue('pdf');
  34. $output_type->setLayout('horizontal');
  35. $this->form->addAction( 'Gerar Relatório', new TAction(array($this, 'onGenerate')), 'fa:download blue');
  36. // wrap the page content using vertical box
  37. $vbox = new TVBox;
  38. $vbox->style = 'width: 100%';
  39. // $vbox->add(new TXMLBreadCrumb('menu.xml', __CLASS__));
  40. $vbox->add($this->form);
  41. parent::add($vbox);
  42. }
  43. /**
  44. * method onGenerate()
  45. * Executed whenever the user clicks at the generate button
  46. */
  47. function onGenerate()
  48. {
  49. try
  50. {
  51. // get the form data into an active record Customer
  52. $data = $this->form->getData();
  53. $this->form->setData($data);
  54. $format = $data->output_type;
  55. // open a transaction with database ''
  56. $source = TTransaction::open('gecon');
  57. // define the query
  58. $query = 'SELECT unidade.numero, tipo_conta.descricao, unidade.areaUtil, condominio_lancamento.valor, (unidade.areaUtil * condominio_lancamento.valor) AS Total
  59. FROM unidade, tipo_conta, condominio_lancamento
  60. WHERE condominio_lancamento.valor > 0
  61. AND tipo_conta.id = condominio_lancamento.tipo_conta_id';
  62. if ( !empty($data->tipo_conta_id) )
  63. {
  64. $query .= " and condominio_lancamento.tipo_conta_id = {$data->tipo_conta_id}";
  65. }
  66. if ( !empty($data->unidade_id) )
  67. {
  68. $query .= " and unidade.id = {$data->unidade_id}";
  69. }
  70. if ( !empty($data->condominio_lancamento_id) )
  71. {
  72. $query .= "and data_lancamento = {$data->condominio_lancamento_id}";
  73. }
  74. $filters = [];
  75. $data = TDatabase::getData($source, $query, null, $filters );
  76. if ($data)
  77. {
  78. $widths = [200,200,200,200,200];
  79. switch ($format)
  80. {
  81. case 'html':
  82. $table = new TTableWriterHTML($widths);
  83. break;
  84. case 'pdf':
  85. $table = new TTableWriterPDF($widths);
  86. break;
  87. case 'rtf':
  88. $table = new TTableWriterRTF($widths);
  89. break;
  90. case 'xls':
  91. $table = new TTableWriterXLS($widths);
  92. break;
  93. }
  94. if (!empty($table))
  95. {
  96. // create the document styles
  97. $table->addStyle('header', 'Helvetica', '16', 'B', '#ffffff', '#4B8E57');
  98. $table->addStyle('title', 'Helvetica', '10', 'B', '#ffffff', '#6CC361');
  99. $table->addStyle('datap', 'Helvetica', '10', '', '#000000', '#E3E3E3', 'LR');
  100. $table->addStyle('datai', 'Helvetica', '10', '', '#000000', '#ffffff', 'LR');
  101. $table->addStyle('footer', 'Helvetica', '10', '', '#2B2B2B', '#B5FFB4');
  102. $table->setHeaderCallback( function($table) {
  103. $table->addRow();
  104. $table->addCell('Relatorio Condomínio Lançamento', 'center', 'header', 4);
  105. $table->addRow();
  106. $table->addCell('Unidade', 'center', 'title');
  107. $table->addCell('Tipo Conta', 'center', 'title');
  108. $table->addCell('Valor', 'center', 'title');
  109. $table->addCell('Total', 'center', 'title');
  110. });
  111. $table->setFooterCallback( function($table) {
  112. $table->addRow();
  113. $table->addCell(date('d/m/Y h:i:s'), 'center', 'footer', 4);
  114. });
  115. // controls the background filling
  116. $colour= FALSE;
  117. $ValorTotal = 0;
  118. // data rows
  119. foreach ($data as $row)
  120. {
  121. $style = $colour ? 'datap' : 'datai';
  122. $table->addRow();
  123. $table->addCell($row['numero'], 'left', $style);
  124. $table->addCell($row['descricao'], 'left', $style);
  125. $table->addCell($row['valor'], 'rigth', $style);
  126. $table->addCell(number_format($row['Total'],2,',','.'), 'rigth', $style);
  127. $ValorTotal += $row['Total'];
  128. $colour = !$colour;
  129. }
  130. $table->addRow();
  131. $table->addCell('Valor Total: ', 'left', 'footer', 1);
  132. $table->addCell(number_format($ValorTotal,2,',','.'), 'rigth', 'footer', 3);
  133. $output = "app/output/tabular.{$format}";
  134. // stores the file
  135. if (!file_exists($output) OR is_writable($output))
  136. {
  137. $table->save($output);
  138. parent::openFile($output);
  139. }
  140. else
  141. {
  142. throw new Exception(_t('Permission denied') . ': ' . $output);
  143. }
  144. // shows the success message
  145. new TMessage('info', 'Relatório gerado. Por favor, ative popups no navegador.');
  146. }
  147. }
  148. else
  149. {
  150. new TMessage('error', 'Registros não encontrado');
  151. }
  152. // close the transaction
  153. TTransaction::close();
  154. }
  155. catch (Exception $e) // in case of exception
  156. {
  157. new TMessage('error', $e->getMessage());
  158. TTransaction::rollback();
  159. }
  160. }
  161. }

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 (2)


LC

Tem que ajustar a data no formato do banco de dados, exemplo para MySql:
 
  1. <?php
  2. if ( !empty($data->data_lancamento ) )
  3. {
  4. $query .= "and data_lancamento = '" . DateTime::createFromFormat('d/m/Y', $data->data_lancamento )->format( 'Y-m-d' ) ."' ";
  5. }
  6. ?>

PC

Valeu Leandro Coelho, somente alterei a linha colocando >= para pegar exemplo: 01/02/2020 até 29/02/2020.

 
  1. <?php
  2. if ( !empty($data->data_lancamento ) )
  3. {
  4. $query .= " and data_lancamento >= '" . DateTime::createFromFormat('d/m/Y', $data->data_lancamento )->format( 'Y-m-d' ) ."' ";
  5. }
  6. ?>