Class: OrderController

Inherits:
ApplicationController
  • Object
show all
Includes:
Ruleby
Defined in:
app/controllers/order_controller.rb

Instance Method Summary collapse

Instance Method Details

#add_addressObject



69
70
71
72
73
# File 'app/controllers/order_controller.rb', line 69

def add_address
  render(:update) do |page|
    page.replace_html("order", :partial => 'form_address')
  end
end

#back_addressesObject

form to addresses



101
102
103
104
105
106
# File 'app/controllers/order_controller.rb', line 101

def back_addresses
  render(:update) do |page|
    page.replace_html("order", :partial => 'new')
    page.replace_html("order_address_#{@address.class.to_s}", display_address(@address)) if @address
  end
end

#change_addressObject

Change select_tag address



109
110
111
112
113
114
115
# File 'app/controllers/order_controller.rb', line 109

def change_address
  @address = Address.find_by_id(params[:id])
  render(:update) do |page|
    page.replace_html("order_address_#{@address.class.to_s}", display_address(@address))
    page.visual_effect :highlight, "order_address_#{@address.class.to_s}"
  end
end

#confirmationObject

Final step, this action is accessible only if session[:order_confirmation] is true



51
52
53
54
# File 'app/controllers/order_controller.rb', line 51

def confirmation
  redirect_to(:action => 'payment') unless session[:order_confirmation]
  session[:order_confirmation] = nil
end

#createObject



60
61
62
63
64
65
66
67
# File 'app/controllers/order_controller.rb', line 60

def create
  valid = valid_shipment(false)
  return false unless valid
  @order = Order.from_cart(current_cart)
  flash[:notice] = I18n.t('thank_you').capitalize
  session[:order_confirmation] = true
  redirect_to(:action => 'payment')
end

#create_addressObject

Make or update Address and back to addresses



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'app/controllers/order_controller.rb', line 126

def create_address
  @address = Address.find_by_id(params[:id])
  if @address
    @address.update_attributes(params[:address])
  else
    if current_user.addresses.size == 0
      [AddressDelivery, AddressInvoice].each do |type|
        current_user.addresses << type.create(params[:address])
      end
    else
      #type = (params[:address][:kind] == 'AddressDelivery') ? AddressDelivery : AddressInvoice
      current_user.addresses << AddressDelivery.create(params[:address])
      current_user.addresses << AddressInvoice.create(params[:address])
    end
  end
  back_addresses
end

#informationsObject

Subscribe or edit user’s informations Link render with partials of user’s views session is initialize with => ‘order’, :action => ‘new’



46
47
48
# File 'app/controllers/order_controller.rb', line 46

def informations
  session[:return_to] = {:controller => 'order', :action => 'new'}
end

#newObject



56
57
58
# File 'app/controllers/order_controller.rb', line 56

def new
  return redirect_to(:action => 'informations') unless current_user
end

#paymentObject

Payment integration



38
39
40
41
# File 'app/controllers/order_controller.rb', line 38

def payment
  # TODO - include ActiveMerchant
  #valid_shipment(false)
end

#update_addressObject

Go to form address



118
119
120
121
122
123
# File 'app/controllers/order_controller.rb', line 118

def update_address
  @address = Address.find_by_id(params[:id])
  render(:update) do |page|
    page.replace_html("order", :partial => 'form_address', :locals => { :address => @address })
  end
end

#update_totalObject



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'app/controllers/order_controller.rb', line 75

def update_total
  #special_offer
  transporter_price = 0
  if current_cart.transporter
    offer_delivery = false
    transporter_price = current_cart.transporter.variables.to_f unless offer_delivery and current_cart.transporter.nil?
  end

  order_total = current_cart.total + transporter_price

  render(:update) do |page|
    #page.replace_html('order_total_price', "#{order_total} #{current_currency.html}")
    page.replace_html('transporter_price', "#{current_cart.total + transporter_price} #{current_currency.html}")
    page.visual_effect :highlight, 'transporter_price'
    page.visual_effect :highlight, 'order_total_price'
  end
end

#update_transporterObject



93
94
95
96
97
98
# File 'app/controllers/order_controller.rb', line 93

def update_transporter
  @transporter_rule = TransporterRule.find_by_id(params[:id])
  current_cart.options[:transporter_rule_id] = @transporter_rule.id if @transporter_rule
  shipping_methods
  update_total
end

#valid_shipment(action = true) ⇒ Object

Save in session address_invoice_id and address_delivery_id. Returns false if miss an address or if shipping_method is not validate by user, returns true else



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'app/controllers/order_controller.rb', line 13

def valid_shipment(action=true)
  current_cart.options[:address_invoice_id] = params[:address_invoice_id] if params[:address_invoice_id]
  current_cart.options[:address_delivery_id] = params[:address_delivery_id] if params[:address_delivery_id]

  # AddressInvoice and AddressDelivery is obligatory for valid an order
  if !current_cart.address_invoice || !current_cart.address_delivery
    flash[:warning] = I18n.t('address', :count=>2).capitalize
    redirect_to(:action => 'new')
    return false
  end

  current_cart.options[:transporter_rule_id] = params[:transporter_rule_id] if params[:transporter_rule_id]

  unless current_cart.transporter or current_cart.free_shipping
    flash[:warning] = I18n.t('shipping_method',:count=>1).capitalize
    redirect_to(:action => 'new')
    return false
  end

  current_cart.save
  redirect_to(:action => 'payment') if action
  return true
end