Class: Spree::Stock::Package
- Inherits:
-
Object
- Object
- Spree::Stock::Package
- Defined in:
- app/models/spree/stock/package.rb
Defined Under Namespace
Classes: ContentItem
Instance Attribute Summary collapse
-
#contents ⇒ Object
readonly
Returns the value of attribute contents.
-
#order ⇒ Object
readonly
Returns the value of attribute order.
-
#shipping_rates ⇒ Object
Returns the value of attribute shipping_rates.
-
#stock_location ⇒ Object
readonly
Returns the value of attribute stock_location.
Instance Method Summary collapse
- #add(line_item, quantity, state = :on_hand, variant = nil) ⇒ Object
- #backordered ⇒ Object
- #currency ⇒ Object
- #empty? ⇒ Boolean
-
#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.
-
#initialize(stock_location, order, contents = []) ⇒ Package
constructor
A new instance of Package.
- #inspect ⇒ Object
- #on_hand ⇒ Object
- #quantity(state = nil) ⇒ Object
- #shipping_categories ⇒ Object
- #shipping_methods ⇒ Object
- #to_shipment ⇒ Object
- #weight ⇒ Object
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
#contents ⇒ Object (readonly)
Returns the value of attribute contents.
6 7 8 |
# File 'app/models/spree/stock/package.rb', line 6 def contents @contents end |
#order ⇒ Object (readonly)
Returns the value of attribute order.
6 7 8 |
# File 'app/models/spree/stock/package.rb', line 6 def order @order end |
#shipping_rates ⇒ Object
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_location ⇒ Object (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 |
#backordered ⇒ Object
28 29 30 |
# File 'app/models/spree/stock/package.rb', line 28 def backordered contents.select { |item| item.state == :backordered } end |
#currency ⇒ Object
57 58 59 |
# File 'app/models/spree/stock/package.rb', line 57 def currency #TODO calculate from first variant? end |
#empty? ⇒ 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 |
#inspect ⇒ Object
69 70 71 72 73 74 75 |
# File 'app/models/spree/stock/package.rb', line 69 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_hand ⇒ Object
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_categories ⇒ Object
61 62 63 |
# File 'app/models/spree/stock/package.rb', line 61 def shipping_categories contents.map { |item| item.variant.shipping_category }.compact.uniq end |
#shipping_methods ⇒ Object
65 66 67 |
# File 'app/models/spree/stock/package.rb', line 65 def shipping_methods shipping_categories.map(&:shipping_methods).reduce(:&).to_a end |
#to_shipment ⇒ Object
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'app/models/spree/stock/package.rb', line 77 def to_shipment shipment = Spree::Shipment.new shipment.address = order.ship_address 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 |
#weight ⇒ Object
20 21 22 |
# File 'app/models/spree/stock/package.rb', line 20 def weight contents.sum { |item| item.variant.weight * item.quantity } end |