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(variant, quantity, state = :on_hand) ⇒ Object



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

def add(variant, quantity, state=:on_hand)
  contents << ContentItem.new(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



76
77
78
# File 'app/models/spree/stock/package.rb', line 76

def currency
  #TODO calculate from first variant?
end

#empty?Boolean

Returns:

  • (Boolean)


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

def empty?
  quantity == 0
end

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



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

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

#flattenedObject



54
55
56
57
58
59
60
61
62
# File 'app/models/spree/stock/package.rb', line 54

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

#flattened=(flattened) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
# File 'app/models/spree/stock/package.rb', line 64

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.variant, item.quantity, item.state)
    end
  end
end

#inspectObject



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

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



39
40
41
42
43
44
45
46
47
48
# File 'app/models/spree/stock/package.rb', line 39

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



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

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

#shipping_methodsObject



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

def shipping_methods
  shipping_categories.map { |sc| sc.shipping_methods }.flatten.uniq
end

#to_shipmentObject



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

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.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