diff --git a/app/models/enterprise.rb b/app/models/enterprise.rb index a79ed0b88a..eb3f3a2e8a 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 <= ? AND spree_products.count_on_hand > 0) 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 AND order_cycles.orders_open_at <= ? AND order_cycles.orders_close_at >= ?)', Time.now, Time.now, Time.now). select('DISTINCT enterprises.*') } diff --git a/spec/models/enterprises_spec.rb b/spec/models/enterprises_spec.rb index dacf8bbd72..86b6bc860f 100644 --- a/spec/models/enterprises_spec.rb +++ b/spec/models/enterprises_spec.rb @@ -51,7 +51,13 @@ describe Enterprise do Enterprise.active_distributors.should == [d] end - it "doesn't show distributors from inactive order cycles" + it "doesn't show distributors from inactive order cycles" do + s = create(:supplier_enterprise) + d = create(:distributor_enterprise) + p = create(:product) + create(:simple_order_cycle, suppliers: [s], distributors: [d], variants: [p.master], orders_open_at: 1.week.from_now, orders_close_at: 2.weeks.from_now) + Enterprise.active_distributors.should be_empty + end end