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-02-12 21:29:55 +00:00
|
|
|
|
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
|
2021-02-12 21:29:55 +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
|
|
|
|
2021-02-12 21:29:55 +00:00
|
|
|
# has_many didn't work here, don't know why offhand.
|
|
|
|
def event_discounts
|
|
|
|
event.event_discounts
|
|
|
|
end
|
|
|
|
|
|
|
|
|
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)
|
2021-02-12 21:29:55 +00:00
|
|
|
init_builder(*expand) do |json|
|
|
|
|
json.(self, :name, :deleted, :order, :limit, :description)
|
2021-01-11 22:34:48 +00:00
|
|
|
json.amount do
|
2021-02-26 20:23:29 +00:00
|
|
|
json.cents amount || 0
|
2021-01-11 22:34:48 +00:00
|
|
|
json.currency event.nonprofit.currency
|
|
|
|
end
|
|
|
|
json.available_to admin_only ? 'admins' : 'everyone'
|
|
|
|
|
2021-04-07 21:43:58 +00:00
|
|
|
json.add_builder_expansion :nonprofit, :event
|
|
|
|
|
|
|
|
json.add_builder_expansion :event_discounts, enum_type: :expandable
|
|
|
|
# if expand.include? :event_discounts
|
|
|
|
# json.event_discounts event_discounts do |disc|
|
|
|
|
# json.merge! disc.to_builder.attributes!
|
|
|
|
# end
|
|
|
|
# else
|
|
|
|
# json.event_discounts event_discounts.pluck(:id)
|
|
|
|
# end
|
2021-01-11 22:34:48 +00:00
|
|
|
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
|