From a8b51afb8e33a98b116eb88940ce0bde0aeec4be Mon Sep 17 00:00:00 2001 From: Jordan Brough Date: Wed, 2 Jul 2014 10:58:11 -0600 Subject: [PATCH] Rework ReturnAuthorization-InventoryUnit relationship * Create a join table: `ReturnAuthorizationInventoryUnit` * Allows inventory units to be part of multiple RMAs (e.g., if an initial RMA was cancelled) while still retaining historical data * Migrate existing data into the new table * Drop the `inventory_units.return_authorization_id` column * Start using nested attributes for adding/removing InventoryUnits to ReturnAuthorizations * Remove the `API::ReturnAuthorizationsController#add` endpoint (use nested attributes instead) * Pave the way for exchanges with an `exchange_variant_id` field * Update the admin interface for `Admin::ReturnAuthorizationsController`: * No longer group inventory units by variant. This will be important for allowing per-unit data, such as for exchanges Created by @richardnuno and @jordan-brough Fixes #4907 --- .../api/return_authorizations_controller.rb | 10 --- .../return_authorizations_controller_spec.rb | 29 ------- .../admin/return_authorizations_controller.rb | 22 ++++-- .../return_authorizations/_form.html.erb | 69 +++++++++-------- .../admin/return_authorizations/new.html.erb | 2 +- .../return_authorizations_controller_spec.rb | 76 ++++++++++++++++++- core/app/models/spree/inventory_unit.rb | 2 + core/app/models/spree/return_authorization.rb | 51 +++---------- .../return_authorization_inventory_unit.rb | 31 ++++++++ ...ree_return_authorization_inventory_unit.rb | 52 +++++++++++++ core/lib/spree/permitted_attributes.rb | 2 +- .../factories/return_authorization_factory.rb | 5 ++ .../factories/shipment_factory.rb | 1 + ...eturn_authorization_inventory_unit_spec.rb | 55 ++++++++++++++ .../models/spree/return_authorization_spec.rb | 53 ++++--------- 15 files changed, 293 insertions(+), 167 deletions(-) create mode 100644 core/app/models/spree/return_authorization_inventory_unit.rb create mode 100644 core/db/migrate/20140702140656_create_spree_return_authorization_inventory_unit.rb create mode 100644 core/spec/models/spree/return_authorization_inventory_unit_spec.rb diff --git a/api/app/controllers/spree/api/return_authorizations_controller.rb b/api/app/controllers/spree/api/return_authorizations_controller.rb index 7448ee940e..502a358861 100644 --- a/api/app/controllers/spree/api/return_authorizations_controller.rb +++ b/api/app/controllers/spree/api/return_authorizations_controller.rb @@ -45,16 +45,6 @@ def update end end - def add - @return_authorization = order.return_authorizations.accessible_by(current_ability, :update).find(params[:id]) - @return_authorization.add_variant params[:variant_id].to_i, params[:quantity].to_i - if @return_authorization.valid? - respond_with @return_authorization, default_template: :show - else - invalid_resource!(@return_authorization) - end - end - def receive @return_authorization = order.return_authorizations.accessible_by(current_ability, :update).find(params[:id]) if @return_authorization.receive diff --git a/api/spec/controllers/spree/api/return_authorizations_controller_spec.rb b/api/spec/controllers/spree/api/return_authorizations_controller_spec.rb index cdbfa4671d..34efc5b1a8 100644 --- a/api/spec/controllers/spree/api/return_authorizations_controller_spec.rb +++ b/api/spec/controllers/spree/api/return_authorizations_controller_spec.rb @@ -105,35 +105,6 @@ module Spree json_response.should have_attributes(attributes) end - it "can add an inventory unit to a return authorization on the order" do - FactoryGirl.create(:return_authorization, :order => order) - return_authorization = order.return_authorizations.first - inventory_unit = return_authorization.returnable_inventory.first - inventory_unit.should be - return_authorization.inventory_units.should be_empty - api_put :add, :id => return_authorization.id, variant_id: inventory_unit.variant.id, quantity: 1 - response.status.should == 200 - json_response.should have_attributes(attributes) - return_authorization.reload.inventory_units.should_not be_empty - end - - it "can mark a return authorization as received on the order with an inventory unit" do - FactoryGirl.create(:new_return_authorization, :order => order, :stock_location_id => order.shipments.first.stock_location.id) - return_authorization = order.return_authorizations.first - return_authorization.state.should == "authorized" - - # prep (use a rspec context or a factory instead?) - inventory_unit = return_authorization.returnable_inventory.first - inventory_unit.should be - return_authorization.inventory_units.should be_empty - api_put :add, :id => return_authorization.id, variant_id: inventory_unit.variant.id, quantity: 1 - # end prep - - api_delete :receive, :id => return_authorization.id - response.status.should == 200 - return_authorization.reload.state.should == "received" - end - it "cannot mark a return authorization as received on the order with no inventory units" do FactoryGirl.create(:new_return_authorization, :order => order) return_authorization = order.return_authorizations.first diff --git a/backend/app/controllers/spree/admin/return_authorizations_controller.rb b/backend/app/controllers/spree/admin/return_authorizations_controller.rb index 8126fbd03e..46d23fee14 100644 --- a/backend/app/controllers/spree/admin/return_authorizations_controller.rb +++ b/backend/app/controllers/spree/admin/return_authorizations_controller.rb @@ -2,9 +2,7 @@ module Spree module Admin class ReturnAuthorizationsController < ResourceController belongs_to 'spree/order', :find_by => :number - - update.after :associate_inventory_units - create.after :associate_inventory_units + before_filter :load_return_authorization_inventory_units, except: [:fire, :destroy, :index] def fire @return_authorization.send("#{params[:e]}!") @@ -12,10 +10,20 @@ def fire redirect_to :back end - protected - def associate_inventory_units - (params[:return_quantity] || []).each { |variant_id, qty| @return_authorization.add_variant(variant_id.to_i, qty.to_i) } - end + private + + # To satisfy how nested attributes works we want to create placeholder ReturnAuthorizationInventoryUnits for + # any InventoryUnits that have not already been added to the ReturnAuthorization. + def load_return_authorization_inventory_units + all_inventory_unit_ids = @return_authorization.order.inventory_units.map(&:id) + rma_inventory_unit_ids = @return_authorization.return_authorization_inventory_units.map(&:inventory_unit_id) + + new_ids = all_inventory_unit_ids - rma_inventory_unit_ids + new_units = new_ids.map { |new_id| Spree::ReturnAuthorizationInventoryUnit.new(inventory_unit_id: new_id) } + + @form_return_authorization_inventory_units = (@return_authorization.return_authorization_inventory_units + new_units).sort_by(&:inventory_unit_id) + end + end end end diff --git a/backend/app/views/spree/admin/return_authorizations/_form.html.erb b/backend/app/views/spree/admin/return_authorizations/_form.html.erb index 7a6869d053..5f08773065 100644 --- a/backend/app/views/spree/admin/return_authorizations/_form.html.erb +++ b/backend/app/views/spree/admin/return_authorizations/_form.html.erb @@ -2,33 +2,29 @@ + - - - - + + - <% @return_authorization.order.line_items.each do |line_item| %> - <% next if line_item.inventory_units.shipped.none? %> - - - - - - + + + + <% end %> @@ -36,11 +32,11 @@ <%= f.field_container :amount do %> <%= f.label :amount, Spree.t(:amount) %> *
- <% if @return_authorization.updatable? %> + <% if @return_authorization.received? %> + <%= @return_authorization.display_amount %> + <% else %> <%= f.text_field :amount, {:style => 'width:80px;'} %> <%= Spree.t(:rma_value) %>: 0.00 <%= f.error_message_on :amount %> - <% else %> - <%= @return_authorization.display_amount %> <% end %> <% end %> @@ -58,23 +54,26 @@ diff --git a/backend/app/views/spree/admin/return_authorizations/new.html.erb b/backend/app/views/spree/admin/return_authorizations/new.html.erb index fa7649f5e6..74bd36a1d4 100644 --- a/backend/app/views/spree/admin/return_authorizations/new.html.erb +++ b/backend/app/views/spree/admin/return_authorizations/new.html.erb @@ -14,7 +14,7 @@ <%= render :partial => 'form', :locals => { :f => f } %>
- <%= button Spree.t(:continue), 'arrow-right' %> + <%= button Spree.t(:create), 'ok' %> <%= Spree.t(:or) %> <%= button_link_to Spree.t('actions.cancel'), admin_order_return_authorizations_url(@order), :icon => 'remove' %>
diff --git a/backend/spec/controllers/spree/admin/return_authorizations_controller_spec.rb b/backend/spec/controllers/spree/admin/return_authorizations_controller_spec.rb index 02701e2f88..aa2f10c810 100644 --- a/backend/spec/controllers/spree/admin/return_authorizations_controller_spec.rb +++ b/backend/spec/controllers/spree/admin/return_authorizations_controller_spec.rb @@ -4,9 +4,79 @@ stub_authorization! # Regression test for #1370 #3 - let!(:order) { create(:order) } + let!(:order) { create(:shipped_order, line_items_count: 3) } + let(:inventory_unit_1) { order.inventory_units.order('id asc')[0] } + let(:inventory_unit_2) { order.inventory_units.order('id asc')[1] } + let(:inventory_unit_3) { order.inventory_units.order('id asc')[2] } + + let(:params) do + { + order_id: order.to_param, + return_authorization: {amount: 0.0, reason: ""}, + } + end + it "can create a return authorization" do - spree_post :create, :order_id => order.to_param, :return_authorization => { :amount => 0.0, :reason => "" } - response.should be_success + spree_post :create, params + response.should redirect_to spree.admin_order_return_authorizations_path(order) + end + + context 'update' do + let(:return_authorization) { create(:return_authorization, order: order) } + let(:params) do + super().merge({ + id: return_authorization.to_param, + return_authorization: {amount: 0.0, reason: ""}.merge(inventory_units_params), + }) + end + + subject { spree_put :update, params } + + context "adding an item" do + let(:inventory_units_params) do + { + return_authorization_inventory_units_attributes: { + '0' => {inventory_unit_id: inventory_unit_1.to_param}, + } + } + end + + context 'without existing items' do + it 'creates a new unit' do + expect { subject }.to change { Spree::ReturnAuthorizationInventoryUnit.count }.by(1) + end + end + + context 'with existing items' do + let!(:return_authorization_inventory_unit) { + create(:return_authorization_inventory_unit, return_authorization: return_authorization, inventory_unit: inventory_unit_1) + } + + it 'does not create new units' do + expect { subject }.to_not change { Spree::ReturnAuthorizationInventoryUnit.count } + expect(assigns[:return_authorization].errors['return_authorization_inventory_units.inventory_unit']).to eq ["has already been taken"] + end + end + end + + context "removing an item" do + let!(:return_authorization_inventory_unit) { + create(:return_authorization_inventory_unit, return_authorization: return_authorization, inventory_unit: inventory_unit_1) + } + + let(:inventory_units_params) do + { + return_authorization_inventory_units_attributes: { + '0' => {id: return_authorization_inventory_unit.to_param, _destroy: '1'}, + } + } + end + + context 'with existing items' do + it 'removes the unit' do + expect { subject }.to change { Spree::ReturnAuthorizationInventoryUnit.count }.by(-1) + end + end + end end end diff --git a/core/app/models/spree/inventory_unit.rb b/core/app/models/spree/inventory_unit.rb index 03e23303bb..2d6a0cb5e4 100644 --- a/core/app/models/spree/inventory_unit.rb +++ b/core/app/models/spree/inventory_unit.rb @@ -6,6 +6,8 @@ class InventoryUnit < Spree::Base belongs_to :return_authorization, class_name: "Spree::ReturnAuthorization" belongs_to :line_item, class_name: "Spree::LineItem", inverse_of: :inventory_units + has_many :return_authorization_inventory_units, inverse_of: :inventory_unit + scope :backordered, -> { where state: 'backordered' } scope :on_hand, -> { where state: 'on_hand' } scope :shipped, -> { where state: 'shipped' } diff --git a/core/app/models/spree/return_authorization.rb b/core/app/models/spree/return_authorization.rb index 349652b9fa..c5ef465e99 100644 --- a/core/app/models/spree/return_authorization.rb +++ b/core/app/models/spree/return_authorization.rb @@ -2,15 +2,18 @@ module Spree class ReturnAuthorization < Spree::Base belongs_to :order, class_name: 'Spree::Order' - has_many :inventory_units + has_many :return_authorization_inventory_units, inverse_of: :return_authorization, dependent: :destroy + has_many :inventory_units, through: :return_authorization_inventory_units has_many :refunds belongs_to :stock_location before_create :generate_number before_validation :force_positive_amount + accepts_nested_attributes_for :return_authorization_inventory_units, allow_destroy: true + validates :order, presence: true validates :amount, numericality: { greater_than_or_equal_to: 0 } - validate :must_have_shipped_units + validate :must_have_shipped_units, on: :create state_machine initial: :authorized do after_transition to: :received, do: :process_return @@ -49,36 +52,8 @@ def display_amount Spree::Money.new(amount, { currency: currency }) end - def add_variant(variant_id, quantity) - order_units = returnable_inventory.group_by(&:variant_id) - returned_units = inventory_units.group_by(&:variant_id) - return false if order_units.empty? - - count = 0 - - if returned_units[variant_id].nil? || returned_units[variant_id].size < quantity - count = returned_units[variant_id].nil? ? 0 : returned_units[variant_id].size - - order_units[variant_id].each do |inventory_unit| - next unless inventory_unit.return_authorization.nil? && count < quantity - - inventory_unit.return_authorization = self - inventory_unit.save! - - count += 1 - end - elsif returned_units[variant_id].size > quantity - (returned_units[variant_id].size - quantity).times do |i| - returned_units[variant_id][i].return_authorization_id = nil - returned_units[variant_id][i].save! - end - end - - order.authorize_return! if inventory_units.reload.size > 0 && !order.awaiting_return? - end - def returnable_inventory - order.shipped_shipments.collect{|s| s.inventory_units.to_a}.flatten + order.inventory_units.shipped end # Used when Adjustment#update! wants to update the related adjustmenrt @@ -111,7 +86,9 @@ def process_refund private def must_have_shipped_units - errors.add(:order, Spree.t(:has_no_shipped_units)) if order.nil? || !order.shipped_shipments.any? + if order.nil? || order.inventory_units.shipped.none? + errors.add(:order, Spree.t(:has_no_shipped_units)) + end end def generate_number @@ -122,15 +99,7 @@ def generate_number end def process_return - inventory_units(include: :variant).each do |iu| - iu.return! - - if iu.variant.should_track_inventory? - if stock_item = Spree::StockItem.find_by(variant_id: iu.variant_id, stock_location_id: stock_location_id) - Spree::StockMovement.create!(stock_item_id: stock_item.id, quantity: 1) - end - end - end + return_authorization_inventory_units.includes(:inventory_unit).each(&:receive!) order.return if inventory_units.all?(&:returned?) end diff --git a/core/app/models/spree/return_authorization_inventory_unit.rb b/core/app/models/spree/return_authorization_inventory_unit.rb new file mode 100644 index 0000000000..9d78be1bf5 --- /dev/null +++ b/core/app/models/spree/return_authorization_inventory_unit.rb @@ -0,0 +1,31 @@ +module Spree + class ReturnAuthorizationInventoryUnit < Spree::Base + belongs_to :return_authorization, inverse_of: :return_authorization_inventory_units + belongs_to :inventory_unit, inverse_of: :return_authorization_inventory_units + belongs_to :exchange_variant, class: 'Spree::Variant' + + validates :return_authorization, presence: true + validates :inventory_unit, presence: true, uniqueness: {scope: :return_authorization} + + scope :received, -> { where.not(received_at: nil) } + + def receive! + update_attributes!(received_at: Time.now) + + inventory_unit.return! + + if inventory_unit.variant.should_track_inventory? && stock_item + Spree::StockMovement.create!(stock_item_id: stock_item.id, quantity: 1) + end + end + + private + + def stock_item + Spree::StockItem.find_by({ + variant_id: inventory_unit.variant_id, + stock_location_id: return_authorization.stock_location_id, + }) + end + end +end diff --git a/core/db/migrate/20140702140656_create_spree_return_authorization_inventory_unit.rb b/core/db/migrate/20140702140656_create_spree_return_authorization_inventory_unit.rb new file mode 100644 index 0000000000..0df585a840 --- /dev/null +++ b/core/db/migrate/20140702140656_create_spree_return_authorization_inventory_unit.rb @@ -0,0 +1,52 @@ +class CreateSpreeReturnAuthorizationInventoryUnit < ActiveRecord::Migration + def up + create_table :spree_return_authorization_inventory_units do |t| + t.integer :return_authorization_id + t.integer :inventory_unit_id + t.integer :exchange_variant_id + t.datetime :received_at + + t.timestamps + end + + execute(<<-SQL) + insert into spree_return_authorization_inventory_units + ( + return_authorization_id, + inventory_unit_id, + received_at, + created_at, + updated_at + ) + select + return_authorization_id, + id, + case state + when 'returned' then updated_at + when 'refunded' then updated_at + else null + end, + created_at, + '#{Time.now.to_s(:db)}' + from spree_inventory_units + where return_authorization_id is not null; + SQL + + remove_column :spree_inventory_units, :return_authorization_id + end + + def down + add_column :spree_inventory_units, :return_authorization_id, :integer, after: :shipment_id + + execute(<<-SQL) + update spree_inventory_units + set return_authorization_id = spree_return_authorization_inventory_units.return_authorization_id + from spree_return_authorization_inventory_units + where spree_return_authorization_inventory_units.inventory_unit_id = spree_inventory_units.id + SQL + + add_index :spree_inventory_units, :return_authorization_id, name: "index_spree_inventory_units_on_return_authorization_id" + + drop_table :spree_return_authorization_inventory_units + end +end diff --git a/core/lib/spree/permitted_attributes.rb b/core/lib/spree/permitted_attributes.rb index cdde56606d..ff94acb061 100644 --- a/core/lib/spree/permitted_attributes.rb +++ b/core/lib/spree/permitted_attributes.rb @@ -60,7 +60,7 @@ module PermittedAttributes @@property_attributes = [:name, :presentation] - @@return_authorization_attributes = [:amount, :reason, :stock_location_id] + @@return_authorization_attributes = [:amount, :reason, :stock_location_id, :inventory_units_attributes] @@shipment_attributes = [ :order, :special_instructions, :stock_location_id, :id, diff --git a/core/lib/spree/testing_support/factories/return_authorization_factory.rb b/core/lib/spree/testing_support/factories/return_authorization_factory.rb index 4d67ffd6e5..fffb8b6d24 100644 --- a/core/lib/spree/testing_support/factories/return_authorization_factory.rb +++ b/core/lib/spree/testing_support/factories/return_authorization_factory.rb @@ -10,4 +10,9 @@ factory :new_return_authorization, class: Spree::ReturnAuthorization do association(:order, factory: :shipped_order) end + + factory :return_authorization_inventory_unit, class: Spree::ReturnAuthorizationInventoryUnit do + association(:return_authorization, factory: :return_authorization) + association(:inventory_unit, factory: :inventory_unit) + end end diff --git a/core/lib/spree/testing_support/factories/shipment_factory.rb b/core/lib/spree/testing_support/factories/shipment_factory.rb index 777a3c4c56..5f143b92d9 100644 --- a/core/lib/spree/testing_support/factories/shipment_factory.rb +++ b/core/lib/spree/testing_support/factories/shipment_factory.rb @@ -13,6 +13,7 @@ shipment.order.line_items.each do |line_item| line_item.quantity.times do shipment.inventory_units.create( + order_id: shipment.order_id, variant_id: line_item.variant_id, line_item_id: line_item.id ) diff --git a/core/spec/models/spree/return_authorization_inventory_unit_spec.rb b/core/spec/models/spree/return_authorization_inventory_unit_spec.rb new file mode 100644 index 0000000000..a64cc11ecf --- /dev/null +++ b/core/spec/models/spree/return_authorization_inventory_unit_spec.rb @@ -0,0 +1,55 @@ +require 'spec_helper' + +describe Spree::ReturnAuthorizationInventoryUnit do + describe '#receive!' do + let(:return_authorization_inventory_unit) { + create(:return_authorization_inventory_unit, { + return_authorization: return_authorization, + inventory_unit: inventory_unit, + }) + } + + let(:order) { create(:shipped_order, line_items_count: 1) } + let(:return_authorization) { create(:return_authorization, order: order, stock_location: stock_location) } + let(:stock_location) { create(:stock_location) } + let(:inventory_unit) { order.line_items.first.inventory_units.first } + let(:stock_item) { stock_location.stock_items.find_by(variant_id: inventory_unit.variant_id) } + let(:now) { Time.now } + + subject { return_authorization_inventory_unit.receive! } + + it 'updates received_at' do + Timecop.freeze(now) { subject } + expect(return_authorization_inventory_unit.received_at).to eq now + end + + it 'returns the inventory unit' do + subject + expect(inventory_unit.reload.state).to eq 'returned' + end + + context 'with a stock location' do + it 'increases the count on hand' do + expect { subject }.to change { stock_item.reload.count_on_hand }.by(1) + end + + context 'when variant does not track inventory' do + before do + inventory_unit.variant.update_attributes!(track_inventory: false) + end + + it 'does not increase the count on hand' do + expect { subject }.to_not change { stock_item.reload.count_on_hand } + end + end + end + + context 'without a stock location' do + let(:stock_location) { nil } + + it 'still works' do + expect { subject }.to_not raise_error + end + end + end +end diff --git a/core/spec/models/spree/return_authorization_spec.rb b/core/spec/models/spree/return_authorization_spec.rb index 8da77e0a6d..d5352bddc8 100644 --- a/core/spec/models/spree/return_authorization_spec.rb +++ b/core/spec/models/spree/return_authorization_spec.rb @@ -2,7 +2,8 @@ describe Spree::ReturnAuthorization do let(:stock_location) { Spree::StockLocation.create(:name => "test") } - let(:order) { FactoryGirl.create(:shipped_order) } + let(:order) { create(:shipped_order) } + let(:inventory_unit_1) { order.inventory_units.first } let(:variant) { order.variants.first } let(:return_authorization) { Spree::ReturnAuthorization.new(:order => order, :stock_location_id => stock_location.id) } @@ -40,35 +41,6 @@ end end - context "add_variant" do - context "on empty rma" do - it "should associate inventory units as shipped" do - return_authorization.add_variant(variant.id, 1) - expect(return_authorization.inventory_units.where(state: 'shipped').size).to eq 1 - end - - it "should update order state" do - order.should_receive(:authorize_return!) - return_authorization.add_variant(variant.id, 1) - end - end - - context "on rma that already has inventory_units" do - before do - return_authorization.add_variant(variant.id, 1) - end - - it "should not associate more inventory units than there are on the order" do - return_authorization.add_variant(variant.id, 1) - expect(return_authorization.inventory_units.size).to eq 1 - end - - it "should not update order state" do - expect{return_authorization.add_variant(variant.id, 1)}.to_not change{order.state} - end - end - end - context "can_receive?" do it "should allow_receive when inventory units assigned" do return_authorization.stub(:inventory_units => [1,2,3]) @@ -85,21 +57,20 @@ let(:inventory_unit) { order.shipments.first.inventory_units.first } context "to the initial stock location" do + let!(:return_authorization_inventory_unit) { create(:return_authorization_inventory_unit, inventory_unit: inventory_unit, return_authorization: return_authorization) } + before do - return_authorization.stub(:inventory_units => [inventory_unit], :amount => -20) return_authorization.stub(:stock_location_id => inventory_unit.shipment.stock_location.id) order.stub(:update!) end it "should mark all inventory units are returned" do - inventory_unit.should_receive(:return!) return_authorization.receive! + expect(inventory_unit.reload.state).to eq 'returned' end it "should update the stock item counts in the stock location" do - count_on_hand = inventory_unit.find_stock_item.count_on_hand - return_authorization.receive! - inventory_unit.find_stock_item.count_on_hand.should == count_on_hand + 1 + expect { return_authorization.receive! }.to change { inventory_unit.find_stock_item.count_on_hand }.by(1) end context 'with Config.track_inventory_levels == false' do @@ -118,17 +89,19 @@ end context "to a different stock location" do - let(:new_stock_location) { FactoryGirl.create(:stock_location, :name => "other") } + let(:new_stock_location) { create(:stock_location, :name => "other") } + let!(:return_authorization_inventory_unit) { create(:return_authorization_inventory_unit, inventory_unit: inventory_unit, return_authorization: return_authorization) } before do return_authorization.stub(:stock_location_id => new_stock_location.id) - return_authorization.stub(:inventory_units => [inventory_unit], :amount => -20) end it "should update the stock item counts in new stock location" do - count_on_hand = Spree::StockItem.where(variant_id: inventory_unit.variant_id, stock_location_id: new_stock_location.id).first.count_on_hand - return_authorization.receive! - Spree::StockItem.where(variant_id: inventory_unit.variant_id, stock_location_id: new_stock_location.id).first.count_on_hand.should == count_on_hand + 1 + expect { + return_authorization.receive! + }.to change { + Spree::StockItem.where(variant_id: inventory_unit.variant_id, stock_location_id: new_stock_location.id).first.count_on_hand + }.by(1) end it "should NOT raise an error when no stock item exists in the stock location" do
<%= check_box_tag 'select-all' %> <%= Spree.t(:product) %><%= Spree.t(:total_per_item) %><%= Spree.t(:quantity_shipped) %><%= Spree.t(:quantity_returned) %><%= Spree.t(:return_quantity) %><%= Spree.t(:state) %><%= Spree.t(:price) %>
-
<%= line_item.variant.name %>
-
<%= line_item.variant.options_text %>
-
<%= line_item.display_rounded_total_per_item %><%= line_item.inventory_units.shipped.size %><%= line_item.inventory_units.returned.size %> - <% if @return_authorization.received? %> - <%= @return_authorization.inventory_units.group_by(&:variant)[line_item.variant].try(:size) || 0 %> - <% else %> - <% returning_count = @return_authorization.inventory_units.group_by(&:variant)[line_item.variant].try(:size) || 0 %> - <%= number_field_tag "return_quantity[#{line_item.variant.id}]", returning_count, { :style => 'width:100px;', - :min => 0, :max => line_item.inventory_units.shipped.size, "data-line-item-id" => line_item.id} %> + <%= f.fields_for :return_authorization_inventory_units, @form_return_authorization_inventory_units do |unit_fields| %> + <% return_authorization_inventory_unit = unit_fields.object %> + <% inventory_unit = return_authorization_inventory_unit.inventory_unit %> +
+ <% if inventory_unit.shipped? %> + <%= unit_fields.hidden_field :inventory_unit_id %> + <%= unit_fields.check_box :_destroy, {checked: return_authorization_inventory_unit.persisted?, class: 'add-item', "data-price" => inventory_unit.line_item.rounded_total_per_item}, '0', '1' %> <% end %> +
<%= inventory_unit.variant.name %>
+
<%= inventory_unit.variant.options_text %>
+
<%= inventory_unit.state.humanize %><%= inventory_unit.line_item.display_rounded_total_per_item %>