<div class="row-fluid">
    <div class="span12">
 <?php echo anchor('reportes/periodoExcel/'.$this->uri->segment(2), '<i class="icon-save"></i>Excel', array('class' => 'btn btn-success btn-app'));?>        
        <table id="ventas-table" class="table table-striped table-bordered table-hover">
            <thead>
                <tr>
                    <th>FechaSistema</th>
                    <th>NumReceta</th>
                    <th>FechaReceta</th>
                    <th>Programa</th>
                    <th>TipoDispensacion</th>
                    <th>CedulaMedico</th>
                    <th>Nombre Medico</th>
                    <th>folioSP</th>
                    <th>Nombre Paciente</th>
                    <th>ClaveSSA</th>
                    <th>DescripcionClave</th>
                    <th>Presentacion</th>                                                                                            
                    <th>CodigoEAN</th>
                    <th>ClaveLote</th>
                    <th>Caducidad</th>
                    <th>Cantidad</th>
                    <th>Precio</th>
                    <th>Sub Total</th>
                    <th>I.V.A.</th>
                    <th>Total</th>
                    <th>TipoInsumo</th>
                </tr>
            </thead>
            <tbody>
                <?php 
                
                $cantidadsurtida = 0;
                $num=0;

                $tImporte = 0;
                $tIVA = 0;
                $total = 0;
                $fecha = 0;
                $gcansur = 0;
                $gimporte = 0;
                $giva = 0;
                $gtotal = 0;
                
                foreach($query->result() as $row){

                    if ($fecha != $row->fecha_sistema && $fecha != 0) {
                        ?>
                        <tr>
                            <td colspan="15" style="text-align: right;">Total <?php echo $fecha; ?></td>
                            <td style="text-align: right;"><?php echo number_format($cantidadsurtida, 0); ?></td>
                            <td></td>
                            <td style="text-align: right;"><?php echo number_format($tImporte, 2); ?></td>
                            <td style="text-align: right;"><?php echo number_format($tIVA, 2); ?></td>
                            <td style="text-align: right;"><?php echo number_format($total, 2); ?></td>
                        </tr>
                        <?php
                        $gcansur = $gcansur + $cantidadsurtida;
                        $gimporte = $gimporte + $tImporte;
                        $giva = $giva + $tIVA;
                        $gtotal = $gtotal + $total;
                        $cantidadsurtida = 0;
                        $tImporte = 0;
                        $tIVA = 0;
                        $total = 0;
                    }
                    
                    $piezas = $row->cantidad;
                            
                    $tImporte = $tImporte + $row->subtotal;
                    $tIVA = $tIVA + $row->iva;
                    $total = $total + $row->total;
                    
                ?>
                <tr>
                    <td><?php echo $row->fecha_sistema; ?></td>
                    <td><?php echo $row->receta; ?></td>
                    <td><?php echo $row->fecha_receta; ?></td>
                    <td><?php echo $row->programa; ?></td>
                    <td><?php echo $row->tipo_dispensacion; ?></td>
                    <td></td>
                    <td></td>
                    <td></td>
                    <td><?php echo $row->beneficiario; ?></td>
                    <td><?php echo ($row->clave); ?></td>
                    <td><?php echo ($row->descripcion); ?></td>
                    <td><?php echo ($row->presentacion); ?></td>
                    <td><?php echo ($row->ean); ?></td>
                    <td><?php echo ($row->lote); ?></td>
                    <td><?php echo ($row->caducidad); ?></td>
                    
                    <td style="text-align: right;"><?php echo number_format($row->cantidad, 0); ?></td>
                    
                    <td style="text-align: right;"><?php echo number_format ($row->precio, 2); ?></td>
                    <td style="text-align: right;"><?php echo number_format ($row->subtotal, 2); ?></td>

                    <td style="text-align: right;"><?php echo number_format ($row->iva, 2); ?></td>
                    <td style="text-align: right;"><?php echo number_format ($row->total, 2); ?></td>
                    <td><?php echo ($row->tipo_insumo); ?></td>

                    
                    </tr>
                    
                    
                <?php 
                    $fecha = $row->fecha_sistema;
                    $cantidadsurtida = $cantidadsurtida + $row->cantidad;
                
                } 
                    ?>
                    <tr>
                        <td colspan="15" style="text-align: right;">Total <?php echo $fecha; ?></td>
                        <td style="text-align: right;"><?php echo number_format($cantidadsurtida, 0); ?></td>
                        <td></td>
                        <td style="text-align: right;"><?php echo number_format($tImporte, 2); ?></td>
                        <td style="text-align: right;"><?php echo number_format($tIVA, 2); ?></td>
                        <td style="text-align: right;"><?php echo number_format($total, 2); ?></td>
                    </tr>
                    <?php
                    $gcansur = $gcansur + $cantidadsurtida;
                    $gimporte = $gimporte + $tImporte;
                    $giva = $giva + $tIVA;
                    $gtotal = $gtotal + $total;
                               
                
                ?>
            </tbody>
            <tfoot>
                <tr>
                    <td colspan="15" style="text-align: right;">Totales</td>
                    
                    <td style="text-align: right;"><?php echo number_format($gcansur, 0); ?></td>
                    
                    <td>&nbsp;</td>
                    <td style="text-align: right;"><?php echo number_format ($gimporte, 2); ?></td>
                    <td style="text-align: right;"><?php echo number_format ($giva, 2); ?></td>
                    <td style="text-align: right;"><?php echo number_format ($gtotal, 2); ?></td>
                    
                </tr>
            </tfoot>
        </table>
        

    </div>
</div>
