diff --git a/app/models/enterprise.rb b/app/models/enterprise.rb index c90b46af63..2fe5c3c5a5 100644 --- a/app/models/enterprise.rb +++ b/app/models/enterprise.rb @@ -25,13 +25,21 @@ class Enterprise < ActiveRecord::Base scope :with_order_cycles_outer, joins('LEFT OUTER JOIN exchanges ON (exchanges.receiver_id = enterprises.id)'). joins('LEFT OUTER JOIN order_cycles ON (order_cycles.id = exchanges.order_cycle_id)') + scope :with_order_cycles_and_exchange_variants_outer, + with_order_cycles_outer. + joins('LEFT OUTER JOIN exchange_variants ON (exchange_variants.exchange_id = exchanges.id)'). + joins('LEFT OUTER JOIN spree_variants ON (spree_variants.id = exchange_variants.variant_id)') 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 AND order_cycles.orders_open_at <= ? AND order_cycles.orders_close_at >= ?)', Time.now, Time.now, Time.now). select('DISTINCT enterprises.*') } - + scope :distributing_product, lambda { |product| + with_distributed_products_outer.with_order_cycles_and_exchange_variants_outer. + where('product_distributions.product_id = ? OR spree_variants.product_id = ?', product, product). + select('DISTINCT enterprises.*') + } def has_supplied_products_on_hand? self.supplied_products.where('count_on_hand > 0').present? diff --git a/spec/models/enterprises_spec.rb b/spec/models/enterprises_spec.rb index 7b4548b431..27563cceab 100644 --- a/spec/models/enterprises_spec.rb +++ b/spec/models/enterprises_spec.rb @@ -60,17 +60,33 @@ describe Enterprise do end end + describe "with_distributed_active_products_on_hand" do + it "returns distributors with products in stock" do + d1 = create(:distributor_enterprise) + d2 = create(:distributor_enterprise) + d3 = create(:distributor_enterprise) + d4 = create(:distributor_enterprise) + create(:product, :distributors => [d1, d2], :on_hand => 5) + create(:product, :distributors => [d1], :on_hand => 5) + create(:product, :distributors => [d3], :on_hand => 0) - it "returns distributors with products in stock" do - d1 = create(:distributor_enterprise) - d2 = create(:distributor_enterprise) - d3 = create(:distributor_enterprise) - d4 = create(:distributor_enterprise) - create(:product, :distributors => [d1, d2], :on_hand => 5) - create(:product, :distributors => [d1], :on_hand => 5) - create(:product, :distributors => [d3], :on_hand => 0) + Enterprise.with_distributed_active_products_on_hand.sort.should == [d1, d2] + end + end - Enterprise.with_distributed_active_products_on_hand.sort.should == [d1, d2] + describe "distributing_product" do + it "returns enterprises distributing via a product distribution" do + d = create(:distributor_enterprise) + p = create(:product, distributors: [d]) + Enterprise.distributing_product(p).should == [d] + end + + it "returns enterprises distributing via an order cycle" do + d = create(:distributor_enterprise) + p = create(:product) + oc = create(:simple_order_cycle, distributors: [d], variants: [p.master]) + Enterprise.distributing_product(p).should == [d] + end end end