Class: OrdersController

Inherits:
ArtfullyOseController show all
Defined in:
app/controllers/orders_controller.rb

Instance Method Summary collapse

Instance Method Details

#indexObject



2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'app/controllers/orders_controller.rb', line 2

def index
  authorize! :manage, Order
  request.format = :csv if params[:commit] == "Download"
  if params[:search]
    @results = search(params[:search]).sort{|a,b| b.created_at <=> a.created_at }
    if @results.length == 1
      redirect_to order_path(@results.first.id)
    end
  else
    @results = current_organization.orders.includes(:person, :items).all.sort{|a,b| b.created_at <=> a.created_at }
  end
  respond_to do |format|
    format.html { @results = @results.paginate(:page => params[:page], :per_page => 25) }
    format.csv do
      filename = "Artfully-Orders-Export-#{DateTime.now.strftime("%m-%d-%y")}.csv"
      send_data @results.to_comma, :filename => filename, :type => "text/csv", :disposition => "attachment"
    end
  end
end

#membershipObject



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'app/controllers/orders_controller.rb', line 50

def membership
  authorize! :view, Order

  @organization = current_organization
  @membership_type  = MembershipType.find_by_id(params[:membership_type_id]) if params[:membership_type_id].present?
  @membership_types = @organization.membership_types

  request.format = :csv if params[:commit] == "Download"

  search_terms = {
    :start        => params[:start],
    :stop         => params[:stop],
    :organization => @organization,
    :membership_type => @membership_type
  }

  @search = MembershipSaleSearch.new(search_terms) do |results|
    results.paginate(:page => params[:page], :per_page => 25)
  end

  respond_to do |format|
    format.html
    format.csv do
      filename = "Artfully-Membership-Sales-Export-#{DateTime.now.strftime("%m-%d-%y")}.csv"
      items = ItemView.where(:product_type => "Membership")
                           .where('items_view.created_at > ? ', @search.start)
                           .where('items_view.created_at < ?',  @search.stop)
                           .where('items_view.organization_id = ?', current_organization)
                           .order('items_view.created_at desc')
      if @membership_type
        items = items.joins(:item).
          joins("INNER JOIN memberships ON (items.product_type = #{::Item.sanitize('Membership')} AND items.product_id = memberships.id)").
          joins("INNER JOIN membership_types ON (membership_types.id = memberships.membership_type_id)").
          where(:membership_types => {:id => @membership_type}).
          group('items_view.order_id')
      end
      csv_string = items.all.to_comma(:membership_sale)
      send_data csv_string, :filename => filename, :type => "text/csv", :disposition => "attachment"
    end
  end
end

#passesObject



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'app/controllers/orders_controller.rb', line 92

def passes
  authorize! :view, Order

  @organization = current_organization
  @pass_type  = PassType.find_by_id(params[:pass_type_id]) if params[:pass_type_id].present?
  @pass_types = @organization.pass_types

  request.format = :csv if params[:commit] == "Download"

  search_terms = {
    :start        => params[:start],
    :stop         => params[:stop],
    :organization => @organization,
    :pass_type    => @pass_type
  }

  @search = PassSaleSearch.new(search_terms) do |results|
    results.paginate(:page => params[:page], :per_page => 25)
  end

  respond_to do |format|
    format.html
    format.csv do
      filename = "Artfully-Pass-Sales-Export-#{DateTime.now.strftime("%m-%d-%y")}.csv"
      items = ItemView.where(:product_type => "Pass")
                           .where('items_view.created_at > ? ', @search.start)
                           .where('items_view.created_at < ?',  @search.stop)
                           .where('items_view.organization_id = ?', current_organization)
                           .order('items_view.created_at desc')
      if @pass_type
        items = items.joins(:item).
          joins("INNER JOIN passes ON (items.product_type = #{::Item.sanitize('Pass')} AND items.product_id = passes.id)").
          joins("INNER JOIN pass_types ON (pass_types.id = passes.pass_type_id)").
          where(:pass_types => {:id => @pass_type}).
          group('items_view.order_id')
      end
      csv_string = items.all.to_comma(:pass_sale)
      send_data csv_string, :filename => filename, :type => "text/csv", :disposition => "attachment"
    end
  end
end

#resendObject



41
42
43
44
45
46
47
48
# File 'app/controllers/orders_controller.rb', line 41

def resend
  authorize! :view, Order
  @order = Order.find(params[:id])
  OrderMailer.delay.confirmation_for(@order)
  
  flash[:notice] = "A copy of the order receipt has been sent to #{@order.person.email}"
  redirect_to order_url(@order)
end

#salesObject



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'app/controllers/orders_controller.rb', line 134

def sales
  authorize! :view, Order

  @organization = current_organization
  @event = Event.find_by_id(params[:event_id]) if params[:event_id].present?
  @events = @organization.events.sort
  @show = @event.shows.find_by_id(params[:show_id]) if @event && params[:show_id].present?
  @shows = @event.shows if @event
  request.format = :csv if params[:commit] == "Download"

  search_terms = {
    :start        => params[:start],
    :stop         => params[:stop],
    :organization => @organization,
    :event        => @event,
    :show         => @show
  }

  respond_to do |format|
    format.html do
      @search = SaleSearch.new(search_terms) do |results|
        results.paginate(:page => params[:page], :per_page => 25)
      end
    end
    format.csv do
      filename = "Artfully-Ticket-Sales-Export-#{DateTime.now.strftime("%m-%d-%y")}.csv"
      items = ItemView.where(:product_type => "Ticket")
                           .where('created_at > ? ', search_terms[:start])
                           .where('created_at < ?',  Sundial.midnightish(current_organization, search_terms[:stop]))
                           .where('organization_id = ?', current_organization)
                           .order('created_at desc')
      items = items.where('event_id = ?', @event.id)  if @event
      items = items.where('show_id = ?', @show.id)    if @show
      csv_string = items.all.to_comma(:ticket_sale)
      send_data csv_string, :filename => filename, :type => "text/csv", :disposition => "attachment"
    end
  end
end

#showObject



22
23
24
25
26
27
# File 'app/controllers/orders_controller.rb', line 22

def show
  @order = Order.includes(:items => :discount).find(params[:id])
  authorize! :view, @order
  @person = Person.find(@order.person_id)
  @total = @order.total
end

#updateObject



29
30
31
32
33
34
35
36
37
38
39
# File 'app/controllers/orders_controller.rb', line 29

def update
  @order = Order.find(params[:id])
  authorize! :manage, Order

  if @order.update_attributes(:notes => params[:order][:notes])
    flash[:success] = "Successfully updated order #{@order.id}."
  else
    flash[:error] = "Could not update order #{@order.id}."
  end
  redirect_to order_path(@order)
end