mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-02-27 01:43:22 +00:00
Working filters for Product&Inventory Report
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
module OpenFoodNetwork
|
||||
|
||||
class ProductsAndInventoryReport
|
||||
attr_reader :params
|
||||
def initialize(user, params = {})
|
||||
@user = user
|
||||
@params = params
|
||||
@@ -48,5 +49,45 @@ module OpenFoodNetwork
|
||||
AND other_spree_variants.is_master = 'f' LIMIT 1) IS NULL")
|
||||
.merge(Spree::Product.managed_by(@user))
|
||||
end
|
||||
|
||||
def filter(variants)
|
||||
filter_on_hand filter_to_supplier filter_to_distributor filter_to_order_cycle variants
|
||||
end
|
||||
|
||||
def filter_on_hand(variants)
|
||||
if params[:report_type] == "inventory"
|
||||
variants.where("spree_variants.count_on_hand > 0")
|
||||
else
|
||||
variants
|
||||
end
|
||||
end
|
||||
|
||||
def filter_to_supplier(variants)
|
||||
if params[:supplier_id].to_i > 0
|
||||
variants.where("spree_products.supplier_id = ?", params[:supplier_id])
|
||||
else
|
||||
variants
|
||||
end
|
||||
end
|
||||
|
||||
def filter_to_distributor(variants)
|
||||
if params[:distributor_id].to_i > 0
|
||||
distributor = Enterprise.find params[:distributor_id]
|
||||
variants.select do |v|
|
||||
Enterprise.distributing_product(v.product_id).include? distributor
|
||||
end
|
||||
else
|
||||
variants
|
||||
end
|
||||
end
|
||||
|
||||
def filter_to_order_cycle(variants)
|
||||
if params[:order_cycle_id].to_i > 0
|
||||
order_cycle = OrderCycle.find params[:order_cycle_id]
|
||||
variants.select! { |v| order_cycle.variants.include? v }
|
||||
else
|
||||
variants
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -89,6 +89,48 @@ module OpenFoodNetwork
|
||||
end
|
||||
end
|
||||
|
||||
describe "Filtering variants" do
|
||||
let(:variants) { Spree::Variant.scoped.joins(:product) }
|
||||
it "should return unfiltered variants sans-params" do
|
||||
product1 = create(:simple_product, supplier: supplier, on_hand: 99)
|
||||
product2 = create(:simple_product, supplier: supplier, on_hand: 0)
|
||||
subject.filter(Spree::Variant.scoped).should == [product1.master, product2.master]
|
||||
end
|
||||
describe "based on report type" do
|
||||
it "returns only variants on hand" do
|
||||
product1 = create(:simple_product, supplier: supplier, on_hand: 99)
|
||||
product2 = create(:simple_product, supplier: supplier, on_hand: 0)
|
||||
|
||||
subject.stub(:params).and_return(report_type: 'inventory')
|
||||
subject.filter(variants).should == [product1.master]
|
||||
end
|
||||
end
|
||||
it "filters to a specific supplier" do
|
||||
supplier2 = create(:supplier_enterprise)
|
||||
product1 = create(:simple_product, supplier: supplier)
|
||||
product2 = create(:simple_product, supplier: supplier2)
|
||||
|
||||
subject.stub(:params).and_return(supplier_id: supplier.id)
|
||||
subject.filter(variants).should == [product1.master]
|
||||
end
|
||||
it "filters to a specific distributor" do
|
||||
distributor = create(:distributor_enterprise)
|
||||
product1 = create(:simple_product, supplier: supplier)
|
||||
product2 = create(:simple_product, supplier: supplier, distributors: [distributor])
|
||||
|
||||
subject.stub(:params).and_return(distributor_id: distributor.id)
|
||||
subject.filter(variants).should == [product2.master]
|
||||
end
|
||||
it "filters to a specific order cycle" do
|
||||
distributor = create(:distributor_enterprise)
|
||||
product1 = create(:simple_product, supplier: supplier, distributors: [distributor])
|
||||
product2 = create(:simple_product, supplier: supplier, distributors: [distributor])
|
||||
order_cycle = create(:simple_order_cycle, suppliers: [supplier], distributors: [distributor], variants: [product1.master])
|
||||
|
||||
subject.stub(:params).and_return(order_cycle_id: order_cycle.id)
|
||||
subject.filter(variants).should == [product1.master]
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
it "should fetch variants"
|
||||
|
||||
Reference in New Issue
Block a user