2019-07-30 21:29:24 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2020-06-12 20:03:43 +00:00
|
|
|
# License: AGPL-3.0-or-later WITH WTO-AP-3.0-or-later
|
|
|
|
# Full license explanation at https://github.com/houdiniproject/houdini/blob/master/LICENSE
|
2019-02-01 19:40:24 +00:00
|
|
|
class TicketLevel < ApplicationRecord
|
2021-02-02 21:04:53 +00:00
|
|
|
include Model::Jbuilder
|
|
|
|
include Model::Eventable
|
2019-08-06 14:08:15 +00:00
|
|
|
# :amount, #integer
|
|
|
|
# :amount_dollars, #accessor, string
|
|
|
|
# :name, #string
|
|
|
|
# :description, #text
|
|
|
|
# :quantity, #integer
|
|
|
|
# :deleted, #bool for soft delete
|
|
|
|
# :event_id,
|
|
|
|
# :admin_only, #bool, only admins can create tickets for this level
|
|
|
|
# :limit, #int: for limiting the number of tickets to be sold
|
|
|
|
# :order #int: order in which to be displayed
|
2021-01-11 22:34:48 +00:00
|
|
|
|
|
|
|
# TODO replace with Discard gem
|
|
|
|
define_model_callbacks :discard
|
2018-03-25 17:30:42 +00:00
|
|
|
|
2021-01-11 22:34:48 +00:00
|
|
|
after_discard :publish_delete
|
|
|
|
|
|
|
|
after_create :publish_create
|
|
|
|
after_update :publish_updated
|
|
|
|
|
2018-03-25 17:30:42 +00:00
|
|
|
attr_accessor :amount_dollars
|
|
|
|
|
|
|
|
has_many :tickets
|
|
|
|
belongs_to :event
|
2021-02-02 21:04:53 +00:00
|
|
|
has_one :nonprofit, through: :event
|
|
|
|
has_many :event_discounts, through: :event
|
2018-03-25 17:30:42 +00:00
|
|
|
|
2019-07-30 21:29:24 +00:00
|
|
|
validates :name, presence: true
|
|
|
|
validates :event_id, presence: true
|
2021-02-02 21:04:53 +00:00
|
|
|
|
|
|
|
validate :amount_hasnt_changed_if_has_tickets, on: :update
|
2018-03-25 17:30:42 +00:00
|
|
|
|
2019-07-30 21:29:24 +00:00
|
|
|
scope :not_deleted, -> { where(deleted: [false, nil]) }
|
2018-03-25 17:30:42 +00:00
|
|
|
|
|
|
|
before_validation do
|
2019-07-30 21:29:24 +00:00
|
|
|
self.amount = Format::Currency.dollars_to_cents(amount_dollars) if amount_dollars.present?
|
|
|
|
self.amount = 0 if amount.nil?
|
2018-03-25 17:30:42 +00:00
|
|
|
end
|
2021-01-11 22:34:48 +00:00
|
|
|
|
|
|
|
# TODO replace with discard gem
|
|
|
|
def discard!
|
|
|
|
run_callbacks(:discard) do
|
|
|
|
self.deleted = true
|
|
|
|
save!
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def to_builder(*expand)
|
|
|
|
Jbuilder.new do |json|
|
|
|
|
json.(self, :id, :name, :deleted, :order, :limit, :description)
|
|
|
|
json.object 'ticket_level'
|
|
|
|
json.amount do
|
|
|
|
json.value_in_cents amount || 0
|
|
|
|
json.currency event.nonprofit.currency
|
|
|
|
end
|
|
|
|
json.available_to admin_only ? 'admins' : 'everyone'
|
|
|
|
if event
|
|
|
|
if event.nonprofit
|
|
|
|
if expand.include? :nonprofit
|
|
|
|
json.nonprofit event.nonprofit.to_builder
|
|
|
|
else
|
|
|
|
json.nonprofit event.nonprofit.id
|
|
|
|
end
|
|
|
|
else
|
|
|
|
json.nonprofit nil
|
|
|
|
end
|
|
|
|
|
|
|
|
if expand.include? :event
|
|
|
|
json.event event.to_builder
|
|
|
|
else
|
|
|
|
json.event event.id
|
|
|
|
end
|
|
|
|
|
|
|
|
if expand.include? :event_discounts
|
|
|
|
json.event_discounts event.event_discounts do |disc|
|
2021-01-13 22:59:04 +00:00
|
|
|
json.merge! disc.to_builder.attributes!
|
2021-01-11 22:34:48 +00:00
|
|
|
end
|
|
|
|
else
|
|
|
|
json.event_discounts event.event_discounts.pluck(:id)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
def publish_create
|
2021-01-13 22:59:04 +00:00
|
|
|
Houdini.event_publisher.announce(:ticket_level_created, to_event('ticket_level.created', :event, :nonprofit, :event_discounts).attributes!)
|
2021-01-11 22:34:48 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def publish_updated
|
|
|
|
# we don't run update when we've really just discarded
|
|
|
|
unless deleted
|
2021-01-13 22:59:04 +00:00
|
|
|
Houdini.event_publisher.announce(:ticket_level_updated, to_event('ticket_level.updated', :event, :nonprofit, :event_discounts).attributes!)
|
2021-01-11 22:34:48 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def publish_delete
|
2021-01-13 22:59:04 +00:00
|
|
|
Houdini.event_publisher.announce(:ticket_level_deleted, to_event('ticket_level.deleted', :event, :nonprofit, :event_discounts).attributes!)
|
2021-01-11 22:34:48 +00:00
|
|
|
end
|
2021-02-02 21:04:53 +00:00
|
|
|
|
|
|
|
def amount_hasnt_changed_if_has_tickets
|
|
|
|
if tickets.any?
|
|
|
|
console.log("YOU can't change amount if tickets already use this #{ticket_level.id}. Please create a new level")
|
|
|
|
#errors.add(:amount, "can't change amount if tickets already use this level. Please create a new level")
|
|
|
|
end
|
|
|
|
end
|
2018-03-25 17:30:42 +00:00
|
|
|
end
|