Class: Spree::Stock::Package

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(stock_location, contents = []) ⇒ Package

Returns a new instance of Package



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

def initialize(stock_location, contents=[])
  @stock_location = stock_location
  @contents = contents
  @shipping_rates = Array.new
end

Instance Attribute Details

#contentsObject (readonly)

Returns the value of attribute contents



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

def contents
  @contents
end

#shipping_ratesObject

Returns the value of attribute shipping_rates



5
6
7
# File 'app/models/spree/stock/package.rb', line 5

def shipping_rates
  @shipping_rates
end

#stock_locationObject (readonly)

Returns the value of attribute stock_location



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

def stock_location
  @stock_location
end

Instance Method Details

#add(inventory_unit, state = :on_hand) ⇒ Object



13
14
15
# File 'app/models/spree/stock/package.rb', line 13

def add(inventory_unit, state = :on_hand)
  contents << ContentItem.new(inventory_unit, state) unless find_item(inventory_unit)
end

#add_multiple(inventory_units, state = :on_hand) ⇒ Object



17
18
19
# File 'app/models/spree/stock/package.rb', line 17

def add_multiple(inventory_units, state = :on_hand)
  inventory_units.each { |inventory_unit| add(inventory_unit, state) }
end

#backorderedObject



40
41
42
# File 'app/models/spree/stock/package.rb', line 40

def backordered
  contents.select(&:backordered?)
end

#currencyObject



60
61
62
# File 'app/models/spree/stock/package.rb', line 60

def currency
  #TODO calculate from first variant?
end

#empty?Boolean

Returns:

  • (Boolean)


56
57
58
# File 'app/models/spree/stock/package.rb', line 56

def empty?
  quantity == 0
end

#find_item(inventory_unit, state = nil) ⇒ Object



44
45
46
47
48
49
# File 'app/models/spree/stock/package.rb', line 44

def find_item(inventory_unit, state = nil)
  contents.detect do |item|
    item.inventory_unit == inventory_unit &&
      (!state || item.state.to_s == state.to_s)
  end
end

#inspectObject



72
73
74
75
76
# File 'app/models/spree/stock/package.rb', line 72

def inspect
  contents.map do |content_item|
    "#{content_item.variant.name} #{content_item.state}"
  end.join(' / ')
end

#on_handObject



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

def on_hand
  contents.select(&:on_hand?)
end

#orderObject

Fix regression that removed package.order. Find it dynamically through an inventory_unit.



28
29
30
# File 'app/models/spree/stock/package.rb', line 28

def order
  contents.detect {|item| !!item.try(:inventory_unit).try(:order) }.try(:inventory_unit).try(:order)
end

#quantity(state = nil) ⇒ Object



51
52
53
54
# File 'app/models/spree/stock/package.rb', line 51

def quantity(state = nil)
  matched_contents = state.nil? ? contents : contents.select { |c| c.state.to_s == state.to_s }
  matched_contents.map(&:quantity).sum
end

#remove(inventory_unit) ⇒ Object



21
22
23
24
# File 'app/models/spree/stock/package.rb', line 21

def remove(inventory_unit)
  item = find_item(inventory_unit)
  @contents -= [item] if item
end

#shipping_categoriesObject



64
65
66
# File 'app/models/spree/stock/package.rb', line 64

def shipping_categories
  contents.map { |item| item.variant.shipping_category }.compact.uniq
end

#shipping_methodsObject



68
69
70
# File 'app/models/spree/stock/package.rb', line 68

def shipping_methods
  shipping_categories.map(&:shipping_methods).reduce(:&).to_a
end

#to_shipmentObject



78
79
80
81
82
83
84
85
86
87
88
89
# File 'app/models/spree/stock/package.rb', line 78

def to_shipment
  # At this point we should only have one content item per inventory unit
  # across the entire set of inventory units to be shipped, which has been
  # taken care of by the Prioritizer
  contents.each { |content_item| content_item.inventory_unit.state = content_item.state.to_s }

  Spree::Shipment.new(
    stock_location: stock_location,
    shipping_rates: shipping_rates,
    inventory_units: contents.map(&:inventory_unit)
  )
end

#weightObject



32
33
34
# File 'app/models/spree/stock/package.rb', line 32

def weight
  contents.sum(&:weight)
end