Add Spree::Variant.in_distributor

This commit is contained in:
Rohan Mitchell
2014-07-11 12:33:25 +10:00
parent edeb820c64
commit 0ecfee79d4
2 changed files with 27 additions and 0 deletions

View File

@@ -18,8 +18,17 @@ Spree::Variant.class_eval do
before_validation :update_weight_from_unit_value
after_save :update_units
scope :with_order_cycles_outer, joins('LEFT OUTER JOIN exchange_variants AS o_exchange_variants ON (o_exchange_variants.variant_id = spree_variants.id)').
joins('LEFT OUTER JOIN exchanges AS o_exchanges ON (o_exchanges.id = o_exchange_variants.exchange_id)').
joins('LEFT OUTER JOIN order_cycles AS o_order_cycles ON (o_order_cycles.id = o_exchanges.order_cycle_id)')
scope :not_deleted, where(deleted_at: nil)
scope :in_stock, where('spree_variants.count_on_hand > 0 OR spree_variants.on_demand=?', true)
scope :in_distributor, lambda { |distributor|
with_order_cycles_outer.
where('o_exchanges.incoming = ? AND o_exchanges.receiver_id = ?', false, distributor).
select('DISTINCT spree_variants.*')
}
def price_with_fees(distributor, order_cycle)

View File

@@ -28,6 +28,24 @@ module Spree
Variant.where(is_master: false).in_stock.sort.should == [@v_in_stock, @v_on_demand].sort
end
end
describe "finding variants in a distributor" do
let!(:d1) { create(:distributor_enterprise) }
let!(:d2) { create(:distributor_enterprise) }
let!(:p1) { create(:simple_product) }
let!(:p2) { create(:simple_product) }
let!(:oc1) { create(:simple_order_cycle, distributors: [d1], variants: [p1.master]) }
let!(:oc2) { create(:simple_order_cycle, distributors: [d2], variants: [p2.master]) }
it "shows variants in an order cycle distribution" do
Variant.in_distributor(d1).should == [p1.master]
end
it "doesn't show duplicates" do
oc_dup = create(:simple_order_cycle, distributors: [d1], variants: [p1.master])
Variant.in_distributor(d1).should == [p1.master]
end
end
end
describe "calculating the price with enterprise fees" do