From 1d46c2febdcd98b76e3b4ca96e1c05db9fd9cf3a Mon Sep 17 00:00:00 2001 From: Maikel Linke Date: Thu, 10 Dec 2015 17:39:35 +1100 Subject: [PATCH] LettuceShare report: display only if count_on_hand > 0 Use the inventory of a distributor if selected. --- app/models/spree/variant_decorator.rb | 11 ++++------- lib/open_food_network/lettuce_share_report.rb | 8 +++++++- .../products_and_inventory_report_base.rb | 8 ++------ .../products_and_inventory_report_spec.rb | 11 ++++++----- 4 files changed, 19 insertions(+), 19 deletions(-) diff --git a/app/models/spree/variant_decorator.rb b/app/models/spree/variant_decorator.rb index b9c9420287..9084214ea7 100644 --- a/app/models/spree/variant_decorator.rb +++ b/app/models/spree/variant_decorator.rb @@ -26,18 +26,15 @@ Spree::Variant.class_eval do after_save :update_units scope :with_order_cycles_inner, joins(exchanges: :order_cycle) - scope :with_order_cycles_outer, joins('LEFT OUTER JOIN exchange_variants AS o_exchange_variants ON (o_exchange_variants.variant_id = spree_variants.id)'). - joins('LEFT OUTER JOIN exchanges AS o_exchanges ON (o_exchanges.id = o_exchange_variants.exchange_id)'). - joins('LEFT OUTER JOIN order_cycles AS o_order_cycles ON (o_order_cycles.id = o_exchanges.order_cycle_id)') scope :not_deleted, where(deleted_at: nil) scope :in_stock, where('spree_variants.count_on_hand > 0 OR spree_variants.on_demand=?', true) scope :in_distributor, lambda { |distributor| - with_order_cycles_outer. - where('o_exchanges.incoming = ? AND o_exchanges.receiver_id = ?', false, distributor). - select('DISTINCT spree_variants.*') + where(id: ExchangeVariant.select(:variant_id). + joins(:exchange). + where('exchanges.incoming = ? AND exchanges.receiver_id = ?', false, distributor) + ) } - scope :in_order_cycle, lambda { |order_cycle| with_order_cycles_inner. merge(Exchange.outgoing). diff --git a/lib/open_food_network/lettuce_share_report.rb b/lib/open_food_network/lettuce_share_report.rb index f629ba93ae..206db6be3e 100644 --- a/lib/open_food_network/lettuce_share_report.rb +++ b/lib/open_food_network/lettuce_share_report.rb @@ -19,7 +19,13 @@ module OpenFoodNetwork end def table - variants.map do |variant| + if params[:distributor_id].to_i > 0 + distributor = Enterprise.find(params[:distributor_id]) + scoper = OpenFoodNetwork::ScopeVariantToHub.new(distributor) + variants.each { |v| scoper.scope(v) } + end + variants.select { |v| v.count_on_hand > 0 } + .map do |variant| [ variant.product.name, variant.full_name, diff --git a/lib/open_food_network/products_and_inventory_report_base.rb b/lib/open_food_network/products_and_inventory_report_base.rb index df5b656848..63205d59a6 100644 --- a/lib/open_food_network/products_and_inventory_report_base.rb +++ b/lib/open_food_network/products_and_inventory_report_base.rb @@ -28,8 +28,6 @@ module OpenFoodNetwork end def filter(variants) - # NOTE: Ordering matters. - # filter_to_order_cycle and filter_to_distributor return arrays not relations filter_to_distributor filter_to_order_cycle filter_on_hand filter_to_supplier filter_not_deleted variants end @@ -56,9 +54,7 @@ module OpenFoodNetwork def filter_to_distributor(variants) if params[:distributor_id].to_i > 0 distributor = Enterprise.find params[:distributor_id] - variants.select do |v| - Enterprise.distributing_product(v.product_id).include? distributor - end + variants.in_distributor(distributor) else variants end @@ -67,7 +63,7 @@ module OpenFoodNetwork def filter_to_order_cycle(variants) if params[:order_cycle_id].to_i > 0 order_cycle = OrderCycle.find params[:order_cycle_id] - variants.select { |v| order_cycle.variants.include? v } + variants.where(id: order_cycle.variants) else variants end diff --git a/spec/lib/open_food_network/products_and_inventory_report_spec.rb b/spec/lib/open_food_network/products_and_inventory_report_spec.rb index 13796c10f6..d9c8754a43 100644 --- a/spec/lib/open_food_network/products_and_inventory_report_spec.rb +++ b/spec/lib/open_food_network/products_and_inventory_report_spec.rb @@ -124,15 +124,16 @@ module OpenFoodNetwork it "filters to a specific distributor" do distributor = create(:distributor_enterprise) product1 = create(:simple_product, supplier: supplier) - product2 = create(:simple_product, supplier: supplier, distributors: [distributor]) + product2 = create(:simple_product, supplier: supplier) + order_cycle = create(:simple_order_cycle, suppliers: [supplier], distributors: [distributor], variants: [product2.variants.first]) subject.stub(:params).and_return(distributor_id: distributor.id) subject.filter(variants).should == [product2.variants.first] end it "filters to a specific order cycle" do distributor = create(:distributor_enterprise) - product1 = create(:simple_product, supplier: supplier, distributors: [distributor]) - product2 = create(:simple_product, supplier: supplier, distributors: [distributor]) + product1 = create(:simple_product, supplier: supplier) + product2 = create(:simple_product, supplier: supplier) order_cycle = create(:simple_order_cycle, suppliers: [supplier], distributors: [distributor], variants: [product1.variants.first]) subject.stub(:params).and_return(order_cycle_id: order_cycle.id) @@ -141,8 +142,8 @@ module OpenFoodNetwork it "should do all the filters at once" do distributor = create(:distributor_enterprise) - product1 = create(:simple_product, supplier: supplier, distributors: [distributor]) - product2 = create(:simple_product, supplier: supplier, distributors: [distributor]) + product1 = create(:simple_product, supplier: supplier) + product2 = create(:simple_product, supplier: supplier) order_cycle = create(:simple_order_cycle, suppliers: [supplier], distributors: [distributor], variants: [product1.variants.first]) subject.stub(:params).and_return(