From 28c7ba3ffdd122d1888704304dd523185eb31256 Mon Sep 17 00:00:00 2001 From: James Strong Date: Thu, 10 Oct 2013 19:35:00 -0500 Subject: [PATCH] Allow coupon codes to be added from admin adjustments index. Fixes #3859 --- .../javascripts/admin/adjustments.js.coffee | 16 ++++++ .../stylesheets/admin/sections/_orders.scss | 4 ++ .../spree/admin/adjustments/index.html.erb | 12 ++++- .../orders/adjustments_promotions_spec.rb | 51 +++++++++++++++++++ core/app/models/spree/order.rb | 11 ++-- core/app/models/spree/promotion.rb | 6 ++- core/config/locales/en.yml | 1 + .../factories/promotion_factory.rb | 15 ++++++ 8 files changed, 111 insertions(+), 5 deletions(-) create mode 100644 backend/app/assets/javascripts/admin/adjustments.js.coffee create mode 100644 backend/spec/features/admin/orders/adjustments_promotions_spec.rb diff --git a/backend/app/assets/javascripts/admin/adjustments.js.coffee b/backend/app/assets/javascripts/admin/adjustments.js.coffee new file mode 100644 index 00000000000..ec11ee53209 --- /dev/null +++ b/backend/app/assets/javascripts/admin/adjustments.js.coffee @@ -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." +) \ No newline at end of file diff --git a/backend/app/assets/stylesheets/admin/sections/_orders.scss b/backend/app/assets/stylesheets/admin/sections/_orders.scss index e8f4d796922..ec2a32025d4 100644 --- a/backend/app/assets/stylesheets/admin/sections/_orders.scss +++ b/backend/app/assets/stylesheets/admin/sections/_orders.scss @@ -52,3 +52,7 @@ } } } + +[data-hook="adjustments_new_coupon_code"] { + margin-bottom: 18px; +} \ No newline at end of file diff --git a/backend/app/views/spree/admin/adjustments/index.html.erb b/backend/app/views/spree/admin/adjustments/index.html.erb index c2972414a86..13c5a99e602 100644 --- a/backend/app/views/spree/admin/adjustments/index.html.erb +++ b/backend/app/views/spree/admin/adjustments/index.html.erb @@ -4,11 +4,21 @@ <%= Spree.t(:adjustments) %> <% end %> -<% content_for :page_actions do %> +<% content_for :page_actions do %>
  • <%= button_link_to Spree.t(:new_adjustment), new_admin_order_adjustment_url(@order), :icon => 'icon-plus' %>
  • <%= button_link_to Spree.t(:back_to_orders_list), admin_orders_path, :icon => 'icon-arrow-left' %>
  • <% end %> <%= render :partial => 'adjustments_table' %> +<% if @order.can_add_coupon? %> +
    + <%= text_field_tag "coupon_code", "", :placeholder => Spree.t(:coupon_code) %> + <%= button Spree.t(:add_coupon_code), 'icon-plus', 'submit', :id => "add_coupon_code" %> +
    +<% 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' %> diff --git a/backend/spec/features/admin/orders/adjustments_promotions_spec.rb b/backend/spec/features/admin/orders/adjustments_promotions_spec.rb new file mode 100644 index 00000000000..cfddb8d0744 --- /dev/null +++ b/backend/spec/features/admin/orders/adjustments_promotions_spec.rb @@ -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 \ No newline at end of file diff --git a/core/app/models/spree/order.rb b/core/app/models/spree/order.rb index 4f1db8bc7b1..b967bbbc1e2 100644 --- a/core/app/models/spree/order.rb +++ b/core/app/models/spree/order.rb @@ -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 @@ -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 @@ -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 @@ -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 diff --git a/core/app/models/spree/promotion.rb b/core/app/models/spree/promotion.rb index a8cdb4d90a0..c0965e1d25e 100644 --- a/core/app/models/spree/promotion.rb +++ b/core/app/models/spree/promotion.rb @@ -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 diff --git a/core/config/locales/en.yml b/core/config/locales/en.yml index ed62ce74b0a..8899c3c92d6 100644 --- a/core/config/locales/en.yml +++ b/core/config/locales/en.yml @@ -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 diff --git a/core/lib/spree/testing_support/factories/promotion_factory.rb b/core/lib/spree/testing_support/factories/promotion_factory.rb index b43f559dca5..fc12648311b 100644 --- a/core/lib/spree/testing_support/factories/promotion_factory.rb +++ b/core/lib/spree/testing_support/factories/promotion_factory.rb @@ -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