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

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

Returns a new instance of Packer.



6
7
8
9
10
11
# 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
  @allocated_inventory_units = []
end

Instance Attribute Details

#allocated_inventory_unitsObject (readonly)

Returns the value of attribute allocated_inventory_units.



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

def allocated_inventory_units
  @allocated_inventory_units
end

#inventory_unitsObject (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

#splittersObject (readonly)

Returns the value of attribute splitters.



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

def splitters
  @splitters
end

#stock_locationObject (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

#default_packageObject



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'app/models/spree/stock/packer.rb', line 21

def default_package
  package = Package.new(stock_location)

  # Group by variant_id as grouping by variant fires cached query.
  inventory_units.group_by(&:variant_id).each do |variant_id, variant_inventory_units|
    variant = Spree::Variant.find(variant_id)
    units = variant_inventory_units.clone
    if variant.should_track_inventory?
      next unless stock_location.stock_item(variant)

      on_hand, backordered = stock_location.fill_status(variant, units.size)
      on_hand_units, backordered_units = units.slice!(0, on_hand), units.slice!(0, backordered)

      package.add_multiple on_hand_units, :on_hand if on_hand > 0
      package.add_multiple backordered_units, :backordered if backordered > 0

      @allocated_inventory_units += (on_hand_units + backordered_units)
    else
      package.add_multiple units
      @allocated_inventory_units += units
    end
  end

  package
end

#packagesObject



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

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