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

$Fecha = isset($_POST['dia']) ? $_POST["dia"]  : "";
$Dnis = isset($_POST['dnis']) ? $_POST["dnis"]  : "";

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

if( $Dnis == "" ) {
   $Dnis = "1140008989";
}


include_once('db_config.php');
ini_set('max_execution_time', 120);

$Search = substr( $Fecha, 6, 4 ) . "-" . substr( $Fecha, 3, 2 ) . "-" . substr( $Fecha, 0, 2 ) . " 00:00:00"; ;
$Fecha_End = substr( $Fecha, 6, 4 ) . "-" . substr( $Fecha, 3, 2 ) . "-" . substr( $Fecha, 0, 2 ) . " 23:59:59";

$Fecha = "";
$Total_Llamados = 0;
$Total_Minutos = 0;
$Importe_Total = 0;
$Total_Dia_Llamados = 0;
$Total_Dia_Minutos = 0;
$Importe_Dia_Total = 0;
$Total_Records = 0;
$Total_Pages = 0;
$CallPerPage = 10;

$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>
  <link href="http://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">

  <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 s2' data-field='name'>Dnis</th> 
                    <th class='col s3' data-field='app'>Aplicacion</th> 
                    <th class="col s1" data-field="id">Fecha</th>
                    <th class="col s1 right-align" data-field="ani" class="col s1 right-align">CID</th>
                    <th class="col s2 right-align" data-field="duracion" class="col s2 right-align">Duracion</th>
                    <th class="col s2 right-align" data-field="tarifa" class="col s2 right-align">Tarifa</th>
                    <th class="col s2 right-align" data-field="price" class="col s2 right-align">Importe</th>
                  </tr>
                </thead>
                <tbody>
<?php

   if( $TotalRecords <= 0 ) {
     $sql = "select sql_no_cache count(id) as TotalRecords from tickets where fecha >='" . $Search . "' and fecha <='" . $Fecha_End . "' and dnis = '" . $Dnis . "'";
     $result = $conn->query($sql);
     if ($result->num_rows > 0) {
         if( $row = $result->fetch_assoc()) {
            $TotalRecords = $row["TotalRecords"];
         }
     }
   }

   $TotalPages = $TotalRecords / $CallsPerPage;
   mysql_free_result( $result );

   $offset = 0;
   $sql = "select sql_no_cache dnis, aplicacion, fecha, date(fecha) as dia, cid, duracion, tarifa, importe from tickets where date(fecha) ='" . $Search . "' and dnis='" . $Dnis . "' order by fecha desc LIMIT " . $offset . ", " . $CallPerPage;
   $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;

    $first = true;
    while($row = $result->fetch_assoc()) {

        $date = date_create($row["fecha"]);
        $formatted = date_format($date, 'd-m-Y h:m:s');
   
        if( ( $segundos = $row["duracion"] ) < 30 ) $segundos = 30;

        $Promedio = $segundos / 60;
        $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 = $segundos / 60 * 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 = $segundos / 60 * $Valor;
        $Importe_SVA_General += $Importe_SVA;
     

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

        echo "<tr>";
        if( $first == true ) {
           echo "<td class='col s2' >" . $row["dnis"] ."</td>";
           echo "<td class='col s3' >" . $row["aplicacion"] . "</td>";
        }
        else {
           echo "<td class='col s2' ></td>";
           echo "<td class='col s3' ></td>";
        }
        echo "<td class='col s1' >" . $formatted . "</td>";
        echo "<td class='col s1 right-align'>" . $row["cid"] . "</td>";
        echo "<td class='col s2 right-align'>" .  $Sec . " </td>";
        echo "<td class='col s2 right-align'>" . number_format ( $row["tarifa"], 5, ',' ,  '.' ) . "</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_Minutos += $segundos;
        $Importe_Total += $Importe;
        $first = false;
    }
  }     
   mysqli_close($conn);
?>          
              </tbody>
            </table>
          </div>
    <div style="height:20px;"></div>
    <div class='container' >
    <div class="col l9 m12 s12">
    <div class="page-content center-align">
<?php

   if( $offset == 0 ) $first_page="disabled";
   else $first_page = "waves-effect";

   echo "<ul class='pagination'>";
       echo "<li class='" . $first_page . "'><a href='#!'><i class='material-icons'>first_page</i></a></li>"; 
       echo "<li class='waves-effect'><a href='#!'><i class='material-icons'>chevron_left</i></a></li>"; 
       echo "<li class='waves-effect'><a href='#!'><i class='material-icons'>chevron_right</i></a></li>"; 
       echo "<li class='waves-effect'><a href='#!'><i class='material-icons'>last_page</i></a></li>"; 
  echo "</ul>"; 
  echo "</div>"; 
  echo "</div>"; 
  echo "</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>
   // Post to the provided URL with the specified parameters.
   function post(path, parameters) {
      var form = $('<form></form>');

       form.attr("method", "post");
       form.attr("action", path);

       $.each(parameters, function(key, value) {
           var field = $('<input></input>');
           field.attr("type", "hidden");
           field.attr("name", key);
           field.attr("value", value);
           form.append(field);
       });

       // The form needs to be a part of the document in
       // order for us to be able to submit it.
       $(document.body).append(form);
      form.submit();
   };

   function OnClick(e) {
         post("dnis.php", {dia: e.currentTarget.cells[2].innerHTML, dnis: e.currentTarget.cells[2].innerHTML }); 
   };

  $(document).ready(function() {

  });   
</script>
</body>
</html>
