Totalizar por grupo de Centro de Custo
Galera,
Estou precisando de uma ajuda para poder agrupar e totalizar os resultados por centro de custo. No PHP, consigo agrupar por centro de custo, mas não estou conseguindo TOTALIZAR por cada centro de custo. Confira nesta imagem como está http://prntscr.com/41hpxn
Veja que a palavra Total: aparece abaixo do PRIMEIRO REGISTRO, mas deveria exibir abaixo do ÚLTIMO registro.
Obs.: já fiz o ORDER BY CENTROCUSTO para poder agrupar.
Segue o código da página:
<table class="no-border">
<?php
if (count($ana) > 0) {
?>
<thead>
<tr class="small no-border">
<th width="5%" class="small">Reserva</th>
<th width="25%" class="small">Emissao</th>
<th width="15%" class="small">Passageiro</th>
<th width="10%" class="small">Cia</th>
<th width="10%" class="small">Trecho</th>
<th width="10%" class="small">Bilhete</th>
<th class="small">Produto</th>
<th class="small">Melhor Tarifa</th>
<th class="small">Tarifa Aplicada</th>
<th class="small">Taxas</th>
<th class="small">Extras</th>
<th class="small">Total</th>
</tr>
</thead>
<tbody class="no-border-x no-border-y">
<?php
$TotDesconto = 0;
$TotTaxa = 0;
$TotTotal = 0;
$TotTotDesconto = 0;
$TotTotTaxa = 0;
$TotTotTotal = 0;
$prev = '';
$tot = '';
foreach ($ana as $linha) {
?>
<?php
if( $prev!==trim($linha['CENTROCUSTO']) ) {
$prev = trim($linha['CENTROCUSTO']);
?>
<tr class="small">
<td colspan="12" class="small"><strong>Centro de Custo: <?php echo trim($linha['CENTROCUSTO']); ?></strong></td>
</tr>
<?php }?>
<tr class="small">
<td class="small"><?php echo trim($linha['RESERVA']); ?></td>
<td class="small"><?php echo trim($linha['EMISSAO']); ?></td>
<td class="small"><?php echo trim($linha['PASSAGEIRO']); ?></td>
<td class="small"><?php echo trim($linha['CIA']); ?></td>
<td class="small"><?php echo trim($linha['TRECHO']); ?></td>
<td class="small"><?php echo trim($linha['BILHETE']); ?></td>
<td class="small"><?php echo trim($linha['PRODUTO']); ?></td>
<td class="small"><?php echo toReal(trim($linha['MELHORVALOR'])); ?></td>
<td class="small"><a href="#" title="Tairfa: <?php echo toReal(trim($linha['TARIFAAPLICADA'])); ?> - Desconto: <?php echo toReal(trim($linha['DESCONTOCONTRATUAL'])); ?>" data-toggle="tooltip"><?php echo toReal(trim($linha['TARIFAAPLICADA']) - trim($linha['DESCONTOCONTRATUAL'])); ?></a></td>
<td class="small"><?php echo toReal(trim($linha['TAXA'])); ?></td>
<td class="small"><?php echo toReal(trim($linha['EXTRAS'])); ?></td>
<td class="small"><?php echo toReal(trim($linha['TOTAL'])); ?></td>
</tr>
<?php
if( $tot!==trim($linha['CENTROCUSTO']) ) {
$tot = trim($linha['CENTROCUSTO']);
?>
<tr class="small">
<td colspan="12" class="small"><strong>Total: <?php //echo trim($linha['CENTROCUSTO']); ?></strong></td>
</tr>
<?php }?>
<?php
}
?>
<tr class="small">
<td colspan="12" align="left" class="small"><strong>Total de Registros:</strong> <?php echo count($ana);?></td>
</tr>
<?php
}else {
?>
<tr class="small alert alert-warning">
<td colspan="12" align="center" class="small"><strong>Não há pax viajando neste Período e/ou Aeroporto</strong></td>
</tr>
</tbody>
<?php
}
$analitico->closeCursor();
$ana = null;
$con_stur = null;
?>
</table>
Obrigado!Discussão (8)
Carregando comentários...