<?php
session_start();

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

$admin = $_SESSION["admin"];

include_once('db_config.php');
include('./Charts/FusionCharts.php');

$Dias = array("Domingo","Lunes","Martes","Miercoles","Jueves","Viernes","Sábado");
$dias = array("Dom","Lun","Mar","Mie","Jue","Vie","Sáb");
$Year     = isset($_POST['year']) ? $_POST["year"]  : "";
$Month  = isset($_POST['month']) ? $_POST['month'] : "";
$Fecha = "";
$Total_Llamados = 0;
$Total_Minutos = 0;
$Importe_Total = 0;
$Total_Dia_Llamados = 0;
$Total_Dia_Minutos = 0;
$Importe_Dia_Total = 0;

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

if( empty($Year) ) {        
   if( $_SESSION["Year"] != "" ) $Year = $_SESSION["Year"];
   else {
      $Year = date("Y");
   }
}

if( empty($Month) ) {       
   if( $_SESSION["Month"] != "" ) $Month = $_SESSION["Month"];
   else {
     $Month = date("n");
   }
}
$_SESSION["Year"] = $Year;
$_SESSION["Month"] = $Month;


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

if( $Year == "2015")	$YS2015 = "selected";
if( $Year == "2016")	$YS2016 = "selected";

if( $admin == true )
   $Movistar = 0.9225;
else $Movistar = 1.476; 
$Personal = 1.476;
$Claro =  0.738;
$Nextel = 1.107;
include 'header_graficos.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>
            <form id="asterisco" action="graficos.php" method="post"> 
            <div class="row">
            <div class="input-field col m3">
    				<select name="month">
 <?php
  					echo "<option value='' disabled>Seleccione el Mes</option>";				
      					echo "<option value='1' " . $sm[0] . ">Enero</option>";
      					echo "<option value='2' " . $sm[1] . ">Febrero</option>";
      					echo "<option value='3' " . $sm[2] . ">Marzo</option>";
      					echo "<option value='4' " . $sm[3] . ">Abril</option>";
      					echo "<option value='5' " . $sm[4] . ">Mayo</option>";
      					echo "<option value='6' " . $sm[5] . ">Junio</option>";
      					echo "<option value='7' " . $sm[6] . ">Julio</option>";
      					echo "<option value='8' " . $sm[7] . ">Agosto</option>";
      					echo "<option value='9' " . $sm[8] . ">Septiembre</option>";
      					echo "<option value='10' " . $sm[9] . ">Octubre</option>";
      					echo "<option value='11' " . $sm[10] . ">Noviembre</option>";
      					echo "<option value='12' " . $sm[11] . ">Diciembre</option>";
?>      					
    					</select>
               <label>Mes</label>
  				</div>			
  				<div class="input-field col  m3">
    					<select  name="year">
    					<option value="" disabled selected>Seleccione el A&ntilde;o</option>
<?php

      					echo "<option value='2015' " . $YS2015 . ">2015</option>";
      					echo "<option value='2016' " . $YS2016 . ">2016</option>";
?>     					
     					</select>
               <label>A&ntilde;o</label>
  				</div>		
  			 	<button class="btn waves-effect waves-light btn col s1" style="margin-top:25px; margin-left: 30px;" type="submit" name="action" >Go!</button>
  				</row>	
  				</form>
  				 <div style="height:70px;"></div>
  			</div>	
