Lançado Adianti Framework 7.6!
Clique aqui para saber mais
Calcular o total das colunas do relatório tabular Bom dia pessoal, preciso de ajuda, como calcular o total das colunas em relatório tabular conforme código abaixo em anexo a imagem... ...
AE
Calcular o total das colunas do relatório tabular  
Bom dia pessoal, preciso de ajuda, como calcular o total das colunas em relatório tabular conforme código abaixo em anexo a imagem...

  1. <?php
  1. <?php
  2. /**
  3.  * Tabular Query Report
  4.  *
  5.  * @version    1.0
  6.  * @package    samples
  7.  * @subpackage tutor
  8.  * @author     Pablo Dall'Oglio
  9.  * @copyright  Copyright (c) 2006 Adianti Solutions Ltd. (http://www.adianti.com.br)
  10.  * @license    http://www.adianti.com.br/framework-license
  11.  */
  12. class RelatorioApolice extends TPage
  13. {
  14.     private $form// form
  15.     
  16.     /**
  17.      * Class constructor
  18.      * Creates the page and the registration form
  19.      */
  20.     function __construct()
  21.     {
  22.         parent::__construct();
  23.         
  24.         // creates the form
  25.         $this->form = new BootstrapFormBuilder('form_RelatorioApolice_report');
  26.         $this->form->setFormTitle'RELATÓRIO COMERCIAL-APÓLICE' );
  27.         
  28.         // create the form fields
  29.         
  30.         $data1      = new TDate('data1');
  31.         $data2      = new TDate('data2');
  32.         
  33.         $row $this->form->addFields( [new TLabel('Data Início *: ')],     [$data1],[new TLabel('Data Fim: *')],     [$data2] );
  34.         $data1->setSize('100%');
  35.         $data2->setSize('100%');
  36.         
  37.         
  38.         $data1->addValidation('Data1 ', new TRequiredValidator);
  39.         $data2->addValidation('Data2 ', new TRequiredValidator);
  40.         
  41.         $output_type  = new TRadioGroup('output_type');
  42.         $this->form->addFields( [new TLabel('Formato:')],   [$output_type] );
  43.         
  44.         // define field properties
  45.         $output_type->setUseButton();
  46.         $options = ['html' =>'HTML''pdf' =>'PDF''rtf' =>'RTF''xls' =>'XLS'];
  47.         $output_type->addItems($options);
  48.         $output_type->setValue('pdf');
  49.         $output_type->setLayout('horizontal');
  50.         
  51.         $this->form->addAction'Generate', new TAction(array($this'onGenerate')), 'fa:download blue');
  52.         
  53.         // wrap the page content using vertical box
  54.         $vbox = new TVBox;
  55.         $vbox->style 'width: 100%';
  56.         // $vbox->add(new TXMLBreadCrumb('menu.xml', __CLASS__));
  57.         $vbox->add($this->form);
  58.         
  59.         parent::add($vbox);
  60.     }
  61.     /**
  62.      * method onGenerate()
  63.      * Executed whenever the user clicks at the generate button
  64.      */
  65.     function onGenerate()
  66.     {
  67.         try
  68.         {
  69.             // get the form data into an active record Customer
  70.             $data $this->form->getData();
  71.             $this->form->setData($data);
  72.             
  73.             $format $data->output_type;
  74.             
  75.             // open a transaction with database 'sgs'
  76.             $source TTransaction::open('sgs');
  77.             
  78.             // define the query
  79.             $query '   SELECT apolice.valor_premio as "valor_premio",
  80.           apolice.data_inicio as "data_inicio",
  81.           apolice.data_fim as "data_fim",
  82.           apolice.tempo as "tempo",
  83.           plano_seguro.designacao as "designacao",
  84.           tomador.nome as "nome",
  85.           apolice_segurado.referencia_apolice as "referencia_apolice"
  86.      FROM apolice,
  87.           plano_seguro,
  88.           tomador,
  89.           apolice_segurado
  90.     WHERE apolice.codigo_plano = plano_seguro.codigo AND 
  91.           apolice.codigo_tomador = tomador.codigo AND 
  92.           apolice_segurado.codigo_apolice = apolice.codigo AND
  93.           apolice.data_inicio BETWEEN :data1 AND :data2
  94. ';
  95.             
  96.             $filters = [];
  97.             $filters['data1'] = $data->data1;
  98.             $filters['data2'] = $data->data2;
  99.             
  100.             $data TDatabase::getData($source$querynull$filters );
  101.             
  102.             if ($data)
  103.             {
  104.                 $widths = [200,200,200,200,200,200,200];
  105.                 
  106.                 switch ($format)
  107.                 {
  108.                     case 'html':
  109.                         $table = new TTableWriterHTML($widths);
  110.                         break;
  111.                     case 'pdf':
  112.                         $table = new TTableWriterPDF($widths);
  113.                         break;
  114.                     case 'rtf':
  115.                         $table = new TTableWriterRTF($widths);
  116.                         break;
  117.                     case 'xls':
  118.                         $table = new TTableWriterXLS($widths);
  119.                         break;
  120.                 }
  121.                 
  122.                 if (!empty($table))
  123.                 {
  124.                     // create the document styles
  125.                     $table->addStyle('header''Helvetica''16''B''#ffffff''#4B5D8E');
  126.                     $table->addStyle('title',  'Helvetica''10''B''#ffffff''#617FC3');
  127.                     $table->addStyle('datap',  'Helvetica''10''',  '#000000''#E3E3E3''LR');
  128.                     $table->addStyle('datai',  'Helvetica''10''',  '#000000''#ffffff''LR');
  129.                     $table->addStyle('footer''Helvetica''10''',  '#2B2B2B''#B4CAFF');
  130.                     
  131.                     $table->setHeaderCallback( function($table) {
  132.                         $table->addRow();
  133.                         $table->addCell('RELATÓRIO COMERCIAL''center''header'7);
  134.                         
  135.                         $table->addRow();
  136.                         $table->addCell('Tomador''left''title');
  137.                         $table->addCell('Apólice''center''title');
  138.                         $table->addCell('Plano''center''title');
  139.                         $table->addCell('Duração''center''title');
  140.                         $table->addCell('Início''center''title');
  141.                         $table->addCell('Fim''center''title');
  142.                         $table->addCell('Total''center''title');
  143.                         
  144.                        
  145.                         
  146.                     });
  147.                     
  148.                     $table->setFooterCallback( function($table) {
  149.                         $table->addRow();
  150.                         $table->addCell(date('Y-m-d h:i:s'), 'center''footer'7);
  151.                     });
  152.                     
  153.                     // controls the background filling
  154.                     $colourFALSE;
  155.                     
  156.                     // data rows
  157.                     foreach ($data as $row)
  158.                     {
  159.                         $style $colour 'datap' 'datai';
  160.                         
  161.                         $table->addRow();
  162.                         $table->addCell($row['nome'], 'left'$style);
  163.                         $table->addCell($row['referencia_apolice'], 'center'$style);
  164.                         $table->addCell($row['designacao'], 'left'$style);
  165.                         $table->addCell($row['tempo'], 'center'$style);
  166.                         $table->addCell($row['data_inicio'], 'center'$style);
  167.                         $table->addCell($row['data_fim'], 'center'$style);
  168.                         $table->addCell('Kz '.number_format($row['valor_premio'],2,',','.'), 'left'$style);
  169.                   
  170.                         
  171.                         $colour = !$colour;
  172.                     }
  173.                     
  174.                     $output "app/output/tabular.{$format}";
  175.                     
  176.                     // stores the file
  177.                     if (!file_exists($output) OR is_writable($output))
  178.                     {
  179.                         $table->save($output);
  180.                         parent::openFile($output);
  181.                     }
  182.                     else
  183.                     {
  184.                         throw new Exception(_t('Permission denied') . ': ' $output);
  185.                     }
  186.                     
  187.                     // shows the success message
  188.                     new TMessage('info'"Relatório gerado com sucesso.<br> <a href='$output'>Click aqui para download</a>");
  189.                 }
  190.             }
  191.             else
  192.             {
  193.                 new TMessage('error''Nenhum dados encontrado');
  194.             }
  195.     
  196.             // close the transaction
  197.             TTransaction::close();
  198.         }
  199.         catch (Exception $e// in case of exception
  200.         {
  201.             new TMessage('error'$e->getMessage());
  202.             TTransaction::rollback();
  203.         }
  204.     }
  205. }
  206. ?>

