Allow editing of completed orders from confirmation page where distributor allows it

This commit is contained in:
Rob Harrington
2017-03-30 17:34:39 +11:00
parent 768240a5ba
commit eec3a21c89
10 changed files with 118 additions and 16 deletions

View File

@@ -1,8 +1,8 @@
$ ->
if ($ 'form#update-cart').is('*')
($ 'form#update-cart a.delete').show().one 'click', ->
($ this).parents('.line-item').first().find('input.line_item_quantity').val 0
($ this).parents('form').first().submit()
if $('form#update-cart').is('*') || $('form#update-order').is('*')
$('form#update-cart a.delete, form#update-order a.delete').show().one 'click', ->
$(this).parents('.line-item').first().find('input.line_item_quantity').val 0
$(this).parents('form').first().submit()
false
($ 'form#update-cart').submit ->

View File

@@ -32,7 +32,7 @@ Spree::OrdersController.class_eval do
def update
@insufficient_stock_lines = []
@order = current_order
@order = order_to_update
unless @order
flash[:error] = t(:order_not_found)
redirect_to root_path and return
@@ -49,6 +49,8 @@ Spree::OrdersController.class_eval do
if params.has_key?(:checkout)
@order.next_transition.run_callbacks if @order.cart?
redirect_to checkout_state_path(@order.checkout_steps.first)
elsif @order.complete?
redirect_to order_path(@order)
else
redirect_to cart_path
end
@@ -204,8 +206,8 @@ Spree::OrdersController.class_eval do
end
def order_to_update
order = Spree::Order.complete.find_by_id(params[:order].andand[:id])
return order if order.present? && can?(:update, order)
order = Spree::Order.complete.find_by_number(params[:id])
return order if order.andand.editable? && can?(:update, order)
current_order
end
end

View File

@@ -289,6 +289,10 @@ Spree::Order.class_eval do
user_id == user.id && distributor.andand.allow_order_changes? && order_cycle.andand.open?
end
def editable?
distributor.andand.allow_order_changes? && order_cycle.andand.open?
end
private
def shipping_address_from_distributor

View File

@@ -21,7 +21,7 @@
= render :partial => 'line_item', :locals => { :variant => item_form.object.variant, :line_item => item_form.object, :item_form => item_form }
%tfoot#edit-cart
= render 'spree/orders/form/cart_actions_row'
= render 'spree/orders/form/cart_actions_row' unless @order.complete?
/ This is the fees row which we want to replace with the pop-over
-# - unless @order.adjustments.eligible.blank?

View File

@@ -17,7 +17,7 @@
= render 'spree/shared/line_item_name', line_item: line_item
- if @insufficient_stock_lines.include? line_item
- if @insufficient_stock_lines.andand.include? line_item
%span.out-of-stock
= variant.in_stock? ? t(:insufficient_stock, :on_hand => variant.on_hand) : t(:out_of_stock)
%br/

View File

@@ -12,7 +12,7 @@
%span= t(:total)
%tbody{"data-hook" => ""}
- order.line_items.each do |item|
%tr{"data-hook" => "order_details_line_item_row"}
%tr.line_item{"data-hook" => "order_details_line_item_row", class: "variant-#{item.variant.id}" }
%td(data-hook = "order_item_description")
%div.item-thumb-image{"data-hook" => "order_item_image"}

View File

@@ -3,3 +3,13 @@
= link_to main_app.shop_path, :class => "button expand" do
%i.ofn-i_008-caret-left
= t(:back_to_store)
- if order.editable?
.columns.show-for-medium-up.medium-6  
-# .columns.small-12.medium-3  
-# = link_to "#", method: :delete, :class => "button secondary expand" do
-# %i.ofn-i_009-close
-# =t(:cancel_order)
.columns.small-12.medium-3
= button_tag :class => 'button primary radius expand', :id => 'update-button' do
%i.ofn-i_051-check-big
= t(:save_changes)

View File

