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.

Parameters:



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

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

Adds an inventory unit to this package.

Parameters:

  • inventory_unit (Spree::InventoryUnit)

    an inventory unit to be added to this package

  • state (:on_hand, :backordered) (defaults to: :on_hand)

    the state of the item to be added to this package



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

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

Adds multiple inventory units to this package.

Parameters:

  • inventory_units (Array<Spree::InventoryUnit>)

    a collection of inventory units to be added to this package

  • state (:on_hand, :backordered) (defaults to: :on_hand)

    the state of the items to be added to this package



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

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

#backorderedArray<Spree::Stock::ContentItem>

Returns the content items in this package which are backordered.

Returns:



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

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

#currencyString

Returns the currency of the order this package belongs to.

Returns:

  • (String)

    the currency of the order this package belongs to



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

def currency
  order.currency
end

#empty?Boolean

Returns true if there are no inventory units in this package.

Returns:

  • (Boolean)

    true if there are no inventory units in this package



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

def empty?
  quantity == 0
end

#find_item(inventory_unit, state = nil) ⇒ Object

Find a content item in this package by inventory unit and optionally state.

Parameters:

  • inventory_unit (Spree::InventoryUnit)

    the desired inventory unit

  • state (:backordered, :on_hand, nil) (defaults to: nil)

    the state of the desired content item, or nil for any state



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

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

#on_handArray<Spree::Stock::ContentItem>

Returns the content items in this package which are on hand.

Returns:



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

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

#orderSpree::Order

Returns the order associated with this package.

Returns:



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

def order
  # Fix regression that removed package.order.
  # Find it dynamically through an inventory_unit.
  contents.detect {|item| !!item.try(:inventory_unit).try(:order) }.try(:inventory_unit).try(:order)
end

#quantity(state = nil) ⇒ Fixnum

Returns the number of inventory units in the package, counting only those in the given state if it was specified.

Parameters:

  • state (:backordered, :on_hand, nil) (defaults to: nil)

    the state of the content items of which we want the quantity, or nil for the full quantity

Returns:

  • (Fixnum)

    the number of inventory units in the package, counting only those in the given state if it was specified



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

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

Removes a given inventory unit from this package.

Parameters:



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

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

#shipping_categoriesArray<Spree::ShippingCategory>

Returns the shipping categories of the variants in this package.

Returns:



104
105
106
# File 'app/models/spree/stock/package.rb', line 104

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

#shipping_methodsArray<Spree::ShippingMethod>

Returns the shipping methods available for this pacakges shipping categories.

Returns:

  • (Array<Spree::ShippingMethod>)

    the shipping methods available for this pacakges shipping categories



110
111
112
# File 'app/models/spree/stock/package.rb', line 110

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

#to_shipmentSpree::Shipment

Returns a new shipment containing this package’s inventory units, with the appropriate shipping rates and associated with the correct stock location.

Returns:

  • (Spree::Shipment)

    a new shipment containing this package’s inventory units, with the appropriate shipping rates and associated with the correct stock location



117
118
119
120
121
122
123
124
125
126
127
128
# File 'app/models/spree/stock/package.rb', line 117

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

#weightFloat

Returns the summed weight of the contents of this package.

Returns:

  • (Float)

    the summed weight of the contents of this package



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

def weight
  contents.sum(&:weight)
end