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


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 = []
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
16
# File 'app/models/spree/stock/package.rb', line 13

def add(inventory_unit, state = :on_hand)
  # Remove find_item check as already taken care by prioritizer
  contents << ContentItem.new(inventory_unit, state)
end

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


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

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

#backorderedObject


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

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

#currencyObject


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

def currency
  order.currency
end

#dimensionObject


101
102
103
# File 'app/models/spree/stock/package.rb', line 101

def dimension
  contents.sum(&:dimension)
end

#empty?Boolean


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

def empty?
  quantity.zero?
end

#find_item(inventory_unit, state = nil) ⇒ Object


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

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


78
79
80
81
82
# File 'app/models/spree/stock/package.rb', line 78

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

#on_handObject


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

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

#orderObject

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


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

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

#quantity(state = nil) ⇒ Object


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

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

#remove(inventory_unit) ⇒ Object


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

def remove(inventory_unit)
  item = find_item(inventory_unit)
  remove_item(item) if item
end

#remove_item(item) ⇒ Object


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

def remove_item(item)
  @contents -= [item]
end

#shipping_categoriesObject


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

def shipping_categories
  Spree::ShippingCategory.joins(products: :variants_including_master).
    where(spree_variants: { id: variant_ids }).distinct
end

#shipping_methodsObject


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

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

#to_shipmentObject


84
85
86
87
88
89
90
91
92
93
94
95
# File 'app/models/spree/stock/package.rb', line 84

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

#volumeObject


97
98
99
# File 'app/models/spree/stock/package.rb', line 97

def volume
  contents.sum(&:volume)
end

#weightObject


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

def weight
  contents.sum(&:weight)
end