Class: Spree::Stock::Packer

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

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (Packer) initialize(stock_location, inventory_units, splitters = [Splitter::Base])

Returns a new instance of Packer



6
7
8
9
10
# File 'app/models/spree/stock/packer.rb', line 6

def initialize(stock_location, inventory_units, splitters = [Splitter::Base])
  @stock_location = stock_location
  @inventory_units = inventory_units
  @splitters = splitters
end

Instance Attribute Details

- (Object) inventory_units (readonly)

Returns the value of attribute inventory_units



4
5
6
# File 'app/models/spree/stock/packer.rb', line 4

def inventory_units
  @inventory_units
end

- (Object) splitters (readonly)

Returns the value of attribute splitters



4
5
6
# File 'app/models/spree/stock/packer.rb', line 4

def splitters
  @splitters
end

- (Object) stock_location (readonly)

Returns the value of attribute stock_location



4
5
6
# File 'app/models/spree/stock/packer.rb', line 4

def stock_location
  @stock_location
end

Instance Method Details

- (Object) default_package



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

def default_package
  package = Package.new(stock_location)
  inventory_units.group_by(&:variant).each do |variant, variant_inventory_units|
    units = variant_inventory_units.clone
    if variant.should_track_inventory?
      stock_item = stock_item_for(variant.id)
      next unless stock_item

      on_hand, backordered = stock_item.fill_status(units.count)
      package.add_multiple units.slice!(0, on_hand), :on_hand if on_hand > 0
      package.add_multiple units.slice!(0, backordered), :backordered if backordered > 0
    else
      package.add_multiple units
    end
  end
  package
end

- (Object) packages



12
13
14
15
16
17
18
# File 'app/models/spree/stock/packer.rb', line 12

def packages
  if splitters.empty?
    [default_package]
  else
    build_splitter.split [default_package]
  end
end