Skip to content

Commit

Permalink
Allow coupon codes to be added from admin adjustments index.
Browse files Browse the repository at this point in the history
  • Loading branch information
heisenbugged authored and radar committed Oct 21, 2013
1 parent 3bd0aa9 commit 28c7ba3
Show file tree
Hide file tree
Showing 8 changed files with 111 additions and 5 deletions.
16 changes: 16 additions & 0 deletions backend/app/assets/javascripts/admin/adjustments.js.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
$(@).ready( ->
$('[data-hook=adjustments_new_coupon_code] #add_coupon_code').click ->
return if $("#coupon_code").val().length == 0
$.ajax
type: 'PUT'
url: Spree.url(Spree.routes.orders_api + '/' + order_number + '/apply_coupon_code.json');
data:
coupon_code: $("#coupon_code").val()
success: ->
window.location.reload();
error: (msg) ->
if msg.responseJSON["error"]
show_flash_error msg.responseJSON["error"]
else
show_flash_error "There was a problem adding this coupon code."
)
4 changes: 4 additions & 0 deletions backend/app/assets/stylesheets/admin/sections/_orders.scss
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,7 @@
}
}
}

[data-hook="adjustments_new_coupon_code"] {
margin-bottom: 18px;
}
12 changes: 11 additions & 1 deletion backend/app/views/spree/admin/adjustments/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,21 @@
<i class="icon-arrow-right"></i> <%= Spree.t(:adjustments) %>
<% end %>

<% content_for :page_actions do %>
<% content_for :page_actions do %>
<li><%= button_link_to Spree.t(:new_adjustment), new_admin_order_adjustment_url(@order), :icon => 'icon-plus' %></li>
<li><%= button_link_to Spree.t(:back_to_orders_list), admin_orders_path, :icon => 'icon-arrow-left' %></li>
<% end %>

<%= render :partial => 'adjustments_table' %>

<% if @order.can_add_coupon? %>
<div data-hook="adjustments_new_coupon_code">
<%= text_field_tag "coupon_code", "", :placeholder => Spree.t(:coupon_code) %>
<%= button Spree.t(:add_coupon_code), 'icon-plus', 'submit', :id => "add_coupon_code" %>
</div>
<% end %>
<%= javascript_tag do -%>
var order_number = '<%= @order.number %>';
<% end -%>

<%= button_link_to Spree.t(:continue), @order.cart? ? new_admin_order_payment_url(@order) : admin_orders_url, :icon => 'icon-arrow-right' %>
51 changes: 51 additions & 0 deletions backend/spec/features/admin/orders/adjustments_promotions_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
require 'spec_helper'

describe "Adjustments Promotions" do
stub_authorization!

before(:each) do
promotion = create(:promotion_with_item_adjustment,
:name => "$10 off",
:path => 'test',
:code => "10_off",
:starts_at => 1.day.ago,
:expires_at => 1.day.from_now,
:adjustment_rate => 10)

order = create(:order_with_totals)
line_item = order.line_items.first
# so we can be sure of a determinate price in our assertions
line_item.update_column(:price, 10)

visit spree.admin_order_adjustments_path(order)
end

context "admin adding a promotion" do
context "successfully" do
it "should create a new adjustment", :js => true do
fill_in "coupon_code", :with => "10_off"
click_button "Add Coupon Code"
page.should have_content("$10 off")
page.should have_content("-10.00")
end
end

context "for non-existing promotion" do
it "should show an error message", :js => true do
fill_in "coupon_code", :with => "does_not_exist"
click_button "Add Coupon Code"
page.should have_content("doesn't exist.")
end
end

context "for already applied promotion" do
it "should show an error message", :js => true do
2.times {
fill_in "coupon_code", :with => "10_off"
click_button "Add Coupon Code"
}
page.should have_content("already been applied")
end
end
end
end
11 changes: 8 additions & 3 deletions core/app/models/spree/order.rb
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ def exclude_tax?
def tax_address
Spree::Config[:tax_using_ship_address] ? ship_address : bill_address
end

def updater
@updater ||= OrderUpdater.new(self)
end
Expand Down Expand Up @@ -446,6 +446,11 @@ def coupon_code=(code)
@coupon_code = code.strip.downcase rescue nil
end

def can_add_coupon?
Spree::Promotion.order_activatable?(self)
end


def shipped?
%w(partial shipped).include?(shipment_state)
end
Expand All @@ -466,7 +471,7 @@ def create_proposed_shipments
#
# At some point the might need to force the order to transition from address
# to delivery again so that proper updated shipments are created.
# e.g. customer goes back from payment step and changes order items
# e.g. customer goes back from payment step and changes order items
def ensure_updated_shipments
if shipments.any?
self.shipments.destroy_all
Expand All @@ -482,7 +487,7 @@ def shipping_eq_billing_address?
(bill_address.empty? && ship_address.empty?) || bill_address.same_as?(ship_address)
end


def set_shipments_cost
shipments.each(&:update_amounts)
updater.update_shipment_total
Expand Down
6 changes: 5 additions & 1 deletion core/app/models/spree/promotion.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,13 @@ def rules_are_eligible?(promotable, options = {})
end
end

def order_activatable?(order)
def self.order_activatable?(order)
order && !UNACTIVATABLE_ORDER_STATES.include?(order.state)
end
def order_activatable?(order)
self.class.order_activatable?(order)
end


# Products assigned to all product rules
def products
Expand Down
1 change: 1 addition & 0 deletions core/config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,7 @@ en:
add: Add
add_action_of_type: Add action of type
add_country: Add Country
add_coupon_code: Add Coupon Code
add_new_header: Add New Header
add_new_style: Add New Style
add_one: Add One
Expand Down
15 changes: 15 additions & 0 deletions core/lib/spree/testing_support/factories/promotion_factory.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,20 @@
FactoryGirl.define do
factory :promotion, class: Spree::Promotion do
name 'Promo'

factory :promotion_with_item_adjustment do
ignore do
adjustment_rate 10
end

after(:create) do |promotion, evaluator|
calculator = Spree::Calculator::FlatRate.new
calculator.preferred_amount = evaluator.adjustment_rate
action = Spree::Promotion::Actions::CreateItemAdjustments.create(:calculator => calculator)
promotion.actions << action
promotion.save
end
end

end
end

0 comments on commit 28c7ba3

Please sign in to comment.