Pacotão Dominando o Adianti Framework 7
O material mais completo de treinamento do Framework.
Curso em vídeo aulas + Livro completo + Códigos fontes do projeto ERPHouse.
Conteúdo Atualizado! Versão 7.4


Dominando o Adianti 7 Quero me inscrever agora!

Comentários (7)


HL

Bom dia!

Você pode fazer seguindo o exemplo abaixo.

  1. <?php
  2. $ValorTotal 0;
  3. foreach ($data as $row)
  4.                     {
  5.                         $style $colour 'datap' 'datai';
  6.                         
  7.                         $table->addRow();
  8.                         $table->addCell($row['nome'], 'left'$style);
  9.                         $table->addCell($row['referencia_apolice'], 'center'$style);
  10.                         $table->addCell($row['designacao'], 'left'$style);
  11.                         $table->addCell($row['tempo'], 'center'$style);
  12.                         $table->addCell($row['data_inicio'], 'center'$style);
  13.                         $table->addCell($row['data_fim'], 'center'$style);
  14.                         $table->addCell('Kz '.number_format($row['valor_premio'],2,',','.'), 'left'$style);
  15.                        $ValorTotal $ValorTotal $row['valor_premio'];
  16.                         
  17.                         $colour = !$colour;
  18.                     }
  19. echo $ValorTotal;
  20. ?>
