Class: Spree::StockLocation
- Inherits:
-
Base
- Object
- ActiveRecord::Base
- Base
- Spree::StockLocation
show all
- Defined in:
- app/models/spree/stock_location.rb
Instance Method Summary
collapse
-
#backorderable?(variant) ⇒ Boolean
-
#count_on_hand(variant) ⇒ Object
-
#fill_status(variant, quantity) ⇒ Object
-
#move(variant, quantity, originator = nil) ⇒ Object
-
#propagate_variant(variant) ⇒ Object
Wrapper for creating a new stock item respecting the backorderable config.
-
#restock(variant, quantity, originator = nil) ⇒ Object
-
#restock_backordered(variant, quantity, originator = nil) ⇒ Object
-
#set_up_stock_item(variant) ⇒ Object
Return either an existing stock item or create a new one.
-
#state_text ⇒ Object
-
#stock_item(variant_id) ⇒ StockItem
Returns an instance of StockItem for the variant id.
-
#stock_item_or_create(variant_or_id) ⇒ StockItem
Attempts to look up StockItem for the variant, and creates one if not found.
-
#unstock(variant, quantity, originator = nil) ⇒ Object
Methods inherited from Base
page
#clear_preferences, #default_preferences, #defined_preferences, #get_preference, #has_preference!, #has_preference?, #preference_default, #preference_type, #set_preference
Instance Method Details
#backorderable?(variant) ⇒ Boolean
63
64
65
|
# File 'app/models/spree/stock_location.rb', line 63
def backorderable?(variant)
stock_item(variant).try(:backorderable?)
end
|
#count_on_hand(variant) ⇒ Object
59
60
61
|
# File 'app/models/spree/stock_location.rb', line 59
def count_on_hand(variant)
stock_item(variant).try(:count_on_hand)
end
|
#fill_status(variant, quantity) ⇒ Object
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
|
# File 'app/models/spree/stock_location.rb', line 88
def fill_status(variant, quantity)
if item = stock_item(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
83
84
85
86
|
# File 'app/models/spree/stock_location.rb', line 83
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
21
22
23
|
# File 'app/models/spree/stock_location.rb', line 21
def propagate_variant(variant)
self.stock_items.create!(variant: variant, backorderable: self.backorderable_default)
end
|
#restock(variant, quantity, originator = nil) ⇒ Object
67
68
69
|
# File 'app/models/spree/stock_location.rb', line 67
def restock(variant, quantity, originator = nil)
move(variant, quantity, originator)
end
|
#restock_backordered(variant, quantity, originator = nil) ⇒ Object
71
72
73
74
75
76
77
|
# File 'app/models/spree/stock_location.rb', line 71
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.now
)
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
28
29
30
|
# File 'app/models/spree/stock_location.rb', line 28
def set_up_stock_item(variant)
self.stock_item(variant) || propagate_variant(variant)
end
|
#state_text ⇒ Object
16
17
18
|
# File 'app/models/spree/stock_location.rb', line 16
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.
37
38
39
|
# File 'app/models/spree/stock_location.rb', line 37
def stock_item(variant_id)
stock_items.where(variant_id: variant_id).order(:id).first
end
|
#stock_item_or_create(variant_or_id) ⇒ 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.
49
50
51
52
53
54
55
56
57
|
# File 'app/models/spree/stock_location.rb', line 49
def stock_item_or_create(variant_or_id)
vid = if variant_or_id.is_a?(Variant)
variant_or_id.id
else
ActiveSupport::Deprecation.warn "Passing a Variant ID is deprecated, and will be removed in Spree 3. Please pass a variant instance instead.", caller
variant_or_id
end
stock_item(vid) || stock_items.create(variant_id: vid)
end
|
#unstock(variant, quantity, originator = nil) ⇒ Object
79
80
81
|
# File 'app/models/spree/stock_location.rb', line 79
def unstock(variant, quantity, originator = nil)
move(variant, -quantity, originator)
end
|