From 84346c7fbdf83dd67c4fdd0d13bd4aade51f2e69 Mon Sep 17 00:00:00 2001 From: Will Marshall Date: Wed, 20 Nov 2013 13:59:02 +1100 Subject: [PATCH] Starting specing P&I Report --- .../products_and_inventory_report.rb | 43 ++++++++++ .../products_and_inventory_report_spec.rb | 83 +++++++++++++++++++ 2 files changed, 126 insertions(+) create mode 100644 lib/open_food_network/products_and_inventory_report.rb create mode 100644 spec/lib/open_food_network/products_and_inventory_report_spec.rb diff --git a/lib/open_food_network/products_and_inventory_report.rb b/lib/open_food_network/products_and_inventory_report.rb new file mode 100644 index 0000000000..052a865d13 --- /dev/null +++ b/lib/open_food_network/products_and_inventory_report.rb @@ -0,0 +1,43 @@ +module OpenFoodNetwork + + class ProductsAndInventoryReport + def initialize(user, params = {}) + @user = user + @params = params + #@variants = fetch_variants + # Fetch filter(variants) + filter(master_variants) + # Fetch master variants + # + # Filter variants + # + # Merge variants + # + # Build table + end + + def header + ["Supplier", "Product", "SKU", "Variant", "On Hand", "Price"] + end + + def table + variants.map do |variant| + [variant.product.supplier.name, + variant.product.name, + variant.sku, + variant.options_text, + variant.count_on_hand, + variant.price] + end + end + + def variants + filter(child_variants) + filter(master_variants) + end + + def child_variants + Spree::Variant.where(:is_master => false) + .joins(:product) + .merge(Spree::Product.managed_by(@user)) + end + end +end diff --git a/spec/lib/open_food_network/products_and_inventory_report_spec.rb b/spec/lib/open_food_network/products_and_inventory_report_spec.rb new file mode 100644 index 0000000000..a6accc0722 --- /dev/null +++ b/spec/lib/open_food_network/products_and_inventory_report_spec.rb @@ -0,0 +1,83 @@ +require 'spec_helper' + +module OpenFoodNetwork + describe ProductsAndInventoryReport do + context "As a site admin" do + let(:user) do + user = create(:user) + user.spree_roles << Spree::Role.find_or_create_by_name!("admin") + user + end + subject do + ProductsAndInventoryReport.new user + end + + it "Should return headers" do + subject.header.should == ["Supplier", "Product", "SKU", "Variant", "On Hand", "Price"] + end + + it "should build a table from a list of variants" do + variant = double(:variant, sku: "sku", + options_text: "Variant Name", + count_on_hand: 10, + price: 100) + variant.stub_chain(:product, :supplier, :name).and_return("Supplier") + variant.stub_chain(:product, :name).and_return("Product Name") + subject.stub(:variants).and_return [variant] + subject.table.should == [[ + "Supplier", + "Product Name", + "sku", + "Variant Name", + 10, + 100]] + end + + it "fetches variants for some params" do + subject.should_receive(:child_variants).and_return ["children"] + subject.should_receive(:master_variants).and_return ["masters"] + subject.should_receive(:filter).with(['children']).and_return ["filter_children"] + subject.should_receive(:filter).with(['masters']).and_return ["filter_masters"] + subject.variants.should == ["filter_children", "filter_masters"] + end + end + + context "As an enterprise user" do + let(:supplier) { create(:supplier_enterprise) } + let(:enterprise_user) do + user = create(:user) + user.enterprise_roles.create(enterprise: supplier) + user.spree_roles = [] + user.save! + user + end + subject do + ProductsAndInventoryReport.new enterprise_user + end + describe "fetching child variants" do + it "returns some variants" do + product1 = create(:simple_product, supplier: supplier) + variant_1 = create(:variant, product: product1) + variant_2 = create(:variant, product: product1) + + subject.child_variants.sort.should == [variant_1, variant_2].sort + end + + it "should only return variants managed by the user" do + product1 = create(:simple_product, supplier: create(:supplier_enterprise)) + product2 = create(:simple_product, supplier: supplier) + variant_1 = create(:variant, product: product1) + variant_2 = create(:variant, product: product2) + + subject.child_variants.should == [variant_2] + end + end + + end + + it "should fetch variants" + it "should should fetch products without variants" + + it "should merge variants and products" + end +end