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

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.



22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'app/models/spree/stock/simple_coordinator.rb', line 22

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

  @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
  )
end

Instance Attribute Details

#orderObject (readonly)

Returns the value of attribute order.



20
21
22
# File 'app/models/spree/stock/simple_coordinator.rb', line 20

def order
  @order
end

Instance Method Details

#shipmentsObject



36
37
38
# File 'app/models/spree/stock/simple_coordinator.rb', line 36

def shipments
  @shipments ||= build_shipments
end