From 2a890f80f42de4e25f084e6a4495758e27ea51da Mon Sep 17 00:00:00 2001 From: Rohan Mitchell Date: Fri, 8 Mar 2013 16:48:35 +1100 Subject: [PATCH] Enterprise.active_distributors doesn't show distributors of out of stock products --- app/models/enterprise.rb | 2 +- spec/models/enterprises_spec.rb | 6 +++++- 2 files changed, 6 insertions(+), 2 deletions(-) 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)