diff --git a/app/helpers/order_cycles_helper.rb b/app/helpers/order_cycles_helper.rb index 7b50c47a11..e5d0d8ea3a 100644 --- a/app/helpers/order_cycles_helper.rb +++ b/app/helpers/order_cycles_helper.rb @@ -24,9 +24,8 @@ module OrderCyclesHelper def order_cycle_options @order_cycles. - select { |oc| oc.distributors.include? current_distributor }. - map { |oc| [order_cycle_close_to_s(oc.orders_close_at), - oc.id] } + with_distributor(current_distributor). + map { |oc| [order_cycle_close_to_s(oc.orders_close_at), oc.id] } end def order_cycle_close_to_s(orders_close_at) diff --git a/app/models/order_cycle.rb b/app/models/order_cycle.rb index dc8e9dd77e..db977a86df 100644 --- a/app/models/order_cycle.rb +++ b/app/models/order_cycle.rb @@ -11,15 +11,21 @@ class OrderCycle < ActiveRecord::Base validates_presence_of :name, :coordinator_id - scope :active, lambda { where('orders_open_at <= ? AND orders_close_at >= ?', Time.now, Time.now) } - scope :active_or_complete, lambda { where('orders_open_at <= ?', Time.now) } - scope :inactive, lambda { where('orders_open_at > ? OR orders_close_at < ?', Time.now, Time.now) } + scope :active, lambda { where('order_cycles.orders_open_at <= ? AND order_cycles.orders_close_at >= ?', Time.now, Time.now) } + scope :active_or_complete, lambda { where('order_cycles.orders_open_at <= ?', Time.now) } + scope :inactive, lambda { where('order_cycles.orders_open_at > ? OR order_cycles.orders_close_at < ?', Time.now, Time.now) } - scope :distributing_product, lambda { |product| joins(:exchanges => :variants). - where('exchanges.sender_id = order_cycles.coordinator_id'). + scope :distributing_product, lambda { |product| + joins(:exchanges => :variants). + merge(Exchange.outgoing). where('spree_variants.id IN (?)', product.variants_including_master.map(&:id)). select('DISTINCT order_cycles.*') } + scope :with_distributor, lambda { |distributor| + joins(:exchanges).merge(Exchange.outgoing).where('exchanges.receiver_id = ?', distributor) + } + + scope :managed_by, lambda { |user| if user.has_spree_role?('admin') scoped diff --git a/spec/models/order_cycle_spec.rb b/spec/models/order_cycle_spec.rb index 0dac0a839c..b22bfcf271 100644 --- a/spec/models/order_cycle_spec.rb +++ b/spec/models/order_cycle_spec.rb @@ -66,6 +66,26 @@ describe OrderCycle do end end + describe "finding order cycles with a particular distributor" do + let(:c) { create(:supplier_enterprise) } + let(:d) { create(:distributor_enterprise) } + + it "returns order cycles with that distributor" do + oc = create(:simple_order_cycle, coordinator: c, distributors: [d]) + OrderCycle.with_distributor(d).should == [oc] + end + + it "does not return order cycles with that enterprise as supplier" do + oc = create(:simple_order_cycle, coordinator: c, suppliers: [d]) + OrderCycle.with_distributor(d).should == [] + end + + it "does not return order cycles without that distributor" do + oc = create(:simple_order_cycle, coordinator: c) + OrderCycle.with_distributor(d).should == [] + end + end + it "reports its suppliers" do oc = create(:simple_order_cycle)