<?php include('meta.php') ?>
<?php $bg_header = "bg_hc"; include('header.php') ?>

<div id="box_body"><!-- body center -->

 <br /><span class="big_title_h2">Informes</span> <a href="home_informes.php"><span class='glyphicon glyphicon-chevron-left'></span>Regresar</a><br />

<!-- 
<php

$start_date = $_POST['start_efec_agen'];
$end_date = $_POST['end_efec_agen'];

echo $start_date." / ".$end_date."<br>";
$result = mysqli_query($con,"SELECT * FROM se_evenement WHERE DATE(start) BETWEEN '$start_date' AND '$end_date'") or die(mysqli_error());  
while($row = mysqli_fetch_assoc($result)){
  $id = $row['id'];  
  $name= $row['name']; 
  echo $id."  * ".$name."<br>";
}
?>

-->
<?php 
// 1 Efectividad de agenda 
$start_efec_agen = $_POST['start_efec_agen'];
$end_efec_agen = $_POST['end_efec_agen'];
  if($start_efec_agen != ""){
      echo "<br /><span class='title'>Informe de  Efectividad de agenda para el <strong>periodo:</strong><span style='color:black'> ".$start_efec_agen." al ".$end_efec_agen."</span></span>";  

            $result = mysqli_query($con,"SELECT  COUNT(*) FROM se_evenement WHERE DATE(start) BETWEEN '$start_efec_agen' AND '$end_efec_agen'");
            $total_citas1 =  mysqli_result($result, 0);
            $result = mysqli_query($con,"SELECT  COUNT(*) FROM se_evenement WHERE DATE(start) BETWEEN '$start_efec_agen' AND '$end_efec_agen' AND status = 'Finalizada'");            
            $total_finalizadas =  mysqli_result($result, 0);    
            $efec_agen_result = ($total_finalizadas*100)/$total_citas1;
            echo   "<div style='padding:0px 10px'><h3>El resultado es de ".round($efec_agen_result)."% </h3></div>"; 
            echo   "<div style='padding:0px 10px'><h3>De ".$total_citas1." citas, ".$total_finalizadas." fueron finalizadas </h3></div><br><br>";

    };
// 2 Efectividad de valoración
$start_efec_val = $_POST['start_efec_val'];
$end_efec_val = $_POST['end_efec_val'];
  if($start_efec_val != ""){
      echo "<br /><span class='title'>Informe de  Efectividad de valoración para el <strong>Periodo:</strong><span style='color:black'> ".$start_efec_val." al ".$end_efec_val."</span></span>";  
            $result = mysqli_query($con,"SELECT COUNT(*) FROM hc_control0 WHERE DATE(date_control) BETWEEN '$start_efec_val' AND '$end_efec_val'");
            $total_control0 =  mysqli_result($result, 0);
            $result = mysqli_query($con,"SELECT COUNT(*) FROM dieta WHERE DATE(date_diet) BETWEEN '$start_efec_val' AND '$end_efec_val' AND n_diet = '1.0'");    
            $total_control1 =  mysqli_result($result, 0);  
            $efec_val_result = ($total_control1*100)/$total_control0;
            echo   "<div style='padding:0px 10px'><h3>El resultado es de ".round($efec_val_result)."% </h3></div>";
            echo   "<div style='padding:0px 10px'><h3>De ".$total_control0." personas, continuaron ".$total_control1."</h3></div><br><br>";
    };
