Class: Spree::StockItem

Inherits:
Base
  • Object
show all
Includes:
Discard::Model, ParanoiaDeprecations
Defined in:
app/models/spree/stock_item.rb

Instance Method Summary collapse

Methods included from ParanoiaDeprecations

#paranoia_delete, #paranoia_destroy

Methods inherited from Base

display_includes, #initialize_preference_defaults, page, preference

Methods included from Preferences::Preferable

#admin_form_preference_names, #default_preferences, #defined_preferences, #get_preference, #has_preference!, #has_preference?, #preference_default, #preference_type, #set_preference

Instance Method Details

#adjust_count_on_hand(value) ⇒ Object

Note:

This will cause backorders to be processed.

Adjusts the count on hand by a given value.

Parameters:

  • value (Fixnum)

    the amount to change the count on hand by, positive or negative values are valid



42
43
44
45
46
47
48
49
# File 'app/models/spree/stock_item.rb', line 42

def adjust_count_on_hand(value)
  with_lock do
    self.count_on_hand = count_on_hand + value
    process_backorders(count_on_hand - count_on_hand_was)

    save!
  end
end

#available?Boolean

Returns true if this stock item can be included in a shipment.

Returns:

  • (Boolean)

    true if this stock item can be included in a shipment



68
69
70
# File 'app/models/spree/stock_item.rb', line 68

def available?
  in_stock? || backorderable?
end

#backordered_inventory_unitsArray<Spree::InventoryUnit>

Returns the backordered inventory units associated with this stock item.

Returns:



33
34
35
# File 'app/models/spree/stock_item.rb', line 33

def backordered_inventory_units
  Spree::InventoryUnit.backordered_for_stock_item(self)
end

#fill_status(quantity) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
# File 'app/models/spree/stock_item.rb', line 79

def fill_status(quantity)
  if count_on_hand >= quantity
    on_hand = quantity
    backordered = 0
  else
    on_hand = count_on_hand
    on_hand = 0 if on_hand < 0
    backordered = backorderable? ? (quantity - on_hand) : 0
  end

  [on_hand, backordered]
end

#in_stock?Boolean

Returns true if this stock item’s count on hand is not zero.

Returns:

  • (Boolean)

    true if this stock item’s count on hand is not zero



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

def in_stock?
  count_on_hand > 0
end

#nameString

Returns the name of this stock item’s variant.

Returns:

  • (String)

    the name of this stock item’s variant



24
# File 'app/models/spree/stock_item.rb', line 24

delegate :name, to: :variant, prefix: true

#reduce_count_on_hand_to_zeroObject

Note:

This processes backorders if the count on hand is not zero.

Sets the count on hand to zero if it not already zero.



75
76
77
# File 'app/models/spree/stock_item.rb', line 75

def reduce_count_on_hand_to_zero
  set_count_on_hand(0) if count_on_hand > 0
end

#set_count_on_hand(value) ⇒ Object

Note:

This will cause backorders to be processed.

Sets this stock item’s count on hand.

Parameters:

  • value (Fixnum)

    the desired count on hand



55
56
57
58
59
60
# File 'app/models/spree/stock_item.rb', line 55

def set_count_on_hand(value)
  self.count_on_hand = value
  process_backorders(count_on_hand - count_on_hand_was)

  save!
end