mirror of
https://github.com/openSUSE/osem.git
synced 2026-04-24 08:52:16 -04:00
modify ticket and ticket_purchases models
This commit is contained in:
@@ -18,24 +18,27 @@ class Ticket < ActiveRecord::Base
|
||||
end
|
||||
|
||||
def paid?(user)
|
||||
ticket_purchases.find_by(user: user, paid: true).present?
|
||||
ticket_purchases.paid.by_user(user).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: false)
|
||||
ticket_purchases.by_user(user).where(paid: paid).sum(:quantity)
|
||||
end
|
||||
|
||||
def total_price(user)
|
||||
quantity_bought_by(user) * price
|
||||
def unpaid?(user)
|
||||
ticket_purchases.unpaid.by_user(user).present?
|
||||
end
|
||||
|
||||
def self.total_price(conference, user)
|
||||
def total_price(user, paid: false)
|
||||
quantity_bought_by(user, paid: paid) * price
|
||||
end
|
||||
|
||||
def self.total_price(conference, user, paid: false)
|
||||
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: paid)
|
||||
if result
|
||||
result += price unless price.zero?
|
||||
else
|
||||
|
||||
@@ -7,23 +7,24 @@ 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
|
||||
delegate :price_cents, to: :ticket
|
||||
delegate :price_currency, to: :ticket
|
||||
|
||||
scope :paid, -> { where(paid: true) }
|
||||
scope :unpaid, -> { where(paid: false) }
|
||||
scope :by_conference, -> (conference) { where(conference_id: conference.id) }
|
||||
scope :by_user, -> (user) { where(user_id: user.id) }
|
||||
|
||||
def self.purchase(conference, user, purchases)
|
||||
errors = []
|
||||
ActiveRecord::Base.transaction do
|
||||
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 +49,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: false).first
|
||||
|
||||
purchase.quantity = quantity if quantity > 0
|
||||
purchase
|
||||
|
||||
Reference in New Issue
Block a user