Class: Spree::StockLocation
- Inherits:
-
Base
- Object
- ActiveRecord::Base
- Base
- Spree::StockLocation
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
-
#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) ⇒ 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
display_includes
#generate_permalink, #save_permalink
Instance Method Details
#backorderable?(variant) ⇒ Boolean
80
81
82
|
# File 'app/models/spree/stock_location.rb', line 80
def backorderable?(variant)
stock_item(variant).try(:backorderable?)
end
|
#count_on_hand(variant) ⇒ Object
76
77
78
|
# File 'app/models/spree/stock_location.rb', line 76
def count_on_hand(variant)
stock_item(variant).try(:count_on_hand)
end
|
#fill_status(variant, quantity) ⇒ Object
108
109
110
111
112
113
114
|
# File 'app/models/spree/stock_location.rb', line 108
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
100
101
102
103
104
105
106
|
# File 'app/models/spree/stock_location.rb', line 100
def move(variant, quantity, originator = nil)
if quantity < 1 && !stock_item(variant)
raise InvalidMovementError.new(I18n.t('spree.negative_movement_absent_item'))
end
stock_item_or_create(variant).stock_movements.create!(quantity:,
originator:)
end
|
#propagate_variant(variant) ⇒ Object
Wrapper for creating a new stock item respecting the backorderable config
41
42
43
|
# File 'app/models/spree/stock_location.rb', line 41
def propagate_variant(variant)
stock_items.create!(variant:, backorderable: backorderable_default)
end
|
#restock(variant, quantity, originator = nil) ⇒ Object
84
85
86
|
# File 'app/models/spree/stock_location.rb', line 84
def restock(variant, quantity, originator = nil)
move(variant, quantity, originator)
end
|
#restock_backordered(variant, quantity, _originator = nil) ⇒ Object
88
89
90
91
92
93
94
|
# File 'app/models/spree/stock_location.rb', line 88
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
48
49
50
|
# File 'app/models/spree/stock_location.rb', line 48
def set_up_stock_item(variant)
stock_item(variant) || propagate_variant(variant)
end
|
#state_text ⇒ Object
36
37
38
|
# File 'app/models/spree/stock_location.rb', line 36
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.
57
58
59
|
# File 'app/models/spree/stock_location.rb', line 57
def stock_item(variant_id)
stock_items.where(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.
67
68
69
70
71
72
73
74
|
# File 'app/models/spree/stock_location.rb', line 67
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
96
97
98
|
# File 'app/models/spree/stock_location.rb', line 96
def unstock(variant, quantity, originator = nil)
move(variant, -quantity, originator)
end
|