mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-02-21 00:47:26 +00:00
Merge pull request #3147 from luisramos0/2-0-vos
[Spree Upgrade] Adapt variant overrides to spree 2
This commit is contained in:
@@ -109,6 +109,20 @@ module VariantStock
|
||||
end
|
||||
end
|
||||
|
||||
# We can have this responsibility here in the variant because there is only one stock item per variant
|
||||
#
|
||||
# This enables us to override this behaviour for variant overrides
|
||||
def move(quantity, originator = nil)
|
||||
raise_error_if_no_stock_item_available
|
||||
|
||||
# Creates a stock movement: it updates stock_item.count_on_hand and fills backorders
|
||||
#
|
||||
# This is the original Spree::StockLocation#move,
|
||||
# except that we raise an error if the stock item is missing,
|
||||
# because, unlike Spree, we should always have exactly one stock item per variant.
|
||||
stock_item.stock_movements.create!(quantity: quantity, originator: originator)
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
# Persists the single stock item associated to this variant. As defined in
|
||||
@@ -126,7 +140,10 @@ module VariantStock
|
||||
raise message if stock_items.empty?
|
||||
end
|
||||
|
||||
# Backwards compatible setting of stock levels in Spree 2.0.
|
||||
# Overwrites stock_item.count_on_hand
|
||||
#
|
||||
# Calling stock_item.adjust_count_on_hand will bypass filling backorders and creating stock movements
|
||||
# If that was required we could call self.move
|
||||
def overwrite_stock_levels(new_level)
|
||||
stock_item.adjust_count_on_hand(new_level - stock_item.count_on_hand)
|
||||
end
|
||||
|
||||
5
app/models/spree/stock_location_decorator.rb
Normal file
5
app/models/spree/stock_location_decorator.rb
Normal file
@@ -0,0 +1,5 @@
|
||||
Spree::StockLocation.class_eval do
|
||||
def move(variant, quantity, originator = nil)
|
||||
variant.move(quantity, originator)
|
||||
end
|
||||
end
|
||||
@@ -6,7 +6,8 @@ class VariantOverride < ActiveRecord::Base
|
||||
belongs_to :hub, class_name: 'Enterprise'
|
||||
belongs_to :variant, class_name: 'Spree::Variant'
|
||||
|
||||
validates_presence_of :hub_id, :variant_id
|
||||
validates :hub_id, presence: true
|
||||
validates :variant_id, presence: true
|
||||
# Default stock can be nil, indicating stock should not be reset or zero, meaning reset to zero. Need to ensure this can be set by the user.
|
||||
validates :default_stock, numericality: { greater_than_or_equal_to: 0 }, allow_nil: true
|
||||
|
||||
@@ -33,45 +34,20 @@ class VariantOverride < ActiveRecord::Base
|
||||
]
|
||||
end
|
||||
|
||||
def self.price_for(hub, variant)
|
||||
self.for(hub, variant).andand.price
|
||||
end
|
||||
|
||||
def self.count_on_hand_for(hub, variant)
|
||||
self.for(hub, variant).andand.count_on_hand
|
||||
end
|
||||
|
||||
def self.stock_overridden?(hub, variant)
|
||||
count_on_hand_for(hub, variant).present?
|
||||
end
|
||||
|
||||
def self.decrement_stock!(hub, variant, quantity)
|
||||
vo = self.for(hub, variant)
|
||||
|
||||
if vo.nil?
|
||||
Bugsnag.notify RuntimeError.new "Attempting to decrement stock level for a variant without a VariantOverride."
|
||||
else
|
||||
vo.decrement_stock! quantity
|
||||
end
|
||||
end
|
||||
|
||||
def stock_overridden?
|
||||
count_on_hand.present?
|
||||
end
|
||||
|
||||
def decrement_stock!(quantity)
|
||||
if stock_overridden?
|
||||
decrement! :count_on_hand, quantity
|
||||
else
|
||||
Bugsnag.notify RuntimeError.new "Attempting to decrement stock level on a VariantOverride without a count_on_hand specified."
|
||||
def move_stock!(quantity)
|
||||
unless stock_overridden?
|
||||
Bugsnag.notify RuntimeError.new "Attempting to move stock of a VariantOverride without a count_on_hand specified."
|
||||
return
|
||||
end
|
||||
end
|
||||
|
||||
def increment_stock!(quantity)
|
||||
if stock_overridden?
|
||||
if quantity > 0
|
||||
increment! :count_on_hand, quantity
|
||||
else
|
||||
Bugsnag.notify RuntimeError.new "Attempting to decrement stock level on a VariantOverride without a count_on_hand specified."
|
||||
elsif quantity < 0
|
||||
decrement! :count_on_hand, -quantity
|
||||
end
|
||||
end
|
||||
|
||||
@@ -83,7 +59,7 @@ class VariantOverride < ActiveRecord::Base
|
||||
if resettable
|
||||
if default_stock?
|
||||
self.attributes = { count_on_hand: default_stock }
|
||||
self.save
|
||||
save
|
||||
else
|
||||
Bugsnag.notify RuntimeError.new "Attempting to reset stock level for a variant with no default stock level."
|
||||
end
|
||||
@@ -93,10 +69,6 @@ class VariantOverride < ActiveRecord::Base
|
||||
|
||||
private
|
||||
|
||||
def self.for(hub, variant)
|
||||
VariantOverride.where(variant_id: variant, hub_id: hub).first
|
||||
end
|
||||
|
||||
def refresh_products_cache_from_save
|
||||
OpenFoodNetwork::ProductsCache.variant_override_changed self
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user