@@ -86,5 +86,9 @@
%br
.row
.columns.large-12
= render partial: 'spree/orders/summary'
= render 'spree/orders/form/update_buttons'
= form_for order, html: {id: 'update-order'} do |order_form|
- if order.editable?
= render 'spree/orders/form', order_form: order_form
-else
= render 'spree/orders/summary', order: order
= render 'spree/orders/form/update_buttons', order: order

View File

@@ -195,7 +195,7 @@ describe Spree::OrdersController do
let!(:current_order) { double(:current_order) }
let!(:order) { create(:order) }
let(:li) { order.line_items.first }
let(:params) { { order: {} } }
let(:params) { { } }
before do
allow(controller).to receive(:current_order) { current_order }
@@ -210,7 +210,7 @@ describe Spree::OrdersController do
context "when an order_id is given in params" do
before do
params[:order].merge!({id: order.id})
params.merge!({id: order.number})
end
context "and the order is not complete" do
@@ -235,8 +235,24 @@ describe Spree::OrdersController do
context "and the user has permission to 'update' the order" do
before { allow(controller).to receive(:can?).with(:update, order) { true } }
it "returns the order" do
expect(controller.send(:order_to_update)).to eq order
context "and the order is not editable" do
it "returns the current_order" do
expect(controller.send(:order_to_update)).to eq current_order
end
end
context "and the order is editable" do
let(:order_cycle) { create(:simple_order_cycle) }
let(:distributor) { create(:enterprise, allow_order_changes: true) }
before do
order.update_attributes(order_cycle_id: order_cycle.id, distributor_id: distributor.id)
end
it "returns the order" do
expect(controller.send(:order_to_update)).to eq order
end
end
end
end

View File

@@ -0,0 +1,66 @@
require 'spec_helper'
feature "Order Management", js: true do
include AuthenticationWorkflow
describe "editing a completed order" do
let(:address) { create(:address) }
let(:user) { create(:user, bill_address: address, ship_address: address) }
let(:distributor) { create(:distributor_enterprise, with_payment_and_shipping: true, charges_sales_tax: true) }
let(:order_cycle) { create(:order_cycle) }
let(:order) { create(:completed_order_with_totals, order_cycle: order_cycle, distributor: distributor, user: user, bill_address: address, ship_address: address) }
let!(:item1) { order.reload.line_items.first }
let!(:item2) { create(:line_item, order: order) }
let!(:item3) { create(:line_item, order: order) }
before do
order.update_attributes(shipping_method_id: distributor.shipping_methods.first.id)
order.reload.save
quick_login_as user
end
context "when the distributor doesn't allow changes to be made to orders" do
before do
order.distributor.update_attributes(allow_order_changes: false)
end
it "doesn't show form elements for editing the order" do
visit spree.order_path(order)
expect(find("tr.variant-#{item1.variant.id}")).to have_content item1.product.name
expect(find("tr.variant-#{item2.variant.id}")).to have_content item2.product.name
expect(find("tr.variant-#{item3.variant.id}")).to have_content item3.product.name
expect(page).to_not have_button I18n.t(:save_changes)
end
end
context "when the distributor allows changes to be made to orders" do
before do
order.distributor.update_attributes(allow_order_changes: true)
end
it "shows already ordered line items" do
visit spree.order_path(order)
within "tr.variant-#{item1.variant.id}" do
expect(page).to have_content item1.product.name
expect(page).to have_field 'order_line_items_attributes_0_quantity'
fill_in 'order_line_items_attributes_0_quantity', with: 2
end
expect(find("tr.variant-#{item2.variant.id}")).to have_content item2.product.name
expect(find("tr.variant-#{item3.variant.id}")).to have_content item3.product.name
click_button I18n.t(:save_changes)
expect(find(".order-total.grand-total")).to have_content "$40.00"
expect(item1.reload.quantity).to eq 2
within "tr.variant-#{item2.variant.id}" do
click_link "delete_line_item_#{item2.id}"
end
expect(find(".order-total.grand-total")).to have_content "$30.00"
expect(Spree::LineItem.find_by_id(item2.id)).to be nil
end
end
end
end