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

Curso completo Meu Negócio Pronto
Use para si, ou transforme em um negócio: Inclui aulas e códigos-fontes
Gestor de conteúdo (SITE) + Loja Virtual (E-Commerce) + Emissor de Notas para infoprodutos


Meu negócio pronto 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. ?>