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

- (Package) initialize(stock_location, contents = [])

Returns a new instance of Package

Parameters:



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

def initialize(stock_location, contents = [])
  @stock_location = stock_location
  @contents = contents
end

Instance Attribute Details

- (Object) contents (readonly)

Returns the value of attribute contents



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

def contents
  @contents
end

- (Object) shipment

Returns the value of attribute shipment



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

def shipment
  @shipment
end

- (Object) stock_location (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

- (Object) add(inventory_unit, state = :on_hand)

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



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

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

- (Object) add_multiple(inventory_units, state = :on_hand)

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



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

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

- (Array<Spree::Stock::ContentItem>) backordered

Returns the content items in this package which are backordered

Returns:



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

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

- (String) currency

Returns the currency of the order this package belongs to

Returns:

  • (String)

    the currency of the order this package belongs to



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

def currency
  order.currency
end

- (Boolean) empty?

Returns true if there are no inventory units in this package

Returns:

  • (Boolean)

    true if there are no inventory units in this package



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

def empty?
  quantity == 0
end

- (Object) find_item(inventory_unit, state = nil)

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



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

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

- (Array<Spree::Stock::ContentItem>) on_hand

Returns the content items in this package which are on hand

Returns:



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

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

- (Spree::Order) order

Returns the order associated with this package

Returns:



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

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

- (Fixnum) quantity(state = nil)

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



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

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

- (Object) remove(inventory_unit)

Removes a given inventory unit from this package.

Parameters:



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

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

- (Array<Spree::ShippingCategory>) shipping_categories

Returns the shipping categories of the variants in this package

Returns:



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

def shipping_categories
  Spree::ShippingCategory.where(id: shipping_category_ids)
end

- (ActiveRecord::Relation) shipping_methods

Returns the [Spree::ShippingMethod]s available for this pacakge based on the stock location and shipping categories.

Returns:

  • (ActiveRecord::Relation)

    the [Spree::ShippingMethod]s available for this pacakge based on the stock location and shipping categories.



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

def shipping_methods
  Spree::ShippingMethod.
    with_all_shipping_category_ids(shipping_category_ids).
    available_in_stock_location(stock_location)
end

- (Spree::Shipment) to_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



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

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(
    order: order,
    stock_location: stock_location,
    inventory_units: contents.map(&:inventory_unit)
  )
end

- (Float) weight

Returns the summed weight of the contents of this package

Returns:

  • (Float)

    the summed weight of the contents of this package



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

def weight
  contents.sum(&:weight)
end