// 3 Efectividad de tratamiento de alimentación 
$start_efec_tal = $_POST['start_efec_tal'];
$end_efec_tal = $_POST['end_efec_tal'];
  if($start_efec_tal != ""){
      echo "<br /><span class='title'>Informe de  Efectividad de tratamiento de alimentación para el <strong>Periodo:</strong><span style='color:black'> ".$start_efec_tal." al ".$end_efec_tal."</span></span>"; 
            $result = mysqli_query($con,"SELECT COUNT(*) FROM hc_control1 WHERE DATE(date_control) BETWEEN '$start_efec_tal' AND '$end_efec_tal' AND n_control LIKE '5%' AND porc_redu >= 5");
            $total_control5 =  mysqli_result($result, 0);
            $result = mysqli_query($con,"SELECT COUNT(*) FROM hc_control1 WHERE DATE(date_control) BETWEEN '$start_efec_tal' AND '$end_efec_tal' AND n_control LIKE '5%'");                 
            $total_control51 =  mysqli_result($result, 0); 
            $efec_tal_result = ($total_control5*100)/$total_control51;
            echo   "<div style='padding:0px 10px'><h3>El resultado es de ".round($efec_tal_result)."% </h3></div>";
            echo   "<div style='padding:0px 10px'><h3>De ".$total_control5." personas, lograron bajar ".$total_control51."</h3></div><br><br>";

    };
// 4 - Efectividad de adherencia ********************************************************************* //
$date_start = $_POST['start_efec_adh'];
$date_end =  $_POST['end_efec_adh'];
  if($date_start  != ""){      
     echo   "<script type='text/javascript'>  
              window.location='home_informes_adh.php?date_start=$date_start&date_end=$date_end';
            </script>";
    };


// 5 Porcentaje de abandono  ********************************************************************* //
$start_efec_aba = $_POST['start_efec_aba'];
$end_efec_aba = $_POST['end_efec_aba'];
// resto 15 dias
$dias_resto= 15; // los días a restar
$end_efec_aba2 = date("Y-m-d", strtotime("$end_efec_aba -$dias_resto day"));


  if($start_efec_aba != ""){
      echo "<br /><span class='title'>Informe del porcentaje de abandono para el <strong>Periodo:</strong><span style='color:black'> ".$start_efec_aba." al ".$end_efec_aba2."</span></span>"; 
            /* $result = mysqli_query($con,"SELECT COUNT(*) FROM dieta WHERE DATE(date_diet) BETWEEN '$start_efec_aba' AND '$end_efec_aba2' AND n_diet LIKE '%4%' ");
            $total_abandono0 =  mysqli_result($result, 0); 
            echo "<br>".$total_abandono0; */       
            $total_abandono0 = mysqli_num_rows(mysqli_query($con,"SELECT COUNT(*) FROM dieta WHERE DATE(date_diet) BETWEEN '$start_efec_aba' AND '$end_efec_aba2'  GROUP BY id"));
            $total_abandono1 = mysqli_num_rows(mysqli_query($con,"SELECT COUNT(*) FROM dieta WHERE DATE(date_diet) BETWEEN '$start_efec_aba' AND '$end_efec_aba2' AND n_diet >= '5' GROUP BY id"));
            $efec_aba_result2 = (1-($total_abandono1/$total_abandono0))*100;
            $res_aban = $total_abandono0-$total_abandono1;
            $result_ader= ($total_abandono1/$total_abandono0)*100;
            echo "<br />ESTE INFORME SE BASA EN LAS DIETAS";      
            echo   "<br /><h4>De ".$total_abandono0." personas siguieron ". $total_abandono1." y abandonaron ".$res_aban."<br /> "; 
             echo   "El resultado es de ".round($efec_aba_result2)."% de abandono y ".round($result_ader)."% de adherencia</h4><br />" ;
            $result3= mysqli_query($con,"SELECT MAX(n_diet) as m_diet, id, date_diet FROM dieta WHERE DATE(date_diet) BETWEEN '$start_efec_aba' AND '$end_efec_aba2' GROUP BY id ORDER BY date_diet ASC");   

             /* 51 71 73 75  ORDER BY id ASC*/ 
            $con = 0;
            echo "<span class='title'>Paciente que abandonaron:</span><br />";
            echo "<table id='allpatient' class='display table table-hover'> \n"; 
            echo " <thead>
                        <tr>
                        <th>N°</th>
                        <th>Nombre</th>
                        <th>Cedula</th>
                        <th>Teléfono</th>
                        <th>Ultima visita</th>
                        <th>#_dieta</th>
                        <th></th>
                        </tr>
                      </thead>
                      <tbody> \n"; 
            while ($registro = mysqli_fetch_assoc($result3))  {   
              //consultar el nombre del usuario
              $cons = mysqli_query($con,"SELECT * FROM hc_paciente WHERE id = ".$registro['id']."");
              $result_cons = mysqli_fetch_assoc($cons);
              $name = $result_cons['name'];  
              $cell = $result_cons['cell']; 
              $cc = $result_cons['cc']; 
              $last_visit = $result_cons['last_visit']; 
              $con = $con+1;
              /* $con */
              if ($registro['m_diet'] < 5){
                echo "<tr> 
                        <td></td>
                        <td><strong>$name</strong></td>
                        <td>$cc</td>
                        <td>$cell</td>
                        <td>".$registro['date_diet']."</td>
                        <td>".$registro['m_diet']."</td>
                        <td><a target='_blank' href='../result.php?id=".$registro['id']."'>Ver mas info</a></td>";

              }
              
            }  
              echo " \n"; 
              echo " </tbody></table> \n";     
    };
