Skip to content

Commit

Permalink
Merge pull request #3199 from nebulab/spaghetticode/fix-3198-order-st…
Browse files Browse the repository at this point in the history
…ate-after-return

Allow order with multiple line items to be marked as "Returned"
  • Loading branch information
kennyadsl authored May 28, 2019
2 parents 5d9fe3f + 4870e08 commit 897dc04
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ class CustomerReturnsController < ResourceController
before_action :load_form_data, only: [:new, :edit]
before_action :build_return_items_from_params, only: [:create]
create.fails :load_form_data
create.after :order_process_return

def edit
@pending_return_items = @customer_return.return_items.select(&:pending?)
Expand All @@ -23,6 +24,10 @@ def edit

private

def order_process_return
@customer_return.process_return!
end

def location_after_save
url_for([:edit, :admin, @order, @customer_return])
end
Expand Down Expand Up @@ -60,11 +65,13 @@ def permitted_resource_params

def build_return_items_from_params
return_items_params = permitted_resource_params.delete(:return_items_attributes).values

@customer_return.return_items = return_items_params.map do |item_params|
next unless item_params.delete('returned') == '1'
return_item = item_params[:id] ? Spree::ReturnItem.find(item_params[:id]) : Spree::ReturnItem.new
return_item.assign_attributes(item_params)

return_item.skip_customer_return_processing = true

if item_params[:reception_status_event].blank?
return redirect_to(new_object_url, flash: { error: 'Reception status choice required' })
end
Expand Down
28 changes: 28 additions & 0 deletions backend/spec/features/admin/orders/customer_returns_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# frozen_string_literal: true

require 'spec_helper'

describe 'Customer returns', type: :feature do
stub_authorization!

context 'when the order has more than one line item' do
let(:order) { create :shipped_order, line_items_count: 2 }

context 'when creating a return with state "Received"' do
it 'marks the order as returned', :js do
visit spree.new_admin_order_customer_return_path(order)

find('#select-all').click
page.execute_script "$('select.add-item').val('receive')"
select 'NY Warehouse', from: 'Stock Location'
click_button 'Create'

expect(page).to have_content 'Customer Return has been successfully created'

within 'dd.order-state' do
expect(page).to have_content 'Returned'
end
end
end
end
end
2 changes: 1 addition & 1 deletion core/app/models/spree/customer_return.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def completely_decided?
end

def process_return!
order.return! if order.all_inventory_units_returned?
order.return! if order.can_return?
end

private
Expand Down
8 changes: 6 additions & 2 deletions core/app/models/spree/return_item.rb
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,8 @@ def reception_completed?
after_transition any => any, do: :persist_acceptance_status_errors
end

attr_accessor :skip_customer_return_processing

# @param inventory_unit [Spree::InventoryUnit] the inventory for which we
# want a return item
# @return [Spree::ReturnItem] a valid return item for the given inventory
Expand Down Expand Up @@ -233,10 +235,12 @@ def currency

def process_inventory_unit!
inventory_unit.return!

if customer_return
customer_return.stock_location.restock(inventory_unit.variant, 1, customer_return) if should_restock?
customer_return.process_return!
unless skip_customer_return_processing
Deprecation.warn 'From Solidus v2.9 onwards, #process_inventory_unit! will not call customer_return#process_return!'
customer_return.process_return!
end
end
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

FactoryBot.define do
factory :return_item, class: 'Spree::ReturnItem' do
skip_customer_return_processing { true }
association(:inventory_unit, factory: :inventory_unit, state: :shipped)
association(:return_reason, factory: :return_reason)
return_authorization do |_return_item|
Expand Down
9 changes: 9 additions & 0 deletions core/spec/models/spree/return_item_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,15 @@
subject
end

context 'when the `skip_customer_return_processing` flag is not set to true' do
before { return_item.skip_customer_return_processing = false }

it 'shows a deprecation warning' do
expect(Spree::Deprecation).to receive(:warn)
subject
end
end

context 'when there is a received return item with the same inventory unit' do
let!(:return_item_with_dupe_inventory_unit) { create(:return_item, inventory_unit: inventory_unit, reception_status: 'received') }

Expand Down

0 comments on commit 897dc04

Please sign in to comment.