app/controllers/sales_controller.rb in artfully_ose-1.2.0 vs app/controllers/sales_controller.rb in artfully_ose-1.3.0.pre1
- old
+ new
@@ -6,58 +6,47 @@
def show
redirect_to new_event_show_sales_path(@event,@show,:render => 'boxoffice')
end
def new
+ authorize! :manage, Sale
@person = Person.new
@sale = Sale.new(@show, @show.chart.ticket_types.box_office, current_box_office_cart, {})
@tickets_remaining = tickets_remaining
+ @show_discount_field, @discount_caption = discount_field
setup_defaults
response.headers["Cache-Control"] = "no-cache, no-store, max-age=0, must-revalidate" # http://stackoverflow.com/a/5493543/2063202
end
def create
+ authorize! :manage, Sale
current_box_office_cart.clear!
+ handler = OrderHandler.new(current_box_office_cart, nil)
@sale = Sale.new(@show, @show.chart.ticket_types.box_office, current_box_office_cart, params[:quantities], params[:order_notes])
- # handle donation
- if params[:donation].present? && params[:donation].to_i > 0
- donation = Donation.new
- donation.amount = params[:donation].to_i * 100
- donation.organization_id = @event.organization_id
- current_box_office_cart.donations << donation
- end
+ #
+ # OrderHandler calls should really be pushed down into Sale
+ #
+ handler.handle_donation(params, @current_user.current_organization)
+ handler.handle_discount_or_pass_code(params)
+ discount_error = handler.error unless handler.error.blank?
- # handle discount
- begin
- discount = Discount.find_by_code_and_event_id(params[:discount].upcase, @event)
- discount.apply_discount_to_cart(current_box_office_cart)
- rescue RuntimeError => e
- discount_error = e.message
- rescue NoMethodError => e
- discount_error = "We could not find your discount. Please try again." if params[:discount].present?
+ if checking_out? && @sale.sell(payment)
+ prep_messages
+ subscribe_user_to_list
end
- if checking_out?
- if @sale.sell(payment)
- @sale.message = "Sold #{self.class.helpers.pluralize(@sale.tickets.length, 'ticket')}. Order total was #{self.class.helpers.number_as_cents @sale.order.total}"
-
- if params[:auto_check_in].present?
- @sale.tickets.map {|t| t.reload; t.validate_ticket!(current_user)}
- end
- end
- end
-
- unless @sale.errors.empty?
+ if @sale.errors.present?
@sale.error = "#{@sale.errors.full_messages.to_sentence.capitalize}."
flash[:error] = @sale.error
Ticket.unlock(@sale.tickets, @sale.cart)
render :js => "window.location = '#{new_event_show_sales_path(@event,@show,:render => 'boxoffice')}'"
return
end
+
render :json => @sale.as_json
.merge(:total => @sale.cart.total)
.merge(:tickets_remaining => tickets_remaining)
.merge(:door_list_rows => door_list_rows)
.merge(:discount_error => discount_error)
@@ -93,10 +82,51 @@
render :partial => "sales/doorlist"
end
private
+ def prep_messages
+ str = "Order submitted successfully."
+ str += "<br/>Sold #{self.class.helpers.pluralize(@sale.tickets.length, 'ticket')}."
+ str += "<br/>Order total was #{self.class.helpers.number_as_cents @sale.order.total}."
+ str += "<br/>Order number is <a target='_blank' href='#{Rails.application.routes.url_helpers.order_path(@sale.order)}'>#{@sale.order.id}</a>."
+
+ if @sale.order.person.email.blank?
+ str += "<br/>No confirmation email was sent because we don't have an email on file for this patron."
+ else
+ str += "<br/>A confirmation email was sent to #{@sale.order.person.email}."
+ end
+ @sale.message = str
+ if params[:auto_check_in].present?
+ @sale.tickets.map {|t| t.reload; t.validate_ticket!(current_user)}
+ end
+ end
+
+ def discount_field
+ discount_active = Discount.where(:event_id => @event.id).any?
+ pass_active = EventsPassType.where(:event_id => @event.id).any?
+ pass_kit = @current_user.current_organization.has_kit?(:passes)
+
+ discount_caption = ""
+ show_discount_field = false
+
+ if discount_active && pass_kit && pass_active
+ discount_caption = "Discount or Pass Code"
+ show_discount_field = true
+ elsif pass_kit && pass_active
+ discount_caption = "Pass Code"
+ show_discount_field = true
+ elsif discount_active
+ discount_caption = "Discount Code"
+ show_discount_field = true
+ else
+ discount_caption = ""
+ end
+
+ [show_discount_field, discount_caption]
+ end
+
# TODO: this should be pulled into TicketTypeSerializer
def tickets_remaining
remaining = {}
@sale.ticket_types.each do |ticket_type|
remaining[ticket_type.id] = ticket_type.available("box_office")
@@ -134,31 +164,28 @@
def find_dummy
@dummy = Person.dummy_for(current_user.current_organization)
end
def person
- return @person unless @person.nil?
+ return @_p if @_p
- #if there's a person_id, use find
- @person = Person.find(params[:person][:id]) unless params[:person][:id].blank?
-
- if user_entered_nothing?
- @person = @dummy
- else
- @person = Person.first_or_create(person_attributes)
+ @_p = Person.first_or_create(person_attributes)
+
+ ActiveRecord::Base.transaction do
+ @_p.phones.find_or_create_by_number_and_person_id(params[:person][:phones_attributes].first[:number], @_p.id) do |phone|
+ phone.person_id = @_p.id
+ phone.number = params[:person][:phones_attributes].first[:number]
+ end
end
-
- @person
+ @_p
end
-
+
def person_attributes
{
- :id => params[:person][:id],
:first_name => params[:person][:first_name],
:last_name => params[:person][:last_name],
:email => params[:person][:email],
- :phones_attributes => params[:person][:phones_attributes],
:organization => current_organization
}
end
def user_entered_nothing?
@@ -191,6 +218,11 @@
def has_card_info?
params[:credit_card].present? and params[:credit_card][:card_number].present?
end
+ def subscribe_user_to_list
+ if params[:subscribe] && person
+ person.subscribe_to_default_mailchimp_list!
+ end
+ end
end