Class: Spree::Stock::SimpleCoordinator

Inherits:
Object
  • Object
show all
Defined in:
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.



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'app/models/spree/stock/simple_coordinator.rb', line 30

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

  @filtered_stock_locations = Spree::Config.stock.location_filter_class.new(load_stock_locations, 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

#allocatorObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



26
27
28
# File 'app/models/spree/stock/simple_coordinator.rb', line 26

def allocator
  @allocator
end

#availabilityObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



26
27
28
# File 'app/models/spree/stock/simple_coordinator.rb', line 26

def availability
  @availability
end

#desiredObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



26
27
28
# File 'app/models/spree/stock/simple_coordinator.rb', line 26

def desired
  @desired
end

#filtered_stock_locationsObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



26
27
28
# File 'app/models/spree/stock/simple_coordinator.rb', line 26

def filtered_stock_locations
  @filtered_stock_locations
end

#inventory_unitsObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



26
27
28
# File 'app/models/spree/stock/simple_coordinator.rb', line 26

def inventory_units
  @inventory_units
end

#inventory_units_by_variantObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



26
27
28
# File 'app/models/spree/stock/simple_coordinator.rb', line 26

def inventory_units_by_variant
  @inventory_units_by_variant
end

#orderObject (readonly)

Returns the value of attribute order.



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

def order
  @order
end

#packagesObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



26
27
28
# File 'app/models/spree/stock/simple_coordinator.rb', line 26

def packages
  @packages
end

#splittersObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



26
27
28
# File 'app/models/spree/stock/simple_coordinator.rb', line 26

def splitters
  @splitters
end

#stock_locationsObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



26
27
28
# File 'app/models/spree/stock/simple_coordinator.rb', line 26

def stock_locations
  @stock_locations
end

Instance Method Details

#shipmentsObject



50
51
52
53
54
55
56
57
58
59
60
# File 'app/models/spree/stock/simple_coordinator.rb', line 50

def shipments
  @shipments ||= begin
    @packages = build_packages
    shipments = build_shipments

    # Make sure we don't add the proposed shipments to the order
    order.shipments = order.shipments - shipments

    shipments
  end
end