// 6 Efectividad de agendamiento P.A.
$start_efec_apa = $_POST['start_efec_apa'];
$end_efec_apa = $_POST['end_efec_apa'];
  if($start_efec_apa != ""){
      echo "<br /><span class='title'>Informe de  Efectividad de agendamiento PA para el <strong>Periodo:</strong><span style='color:black'> ".$start_efec_apa." al ".$end_efec_apa."</span></span>"; 
            $result = mysqli_query($con,"SELECT COUNT(*) FROM se_evenement WHERE DATE(start) BETWEEN '$start_efec_apa' AND '$end_efec_apa' AND t_servicio = 'Valoracion'");
            $total_apa0 =  mysqli_result($result, 0); 
            $result = mysqli_query($con,"SELECT COUNT(*) FROM se_evenement WHERE DATE(start) BETWEEN '$start_efec_apa' AND '$end_efec_apa' AND t_servicio = 'Valoracion' AND status ='Finalizada'");                 
            $total_apa1 =  mysqli_result($result, 0); 
            $efec_apa_result = ($total_apa1*100)/$total_apa0;
            echo   "<div style='padding:0px 10px'><h3>El resultado es de ".round($efec_apa_result)."% </h3></div>";
             echo   "<div style='padding:0px 10px'><h3>De ".$total_apa0." citas, ".$total_apa1." fueron para control 0</h3></div><br><br>";
    };

// 7 Efectividad de tiempo de cita
$start_efec_time = $_POST['start_efec_time'];
$end_efec_time = $_POST['end_efec_time'];
  if($start_efec_time != ""){
      echo "<br /><span class='title'>Informe de  Efectividad de tiempo de cita <strong>Periodo:</strong><span style='color:black'> ".$start_efec_time." al ".$end_efec_time."</span></span>"; 


             $result2 = mysqli_query($con,"SELECT * FROM se_evenement WHERE DATE(start) BETWEEN '$start_efec_time' AND '$end_efec_time' AND status = 'Finalizada'");
            $registro2 = mysqli_fetch_assoc($result2);
            $tt = $registro2['last_modified'];  
            echo $tt;

             $result = mysqli_query($con,"SELECT * FROM se_evenement WHERE DATE(start) BETWEEN '$start_efec_time' AND '$end_efec_time' AND status = 'Finalizada'");
            $total_time0 =  mysqli_result($result,0); 
            echo $total_time0;





            $result = mysqli_query($con,"SELECT COUNT(end_events) FROM se_evenement WHERE DATE(start) BETWEEN '$start_efec_time' AND '$end_efec_time' AND status ='Finalizada'");                 
            $total_time1 =  mysqli_result($result, 0); 
            echo $total_time1;
            $efec_time_result = ($total_time1*100)/$total_time0;
            echo   "<div style='padding:0px 10px'><h3>El promedio es de ".round($efec_time_result)."% </h3></div>";
             echo   "<div style='padding:0px 10px'><h3>De ".$total_time0." citas, el promedio de tiempo que duro la cita fue de ".$total_time1."</h3></div><br><br>";
    };


?>       
  </div><!-- END body center -->
<?php include('../footer.php') ?>
