Add variant scoping to availability_validator_decorator by using line_item.scoper and moving Spree::Stock::Quantifier.can_supply? to VariantStock so that it becomes overridable

This commit is contained in:
luisramos0
2018-12-20 20:29:03 +00:00
parent c0a095ffaf
commit d641705053
3 changed files with 31 additions and 12 deletions

View File

@@ -114,6 +114,17 @@ module VariantStock
end
end
# Moving Spree::Stock::Quantifier.can_supply? to the variant enables us to override this behaviour for variant overrides
# We can have this responsibility here in the variant because there is only one stock item per variant
#
# Here we depend only on variant.total_on_hand and variant.on_demand.
# This way, variant_overrides only need to override variant.total_on_hand and variant.on_demand.
def can_supply?(quantity)
return true unless Spree::Config[:track_inventory_levels]
on_demand || total_on_hand >= quantity
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

View File

@@ -125,6 +125,10 @@ Spree::LineItem.class_eval do
end
end
def scoper
@scoper ||= OpenFoodNetwork::ScopeVariantToHub.new(order.distributor)
end
private
def update_inventory_with_scoping
@@ -133,10 +137,6 @@ Spree::LineItem.class_eval do
end
alias_method_chain :update_inventory, :scoping
def scoper
@scoper ||= OpenFoodNetwork::ScopeVariantToHub.new(order.distributor)
end
def calculate_final_weight_volume
if final_weight_volume.present? && quantity_was > 0
self.final_weight_volume = final_weight_volume * quantity / quantity_was

View File

@@ -24,18 +24,26 @@ Spree::Stock::AvailabilityValidator.class_eval do
def line_item_shipment(line_item)
return line_item.target_shipment if line_item.target_shipment
return line_item.order.shipments.first if line_item.order.present? && line_item.order.shipments.any?
return line_item.order.shipments.first if line_item.order.andand.shipments.any?
end
# This is the spree v2.0.4 implementation of validate
# But using the calculated quantity instead of the line_item.quantity.
# Overrides Spree v2.0.4 validate method version to:
# - scope variants to hub and thus acivate variant overrides
# - use calculated quantity instead of the line_item.quantity
# - rely on Variant.can_supply? instead of Stock::Quantified.can_supply?
# so that it works correctly for variant overrides
def validate_quantity(line_item, quantity)
quantifier = Spree::Stock::Quantifier.new(line_item.variant_id)
return if quantifier.can_supply? quantity
line_item.scoper.scope(line_item.variant)
add_out_of_stock_error(line_item) unless line_item.variant.can_supply? quantity
end
def add_out_of_stock_error(line_item)
variant = line_item.variant
display_name = %Q{#{variant.name}}
display_name += %Q{ (#{variant.options_text})} unless variant.options_text.blank?
line_item.errors[:quantity] << Spree.t(:out_of_stock, :scope => :order_populator, :item => display_name.inspect)
display_name = %{#{variant.name}}
display_name += %{(#{variant.options_text})} if variant.options_text.present?
line_item.errors[:quantity] << Spree.t(:out_of_stock,
scope: :order_populator,
item: display_name.inspect)
end
end