Class: Spree::Stock::SimpleCoordinator
- Inherits:
-
Object
- Object
- Spree::Stock::SimpleCoordinator
- 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
- #allocator ⇒ Object readonly private
- #availability ⇒ Object readonly private
- #desired ⇒ Object readonly private
- #filtered_stock_locations ⇒ Object readonly private
- #inventory_units ⇒ Object readonly private
- #inventory_units_by_variant ⇒ Object readonly private
-
#order ⇒ Object
readonly
Returns the value of attribute order.
- #packages ⇒ Object readonly private
- #splitters ⇒ Object readonly private
- #stock_locations ⇒ Object readonly private
Instance Method Summary collapse
-
#initialize(order, inventory_units = nil) ⇒ SimpleCoordinator
constructor
A new instance of SimpleCoordinator.
- #shipments ⇒ Object
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
#allocator ⇒ Object (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 |
#availability ⇒ Object (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 |
#desired ⇒ Object (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_locations ⇒ Object (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_units ⇒ Object (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_variant ⇒ Object (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 |
#order ⇒ Object (readonly)
Returns the value of attribute order.
23 24 25 |
# File 'app/models/spree/stock/simple_coordinator.rb', line 23 def order @order end |
#packages ⇒ Object (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 |
#splitters ⇒ Object (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_locations ⇒ Object (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
#shipments ⇒ Object
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 |