modify controllers and models to just save paid ticket purchases
This commit is contained in:
parent
900a1b32a8
commit
81ef41be8b
4 changed files with 25 additions and 17 deletions
|
|
@ -28,8 +28,10 @@ class ConferenceRegistrationsController < ApplicationController
|
|||
end
|
||||
|
||||
def show
|
||||
@total_price = Ticket.total_price(@conference, current_user)
|
||||
TicketPurchase.destroy_all(user_id: current_user.id, conference_id: @conference.id, paid: 'f')
|
||||
@total_price = Ticket.total_price(@conference, current_user, 't')
|
||||
@tickets = current_user.ticket_purchases.where(conference_id: @conference.id)
|
||||
@ticket_payments = @tickets.group_by(&:payment_id)
|
||||
end
|
||||
|
||||
def edit; end
|
||||
|
|
|
|||
|
|
@ -6,9 +6,8 @@ class TicketPurchasesController < ApplicationController
|
|||
def create
|
||||
message = TicketPurchase.purchase(@conference, current_user, params[:tickets][0])
|
||||
if message.blank?
|
||||
if current_user.ticket_purchases.any?
|
||||
redirect_to conference_conference_registration_path(@conference.short_title),
|
||||
notice: "Thank you for supporting #{@conference.title} by purchasing a ticket."
|
||||
if current_user.ticket_purchases.where(paid: 'f').any?
|
||||
redirect_to new_conference_payment_path, notice: 'Please pay here to purchase tickets.'
|
||||
else
|
||||
redirect_to conference_conference_registration_path(@conference.short_title)
|
||||
end
|
||||
|
|
|
|||
|
|
@ -21,21 +21,31 @@ class Ticket < ActiveRecord::Base
|
|||
ticket_purchases.find_by(user: user, paid: true).present?
|
||||
end
|
||||
|
||||
def quantity_bought_by(user)
|
||||
result = ticket_purchases.where(user_id: user.id).first
|
||||
result ? result.quantity : 0
|
||||
def quantity_bought_by(user, paid)
|
||||
result = ticket_purchases.where(user_id: user.id, paid: paid)
|
||||
quantity = 0
|
||||
if result
|
||||
result.each do |ticket|
|
||||
quantity += ticket.quantity
|
||||
end
|
||||
end
|
||||
quantity
|
||||
end
|
||||
|
||||
def total_price(user)
|
||||
quantity_bought_by(user) * price
|
||||
def unpaid?(user)
|
||||
ticket_purchases.find_by(user: user, paid: false).present?
|
||||
end
|
||||
|
||||
def self.total_price(conference, user)
|
||||
def total_price(user, paid)
|
||||
quantity_bought_by(user, paid) * price
|
||||
end
|
||||
|
||||
def self.total_price(conference, user, paid)
|
||||
tickets = Ticket.where(conference_id: conference.id)
|
||||
result = nil
|
||||
begin
|
||||
tickets.each do |ticket|
|
||||
price = ticket.total_price(user)
|
||||
price = ticket.total_price(user, paid)
|
||||
if result
|
||||
result += price unless price.zero?
|
||||
else
|
||||
|
|
|
|||
|
|
@ -7,10 +7,6 @@ class TicketPurchase < ActiveRecord::Base
|
|||
|
||||
validates_numericality_of :quantity, greater_than: 0
|
||||
|
||||
validates_uniqueness_of :user_id,
|
||||
scope: :ticket_id,
|
||||
message: 'already bought this ticket!'
|
||||
|
||||
delegate :title, to: :ticket
|
||||
delegate :description, to: :ticket
|
||||
delegate :price, to: :ticket
|
||||
|
|
@ -23,7 +19,7 @@ class TicketPurchase < ActiveRecord::Base
|
|||
conference.tickets.each do |ticket|
|
||||
quantity = purchases[ticket.id.to_s].to_i
|
||||
# if the user bought the ticket, just update the quantity
|
||||
if ticket.bought?(user)
|
||||
if ticket.bought?(user) && ticket.unpaid?(user)
|
||||
purchase = update_quantity(conference, quantity, ticket, user)
|
||||
else
|
||||
purchase = purchase_ticket(conference, quantity, ticket, user)
|
||||
|
|
@ -48,7 +44,8 @@ class TicketPurchase < ActiveRecord::Base
|
|||
def self.update_quantity(conference, quantity, ticket, user)
|
||||
purchase = TicketPurchase.where(ticket_id: ticket.id,
|
||||
conference_id: conference.id,
|
||||
user_id: user.id).first
|
||||
user_id: user.id,
|
||||
paid: 'f').first
|
||||
|
||||
purchase.quantity = quantity if quantity > 0
|
||||
purchase
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue