Class: Spree::StockLocation

Inherits:
Base
  • Object
show all
Defined in:
app/models/spree/stock_location.rb

Overview

Records the name and addresses from which stock items are fulfilled in cartons.

Defined Under Namespace

Classes: InvalidMovementError

Instance Method Summary collapse

Methods inherited from Base

display_includes, #initialize_preference_defaults, page, preference

Methods included from Preferences::Preferable

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

Instance Method Details

#backorderable?(variant) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#count_on_hand(variant) ⇒ Object



70
71
72
# File 'app/models/spree/stock_location.rb', line 70

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

#fill_status(variant, quantity) ⇒ Object



102
103
104
105
106
107
108
# File 'app/models/spree/stock_location.rb', line 102

def fill_status(variant, quantity)
  if item = stock_item(variant)
    item.fill_status(quantity)
  else
    [0, 0]
  end
end

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



94
95
96
97
98
99
100
# File 'app/models/spree/stock_location.rb', line 94

def move(variant, quantity, originator = nil)
  if quantity < 1 && !stock_item(variant)
    raise InvalidMovementError.new(Spree.t(:negative_movement_absent_item))
  end
  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



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

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

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



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

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

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



82
83
84
85
86
87
88
# File 'app/models/spree/stock_location.rb', line 82

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



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

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

#state_textObject



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

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.



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

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

#stock_item_or_create(variant) ⇒ StockItem

Attempts to look up StockItem for the variant, and creates one if not found. This method accepts an id or instance of the variant since it is used in multiple ways. Other methods in this model attempt to pass a variant, but controller actions can pass just the variant id as a parameter.

Returns:

  • (StockItem)

    Corresponding StockItem for the StockLocation’s variant.



61
62
63
64
65
66
67
68
# File 'app/models/spree/stock_location.rb', line 61

def stock_item_or_create(variant)
  vid = if variant.is_a?(Variant)
    variant.id
  else
    variant
  end
  stock_item(vid) || stock_items.create(variant_id: vid)
end

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



90
91
92
# File 'app/models/spree/stock_location.rb', line 90

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