<div class="row-fluid">
<div class="span12">
    <form action="Paciente_p_Excel" method="POST">        
       
       <input type="hidden" name="fecha1" value="<?php echo $fecha1; ?>" /> 
       <input type="hidden" name="fecha2" value="<?php echo $fecha2; ?>" /> 
       <input type="hidden" name="expediente" value="0" />

       <button type="submit" class="btn btn-success btn-app"><i class="icon-save"></i>Excel</button>       
     
    </form>     

<h3>Medicamentos recetados por grupo familiar</h3>

    <table id="ventas-table" class="table table-striped table-bordered table-hover">
        <thead>
            <tr>
                <th>#</th>
                <th>CLAVE PACIENTE</th>
                <th>NOMBRE</th>
                <th>FECHA</th>
                <th>FOLIO</th>
                <th>PROGRAMA</th>
                <th>CLAVE MEDICO</th>
                <th>MEDICO</th>
                <th>CLAVE</th>
                <th>DESCRIPCION</th>
                <th>REQUERIDAS</th>                
             </tr>
        </thead>
        <tbody>
            <?php 
            
            $num = 0;
            $req = 0;
            $sub_tot = 0;
            
            foreach($query->result() as $row){
                $num++;
                if ($row === reset($query->result())) {
                    $mystring = $row->cvepaciente;
                    $findme   = '-';
                    $pos = strpos($mystring, $findme);
                    if ($pos === false)
                    {
                        $clave1 = $row->cvepaciente;
                    }else{
                        $clave1 = substr($row->cvepaciente, 0, ($pos));
                    }                                         
                    $totaliza = 0;
                }else{
                    $mystring = $row->cvepaciente;
                    $findme   = '-';
                    $pos = strpos($mystring, $findme);
                    if ($pos === false)
                    {
                        $clave = $row->cvepaciente;
                    }else{
                        $clave = substr($row->cvepaciente, 0, ($pos));
                    } 
                    if ($clave == $clave1) {                        
                        $totaliza = 0;
                    }else{
                        $clave1 = $clave; 
                        $totaliza = 1;
                    }

                }                
                if ($totaliza == 0)
                {
                    $sub_tot = $sub_tot + $row->req;
                    ?>                              
                    <tr>
                        <td><?php echo $num; ?></td>
                        <td><?php echo $row->cvepaciente; ?></td>
                        <td><?php echo $row->nombre; ?></td>
                        <td><?php echo $row->fecha; ?></td>
                        <td><?php echo ($row->recetaID); ?></td>
                        <td><?php echo ($row->programa); ?></td>
                        <td><?php echo ($row->cvemedico); ?></td>
                        <td><?php echo ($row->nombremedico); ?></td>
                        <td><?php echo ($row->cvearticulo); ?></td>
                        <td><?php echo ($row->completo); ?></td>
                        <td style="text-align: right;"><?php echo number_format($row->req, 0); ?></td>
                    </tr>                                
                    <?php                    
                }elseif($totaliza == 1) 
                {
                    ?>

                    <tr>
                        <td style="text-align: right;" colspan="9">Total piezas titular y dependientes:</td>
                        <td></td>
                        <td style="text-align: right;" id="req"><?php echo number_format ($sub_tot, 0); ?></td>
                    </tr>
                    <tr>
                        <td><?php echo $num; ?></td>
                        <td><?php echo $row->cvepaciente; ?></td>
                        <td><?php echo $row->nombre; ?></td>
                        <td><?php echo $row->fecha; ?></td>
                        <td><?php echo ($row->recetaID); ?></td>
                        <td><?php echo ($row->programa); ?></td>
                        <td><?php echo ($row->cvemedico); ?></td>
                        <td><?php echo ($row->nombremedico); ?></td>
                        <td><?php echo ($row->cvearticulo); ?></td>
                        <td><?php echo ($row->completo); ?></td>
                        <td style="text-align: right;"><?php echo number_format($row->req, 0); ?></td>
                    </tr>                     
                    <?php
                    $sub_tot = $row->req;                    
                }
                                                                                       
                $req = $req + $row->req;
            
            }
            
            ?>
        </tbody>
        <tfoot>
            <tr>
                <td style="text-align: right;" colspan="9">Total piezas titular y dependientes:</td>
                <td></td>
                <td style="text-align: right;" id="req"><?php echo number_format ($sub_tot, 0); ?></td>
            </tr>
            <tr>
                <td style="text-align: right;" colspan="9">Total piezas recetadas del periodo</td>
                <td></td>
                <td style="text-align: right;" id="req"><?php echo number_format ($req, 0); ?></td>

            </tr>
            <tr>
                <th>#</th>
                <th>CLAVE PACIENTE</th>
                <th>NOMBRE</th>
                <th>FECHA</th>
                <th>FOLIO</th>
                <th>PROGRAMA</th>
                <th>CLAVE MEDICO</th>
                <th>MEDICO</th>
                <th>CLAVE</th>
                <th>DESCRIPCION</th>
                <th>REQUERIDAS</th> 

             </tr>
        </tfoot>
    </table>


    <h3>Cantidad de visitas al médico por grupo familiar</h3>

    <table id="ventas-table2" class="table table-striped table-bordered table-hover">
        <thead>
            <tr>
                <th>#</th>
                <th>CLAVE PACIENTE</th>
                <th>NOMBRE</th>
                <th>FECHA</th>
                <th>FOLIO</th>                
                <th>CLAVE MEDICO</th>
                <th>MEDICO</th>
                <th>DEPENDIENTE</th>
                <th>DIAGNOSTICO PRIMARIO CIE103</th>
                <th>DIAGNOSTICO SECUNDARIO CIE104</th>
             </tr>
        </thead>
        <tbody>
            <?php 
            
            $num = 0;
            $req = 0;
            $sub_tot = 0;
            
            foreach($query2->result() as $row){                
                $num++;  
                if ($row === reset($query2->result())) {
                    $mystring = $row->cvepaciente;
                    $findme   = '-';
                    $pos = strpos($mystring, $findme);
                    if ($pos === false)
                    {
                        $clave1 = $row->cvepaciente;
                    }else{
                        $clave1 = substr($row->cvepaciente, 0, ($pos));
                    }                                         
                    $totaliza = 0;
                }else{
                    $mystring = $row->cvepaciente;
                    $findme   = '-';
                    $pos = strpos($mystring, $findme);
                    if ($pos === false)
                    {
                        $clave = $row->cvepaciente;
                    }else{
                        $clave = substr($row->cvepaciente, 0, ($pos));
                    } 
                    if ($clave == $clave1) {                        
                        $totaliza = 0;
                    }else{
                        $clave1 = $clave; 
                        $totaliza = 1;
                    }

                }
                if ($totaliza == 0)
                {
                    $sub_tot = $sub_tot + 1;                                                   
                ?>
                              
                    <tr>
                        <td><?php echo $num; ?></td>
                        <td><?php echo $row->cvepaciente; ?></td>
                        <td><?php echo $row->nombre; ?></td>
                        <td><?php echo $row->fecha; ?></td>
                        <td><?php echo ($row->recetaID); ?></td>                
                        <td><?php echo ($row->cvemedico); ?></td>
                        <td><?php echo ($row->nombremedico); ?></td>
                        <td><?php echo ($row->dependencia_desc); ?></td>
                        <td><?php echo ($row->cie103); ?></td>
                        <td><?php echo ($row->cie104); ?></td>
                        
                    </tr>
                <?php 
                }elseif($totaliza == 1) 
                {
                ?>
                   <tr>
                        <td style="text-align: right;" colspan="4">Total de visitas del titular y dependientes:</td>
                        <td></td>
                        <td style="text-align: right;" id="req"><?php echo number_format ($sub_tot, 0); ?></td>
                        <td colspan="4"></td>
                    </tr>                
                    <tr>
                        <td><?php echo $num; ?></td>
                        <td><?php echo $row->cvepaciente; ?></td>
                        <td><?php echo $row->nombre; ?></td>
                        <td><?php echo $row->fecha; ?></td>
                        <td><?php echo ($row->recetaID); ?></td>                
                        <td><?php echo ($row->cvemedico); ?></td>
                        <td><?php echo ($row->nombremedico); ?></td>
                        <td><?php echo ($row->dependencia_desc); ?></td>
                        <td><?php echo ($row->cie103); ?></td>
                        <td><?php echo ($row->cie104); ?></td>
                        
                    </tr>
                <?php
                    $sub_tot = 1;
                }
            
            }
            
            ?>
        </tbody>
        <tfoot>
            <tr>
                <td style="text-align: right;" colspan="4">Total de visitas del titular y dependientes:</td>
                <td></td>
                <td style="text-align: right;" id="req2"><?php echo number_format ($sub_tot, 0); ?></td>
                <td colspan="4"></td>
            </tr>
            <tr>
                <td style="text-align: right;" colspan="4">Total de pasientes atendidos</td>
                <td></td>
                <td style="text-align: right;" id="req"><?php echo number_format ($num, 0); ?></td>
                <td colspan="4"></td>

            </tr>            
            <tr>
                <th>#</th>
                <th>CLAVE PACIENTE</th>
                <th>NOMBRE</th>
                <th>FECHA</th>
                <th>FOLIO</th>                
                <th>CLAVE MEDICO</th>
                <th>MEDICO</th>
                <th>DEPENDIENTE</th>
                <th>DIAGNOSTICO PRIMARIO CIE103</th>
                <th>DIAGNOSTICO SECUNDARIO CIE104</th>

             </tr>
        </tfoot>
    </table>    
    

</div>
</div>

<div class="row-fluid">
<div class="span12">
<div id="grafica"></div>
</div>
</div>
