Skip to content

Commit

Permalink
Merge pull request #2199 from jhawthorn/simple_stock_coordinator
Browse files Browse the repository at this point in the history
Replace Stock::Coordinator with Stock::SimpleCoordinator
  • Loading branch information
jhawthorn authored Sep 12, 2017
2 parents 53b4acb + 332e216 commit 0d9c043
Show file tree
Hide file tree
Showing 19 changed files with 679 additions and 557 deletions.
1 change: 1 addition & 0 deletions api/spec/requests/spree/api/orders_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -506,6 +506,7 @@ module Spree

context "with a line item" do
let(:order) { create(:order_with_line_items) }
let(:line_item) { order.line_items.first }

it "can empty an order" do
create(:adjustment, order: order, adjustable: order)
Expand Down
28 changes: 0 additions & 28 deletions core/app/models/spree/stock/adjuster.rb

This file was deleted.

70 changes: 70 additions & 0 deletions core/app/models/spree/stock/availability.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
module Spree
module Stock
# This class manages checking stock availability efficiently for a set of
# Variants and StockLocations.
#
# This serves a similar role to Spree::Stock::Quantifier, but is more
# efficient by checking multiple variants at once.
class Availability
# @param variants [Array<Spree::Variant>] variants to check stock of
# @param stock_locations [Array<Spree::StockLocation>] stock_locations to check for stock in
def initialize(variants:, stock_locations: Spree::StockLocation.active)
@variants = variants
@variant_map = variants.index_by(&:id)
@stock_locations = stock_locations
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 on_hand_by_stock_location_id
counts_on_hand.to_a.group_by do |(_, stock_location_id), _|
stock_location_id
end.transform_values do |values|
Spree::StockQuantities.new(
values.map do |(variant_id, _), count|
variant = @variant_map[variant_id]
count = Float::INFINITY if !variant.should_track_inventory?
count = 0 if count < 0
[variant, count]
end.to_h
)
end
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|
Spree::StockQuantities.new(
variant_ids.map do |variant_id, _|
variant = @variant_map[variant_id]
[variant, Float::INFINITY]
end.to_h
)
end
end

private

def counts_on_hand
@counts_on_hand ||=
stock_item_scope.
group(:variant_id, :stock_location_id).
sum(:count_on_hand)
end

def backorderables
@backorderables ||=
stock_item_scope.
where(backorderable: true).
pluck(:variant_id, :stock_location_id)
end

def stock_item_scope
Spree::StockItem.
where(variant_id: @variants).
where(stock_location_id: @stock_locations)
end
end
end
end
150 changes: 0 additions & 150 deletions core/app/models/spree/stock/coordinator.rb

This file was deleted.

51 changes: 0 additions & 51 deletions core/app/models/spree/stock/packer.rb

This file was deleted.

48 changes: 0 additions & 48 deletions core/app/models/spree/stock/prioritizer.rb

This file was deleted.

Loading

0 comments on commit 0d9c043

Please sign in to comment.