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 Rails.application.config.spree.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



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

def initialize(order, inventory_units = nil)
  @order = order
  @inventory_units = inventory_units || InventoryUnitBuilder.new(order).units
  @splitters = Rails.application.config.spree.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



18
19
20
# File 'app/models/spree/stock/simple_coordinator.rb', line 18

def order
  @order
end

Instance Method Details

#shipmentsObject



34
35
36
# File 'app/models/spree/stock/simple_coordinator.rb', line 34

def shipments
  @shipments ||= build_shipments
end