Skip to content

Commit

Permalink
fixing admin order creation/checkout
Browse files Browse the repository at this point in the history
  • Loading branch information
Zac Williams committed Sep 24, 2010
1 parent c1ba4e4 commit b303504
Show file tree
Hide file tree
Showing 15 changed files with 377 additions and 71 deletions.
73 changes: 62 additions & 11 deletions core/app/controllers/admin/checkout_controller.rb
@@ -1,23 +1,74 @@
class Admin::CheckoutController < Admin::BaseController

helper :checkout
before_filter :load_data
before_filter :load_order

def update
if @order.update_attributes(params[:order])
if @order.completed?
redirect_to admin_checkout_url(@order)
else
redirect_to edit_admin_order_shipment_url(@order, @order.shipment)
begin
if @order.update_attributes(object_params)
if @order.next
state_callback(:after)
if @order.state == "complete"
flash[:notice] = I18n.t(:order_processed_successfully)
redirect_to admin_order_path(@order) and return
else
redirect_to admin_checkout_state_path(@order.state) and return
end
end
end
else
render :edit
rescue Spree::GatewayError
flash[:error] = I18n.t(:payment_processing_failed)
redirect_to admin_checkout_state_path('payment')
return
end
render :edit
end

private
def load_data
@order = Order.find_by_number(params[:number])

def object_params
# For payment step, filter order parameters to produce the expected nested attributes for a single payment and its source, discarding attributes for payment methods other than the one selected
if @order.payment?
if params[:payment_source].present? && source_params = params.delete(:payment_source)[params[:order][:payments_attributes].first[:payment_method_id].underscore]
params[:order][:payments_attributes].first[:source_attributes] = source_params
end
if (params[:order][:payments_attributes])
params[:order][:payments_attributes].first[:amount] = @order.total
end
end
params[:order]
end

def load_order
@order = current_order
@order.state = params[:state] if params[:state]
state_callback(:before)
end

def before_address
@order.bill_address ||= Address.new(:country => default_country)
@order.ship_address ||= Address.new(:country => default_country)
end

def before_delivery
@order.shipping_method ||= (@order.rate_hash.first && @order.rate_hash.first[:shipping_method])
end

def before_payment
current_order.payments.destroy_all if request.put?
end

def after_complete
session[:order_id] = nil
end

def default_country
Country.find Spree::Config[:default_country_id]
end

def state_callback(before_or_after = :before)
method_name = :"#{before_or_after}_#{@order.state}"
send(method_name) if respond_to?(method_name, true)
end

end
12 changes: 8 additions & 4 deletions core/app/controllers/admin/orders_controller.rb
Expand Up @@ -9,16 +9,20 @@ class Admin::OrdersController < Admin::BaseController
update do
flash nil
wants.html do
if @order.bill_address.nil? || @order.ship_address.nil?
redirect_to edit_admin_order_url(@order)
if !@order.line_items.empty?
unless @order.complete?
redirect_to admin_checkout_path
else
redirect_to admin_order_path(@order)
end
else
redirect_to admin_order_url(@order)
render :action => :new
end
end
end

def new
@order = Order.create
@order = current_order(true)
end

def fire
Expand Down
3 changes: 2 additions & 1 deletion core/app/models/order.rb
Expand Up @@ -48,7 +48,7 @@ def to_param
end

def completed?
!! completed_at
complete?
end

# Indicates whether or not the user is allowed to proceed to checkout. Currently this is implemented as a
Expand All @@ -67,6 +67,7 @@ def item_count
state_machine :initial => 'cart', :use_transactions => false do

event :next do
transition :from => 'cart', :to => 'address'
transition :from => 'address', :to => 'delivery'
transition :from => 'delivery', :to => 'payment'
transition :from => 'payment', :to => 'confirm'
Expand Down
6 changes: 6 additions & 0 deletions core/app/views/admin/checkout/_address.html.erb
@@ -0,0 +1,6 @@
<%= render 'billing_fields', :form => form %>
<%= render 'shipping_fields', :form => form %>

<p class="form-buttons">
<%= button t('continue') %>
</p>
91 changes: 91 additions & 0 deletions core/app/views/admin/checkout/_billing_fields.html.erb
@@ -0,0 +1,91 @@
<fieldset id='billing'>
<%= form.fields_for :bill_address do |bill_form| %>

<legend><%= t("billing_address")%></legend>

<table>
<tr>
<td>
<%= bill_form.text_field :firstname %><span class="req">*</span>
<%= bill_form.label :firstname, t(:first_name) %>
</td>
<td>
<%= bill_form.text_field :lastname %><span class="req">*</span>
<%= bill_form.label :lastname, t(:last_name) %>
</td>
</tr>

<tr>
<td>
<%= bill_form.text_field :address1 %><span class="req">*</span>
<%= bill_form.label :address1, t(:street_address) %>
</td>
<td>
<%= bill_form.text_field :address2 %>
<%= bill_form.label :address2, t(:street_address_2) %>
</td>
</tr>