<?php
   $sql = "select  sql_no_cache dnis, date(fecha) as dia, dnis, aplicacion, count(id) as llamados, sum(duracion) as segundos, sum(duracion_facturada) as minutos, sum( duracion_facturada ) * Tarifa as importe from tickets where year(fecha) ='" . $Year . "' and Month(fecha)='" . $Month . "' and dnis  >= '1140008966' and dnis <= '1140008970' and dnis <> '1140008968' and fecha >= '2016-02-01' group by date(fecha)";
   $result = $conn->query($sql);

   if ($result->num_rows > 0) {
      
      echo "<div class='row'>";
      echo "<div class='col s12'>";

      $xmlData = "<chart showFCMenuItem='0' palette='1' shownames='1' showvalues='0' decimals='0' formatNumberScale='0' enableSmartLabels='1' yAxisName='Minutos' rotateNames='1' labelDisplay='ROTATE' slantLabels='1' showBorder='0' ";
//    $xmlData .= "bgColor='f4f4f4' overlapColumns='1' ";
      $xmlData .= "bgColor='cacaca' overlapColumns='1' ";
      $xmlData .= "caption='Total Minutos por Dia'";
      $xmlData .= " >";

      setlocale(LC_ALL,"es_ES");
 		while($row = $result->fetch_assoc()) {

         $date = date_create($row["dia"]);
         $formatted = $dias[date_format($date,'w')] . " " . date_format($date, 'd'); 
         $xmlData .= "<set label='" . $formatted . "' value='" . $row["minutos"] . "' />";
      }
      
      $xmlData .= "<trendlines>";
      $xmlData .= "<line startValue='42000' color='91C728' displayValue='Target'/>";
      $xmlData .= "</trendlines>";
      
      $xmlData .= "<styles>";
      $xmlData .= "<definition>";
      $xmlData .= "<style name='titleFont' type='font' font='segoe ui,Arial' bold='0' size='13' underline='1' color='444444' />";
      $xmlData .= "<style name='subtitleFont' type='font' font='segoe ui,Arial' bold='0' size='13' underline='0' color='444444' />";
      $xmlData .= "<style name='myBevel' type='Bevel' angle='0' />";
      $xmlData .= "<style name='myShadow' type='Shadow' distance='5' angle='45'/>";
      $xmlData .= "</definition>";
      $xmlData .= "<application>";
      $xmlData .= "<apply toObject='Caption' styles='titleFont' />";
      $xmlData .= "<apply toObject='subCaption' styles='subtitleFont' />";
      $xmlData .= "<apply toObject='Canvas' styles='myBevel,myShadow' />";
      $xmlData .= "</application>";
      $xmlData .= "</styles>";
      $xmlData .= "</chart>";
      echo renderChart("./Charts/Column3D.swf?ChartNoDataText=No hay datos para graficar.&PBarLoadingText=Cargando Grafico. Por favor espere...&XMLLoadingText=", "", $xmlData, "Grafico_Llamados", "100%", "280", false, false);
    	echo "</div>";
    	echo "</div>";
   } 
   mysql_free_result( $result );

   // Distribucion Trafico por Operadora
   $sql = "select sql_no_cache ( sum( case when dnis = '1140008966' then duracion else 0 end ) ) as movistar, (sum( case when dnis = '1140008967' then duracion else 0 end ) ) as personal, (sum( case when dnis = '1140008969' then duracion else 0 end ) ) as claro,  ( sum( case when dnis = '1140008970' then duracion else 0 end ) )as nextel  from tickets where year(fecha) ='" . $Year . "' and Month(fecha)='" . $Month . "' and dnis  >= '1140008966' and dnis <= '1140008970' and dnis <> '1140008968' and fecha >= '2016-02-01'";
   $result = $conn->query($sql);

   echo "<div class='row'>";
   echo "<div class='col s6'>";
   if ($result->num_rows > 0) {

      $xmlData = "<chart showFCMenuItem='0' palette='1' shownames='1' showvalues='0' decimals='0' formatNumberScale='0' enableSmartLabels='1' showPercentageValues='1' showBorder='0' ";
      $xmlData .= "bgColor='CACACA' ";
//    $xmlData .= "bgColor='f4f4f4' ";
      $xmlData .= "caption='Trafico por Operadora'";
      $xmlData .= " >";

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

         $xmlData .= "<set label='Personal' value='" . $row["personal"] . "' />";
         $xmlData .= "<set label='Claro' value='" . $row["claro"] . "' />";
         $xmlData .= "<set label='Movistar' value='" . $row["movistar"] . "' />";
         $xmlData .= "<set label='Nextel' value='" . $row["nextel"] . "' />";

      }

      $xmlData .= "<styles>";
      $xmlData .= "<definition>";
      $xmlData .= "<style name='titleFont' type='font' font='segoe ui,Arial' bold='0' size='13' underline='1' color='444444' />";
      $xmlData .= "<style name='subtitleFont' type='font' font='segoe ui,Arial' bold='0' size='13' underline='0' color='444444' />";
      $xmlData .= "<style name='myBevel' type='Bevel' angle='0' />";
      $xmlData .= "<style name='myShadow' type='Shadow' distance='5' angle='45'/>";
      $xmlData .= "</definition>";
      $xmlData .= "<application>";
      $xmlData .= "<apply toObject='Caption' styles='titleFont' />";
      $xmlData .= "<apply toObject='subCaption' styles='subtitleFont' />";
      $xmlData .= "<apply toObject='Canvas' styles='myBevel,myShadow' />";
      $xmlData .= "</application>";
      $xmlData .= "</styles>";
      $xmlData .= "</chart>";
      echo renderChart("./Charts/Doughnut3D.swf?ChartNoDataText=No hay datos para graficar.&PBarLoadingText=Cargando Grafico. Por favor espere...&XMLLoadingText=", "", $xmlData, "Grafico_Operadora", "100%", "280", false, false);
   } 
 	echo "</div>";
   mysql_free_result( $result );


   // Distribucion Revenue por Operadora
   echo "<div class='col s6'>";
   $sql = "select sql_no_cache ( sum( case when dnis = '1140008966' then duracion else 0 end ) ) as movistar, (sum( case when dnis = '1140008967' then duracion else 0 end ) ) as personal, (sum( case when dnis = '1140008969' then duracion else 0 end ) ) as claro,  ( sum( case when dnis = '1140008970' then duracion else 0 end ) )as nextel  from tickets where year(fecha) ='" . $Year . "' and Month(fecha)='" . $Month . "' and dnis  >= '1140008966' and dnis <= '1140008970' and dnis <> '1140008968' and fecha >= '2016-02-01'";
   $result = $conn->query($sql);
   if ($result->num_rows > 0) {

      $xmlData = "<chart showFCMenuItem='0' palette='1' shownames='1' showvalues='0' decimals='0' formatNumberScale='0' enableSmartLabels='1' showBorder='0' ";
      $xmlData .= "bgColor='CACACA' ";
      $xmlData .= "caption='RS por Operadora'";
      $xmlData .= " >";

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

         $xmlData .= "<set label='Personal' value='" . intval( $row["personal"] / 60 * $Personal ) . "' />";
         $xmlData .= "<set label='Claro' value='" . intval( $row["claro"] / 60 * $Claro ) . "' />";
         $xmlData .= "<set label='Movistar' value='" . intval( $row["movistar"] /60 * $Movistar ) . "' />";
         $xmlData .= "<set label='Nextel' value='" . intval( $row["nextel"] / 60 * $Nextel ) . "' />";
      }

      $xmlData .= "<styles>";
      $xmlData .= "<definition>";
      $xmlData .= "<style name='titleFont' type='font' font='segoe ui,Arial' bold='0' size='13' underline='1' color='444444' />";
      $xmlData .= "<style name='subtitleFont' type='font' font='segoe ui,Arial' bold='0' size='13' underline='0' color='444444' />";
      $xmlData .= "<style name='myBevel' type='Bevel' angle='0' />";
      $xmlData .= "<style name='myShadow' type='Shadow' distance='5' angle='45'/>";
      $xmlData .= "</definition>";
      $xmlData .= "<application>";
      $xmlData .= "<apply toObject='Caption' styles='titleFont' />";
      $xmlData .= "<apply toObject='subCaption' styles='subtitleFont' />";
      $xmlData .= "<apply toObject='Canvas' styles='myBevel,myShadow' />";
      $xmlData .= "</application>";
      $xmlData .= "</styles>";
      $xmlData .= "</chart>";
      
//    FC_SetRenderer( "javascript" );
      echo renderChart("./Charts/Doughnut3D.swf?ChartNoDataText=No hay datos para graficar.&PBarLoadingText=Cargando Grafico. Por favor espere...&XMLLoadingText=", "", $xmlData, "Grafico_RS", "100%", "280", false, false);
   } 
 	echo "</div>";
   echo "</div>";
//   echo "</td>";
// 	echo "</tr>";

   mysqli_close($conn);
?>					
<!--  	</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() {

      $('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 )
      });		
   });		
</script>
</body>
</html>
