<?php

	session_start();
	include('conexion.php');	
	$tarjeta = $_GET['tarjeta'];
	$fecha_inicio = $_GET['fecha_inicio'];
	$fecha_fin = $_GET['fecha_fin'];	
?>

<HTML>
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
	<link rel="shortcut icon" href="images/Sysmental.jpg" type="image/jpeg"/>
	<title>Flotillas</title>
	<link href="css/bootstrap.min.css" rel="stylesheet">
	<script src="//code.jquery.com/jquery-1.10.2.js"></script>
	<script src="//code.jquery.com/ui/1.11.1/jquery-ui.js"></script>
	<!--<script src="//code.jquery.com/ui/1.11.1/jquery-1.11.3.min.js"></script>-->
	<script type="text/javascript" src="//cdn.jsdelivr.net/jquery/1/jquery.min.js"></script>
<script type="text/javascript" src="//cdn.jsdelivr.net/momentjs/latest/moment.min.js"></script>
 
<!-- Include Date Range Picker -->
<script type="text/javascript" src="//cdn.jsdelivr.net/bootstrap.daterangepicker/2/daterangepicker.js"></script>
<link rel="stylesheet" type="text/css" href="//cdn.jsdelivr.net/bootstrap.daterangepicker/2/daterangepicker.css" />
<!--alert-->
	<script src = "sweetalert-master/dist/sweetalert.min.js" ></script>  
	<link rel = "stylesheet" type = "text/css" href = "sweetalert-master/dist/sweetalert.css" >
</head>
<body>
<?php
	$sql="select ";
	$sql.="a.id_tag as tag, ";
	$sql.="c.descripcion as descripcion, ";
	$sql.="count(*) as consumos, ";
	$sql.="sum(b.volumen) as volumen, ";
	$sql.="sum(b.monto) as monto ";
	$sql.="from ";
	$sql.="transacciones a, ";
	$sql.="transacciones_detalle b, ";
	$sql.="vehiculos c ";
	$sql.="where ";
	$sql.="a.id_transaccion=b.id_transaccion and ";
	$sql.="a.id_vehiculo=c.id_vehiculo and ";
	$sql.="a.id_cliente = ".$_SESSION['id_cliente']." and ";
	$sql.="a.fecha between '" . $fecha_inicio ."' and '" . $fecha_fin . "' ";
	
	if($tarjeta != 0)
		$sql.="and a.id_tag = ".$tarjeta." ";
	
	$sql.="group by ";
	$sql.="a.id_tag, ";
	$sql.="c.descripcion ";
	$sql.="order by ";
	$sql.="a.id_tag ";
	
	$resultado = $iden->query($sql); 
								
	if(!$resultado) 
		die("Error: no se pudo realizar la consulta estacion" .$sql);
		
	$número_filas = $resultado->num_rows;
	$num_consumos = 0;
	if($número_filas > 0)
	{
		echo '<table>';
		echo '<tr>';
		echo '<th>Tarjeta</th>';		
		echo '<th>Descripcion</th>';		
		echo '<th>Consumos</th>';		
		echo '<th>Litros</th>';		
		echo '<th>Volumen</th>';				
		echo '</tr>';		
		while($fila = $resultado->fetch_assoc()) 
		{
			echo '<tr>';
			echo '<td>'.$fila['tag'].'</td>';
			echo '<td>'.$fila['descripcion'].'</td>';
			echo '<td>'.$fila['consumos'].'</td>';
			echo '<td>'.number_format($fila['volumen'],3).'</td>';
			echo '<td>'.number_format($fila['monto'],2).'</td>';			
			echo '</tr>';
			$num_consumos = $num_consumos + $fila['consumos'];
			$total_volumen = $total_volumen + $fila['volumen'];
			$total_monto = $total_monto + $fila['monto'];
		}
		$resultado->free();
		echo '<tr>';	
		echo '<td>Total tarjetas '.$número_filas.' </td>';	
		echo '<td>Total</td>';
		echo '<td>'.$num_consumos.'</td>';
		echo '<td>'.number_format($total_volumen,3).'</td>';
		echo '<td>'.number_format($total_monto,2).'</td>';	
		echo '</tr>';
		echo '</table>';		
		mysqli_close($iden);
	}
?>
	<div align="right">
		<input type="button" id="btnimpresion" onclick="impresion(2)" class="btn btn-default" value="imprimir"/>
	</div>
</body>
</html>