<tr>
<td>
<%= bill_form.text_field :city %><span class="req">*</span>
<%= bill_form.label :city, t(:city) %>
</td>
<td>
<%= bill_form.text_field :zipcode %><span class="req">*</span>
<%= bill_form.label :zipcode, t(:zip) %>
</td>
</tr>
<% if Spree::Config[:address_requires_state] %>
<tr>
<td>
<span id="bstate">
<% have_states = !@order.bill_address.country.states.empty? %>

<noscript>
<%= bill_form.text_field :state_name %>
</noscript>
<% state_elements = [
bill_form.collection_select(:state_id, @order.bill_address.country.states,
:id, :name,
{:include_blank => true},
{:class => have_states ? "" : "hidden",
:disabled => !have_states}) +
bill_form.text_field(:state_name,
:class => !have_states ? "" : "hidden",
:disabled => have_states)
].join.gsub('"', "'").gsub("\n", "")
%>
<script type="text/javascript" language="javascript" charset="utf-8">
// <![CDATA[
document.write("<%= raw state_elements %>");
// ]]>
</script>
</span>
<%= bill_form.label :state, t(:state) %> <span class="req">*</span>
</td>
</tr>
<% end %>

<tr>
<td>
<span id="bcountry"><%= bill_form.collection_select :country_id, available_countries, :id, :name, {} %></span>
<%= bill_form.label :country_id, t(:country) %>
</td>
</tr>

<tr>
<td>
<%= bill_form.label :phone, t(:phone) %>
<%= bill_form.text_field :phone %><span class="req">*</span>
</td>
<% if Spree::Config[:alternative_billing_phone] %>
<td>
<%= bill_form.label :alternative_phone, t(:alternative_phone) %>
<%= bill_form.text_field :alternative_phone %>
</td>
<% end %>
</tr>
</table>
<% end %>
</fieldset>
33 changes: 33 additions & 0 deletions core/app/views/admin/checkout/_cart.html.erb
@@ -0,0 +1,33 @@

<h3><%= t("customer_details") %></h3>
<fieldset>
<legend><%= t('account') %></legend>
<table class="index">
<tbody>
<tr>
<td class="lbl-col"><%= form.label :email, t(:email)+':' %></td>
<td class="val-col" colspan="3"><%= form.text_field :email, :class => "fullwidth" %></td>
<td class="lbl-col"><label><%= t('guest_checkout') %>:</label></td>
<td class="val-col" colspan="3">
<% unless @order.state == "complete" %>
<label class="sub">
<%= radio_button_tag :guest_checkout, true, @order.user.nil? %>
<%= t("yes") %>
</label> &nbsp;
<label class="sub">
<%= radio_button_tag :guest_checkout, false, !@order.user.nil?, :disabled => @order.state == "in_progress" %>
<%= t("no") %>
</label>
<%= hidden_field_tag :user_id, @order.user_id %>
<% else %>
<%= @order.user.nil? ? "Yes" : "No" %>
<% end %>
</td>
</tr>
</tbody>
</table>
</fieldset>

<p class="form-buttons">
<%= button t('continue') %>
</p>
11 changes: 11 additions & 0 deletions core/app/views/admin/checkout/_confirm.html.erb
@@ -0,0 +1,11 @@
<fieldset id="order_details">
<div class="clear"></div>
<legend><%= t("confirm") %></legend>
<%= render :partial => 'shared/order_details', :locals => {:order => @order} %>
</fieldset>

<hr />

<p class="form-buttons">
<%= button t('place_order') %>
</p>
22 changes: 22 additions & 0 deletions core/app/views/admin/checkout/_delivery.html.erb
@@ -0,0 +1,22 @@
<fieldset id='shipping_method'>
<legend><%= t("shipping_method") %></legend>

<% @order.rate_hash.each do |shipping_method| %>
<label>
<%= radio_button(:order, :shipping_method_id, shipping_method[:id]) %>
<%= shipping_method[:name] %> <%= number_to_currency shipping_method[:cost] %>
</label><br />
<% end %>
<% if Spree::Config[:shipping_instructions] && @available_methods.present? %>
<p id="minstrs">
<%= checkout_form.label :special_instructions, t("shipping_instructions") %><br />
<%= checkout_form.text_area :special_instructions, :cols => 40, :rows => 7 %>
</p>
<% end %>

</fieldset>

<p class="form-buttons">
<%= button t('continue') %>
</p>
42 changes: 0 additions & 42 deletions core/app/views/admin/checkout/_form.html.erb

This file was deleted.

27 changes: 27 additions & 0 deletions core/app/views/admin/checkout/_payment.html.erb
@@ -0,0 +1,27 @@
<fieldset id="payment">
<legend><%= t("payment_information") %></legend>

<% @order.available_payment_methods.each do |method| %>
<p>
<label>
<%= radio_button_tag "order[payments_attributes][][payment_method_id]", method.id, method == @order.payment_method %>
<%= t(method.name, :scope => :payment_methods, :default => method.name) %>
</label>
</p>
<% end %>

<ul >
<% @order.available_payment_methods.each do |method| %>
<li>

<%= render "checkout/payment/#{method.method_type}", :payment_method => method %>

</li>
<% end %>
</ul>

</fieldset>

<p class="form-buttons">
<%= button t('continue') %>
</p>

0 comments on commit b303504

Please sign in to comment.