<?php
session_start();
if( $_SESSION["user"] == "" || $_SESSION["passwd"] == "" ) {
   header("Location: index.php");
}

$Fecha = isset($_POST['dia']) ? $_POST["dia"]  : "";
if( $Fecha == "" ) {

//if( $_SESSION["dia"] == "" ) {
//   header("Location: llamados.php");
   $Fecha = "2016-02-24";
}

include_once('db_config.php');

ini_set('max_execution_time', 240);

//echo $Fecha . "<br>";
$Search = substr( $Fecha, 6, 4 ) . "-" . substr( $Fecha, 3, 2 ) . "-" . substr( $Fecha, 0, 2 );
//echo $Search . "<br>";
$Fecha = "";
$Total_Llamados = 0;
$Total_Minutos = 0;
$Importe_Total = 0;
$Total_Dia_Llamados = 0;
$Total_Dia_Minutos = 0;
$Importe_Dia_Total = 0;

$YS2015 = "";
$YS2016 = "";

$sm = array("", "", "", "", "", "", "", "", "", "", "", "");
$sm[$Month-1] = "selected";

$admin = false;
$Movistar = 0.9225;
$Personal = 1.476;
$Claro =  0.738;
$Nextel = 1.107;
include 'header.php'; 
?>
<style>
.dropdown-content {
  overflow-x: hidden;
}
</style>
  <div id="content">
  <div class="container" >
    <div style="height: 15px;"></div>
    <div class="col l9 m12 s12">
      <div class="page-content">
        <div style="height:20px;"></div>
        </div>  
        <table class="highlight">
                <thead>
                  <tr class="red lighten-3">
                      <th class="col s1" data-field="id">Fecha</th>
<?php
          if( $admin == true ) {
                        echo "<th class='col s2' data-field='name'>Dnis</th>"; 
                      }
                      echo "<th class='col s3' data-field='app'>Aplicacion</th>"; 
?>                      
                    <th class="col s1 right-align" data-field="calls" class="col s1 right-align">Llamados</th>
                    <th class="col s2 right-align" data-field="minutes" class="col s2 right-align">Minutos</th>
                    <th class="col s2 right-align" data-field="promedio" class="col s2 right-align">Promedio</th>
                    <th class="col s2 right-align" data-field="price" class="col s2 right-align">Importe</th>
                    <th class="col s2 right-align" data-field="sva" class="col s2 right-align">$ SVA</th>
                  </tr>
                </thead>
                <tbody>
