2018-05-07 16:47:29 -07:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
2014-08-28 15:21:05 +02:00
|
|
|
class TicketPurchasesController < ApplicationController
|
2017-09-05 22:23:34 +03:00
|
|
|
before_action :authenticate_user!
|
2014-08-28 15:21:05 +02:00
|
|
|
load_resource :conference, find_by: :short_title
|
|
|
|
|
authorize_resource :conference_registrations, class: Registration
|
2017-07-20 18:14:38 +05:30
|
|
|
authorize_resource
|
2014-08-28 15:21:05 +02:00
|
|
|
|
|
|
|
|
def create
|
2016-08-11 17:47:52 +05:30
|
|
|
current_user.ticket_purchases.by_conference(@conference).unpaid.destroy_all
|
2021-03-03 13:29:54 -08:00
|
|
|
|
|
|
|
|
# Create a ticket purchase which can be paid or unpaid
|
2021-03-04 11:55:45 -08:00
|
|
|
count_registration_tickets_before = current_user.count_registration_tickets(@conference)
|
2021-03-03 13:29:54 -08:00
|
|
|
message = TicketPurchase.purchase(@conference, current_user, params[:tickets].try(:first))
|
2021-03-04 11:37:16 -08:00
|
|
|
# The new ticket_purchase has been added to the database. current_user.ticket_purchases contains the new one.
|
2021-03-04 11:55:45 -08:00
|
|
|
count_registration_tickets_after = current_user.count_registration_tickets(@conference)
|
2021-03-04 16:34:47 -08:00
|
|
|
|
2021-03-03 13:29:54 -08:00
|
|
|
# Failed to create ticket purchase
|
2021-03-04 16:34:47 -08:00
|
|
|
unless message.blank?
|
2021-03-03 13:29:54 -08:00
|
|
|
redirect_to conference_tickets_path(@conference.short_title),
|
|
|
|
|
error: "Oops, something went wrong with your purchase! #{message}"
|
|
|
|
|
return
|
|
|
|
|
end
|
|
|
|
|
|
2021-03-04 11:55:45 -08:00
|
|
|
# Current user already paid for a registration ticket and the current ticket purchase contains one
|
|
|
|
|
if count_registration_tickets_before == 1 && count_registration_tickets_after > 1
|
|
|
|
|
redirect_to conference_physical_tickets_path,
|
2021-03-04 21:50:26 -08:00
|
|
|
error: 'You already have one registration ticket for the conference.'
|
2021-03-04 11:55:45 -08:00
|
|
|
return
|
2021-03-03 13:06:38 -08:00
|
|
|
end
|
2021-03-03 13:29:54 -08:00
|
|
|
|
2021-03-04 21:50:26 -08:00
|
|
|
# User needs to pay for tickets if any of them is not free.
|
2021-03-04 16:32:36 -08:00
|
|
|
if current_user.ticket_purchases.by_conference(@conference).unpaid.any?
|
2021-03-09 18:25:15 -08:00
|
|
|
has_registration_ticket = count_registration_tickets_before.zero? && count_registration_tickets_after == 1
|
2021-03-04 21:50:26 -08:00
|
|
|
redirect_to new_conference_payment_path(has_registration_ticket: has_registration_ticket),
|
2021-03-04 16:32:36 -08:00
|
|
|
notice: 'Please pay here to get tickets.'
|
|
|
|
|
return
|
|
|
|
|
end
|
|
|
|
|
|
2021-03-04 21:50:26 -08:00
|
|
|
# Redirect to registration page for a user who didn't have a registration ticket and is purchasing one
|
2021-03-09 18:25:15 -08:00
|
|
|
if count_registration_tickets_before.zero? && count_registration_tickets_after == 1
|
2021-03-04 21:50:26 -08:00
|
|
|
redirect_to new_conference_conference_registration_path(@conference.short_title),
|
|
|
|
|
notice: 'Thanks! Your ticket is booked successfully. Please register for the conference.'
|
2021-03-04 16:34:47 -08:00
|
|
|
else
|
2021-03-04 21:50:26 -08:00
|
|
|
redirect_to conference_physical_tickets_path,
|
|
|
|
|
notice: 'Thanks! Your ticket is booked successfully.'
|
2021-03-04 15:47:48 -08:00
|
|
|
end
|
2014-08-28 15:21:05 +02:00
|
|
|
end
|
|
|
|
|
|
2017-06-26 06:07:52 +05:30
|
|
|
def index
|
|
|
|
|
@unpaid_ticket_purchases = current_user.ticket_purchases.by_conference(@conference).unpaid
|
|
|
|
|
end
|
|
|
|
|
|
2015-10-28 18:05:15 +02:00
|
|
|
private
|
|
|
|
|
|
|
|
|
|
def ticket_purchase_params
|
|
|
|
|
params.require(:ticket_purchase).permit(:ticket_id, :user_id, :conference_id, :quantity)
|
|
|
|
|
end
|
2014-08-28 15:21:05 +02:00
|
|
|
end
|