Class: Spree::Stock::Reserver
- Inherits:
-
Object
- Object
- Spree::Stock::Reserver
- Defined in:
- app/models/spree/stock/reserver.rb
Defined Under Namespace
Classes: InvalidQuantityError
Instance Method Summary collapse
-
#initialize(reserved_stock_location = nil) ⇒ Reserver
constructor
A new instance of Reserver.
-
#reserve(variant, original_stock_location, user, quantity, expires_at = nil) ⇒ Object
TODO: Use stock transfers.
-
#restock(variant, user, quantity = nil) ⇒ Object
TODO: Use stock transfers TODO: Add locale files.
- #restock_expired ⇒ Object
Constructor Details
Instance Method Details
#reserve(variant, original_stock_location, user, quantity, expires_at = nil) ⇒ Object
TODO: Use stock transfers. TODO: Make stock_location optional, and if not present use whatever’s available
12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'app/models/spree/stock/reserver.rb', line 12 def reserve(variant, original_stock_location, user, quantity, expires_at=nil) if quantity < 1 raise InvalidQuantityError.new(Spree.t(:quantity_must_be_positive)) end if quantity > original_stock_location.count_on_hand(variant) raise InvalidQuantityError.new(Spree.t(:insufficient_stock_available)) end reserved_stock_item = user.reserved_stock_item_or_create(variant, original_stock_location, expires_at) reserved_stock_item.stock_movements.create!(quantity: quantity) original_stock_location.unstock(variant, quantity) reserved_stock_item end |
#restock(variant, user, quantity = nil) ⇒ Object
TODO: Use stock transfers TODO: Add locale files
27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'app/models/spree/stock/reserver.rb', line 27 def restock(variant, user, quantity=nil) reserved_stock_item = user.reserved_stock_item(variant) raise InvalidQuantityError.new(Spree.t(:no_stock_reserved_for_user_and_variant)) unless reserved_stock_item.present? if quantity if quantity < 1 raise InvalidQuantityError.new(Spree.t(:quantity_must_be_positive)) end if quantity > reserved_stock_item.count_on_hand raise InvalidQuantityError.new(Spree.t(:insufficient_reserved_stock_available)) end end quantity ||= reserved_stock_item.count_on_hand perform_restock(reserved_stock_item, quantity) end |
#restock_expired ⇒ Object
42 43 44 45 46 |
# File 'app/models/spree/stock/reserver.rb', line 42 def restock_expired @reserved_stock_location.expired_stock_items.each do |reserved_stock_item| perform_restock(reserved_stock_item, reserved_stock_item.count_on_hand) end end |