Skip to content

Commit

Permalink
Fix solidus stock locations sorting
Browse files Browse the repository at this point in the history
The order of the stock locations that is returned from the configured
sorter is lost in the SimpleCoordinator. This occurs in
the Stock::Availiability class when creating the #stock_item_scope.

Previously, the now unused and unusable #sort_availability
method was used to re-order the stock locations but this was
lost during the refactor to allow extending the default Solidus
allocation logic.

This commit re-orders the stock locations for all methods
called by the stock allocators.
  • Loading branch information
ikraamg committed Feb 23, 2021
1 parent b3b0978 commit 86caae0
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 12 deletions.
12 changes: 10 additions & 2 deletions core/app/models/spree/stock/availability.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def initialize(variants:, stock_locations: Spree::StockLocation.active)
# Get the on_hand stock quantities
# @return [Hash<Integer=>Spree::StockQuantities>] A map of stock_location_ids to the stock quantities available in that location
def on_hand_by_stock_location_id
counts_on_hand.to_a.group_by do |(_, stock_location_id), _|
on_hand_quantities = counts_on_hand.to_a.group_by do |(_, stock_location_id), _|
stock_location_id
end.transform_values do |values|
Spree::StockQuantities.new(
Expand All @@ -31,19 +31,21 @@ def on_hand_by_stock_location_id
end.to_h
)
end
stock_location_sorter(on_hand_quantities)
end

# Get the on_hand stock quantities
# @return [Hash<Integer=>Spree::StockQuantities>] A map of stock_location_ids to the stock quantities available in that location
def backorderable_by_stock_location_id
backorderables.group_by(&:second).transform_values do |variant_ids|
backorderable_quantities = backorderables.group_by(&:second).transform_values do |variant_ids|
Spree::StockQuantities.new(
variant_ids.map do |variant_id, _|
variant = @variant_map[variant_id]
[variant, Float::INFINITY]
end.to_h
)
end
stock_location_sorter(backorderable_quantities)
end

private
Expand All @@ -67,6 +69,12 @@ def stock_item_scope
where(variant_id: @variants).
where(stock_location_id: @stock_locations)
end

def stock_location_sorter(stock_quantities)
location_ids = @stock_locations.map(&:id)

stock_quantities.sort_by { |stock_quantity| location_ids.index(stock_quantity[0]) }.to_h
end
end
end
end
10 changes: 0 additions & 10 deletions core/app/models/spree/stock/simple_coordinator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -87,16 +87,6 @@ def split_packages(initial_packages)
end
end

def sort_availability(availability)
sorted_availability = availability.sort_by do |stock_location_id, _|
@stock_locations.find_index do |stock_location|
stock_location.id == stock_location_id
end
end

Hash[sorted_availability]
end

def get_units(quantities)
# Change our raw quantities back into inventory units
quantities.flat_map do |variant, quantity|
Expand Down

0 comments on commit 86caae0

Please sign in to comment.