Class: Admin::OrdersController

Inherits:
BaseController
  • Object
show all
Defined in:
app/controllers/admin/orders_controller.rb

Instance Method Summary collapse

Instance Method Details

#editObject



44
45
46
47
# File 'app/controllers/admin/orders_controller.rb', line 44

def edit
  load_order
  respond_with(@order)
end

#fireObject



93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'app/controllers/admin/orders_controller.rb', line 93

def fire
  # TODO - possible security check here but right now any admin can before any transition (and the state machine
  # itself will make sure transitions are not applied in the wrong state)
  event = params[:e]
  if @order.send("#{event}")
    flash.notice = t('order_updated')
  else
    flash[:error] = t('cannot_perform_operation')
  end
rescue Spree::GatewayError => ge
  flash[:error] = "#{ge.message}"
ensure
  respond_with(@order) { |format| format.html { redirect_to :back } }
end

#indexObject



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'app/controllers/admin/orders_controller.rb', line 9

def index
  params[:search] ||= {}
  params[:search][:completed_at_is_not_null] ||= '1' if Spree::Config[:show_only_complete_orders_by_default]
  @show_only_completed = params[:search][:completed_at_is_not_null].present?
  params[:search][:meta_sort] ||= @show_only_completed ? 'completed_at.desc' : 'created_at.desc'

  @search = Order.metasearch(params[:search])

  if !params[:search][:created_at_greater_than].blank?
    params[:search][:created_at_greater_than] = Time.zone.parse(params[:search][:created_at_greater_than]).beginning_of_day rescue ""
  end

  if !params[:search][:created_at_less_than].blank?
    params[:search][:created_at_less_than] = Time.zone.parse(params[:search][:created_at_less_than]).end_of_day rescue ""
  end

  if @show_only_completed
    params[:search][:completed_at_greater_than] = params[:search].delete(:created_at_greater_than)
    params[:search][:completed_at_less_than] = params[:search].delete(:created_at_less_than)
  end

  @orders = Order.metasearch(params[:search]).includes([:user, :shipments, :payments]).page(params[:page]).per(Spree::Config[:orders_per_page])
  respond_with(@orders)
end

#newObject



39
40
41
42
# File 'app/controllers/admin/orders_controller.rb', line 39

def new
  @order = Order.create
  respond_with(@order)
end

#resendObject



108
109
110
111
112
113
# File 'app/controllers/admin/orders_controller.rb', line 108

def resend
  OrderMailer.confirm_email(@order, true).deliver
  flash.notice = t('order_email_resent')

  respond_with(@order) { |format| format.html { redirect_to :back } }
end

#showObject



34
35
36
37
# File 'app/controllers/admin/orders_controller.rb', line 34

def show
  load_order
  respond_with(@order)
end

#updateObject



49
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/admin/orders_controller.rb', line 49

def update
  return_path = nil
  load_order
  if @order.update_attributes(params[:order]) && @order.line_items.present?
    unless @order.complete?
      if params[:order].key?(:email)
        shipping_method = @order.available_shipping_methods(:front_end).first
        if shipping_method
          @order.shipping_method = shipping_method

          if params[:guest_checkout] == 'false' && params[:user_id].present?
            @order.user_id = params[:user_id]
            @order.user true
          end
          @order.save
          @order.create_shipment!
          return_path = edit_admin_order_shipment_path(@order, @order.shipment)
        else
          flash[:error] = t('errors.messages.no_shipping_methods_available')
          return_path = user_admin_order_path(@order)
        end
      else
        return_path = user_admin_order_path(@order)
      end

    else
      return_path = admin_order_path(@order)
    end
  else
    @order.errors.add(:line_items, t('errors.messages.blank'))
  end

  respond_with(@order) do |format|
    format.html do
      if return_path
        redirect_to return_path
      else
        render :action => :edit
      end
    end
  end
end

#userObject



115
116
117
118
# File 'app/controllers/admin/orders_controller.rb', line 115

def user
  @order.build_bill_address(:country_id => Spree::Config[:default_country_id]) if @order.bill_address.nil?
  @order.build_ship_address(:country_id => Spree::Config[:default_country_id]) if @order.ship_address.nil?
end