Class: Spree::StockLocation
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- Spree::StockLocation
- 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.
- #stock_item(variant) ⇒ Object
- #stock_item_or_create(variant) ⇒ Object
- #unstock(variant, quantity, originator = nil) ⇒ Object
Instance Method Details
#backorderable?(variant) ⇒ Boolean
39 40 41 |
# File 'app/models/spree/stock_location.rb', line 39 def backorderable?(variant) stock_item(variant).try(:backorderable?) end |
#count_on_hand(variant) ⇒ Object
35 36 37 |
# File 'app/models/spree/stock_location.rb', line 35 def count_on_hand(variant) stock_item(variant).try(:count_on_hand) end |
#fill_status(variant, quantity) ⇒ Object
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'app/models/spree/stock_location.rb', line 64 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
59 60 61 62 |
# File 'app/models/spree/stock_location.rb', line 59 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
16 17 18 |
# File 'app/models/spree/stock_location.rb', line 16 def propagate_variant(variant) self.stock_items.create!(variant: variant, backorderable: self.backorderable_default) end |
#restock(variant, quantity, originator = nil) ⇒ Object
43 44 45 |
# File 'app/models/spree/stock_location.rb', line 43 def restock(variant, quantity, originator = nil) move(variant, quantity, originator) end |
#restock_backordered(variant, quantity, originator = nil) ⇒ Object
47 48 49 50 51 52 53 |
# File 'app/models/spree/stock_location.rb', line 47 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
23 24 25 |
# File 'app/models/spree/stock_location.rb', line 23 def set_up_stock_item(variant) self.stock_item(variant) || propagate_variant(variant) end |
#stock_item(variant) ⇒ Object
27 28 29 |
# File 'app/models/spree/stock_location.rb', line 27 def stock_item(variant) stock_items.where(variant_id: variant).order(:id).first end |
#stock_item_or_create(variant) ⇒ Object
31 32 33 |
# File 'app/models/spree/stock_location.rb', line 31 def stock_item_or_create(variant) stock_item(variant) || stock_items.create(variant: variant) end |
#unstock(variant, quantity, originator = nil) ⇒ Object
55 56 57 |
# File 'app/models/spree/stock_location.rb', line 55 def unstock(variant, quantity, originator = nil) move(variant, -quantity, originator) end |