<?php

  $sql = "select sql_no_cache fecha as dia, dnis, aplicacion, llamados, (minutos * 60 ) as segundos, minutos, importe from diario where Importe > 0 and date(fecha)='"  . $Search . "' order by fecha, dnis";
  $result = $conn->query($sql);

  if ($result->num_rows > 0) {
        // output data of each row
    $Valor = 0;
    $Fecha = "";
    $Total_Llamados = 0;
    $Total_Minutos = 0;
    $Importe_Total = 0;
    $Total_Dia_Llamados = 0;
    $Total_Dia_Minutos = 0;
    $Importe_Dia_Total = 0;
    $Importe_SVA = 0;
    $Importe_SVA_General = 0;
    $segundos = 0;

        while($row = $result->fetch_assoc()) {

            $date = date_create($row["dia"]);
            $formatted = date_format($date, 'd-m-Y');
            if( ( $segundos = $row["segundos"] ) < 30 ) $segundos = 30;
            if( $row["llamados"] == 0 ) $Promedio = 0;  
            else $Promedio = $segundos / 60 / $row["llamados"];
            $Horas = intval($Promedio / 60);
            $Promedio_Min = intval($Promedio - $Horas);
            $Promedio_Sec = ($Promedio - $Horas - $Promedio_Min ) * 60;

            if( $Horas > 0 )
              $Sec = sprintf("%d:%02d:%02d", $Horas, $Promedio_Min, $Promedio_Sec);
            else $Sec = sprintf("%d:%02d",$Promedio_Min, $Promedio_Sec);
            $Valor = 0;

            $Importe = $row["minutos"] * 0.00974 * 0.80;

            switch( $row["dnis"] ) {
               case "1140008966":  $Valor = 0.92225;  break;
               case "1140008967":  $Valor = 1.476;    break;
               case "1140008969":  $Valor = 0.738;    break;
               case "1140008970":  $Valor = 1.107;    break;
               default:            $Valor = 0;        break;
            }

            $Importe_SVA = $row["minutos"] * $Valor;
            $Importe_SVA_General += $Importe_SVA;
         

            if( $Fecha == $row["dia"] ) $formatted = "";
            $Fecha = $row["dia"];

              echo "<tr>";
              echo "<td class='col s1' >" . $formatted . "</td>";
              If( $admin == true ) {
                 echo "<td class='col s2' >" . $row["dnis"] ."</td>";
//                echo "<td class='col s2' ></td>";
              }                
                echo "<td class='col s3' >" . $row["aplicacion"] . "</td>";
                echo "<td class='col s1 right-align'>" . number_format ( $row["llamados"] , 0, ',' ,  '.' ) . "</td>";
                echo "<td class='col s2 right-align'>" . number_format ( $segundos / 60  , 0, ',' ,  '.' ) . "</td>";
                echo "<td class='col s2 right-align'>" .  $Sec . " </td>";
                echo "<td class='col s2 right-align'>" . number_format ( $Importe , 2, ',' ,  '.' ) . "</td>";
                echo "<td class='col s2 right-align'>" . number_format ( $Importe_SVA , 2, ',' ,  '.' ) . "</td>";
                echo "</tr>";
                $Total_Llamados += $row["llamados"];
                $Total_Minutos += $segundos / 60 ;
                $Importe_Total += $Importe;
      }
    } 
    mysqli_close($conn);

    if( $Total_Llamados == 0 ) $Promedio = 0;
    else $Promedio = $Total_Minutos / $Total_Llamados;

    $Horas = intval($Promedio / 60);
     $Promedio_Min = intval( $Promedio - $Horas );
     $Promedio_Sec = ($Promedio - $Horas - $Promedio_Min ) * 60;
     $Sec = sprintf("%d:%02d", $Promedio_Min, $Promedio_Sec);

          echo "<tfoot>";
          echo "<tr style='color:white; font-weight: 500;'>";
          echo "<td class='col s1'></td>";
          If( $admin == true ) {
                    echo "<td class='col s2'></td>";
          }
                  echo "<td class='col s3'></td>";
                  echo "<td class='col s1 right-align red darken-4'>" . number_format ( $Total_Llamados, 0, ',' ,  '.' ) . "</td>";
                  echo "<td class='col s2 right-align red darken-4'>" . number_format ( $Total_Minutos, 0, ',' ,  '.' ) . "</td>";
                  echo "<td class='col s2 right-align red darken-4'>" . $Sec . "</td>";
                  echo "<td class='col s2 right-align red darken-4'>" . number_format ( $Importe_Total, 2, ',' ,  '.' ) . "</td>";
                  echo "<td class='col s2 right-align red darken-4'>" . number_format ( $Importe_SVA_General, 2, ',' ,  '.' ) . "</td>";
                  echo "</tr>";
                  echo "</tfoot>";
?>          
              </tbody>
            </table>
          </div>
    <div style="height:100px;"></div>
  </div>
  </div>
  <script type="text/javascript" src="js/jquery.min.js"></script>
      <script type="text/javascript" src="js/jquery.form.js"></script> 
  <script type="text/javascript" src="js/materialize.min.js"></script>
<script>
  $(document).ready(function() {

              // bind 'myForm' and provide a simple callback function 
              /*
              $('#asterisco').ajaxForm(function() { 
                    alert("Thank you for your comment!"; 
              }); */ 

    $('select').material_select();

    $('#month').on('change', function() {
        Month = this.value;
        console.log( this.value )
    });   
    $('#year').on('change', function() {
        Year = this.value;
        console.log( this.value )
    });   

    console.log( "Ready")
  });   
</script>
</body>
</html>
