Class: Spree::StockLocation

Inherits:
Base
  • Object
show all
Includes:
Spree::Security::StockLocations, UniqueName, VendorConcern, Webhooks::HasWebhooks
Defined in:
app/models/spree/stock_location.rb

Instance Method Summary collapse

Methods inherited from Base

belongs_to_required_by_default, for_store, has_many_inversing, json_api_columns, json_api_permitted_attributes, json_api_type, page, spree_base_scopes, spree_base_uniqueness_scope

Methods included from Preferences::Preferable

#clear_preferences, #default_preferences, #defined_preferences, #deprecated_preferences, #get_preference, #has_preference!, #has_preference?, #preference_default, #preference_deprecated, #preference_type, #set_preference

Instance Method Details

#backorderable?(variant) ⇒ Boolean

Returns:

  • (Boolean)


78
79
80
# File 'app/models/spree/stock_location.rb', line 78

def backorderable?(variant)
  stock_item(variant).try(:backorderable?)
end

#count_on_hand(variant) ⇒ Object



74
75
76
# File 'app/models/spree/stock_location.rb', line 74

def count_on_hand(variant)
  stock_item(variant).try(:count_on_hand)
end

#fill_status(variant, quantity) ⇒ Object



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'app/models/spree/stock_location.rb', line 103

def fill_status(variant, quantity)
  if item = stock_item_or_create(variant)
    if item.count_on_hand >= quantity
      on_hand = quantity
      backordered = 0
    else
      on_hand = item.count_on_hand
      on_hand = 0 if on_hand < 0
      backordered = item.backorderable? ? (quantity - on_hand) : 0
    end

    [on_hand, backordered]
  else
    [0, 0]
  end
end

#move(variant, quantity, originator = nil) ⇒ Object



98
99
100
101
# File 'app/models/spree/stock_location.rb', line 98

def move(variant, quantity, originator = nil)
  stock_item_or_create(variant).stock_movements.create!(quantity: quantity,
                                                        originator: originator)
end

#propagate_variant(variant) ⇒ Object

Wrapper for creating a new stock item respecting the backorderable config



34
35
36
# File 'app/models/spree/stock_location.rb', line 34

def propagate_variant(variant)
  stock_items.create!(variant: variant, backorderable: backorderable_default)
end

#restock(variant, quantity, originator = nil) ⇒ Object



82
83
84
# File 'app/models/spree/stock_location.rb', line 82

def restock(variant, quantity, originator = nil)
  move(variant, quantity, originator)
end

#restock_backordered(variant, quantity, _originator = nil) ⇒ Object



86
87
88
89
90
91
92
# File 'app/models/spree/stock_location.rb', line 86

def restock_backordered(variant, quantity, _originator = nil)
  item = stock_item_or_create(variant)
  item.update_columns(
    count_on_hand: item.count_on_hand + quantity,
    updated_at: Time.current
  )
end

#set_up_stock_item(variant) ⇒ Object

Return either an existing stock item or create a new one. Useful in scenarios where the user might not know whether there is already a stock item for a given variant



41
42
43
# File 'app/models/spree/stock_location.rb', line 41

def set_up_stock_item(variant)
  stock_item(variant) || propagate_variant(variant)
end

#state_textObject



29
30
31
# File 'app/models/spree/stock_location.rb', line 29

def state_text
  state.try(:abbr) || state.try(:name) || state_name
end

#stock_item(variant_id) ⇒ StockItem

Returns an instance of StockItem for the variant id.

Parameters:

  • variant_id (String)

    The id of a variant.

Returns:

  • (StockItem)

    Corresponding StockItem for the StockLocation’s variant.



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

def stock_item(variant_id)
  stock_items.where(variant_id: variant_id).order(:id).first
end

#stock_item_or_create(variant_or_variant_id) ⇒ StockItem

Attempts to look up StockItem for the variant, and creates one if not found.

Parameters:

  • variant

    Variant instance or Variant ID

Returns:

  • (StockItem)

    Corresponding StockItem for the StockLocation’s variant.



63
64
65
66
67
68
69
70
71
72
# File 'app/models/spree/stock_location.rb', line 63

def stock_item_or_create(variant_or_variant_id)
  if variant_or_variant_id.is_a?(Spree::Variant)
    variant_id = variant_or_variant_id.id
    variant = variant_or_variant_id
  else
    variant_id = variant_or_variant_id
    variant = Spree::Variant.find(variant_or_variant_id)
  end
  stock_item(variant_id) || propagate_variant(variant)
end

#stocks?(variant) ⇒ Boolean

Returns:

  • (Boolean)


54
55
56
# File 'app/models/spree/stock_location.rb', line 54

def stocks?(variant)
  stock_items.exists?(variant: variant)
end

#unstock(variant, quantity, originator = nil) ⇒ Object



94
95
96
# File 'app/models/spree/stock_location.rb', line 94

def unstock(variant, quantity, originator = nil)
  move(variant, -quantity, originator)
end