Find all supplied and distributed taxons

This commit is contained in:
Rohan Mitchell
2015-05-22 10:57:14 +10:00
parent 3afd636577
commit 2c92b5a751
2 changed files with 64 additions and 0 deletions

View File

@@ -9,4 +9,40 @@ Spree::Taxon.class_eval do
#fs << Spree::ProductFilters.distributor_filter if Spree::ProductFilters.respond_to? :distributor_filter
fs
end
# Find all the taxons of supplied products for each enterprise, indexed by enterprise.
# Format: {enterprise_id => [taxon_id, ...]}
def self.supplied_taxons
taxons = {}
Spree::Taxon.
joins(:products => :supplier).
select('spree_taxons.*, enterprises.id AS enterprise_id').
each do |t|
taxons[t.enterprise_id.to_i] ||= Set.new
taxons[t.enterprise_id.to_i] << t.id
end
taxons
end
# Find all the taxons of distributed products for each enterprise, indexed by enterprise.
# Format: {enterprise_id => [taxon_id, ...]}
def self.distributed_taxons
taxons = {}
Spree::Taxon.
joins(:products).
merge(Spree::Product.with_order_cycles_outer).
where('o_exchanges.incoming = ?', false).
select('spree_taxons.*, o_exchanges.receiver_id AS enterprise_id').
each do |t|
taxons[t.enterprise_id.to_i] ||= Set.new
taxons[t.enterprise_id.to_i] << t.id
end
taxons
end
end

View File

@@ -0,0 +1,28 @@
require 'spec_helper'
module Spree
describe Taxon do
let(:e) { create(:supplier_enterprise) }
let(:t0) { p1.taxons.order('id ASC').first }
let(:t1) { create(:taxon) }
let(:t2) { create(:taxon) }
describe "finding all supplied taxons" do
let!(:p1) { create(:simple_product, supplier: e, taxons: [t1, t2]) }
it "finds taxons" do
Taxon.supplied_taxons.should == {e.id => Set.new([t0.id, t1.id, t2.id])}
end
end
describe "finding all distributed taxons" do
let!(:oc) { create(:simple_order_cycle, distributors: [e], variants: [p1.master]) }
let(:s) { create(:supplier_enterprise) }
let(:p1) { create(:simple_product, supplier: s, taxons: [t1, t2]) }
it "finds taxons" do
Taxon.distributed_taxons.should == {e.id => Set.new([t0.id, t1.id, t2.id])}
end
end
end
end