## Backend Implementation

Enhanced TicketType model with helper methods and better validations

So the full context is:

## Backend Implementation
- Enhanced TicketType model with helper methods and better validations
- New Promoter::TicketTypesController with full authorization
- Sales status tracking (draft, available, upcoming, expired, sold_out)
- New Promoter::TicketTypesController with full authorization
- Safe calculation methods preventing nil value errors
- Sales status tracking (draft, available, upcoming, expired, sold_out)

## Frontend Features
- Modern responsive UI with Tailwind CSS styling
- Interactive forms with Stimulus controller for dynamic calculations
- Revenue calculators showing potential, current, and remaining revenue
- Status indicators with appropriate colors and icons
- Buyer analytics and purchase history display

## JavaScript Enhancements
- New TicketTypeFormController for dynamic pricing calculations
- Real-time total updates as users type price/quantity
- Proper French currency formatting
- Form validation for minimum quantities based on existing sales

## Bug Fixes
 Fixed nil value errors in price_euros method when price_cents is nil
 Added defensive programming for all calculation methods
 Graceful handling of incomplete ticket types during creation
 Proper default values for new ticket type instances

## Files Added/Modified
- app/controllers/promoter/ticket_types_controller.rb (new)
- app/javascript/controllers/ticket_type_form_controller.js (new)
- app/views/promoter/ticket_types/*.html.erb (4 new view files)
- app/models/ticket_type.rb (enhanced with helper methods)
- config/routes.rb (added nested ticket_types routes)
- db/migrate/*_add_requires_id_to_ticket_types.rb (new migration)

## Integration
- Seamless integration with existing event management system
- Updated promoter event show page with ticket management link
- Proper scoping ensuring promoters only manage their own tickets
- Compatible with existing ticket purchasing and checkout flow
This commit is contained in:
kbe
2025-09-01 00:03:35 +02:00
parent aa5dccb508
commit e838e91162
12 changed files with 1057 additions and 3 deletions

View File

@@ -12,7 +12,64 @@ class TicketType < ApplicationRecord
validates :sale_end_at, presence: true
validates :minimum_age, numericality: { only_integer: true, greater_than_or_equal_to: 0, less_than_or_equal_to: 120 }, allow_nil: true
validates :event_id, presence: true
validates :requires_id, inclusion: { in: [true, false] }
# Custom validations
validate :sale_end_after_start
validate :sale_times_within_event_period
# Scopes
scope :available_now, -> { where("sale_start_at <= ? AND sale_end_at >= ?", Time.current, Time.current) }
scope :upcoming, -> { where("sale_start_at > ?", Time.current) }
scope :expired, -> { where("sale_end_at < ?", Time.current) }
# Helper methods
def price_euros
return 0.0 if price_cents.nil?
price_cents / 100.0
end
def price_euros=(value)
self.price_cents = (value.to_f * 100).to_i
end
def available?
return false if sale_start_at.nil? || sale_end_at.nil?
sale_start_at <= Time.current && sale_end_at >= Time.current
end
def sold_out?
return false if quantity.nil?
tickets.count >= quantity
end
def available_quantity
return 0 if quantity.nil?
[quantity - tickets.count, 0].max
end
def sales_status
return :draft if sale_start_at.nil? || sale_end_at.nil?
return :expired if sale_end_at < Time.current
return :upcoming if sale_start_at > Time.current
return :sold_out if sold_out?
return :available
end
def total_potential_revenue
return 0.0 if quantity.nil? || price_cents.nil?
quantity * price_euros
end
def current_revenue
return 0.0 if price_cents.nil?
tickets.count * price_euros
end
def remaining_potential_revenue
return 0.0 if quantity.nil? || price_cents.nil?
available_quantity * price_euros
end
private
@@ -20,4 +77,9 @@ class TicketType < ApplicationRecord
return unless sale_start_at && sale_end_at
errors.add(:sale_end_at, "must be after sale start") if sale_end_at <= sale_start_at
end
def sale_times_within_event_period
return unless event&.start_time && sale_end_at
errors.add(:sale_end_at, "cannot be after the event starts") if sale_end_at > event.start_time
end
end