feat: PDF ticket generation

- Each ticket has a unique URL for viewing and downloading
- Only the ticket owner can access their ticket
- The customer's name is clearly displayed on the ticket
- The PDF can be downloaded directly from the ticket view page
- All existing functionality continues to work as expected
This commit is contained in:
kbe
2025-09-05 21:19:41 +02:00
parent 01b545c83e
commit a984243fe2
6 changed files with 135 additions and 11 deletions

View File

@@ -54,6 +54,32 @@ class TicketsController < ApplicationController
rescue ActiveRecord::RecordNotFound
redirect_to dashboard_path, alert: "Billet non trouvé"
end
# Download PDF ticket - only accessible by ticket owner
def download_ticket
# Find ticket and ensure it belongs to current user
@ticket = current_user.orders.joins(:tickets).find_by(tickets: { id: params[:ticket_id] })
if @ticket.nil?
redirect_to dashboard_path, alert: "Billet non trouvé ou vous n'avez pas l'autorisation d'accéder à ce billet"
return
end
# Generate PDF
pdf_content = @ticket.to_pdf
# Send PDF as download
send_data pdf_content,
filename: "ticket_#{@ticket.id}_#{@ticket.event.name.parameterize}.pdf",
type: "application/pdf",
disposition: "attachment"
rescue ActiveRecord::RecordNotFound
redirect_to dashboard_path, alert: "Billet non trouvé"
rescue => e
Rails.logger.error "Error generating ticket PDF: #{e.message}"
redirect_to dashboard_path, alert: "Erreur lors de la génération du billet"
end
private
def set_event

View File

@@ -118,7 +118,7 @@ export default class extends Controller {
await this.storeCartInSession(cartData);
// Redirect to event-scoped orders/new page
const OrderNewUrl = `/events/${this.eventSlugValue}.${this.eventIdValue}/orders/new`;
const OrderNewUrl = `/orders/new/events/${this.eventSlugValue}.${this.eventIdValue}`;
window.location.href = OrderNewUrl;
} catch (error) {
console.error("Error storing cart:", error);

View File

@@ -32,13 +32,18 @@ class TicketPdfGenerator
# Ticket info box
pdf.stroke_color "E5E7EB"
pdf.fill_color "F9FAFB"
pdf.rounded_rectangle [ 0, pdf.cursor ], 310, 120, 10
pdf.rounded_rectangle [ 0, pdf.cursor ], 310, 150, 10
pdf.fill_and_stroke
pdf.move_down 10
pdf.fill_color "000000"
pdf.font "Helvetica", size: 12
# Customer name
pdf.text "Ticket Holder:", style: :bold
pdf.text "#{ticket.first_name} #{ticket.last_name}"
pdf.move_down 8
# Ticket details
pdf.text "Ticket Type:", style: :bold
pdf.text ticket.ticket_type.name

View File

@@ -157,7 +157,7 @@
<% end %>
<% if @ticket.status == 'active' %>
<%= link_to "#",
<%= link_to download_ticket_path(@ticket.id),
class: "flex-1 bg-gradient-to-r from-purple-600 to-indigo-600 hover:from-purple-700 hover:to-indigo-700 text-white font-medium py-3 px-6 rounded-xl shadow-sm transition-all duration-200 focus:outline-none focus:ring-2 focus:ring-purple-500 focus:ring-offset-2 transform hover:-translate-y-0.5 text-center" do %>
<svg class="w-4 h-4 inline-block mr-2" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 10v6m0 0l-3-3m3 3l3-3m2 8H7a2 2 0 01-2-2V5a2 2 0 012-2h5.586a1 1 0 01.707.293l5.414 5.414a1 1 0 01.293.707V19a2 2 0 01-2 2z"/>