mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-03-03 02:21:33 +00:00
41 lines
1.2 KiB
Ruby
41 lines
1.2 KiB
Ruby
class DistributionChangeValidator
|
|
|
|
def initialize order
|
|
@order = order
|
|
end
|
|
|
|
def can_change_distributor?
|
|
# Distributor may not be changed once an item has been added to the cart/order
|
|
@order.line_items.empty? || available_distributors(Enterprise.all).length > 1
|
|
end
|
|
|
|
def can_change_to_distributor? distributor
|
|
# Distributor may not be changed once an item has been added to the cart/order, unless all items are available from the specified distributor
|
|
@order.line_items.empty? || all_available_distributors.include?(distributor)
|
|
end
|
|
|
|
def product_compatible_with_current_order(product)
|
|
@order.nil? || available_distributors_for(product).present?
|
|
end
|
|
|
|
def available_distributors_for(product)
|
|
distributors = Enterprise.distributing_product(product)
|
|
|
|
if @order.andand.line_items.present?
|
|
distributors = available_distributors(distributors)
|
|
end
|
|
|
|
distributors
|
|
end
|
|
|
|
def all_available_distributors
|
|
@all_available_distributors ||= (available_distributors(Enterprise.all) || [])
|
|
end
|
|
|
|
def available_distributors enterprises
|
|
enterprises.select do |e|
|
|
(@order.line_item_variants - e.distributed_variants).empty?
|
|
end
|
|
end
|
|
end
|