<?php		
	session_start();

	include('conexion.php');
	include 'mysql_Agregar_auditoria.php';	
	require('./fpdf/fpdf.php');

	$cliente = $_SESSION['id_cliente'];
	$id_vehiculo = $_GET['id_vehiculo'];
	$fecha_inicio = $_GET['fecha_inicio'];
	$fecha_fin = $_GET['fecha_fin'];
	$fini = date_create($fecha_inicio);
	$finiMuestra =  date_format($fini, 'Ymd');	
	$ffin = date_create($fecha_fin);
	$ffinMuestra =  date_format($ffin, 'Ymd');	
	
	$sql = "SELECT ";
	$sql .= "id_cliente, ";
	$sql .= "razon_social, ";
	$sql .= "nombre_comercial, ";
	$sql .= "saldo ";
	$sql .= "FROM ";
	$sql .= "clientes ";
	$sql .= "WHERE ";
	$sql .= "id_cliente = " . $cliente;
	
	$resultado = $conexion->query($sql); 
								
	if (!$resultado) 
	{
		die("Error: no se pudo realizar la consulta cliente");
	}
		
	while ($fila = $resultado->fetch_assoc()) 
	{
		$_SESSION['id_cliente'] = $fila['id_cliente'];
		$_SESSION['razon_cliente'] = $fila['razon_social'];
		$_SESSION['nombre_cliente'] = $fila['nombre_comercial'];
		$_SESSION['saldo_muestra'] = $fila['saldo'];
	}

	$resultado->free();

	number_format($_SESSION['saldo_muestra'], 2, '.', ',');
	$_SESSION['fech1'] = $fecha_inicio;	
	$_SESSION['fech2'] = $fecha_fin;

	class PDF extends FPDF
	{
		function Header()
		{
			$this->SetFont('Arial','B',14);
			$this->Cell(130,10,'Reporte Consumos');
			$this->SetFont('Arial','',10);
			date_default_timezone_set('America/Mazatlan');
			$time = time();	
			$this->Cell(50,10,'Fecha: ' . date("Y/m/d  H:i:s", $time) );
			$this->Ln();
			$this->SetFont('Arial','',10);			
			$this->Cell(25,5,"Cliente");
			$this->Cell(50,5,$_SESSION["id_cliente"]);
			$this->Ln();		
			$this->Cell(25,5,"Razon Social");
			$this->Cell(50,5,$_SESSION['razon_cliente']);
			$this->Ln();		
			$this->Cell(25,5,"Saldo");
			$this->Cell(50,5,$_SESSION['saldo_muestra']);
			$this->Ln();
			$this->Cell(25,5,"Fecha Inicio");
			$this->Cell(50,5,$_SESSION['fech1']);
			$this->Ln();		
			$this->Cell(25,5,"Fecha Fin");
			$this->Cell(50,5,$_SESSION['fech2']);
			$this->Ln();			
			$this->SetFont('Arial','B',14);
			$this->ln();
			$this->SetFont('Arial','',9);
			$this->Cell(40,5, 'Vehiculo','B',0,'L');
			$this->Cell(80,5, 'Descripcion','B',0,'L');
			$this->Cell(15,5, 'Consumos','B',0,'R');
			$this->Cell(20,5, 'Litros','B',0,'R');
			$this->Cell(20,5, 'Volumen','B',0,'R');
		}
	}

	$pdf = new PDF();
	$pdf->AliasNbPages();
	$pdf->SetTitle('Reporte Consumos');
	$pdf->AddPage();	

	$sql = "select ";
	$sql .= "c.id_tag as tag, ";
	$sql .= "c.descripcion as descripcion, ";
	$sql .= "count(*) as consumos, ";
	$sql .= "round(sum(b.volumen),3) as volumen, ";
	$sql .= "round(sum(b.monto),2) 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.id_turno >= '" . $finiMuestra . "00" . "' and a.id_turno <= '" . $ffinMuestra . "06" . "' ";
	
	if ($id_vehiculo != 0)
	{
		$sql .= "and c.id_vehiculo = " . $id_vehiculo . " ";
	}
	
	$sql .= "group by c.id_tag, c.descripcion ";
	$sql .= "order by c.id_tag ";
	
	$resultado = $conexion->query($sql); 

	$numero_filas = $resultado -> num_rows;
	$num_consumos = 0;	
	
	if (!$resultado) 
	{
		die("Error: no se pudo realizar la consulta".$sql);
	}

	while ($fila = $resultado->fetch_assoc()) 
	{			
		$pdf->ln();
		$pdf->Cell(40,2,$fila["tag"],0,0,'L');	
		$pdf->Cell(80,2,$fila["descripcion"],0,0,'L');	
		$pdf->Cell(15,2,$fila["consumos"],0,0,'R');	
		$pdf->Cell(20,2,$fila["volumen"],0,0,'R');	
		$pdf->Cell(20,2,$fila["monto"],0,0,'R');			
		$pdf->ln();		

		$num_consumos = $num_consumos + $fila['consumos'];
		$total_volumen = $total_volumen + $fila['volumen'];
		$total_monto = $total_monto + $fila['monto'];
	}

	$resultado->free();

	$pdf->ln();
	$pdf->Cell(40,10,'Total vehiculos: '. $numero_filas . ' ',0 ,0 , 'L');	
	$pdf->Cell(80,10,'Total:',0,0,'R');	
	$pdf->Cell(15,10,$num_consumos,0,0,'R');	
	$pdf->Cell(20,10,number_format($total_volumen,3),0,0,'R');	
	$pdf->Cell(20,10,number_format($total_monto,2),0,0,'R');					
	$pdf->ln();	
	$pdf->Output();	

	$informacion = 'Genero Reporte Consumos formato PDF ';
	$datos = "Con filtros: ";
	if ($id_vehiculo != 0)
	{
		$datos=$datos." Vehiculo=".$id_vehiculo;
	}
	$datos=$datos." Del id_turno=". $finiMuestra . "00" . " al id_turno =" . $ffinMuestra . "06";	
	insertAuditoria($informacion,$datos,$_SESSION['id_usuario'],$_SESSION['direccion_ip'],$_SESSION['direccion_mac']);

	mysqli_close($conexion);
?>