modify controllers and models to just save paid ticket purchases

This commit is contained in:
Rishabh Saxena 2016-06-18 18:25:20 +05:30
parent 900a1b32a8
commit 81ef41be8b
4 changed files with 25 additions and 17 deletions

View file

@ -28,8 +28,10 @@ class ConferenceRegistrationsController < ApplicationController
end end
def show 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) @tickets = current_user.ticket_purchases.where(conference_id: @conference.id)
@ticket_payments = @tickets.group_by(&:payment_id)
end end
def edit; end def edit; end

View file

@ -6,9 +6,8 @@ class TicketPurchasesController < ApplicationController
def create def create
message = TicketPurchase.purchase(@conference, current_user, params[:tickets][0]) message = TicketPurchase.purchase(@conference, current_user, params[:tickets][0])
if message.blank? if message.blank?
if current_user.ticket_purchases.any? if current_user.ticket_purchases.where(paid: 'f').any?
redirect_to conference_conference_registration_path(@conference.short_title), redirect_to new_conference_payment_path, notice: 'Please pay here to purchase tickets.'
notice: "Thank you for supporting #{@conference.title} by purchasing a ticket."
else else
redirect_to conference_conference_registration_path(@conference.short_title) redirect_to conference_conference_registration_path(@conference.short_title)
end end

View file

@ -21,21 +21,31 @@ class Ticket < ActiveRecord::Base
ticket_purchases.find_by(user: user, paid: true).present? ticket_purchases.find_by(user: user, paid: true).present?
end end
def quantity_bought_by(user) def quantity_bought_by(user, paid)
result = ticket_purchases.where(user_id: user.id).first result = ticket_purchases.where(user_id: user.id, paid: paid)
result ? result.quantity : 0 quantity = 0
if result
result.each do |ticket|
quantity += ticket.quantity
end
end
quantity
end end
def total_price(user) def unpaid?(user)
quantity_bought_by(user) * price ticket_purchases.find_by(user: user, paid: false).present?
end 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) tickets = Ticket.where(conference_id: conference.id)
result = nil result = nil
begin begin
tickets.each do |ticket| tickets.each do |ticket|
price = ticket.total_price(user) price = ticket.total_price(user, paid)
if result if result
result += price unless price.zero? result += price unless price.zero?
else else

View file

@ -7,10 +7,6 @@ class TicketPurchase < ActiveRecord::Base
validates_numericality_of :quantity, greater_than: 0 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 :title, to: :ticket
delegate :description, to: :ticket delegate :description, to: :ticket
delegate :price, to: :ticket delegate :price, to: :ticket
@ -23,7 +19,7 @@ class TicketPurchase < ActiveRecord::Base
conference.tickets.each do |ticket| conference.tickets.each do |ticket|
quantity = purchases[ticket.id.to_s].to_i quantity = purchases[ticket.id.to_s].to_i
# if the user bought the ticket, just update the quantity # 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) purchase = update_quantity(conference, quantity, ticket, user)
else else
purchase = purchase_ticket(conference, quantity, ticket, user) purchase = purchase_ticket(conference, quantity, ticket, user)
@ -48,7 +44,8 @@ class TicketPurchase < ActiveRecord::Base
def self.update_quantity(conference, quantity, ticket, user) def self.update_quantity(conference, quantity, ticket, user)
purchase = TicketPurchase.where(ticket_id: ticket.id, purchase = TicketPurchase.where(ticket_id: ticket.id,
conference_id: conference.id, conference_id: conference.id,
user_id: user.id).first user_id: user.id,
paid: 'f').first
purchase.quantity = quantity if quantity > 0 purchase.quantity = quantity if quantity > 0
purchase purchase