Conheça  A Ferramenta LowCode mais moderna e veloz para desenvolvimento PHP: Adianti Creator
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. * Class constructor
  17. * Creates the page and the registration form
  18. */
  19. function __construct()
  20. {
  21. parent::__construct();
  22. // creates the form
  23. $this->form = new BootstrapFormBuilder('form_RelatorioApolice_report');
  24. $this->form->setFormTitle( 'RELATÓRIO COMERCIAL-APÓLICE' );
  25. // create the form fields
  26. $data1 = new TDate('data1');
  27. $data2 = new TDate('data2');
  28. $row = $this->form->addFields( [new TLabel('Data Início *: ')], [$data1],[new TLabel('Data Fim: *')], [$data2] );
  29. $data1->setSize('100%');
  30. $data2->setSize('100%');
  31. $data1->addValidation('Data1 ', new TRequiredValidator);
  32. $data2->addValidation('Data2 ', new TRequiredValidator);
  33. $output_type = new TRadioGroup('output_type');
  34. $this->form->addFields( [new TLabel('Formato:')], [$output_type] );
  35. // define field properties
  36. $output_type->setUseButton();
  37. $options = ['html' =>'HTML', 'pdf' =>'PDF', 'rtf' =>'RTF', 'xls' =>'XLS'];
  38. $output_type->addItems($options);
  39. $output_type->setValue('pdf');
  40. $output_type->setLayout('horizontal');
  41. $this->form->addAction( 'Generate', new TAction(array($this, 'onGenerate')), 'fa:download blue');
  42. // wrap the page content using vertical box
  43. $vbox = new TVBox;
  44. $vbox->style = 'width: 100%';
  45. // $vbox->add(new TXMLBreadCrumb('menu.xml', __CLASS__));
  46. $vbox->add($this->form);
  47. parent::add($vbox);
  48. }
  49. /**
  50. * method onGenerate()
  51. * Executed whenever the user clicks at the generate button
  52. */
  53. function onGenerate()
  54. {
  55. try
  56. {
  57. // get the form data into an active record Customer
  58. $data = $this->form->getData();
  59. $this->form->setData($data);
  60. $format = $data->output_type;
  61. // open a transaction with database 'sgs'
  62. $source = TTransaction::open('sgs');
  63. // define the query
  64. $query = ' SELECT apolice.valor_premio as "valor_premio",
  65. apolice.data_inicio as "data_inicio",
  66. apolice.data_fim as "data_fim",
  67. apolice.tempo as "tempo",
  68. plano_seguro.designacao as "designacao",
  69. tomador.nome as "nome",
  70. apolice_segurado.referencia_apolice as "referencia_apolice"
  71. FROM apolice,
  72. plano_seguro,
  73. tomador,
  74. apolice_segurado
  75. WHERE apolice.codigo_plano = plano_seguro.codigo AND
  76. apolice.codigo_tomador = tomador.codigo AND
  77. apolice_segurado.codigo_apolice = apolice.codigo AND
  78. apolice.data_inicio BETWEEN :data1 AND :data2
  79. ';
  80. $filters = [];
  81. $filters['data1'] = $data->data1;
  82. $filters['data2'] = $data->data2;
  83. $data = TDatabase::getData($source, $query, null, $filters );
  84. if ($data)
  85. {
  86. $widths = [200,200,200,200,200,200,200];
  87. switch ($format)
  88. {
  89. case 'html':
  90. $table = new TTableWriterHTML($widths);
  91. break;
  92. case 'pdf':
  93. $table = new TTableWriterPDF($widths);
  94. break;
  95. case 'rtf':
  96. $table = new TTableWriterRTF($widths);
  97. break;
  98. case 'xls':
  99. $table = new TTableWriterXLS($widths);
  100. break;
  101. }
  102. if (!empty($table))
  103. {
  104. // create the document styles
  105. $table->addStyle('header', 'Helvetica', '16', 'B', '#ffffff', '#4B5D8E');
  106. $table->addStyle('title', 'Helvetica', '10', 'B', '#ffffff', '#617FC3');
  107. $table->addStyle('datap', 'Helvetica', '10', '', '#000000', '#E3E3E3', 'LR');
  108. $table->addStyle('datai', 'Helvetica', '10', '', '#000000', '#ffffff', 'LR');
  109. $table->addStyle('footer', 'Helvetica', '10', '', '#2B2B2B', '#B4CAFF');
  110. $table->setHeaderCallback( function($table) {
  111. $table->addRow();
  112. $table->addCell('RELATÓRIO COMERCIAL', 'center', 'header', 7);
  113. $table->addRow();
  114. $table->addCell('Tomador', 'left', 'title');
  115. $table->addCell('Apólice', 'center', 'title');
  116. $table->addCell('Plano', 'center', 'title');
  117. $table->addCell('Duração', 'center', 'title');
  118. $table->addCell('Início', 'center', 'title');
  119. $table->addCell('Fim', 'center', 'title');
  120. $table->addCell('Total', 'center', 'title');
  121. });
  122. $table->setFooterCallback( function($table) {
  123. $table->addRow();
  124. $table->addCell(date('Y-m-d h:i:s'), 'center', 'footer', 7);
  125. });
  126. // controls the background filling
  127. $colour= FALSE;
  128. // data rows
  129. foreach ($data as $row)
  130. {
  131. $style = $colour ? 'datap' : 'datai';
  132. $table->addRow();
  133. $table->addCell($row['nome'], 'left', $style);
  134. $table->addCell($row['referencia_apolice'], 'center', $style);
  135. $table->addCell($row['designacao'], 'left', $style);
  136. $table->addCell($row['tempo'], 'center', $style);
  137. $table->addCell($row['data_inicio'], 'center', $style);
  138. $table->addCell($row['data_fim'], 'center', $style);
  139. $table->addCell('Kz '.number_format($row['valor_premio'],2,',','.'), 'left', $style);
  140. $colour = !$colour;
  141. }
  142. $output = "app/output/tabular.{$format}";
  143. // stores the file
  144. if (!file_exists($output) OR is_writable($output))
  145. {
  146. $table->save($output);
  147. parent::openFile($output);
  148. }
  149. else
  150. {
  151. throw new Exception(_t('Permission denied') . ': ' . $output);
  152. }
  153. // shows the success message
  154. new TMessage('info', "Relatório gerado com sucesso.<br> <a href='$output'>Click aqui para download</a>");
  155. }
  156. }
  157. else
  158. {
  159. new TMessage('error', 'Nenhum dados encontrado');
  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. }
  171. ?>

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

 
  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'], 'esquerda', $ style);
  11. $ table -> addCell ( $ row [ 'valor' ], 'rigth' , $ estilo );
  12. $ table -> addCell ( number_format ( $ row [ 'Total' ], 2 , '' , '' ), 'rigth' , $ estilo );
  13. $ ValorTotal + = $ row [ 'Total' ];
  14. $ color =! $ color ;
  15. }
  16. $table->addRow();
  17. $ output = "app / output / tabular. { $ format } " ;
  18. ?>
AE

 
  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'], 'esquerda', $ style);
  11. $ table -> addCell ( $ row [ 'valor' ], 'rigth' , $ estilo );
  12. $ table -> addCell ( number_format ( $ row [ 'Total' ], 2 , '' , '' ), 'rigth' , $ estilo );
  13. $ ValorTotal + = $ row [ 'Total' ];
  14. $ color =! $ color ;
  15. }
  16. $table->addRow();
  17. $ output = "app / output / tabular. { $ format } " ;
  18. ?>
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 ($ ValorTotal, 2, ',', '.'), 'centro', 'rodapé', 4);
  19. $ output = "app / output / tabular.
  20. ?>