Class: Spree::Stock::Package

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

Defined Under Namespace

Classes: ContentItem

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Package.



9
10
11
12
13
14
# File 'app/models/spree/stock/package.rb', line 9

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

Instance Attribute Details

#contentsObject (readonly)

Returns the value of attribute contents.



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

def contents
  @contents
end

#orderObject (readonly)

Returns the value of attribute order.



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

def order
  @order
end

#shipping_ratesObject

Returns the value of attribute shipping_rates.



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

def shipping_rates
  @shipping_rates
end

#stock_locationObject (readonly)

Returns the value of attribute stock_location.



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

def stock_location
  @stock_location
end

Instance Method Details

#add(line_item, quantity, state = :on_hand, variant = nil) ⇒ Object



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

def add(line_item, quantity, state = :on_hand, variant = nil)
  contents << ContentItem.new(line_item, variant || line_item.variant, quantity, state)
end

#backorderedObject



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

def backordered
  contents.select { |item| item.state == :backordered }
end

#currencyObject



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

def currency
  #TODO calculate from first variant?
end

#empty?Boolean

Returns:

  • (Boolean)


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

def empty?
  quantity == 0
end

#find_item(variant, state = :on_hand, line_item = nil) ⇒ Object

Consider extensions and applications might create a inventory unit where the variant and the line_item might not refer to the same product



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

def find_item(variant, state = :on_hand, line_item = nil)
  contents.select do |item|
    item.variant == variant &&
    item.state == state &&
    (line_item.nil? || line_item == item.line_item)
  end.first
end

#flattenedObject



57
58
59
60
61
62
63
64
65
# File 'app/models/spree/stock/package.rb', line 57

def flattened
  flat = []
  contents.each do |item|
    item.quantity.times do
      flat << ContentItem.new(item.line_item, item.variant, 1, item.state)
    end
  end
  flat
end

#flattened=(flattened) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
# File 'app/models/spree/stock/package.rb', line 67

def flattened=(flattened)
  contents.clear
  flattened.each do |item|
    current_item = find_item(item.variant, item.state)
    if current_item
      current_item.quantity += 1
    else
      add(item.line_item, item.quantity, item.state)
    end
  end
end

#inspectObject



91
92
93
94
95
96
97
# File 'app/models/spree/stock/package.rb', line 91

def inspect
  out = "#{order} - "
  out << contents.map do |content_item|
    "#{content_item.variant.name} #{content_item.quantity} #{content_item.state}"
  end.join('/')
  out
end

#on_handObject



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

def on_hand
  contents.select { |item| item.state == :on_hand }
end

#quantity(state = nil) ⇒ Object



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

def quantity(state=nil)
  case state
  when :on_hand
    on_hand.sum { |item| item.quantity }
  when :backordered
    backordered.sum { |item| item.quantity }
  else
    contents.sum { |item| item.quantity }
  end
end

#shipping_categoriesObject



83
84
85
# File 'app/models/spree/stock/package.rb', line 83

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

#shipping_methodsObject



87
88
89
# File 'app/models/spree/stock/package.rb', line 87

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

#to_shipmentObject



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'app/models/spree/stock/package.rb', line 99

def to_shipment
  shipment = Spree::Shipment.new
  shipment.order = order
  shipment.stock_location = stock_location
  shipment.shipping_rates = shipping_rates

  contents.each do |item|
    item.quantity.times do |n|
      unit = shipment.inventory_units.build
      unit.pending = true
      unit.order = order
      unit.variant = item.variant
      unit.line_item = item.line_item
      unit.state = item.state.to_s
    end
  end

  shipment
end

#weightObject



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

def weight
  contents.sum { |item| item.variant.weight * item.quantity }
end