app/controllers/sales_controller.rb in artfully_ose-1.2.0.pre.24 vs app/controllers/sales_controller.rb in artfully_ose-1.2.0.pre.26

- old
+ new

@@ -1,47 +1,74 @@ class SalesController < ArtfullyOseController include CartFinder before_filter :find_event, :find_show, :find_people, :find_dummy - before_filter :create_door_list, :only => ['show', 'new'] + before_filter :create_door_list, :only => ['show', 'new', 'door_list'] def show - redirect_to new_event_show_sales_path(@event, @show) + redirect_to new_event_show_sales_path(@event,@show,:render => 'boxoffice') end def new @person = Person.new @sale = Sale.new(@show, @show.chart.ticket_types.box_office, current_box_office_cart, {}) @tickets_remaining = tickets_remaining setup_defaults + response.headers["Cache-Control"] = "no-cache, no-store, max-age=0, must-revalidate" # http://stackoverflow.com/a/5493543/2063202 end def create current_box_office_cart.clear! - @sale = Sale.new(@show, @show.chart.ticket_types.box_office, current_box_office_cart, params[:quantities]) + + @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 + + # 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? + 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.cart.total}" + @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? @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 :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(:door_list_rows => door_list_rows) + .merge(:discount_error => discount_error) + .merge(:discount_amount => current_box_office_cart.discount_amount), :status => 200 end def checking_out? - !params[:commit].blank? + params[:commit].present? end def door_list_rows door_list_rows = [] @@ -59,10 +86,15 @@ end end door_list_rows end + def door_list + # create_door_list + render :partial => "sales/doorlist" + end + private # TODO: this should be pulled into TicketTypeSerializer def tickets_remaining remaining = {} @@ -118,15 +150,16 @@ @person end def person_attributes { - :id => params[:person][:id], - :first_name => params[:person][:first_name], - :last_name => params[:person][:last_name], - :email => params[:person][:email], - :organization => current_organization + :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? params[:person][:id].blank? && @@ -144,10 +177,15 @@ params[:credit_card][:year] = swiped_data.track1.expiration_year end params[:benefactor] = current_user - payment = Payment.create(params[:payment_method], params) + # set payment as cash if total is 0 (e.g. free tickets) + if params[:payment_method].blank? && params[:total] == '0' + payment = Payment.create('cash', params) + else + payment = Payment.create(params[:payment_method], params) + end payment.customer = person payment end def has_card_info?