AE

Valeu Hellton Lacerda, deu certo...
PC

No meu relatório Tabular, minha soma esta fazendo corretamente, mas não consigo coloca-la no PDF. E ao selecionar uma unidade não aparece somente a selecionada, esta aparecendo todas.

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

  1. <?php
  2.  ValorTotal  =  ;
  3.                     // linhas de dados
  4.                     foreach ( $ dados  como  linha )
  5.                     {
  6.                         $ style  =  $ color  'datap'  :  'datai' ;
  7.                         
  8.                         $ tabela -> addRow ();
  9.                         $ table -> addCell ( $ linha 'numero' ],  'esquerda' ,  $ estilo );
  10.                         $ table -> addCell ( $ row 'descricao' ],  'esquerda' ,  $ estilo );
  11.                         // $ table-> addCell ($ row ['areaUtil'], 'esquerda', $ style);
  12.                         table -> addCell ( $ row 'valor' ],  'rigth' ,  $ estilo );
  13.                         $ table -> addCell number_format ( $ row 'Total' ], '' '' ),  'rigth' ,  $ estilo );
  14.                         
  15.                         $ ValorTotal  + =  $ row 'Total' ];
  16.                         
  17.                         $ color  =! $ color ;
  18.                     }
  19.                     
  20.                     $table->addRow();
  21.                     $ output  =  "app / output / tabular. { $ format } " ;
  22. ?>
AE

  1. <?php
  2.  ValorTotal  =  ;
  3.                     // linhas de dados
  4.                     foreach ( $ dados  como  linha )
  5.                     {
  6.                         $ style  =  $ color  'datap'  :  'datai' ;
  7.                         
  8.                         $ tabela -> addRow ();
  9.                         $ table -> addCell ( $ linha 'numero' ],  'esquerda' ,  $ estilo );
  10.                         $ table -> addCell ( $ row 'descricao' ],  'esquerda' ,  $ estilo );
  11.                         // $ table-> addCell ($ row ['areaUtil'], 'esquerda', $ style);
  12.                         table -> addCell ( $ row 'valor' ],  'rigth' ,  $ estilo );
  13.                         $ table -> addCell number_format ( $ row 'Total' ], '' '' ),  'rigth' ,  $ estilo );
  14.                         
  15.                         $ ValorTotal  + =  $ row 'Total' ];
  16.                         
  17.                         $ color  =! $ color ;
  18.                     }
  19.                     
  20.                     $table->addRow();
  21.                     $ output  =  "app / output / tabular. { $ format } " ;
  22. ?>
AE

Segue o código abaixo:

<? php
$ ValorTotal = 0 ;
// linhas de dados
foreach ($ dados como $ linha )
{
$ style = $ color ? 'datap' : 'datai' ;

$ tabela -> addRow ();
$ table -> addCell ($ linha [ 'numero' ], 'esquerda' , $ estilo );
$ table -> addCell ($ row [ 'descricao' ], 'esquerda' , $ estilo );
// $ table-> addCell ($ row ['areaUtil'], 'esquerda', $ style);
$ table -> addCell ($ row [ 'valor' ], 'rigth' , $ estilo );
$ Mesa -> addCell ( number_format ($ row [ 'Total' ], 2 , '' , '' ), 'rigth' , $ estilo );

$ ValorTotal + = $ row [ 'Total' ];

$ color =! $ color ;
}

//Acrescenta estas duas linhas
$ tabela -> addRow ();
$table->addCell('TOTAL: Kz ' .number_format($ValorTotal,2,',','.'),'center', 'footer',4);
$ output = "app / output / tabular. {$ format}" ;
?>
AE

Alexandre Epalanga : ( 2020-03-02)
Segue o código abaixo:

  1. <?php
  2. ValorTotal 0;
  3. // linhas de dados
  4. foreach ($ dados como linha)
  5. {
  6. style = $ color'datap''datai';
  7. tabela -> addRow ();
  8. table -> addCell ($ linha ['numero'], 'esquerda', $ estilo);
  9. table -> addCell ($ row ['descricao'], 'esquerda', $ estilo);
  10. // $ table-> addCell ($ row ['areaUtil'], '
  11. table -> addCell ($ row ['valor'], 'rigth', $ estilo);
  12. Mesa -> addCell (número_format ($ linha ['Total']], 2''''), 'rigth', $ estilo);
  13. ValorTotal + = $ row ['Total'];
  14. color =! $ color;
  15. }
  16. // Adiciona estas duas linhas
  17. tabela -> addRow ();
  18. table-> addCell ('TOTAL: Kz'número_format ($ ValorTotal2',''.'), 'centro''rodapé'4);
  19. output "app / output / tabular.
  20. ?>