diff --git a/lib/open_food_network/products_renderer.rb b/lib/open_food_network/products_renderer.rb index 87d9052a35..e6e3fbe460 100644 --- a/lib/open_food_network/products_renderer.rb +++ b/lib/open_food_network/products_renderer.rb @@ -34,17 +34,60 @@ module OpenFoodNetwork Spree::Product.where(id: distributed_products). order(taxon_order). - each { |product| scoper.scope(product) }. - select do |product| - product.has_stock_for_distribution?(@order_cycle, @distributor) - end + each { |product| scoper.scope(product) } end def distributed_products @order_cycle. variants_distributed_by(@distributor). - includes(:product). - select(:product_id) + merge(stocked_variants_with_overrides). + select("DISTINCT spree_variants.product_id") + end + + def stocked_variants_with_overrides + Spree::Variant. + joins("LEFT OUTER JOIN variant_overrides ON variant_overrides.variant_id = spree_variants.id AND variant_overrides.hub_id = #{@distributor.id}"). + joins(:stock_items). + where(query_stock_with_overrides) + end + + def query_stock_with_overrides + "( #{variant_not_overriden} AND ( #{variant_in_stock} OR #{variant_on_demand} ) ) + OR ( #{variant_overriden} AND ( #{override_on_demand} OR #{override_in_stock} ) ) + OR ( #{variant_overriden} AND ( #{override_on_demand_null} AND #{variant_on_demand} ) ) + OR ( #{variant_overriden} AND ( #{override_on_demand_null} AND #{variant_not_on_demand} AND #{variant_in_stock} ) )" + end + + def variant_not_overriden + "variant_overrides.id IS NULL" + end + + def variant_overriden + "variant_overrides.id IS NOT NULL" + end + + def variant_in_stock + "spree_stock_items.count_on_hand > 0" + end + + def variant_on_demand + "spree_stock_items.backorderable IS TRUE" + end + + def variant_not_on_demand + "spree_stock_items.backorderable IS FALSE" + end + + def override_on_demand + "variant_overrides.on_demand IS TRUE" + end + + def override_in_stock + "variant_overrides.count_on_hand > 0" + end + + def override_on_demand_null + "variant_overrides.on_demand IS NULL" end def scoper