Class: Spree::Stock::SimpleCoordinator

Inherits:
Object
  • Object
show all
Defined in:
core/app/models/spree/stock/simple_coordinator.rb

Overview

A simple implementation of Stock Coordination

The algorithm for allocating inventory is naive:

* For each available Stock Location
  * Allocate as much on hand inventory as possible from this location
  * Remove the amount allocated from the amount desired
* Repeat but for backordered inventory
* Combine allocated and on hand inventory into a single shipment per-location

Allocation logic can be changed using a custom class (as configured in Spree::Config::stock_allocator_class )

After allocation, splitters are run on each Package (as configured in Spree::Config.environment.stock_splitters)

Finally, shipping rates are calculated using the class configured as Spree::Config.stock.estimator_class.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(order, inventory_units = nil) ⇒ SimpleCoordinator

Returns a new instance of SimpleCoordinator



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'core/app/models/spree/stock/simple_coordinator.rb', line 25

def initialize(order, inventory_units = nil)
  @order = order
  @inventory_units = inventory_units || InventoryUnitBuilder.new(order).units
  @splitters = Spree::Config.environment.stock_splitters

  filtered_stock_locations = Spree::Config.stock.location_filter_class.new(Spree::StockLocation.all, @order).filter
  sorted_stock_locations = Spree::Config.stock.location_sorter_class.new(filtered_stock_locations).sort
  @stock_locations = sorted_stock_locations

  @inventory_units_by_variant = @inventory_units.group_by(&:variant)
  @desired = Spree::StockQuantities.new(@inventory_units_by_variant.transform_values(&:count))
  @availability = Spree::Stock::Availability.new(
    variants: @desired.variants,
    stock_locations: @stock_locations
  )

  @allocator = Spree::Config.stock.allocator_class.new(@availability)
end

Instance Attribute Details

#orderObject (readonly)

Returns the value of attribute order



23
24
25
# File 'core/app/models/spree/stock/simple_coordinator.rb', line 23

def order
  @order
end

Instance Method Details

#shipmentsObject



44
45
46
# File 'core/app/models/spree/stock/simple_coordinator.rb', line 44

def shipments
  @shipments ||= build_shipments
end