2014-08-28 15:21:05 +02:00
|
|
|
class TicketPurchase < ActiveRecord::Base
|
|
|
|
|
belongs_to :ticket
|
|
|
|
|
belongs_to :user
|
|
|
|
|
belongs_to :conference
|
2017-06-22 18:14:13 +05:30
|
|
|
belongs_to :payment
|
2014-08-28 15:21:05 +02:00
|
|
|
|
|
|
|
|
validates :ticket_id, :user_id, :conference_id, :quantity, presence: true
|
|
|
|
|
|
2017-03-28 21:47:17 +02:00
|
|
|
validates :quantity, numericality: { greater_than: 0 }
|
2014-08-28 15:21:05 +02:00
|
|
|
|
2014-12-05 16:04:34 +01:00
|
|
|
delegate :title, to: :ticket
|
|
|
|
|
delegate :description, to: :ticket
|
|
|
|
|
delegate :price, to: :ticket
|
|
|
|
|
delegate :price_cents, to: :ticket
|
|
|
|
|
delegate :price_currency, to: :ticket
|
|
|
|
|
|
2017-06-03 16:05:59 +05:30
|
|
|
has_many :physical_tickets
|
|
|
|
|
|
2016-07-27 19:41:42 +05:30
|
|
|
scope :paid, -> { where(paid: true) }
|
|
|
|
|
scope :unpaid, -> { where(paid: false) }
|
2017-03-28 21:47:17 +02:00
|
|
|
scope :by_conference, ->(conference) { where(conference_id: conference.id) }
|
|
|
|
|
scope :by_user, ->(user) { where(user_id: user.id) }
|
2016-07-27 19:41:42 +05:30
|
|
|
|
2017-04-19 16:47:59 +03:00
|
|
|
after_create :set_week
|
|
|
|
|
|
2014-08-28 15:21:05 +02:00
|
|
|
def self.purchase(conference, user, purchases)
|
|
|
|
|
errors = []
|
|
|
|
|
ActiveRecord::Base.transaction do
|
|
|
|
|
conference.tickets.each do |ticket|
|
|
|
|
|
quantity = purchases[ticket.id.to_s].to_i
|
2016-08-02 01:37:55 +05:30
|
|
|
# if the user bought the ticket and is still unpaid, just update the quantity
|
2017-07-14 14:12:54 +02:00
|
|
|
purchase = if ticket.bought?(user) && ticket.unpaid?(user)
|
|
|
|
|
update_quantity(conference, quantity, ticket, user)
|
|
|
|
|
else
|
|
|
|
|
purchase_ticket(conference, quantity, ticket, user)
|
|
|
|
|
end
|
2014-08-28 15:21:05 +02:00
|
|
|
|
|
|
|
|
if purchase && !purchase.save
|
|
|
|
|
errors.push(purchase.errors.full_messages)
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
errors.join('. ')
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def self.purchase_ticket(conference, quantity, ticket, user)
|
2016-08-02 01:37:55 +05:30
|
|
|
if quantity > 0
|
2016-11-22 14:43:59 +05:30
|
|
|
purchase = new(ticket_id: ticket.id,
|
|
|
|
|
conference_id: conference.id,
|
|
|
|
|
user_id: user.id,
|
2017-06-03 16:05:59 +05:30
|
|
|
quantity: quantity)
|
|
|
|
|
purchase.pay(nil) if ticket.price_cents.zero?
|
2016-08-02 01:37:55 +05:30
|
|
|
end
|
2014-08-28 15:21:05 +02:00
|
|
|
purchase
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def self.update_quantity(conference, quantity, ticket, user)
|
|
|
|
|
purchase = TicketPurchase.where(ticket_id: ticket.id,
|
|
|
|
|
conference_id: conference.id,
|
2016-07-27 19:41:42 +05:30
|
|
|
user_id: user.id,
|
|
|
|
|
paid: false).first
|
2014-08-28 15:21:05 +02:00
|
|
|
|
|
|
|
|
purchase.quantity = quantity if quantity > 0
|
|
|
|
|
purchase
|
|
|
|
|
end
|
2017-06-03 16:05:59 +05:30
|
|
|
|
|
|
|
|
def pay(payment)
|
|
|
|
|
update_attributes(paid: true, payment: payment)
|
|
|
|
|
PhysicalTicket.transaction do
|
|
|
|
|
quantity.times { physical_tickets.create }
|
|
|
|
|
end
|
|
|
|
|
end
|
2014-08-28 15:21:05 +02:00
|
|
|
end
|
2017-04-19 16:47:59 +03:00
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
|
|
def set_week
|
|
|
|
|
self.week = created_at.strftime('%W')
|
|
|
|
|
save!
|
|
|
|
|
end
|