diff --git a/app/models/enterprise.rb b/app/models/enterprise.rb index ce14997734..a79ed0b88a 100644 --- a/app/models/enterprise.rb +++ b/app/models/enterprise.rb @@ -35,7 +35,7 @@ class Enterprise < ActiveRecord::Base scope :active_distributors, lambda { with_distributed_products_outer.with_order_cycles_outer. - where('(product_distributions.product_id IS NOT NULL AND spree_products.deleted_at IS NULL AND spree_products.available_on <= ?) OR order_cycles.id IS NOT NULL', Time.now). + where('(product_distributions.product_id IS NOT NULL AND spree_products.deleted_at IS NULL AND spree_products.available_on <= ? AND spree_products.count_on_hand > 0) OR order_cycles.id IS NOT NULL', Time.now). select('DISTINCT enterprises.*') } diff --git a/spec/models/enterprises_spec.rb b/spec/models/enterprises_spec.rb index 03c63261c9..dacf8bbd72 100644 --- a/spec/models/enterprises_spec.rb +++ b/spec/models/enterprises_spec.rb @@ -37,7 +37,11 @@ describe Enterprise do Enterprise.active_distributors.should be_empty end - it "doesn't show distributors of out of stock products" + it "doesn't show distributors of out of stock products" do + d = create(:distributor_enterprise) + create(:product, :distributors => [d], :on_hand => 0) + Enterprise.active_distributors.should be_empty + end it "finds active distributors by order cycles" do s = create(:supplier_enterprise)