diff --git a/app/controllers/spree/admin/reports_controller_decorator.rb b/app/controllers/spree/admin/reports_controller_decorator.rb index 73c5752233..150a766cc5 100644 --- a/app/controllers/spree/admin/reports_controller_decorator.rb +++ b/app/controllers/spree/admin/reports_controller_decorator.rb @@ -3,13 +3,14 @@ require 'open_food_network/order_and_distributor_report' require 'open_food_network/products_and_inventory_report' require 'open_food_network/group_buy_report' require 'open_food_network/order_grouper' +require 'open_food_network/customers_report' require 'open_food_network/model_class_from_controller_name' Spree::Admin::ReportsController.class_eval do include OpenFoodNetwork::ModelClassFromControllerName # Fetches user's distributors, suppliers and order_cycles - before_filter :load_data, only: :products_and_inventory + before_filter :load_data, only: [:customers, :products_and_inventory] # Render a partial for orders and fulfillment description respond_override :index => { :html => { :success => lambda { @@ -35,9 +36,19 @@ Spree::Admin::ReportsController.class_eval do products_and_inventory: [ ['All products', :all_products], ['Inventory (on hand)', :inventory] + ], + customers: [ + ["Mailing List", :mailing_list], + ["Addresses", :addresses] ] } + def customers + @search = Spree::Order.search + @report_types = REPORT_TYPES[:customers] + @report = OpenFoodNetwork::CustomersReport.new spree_current_user, params + end + def orders_and_distributors params[:q] = {} unless params[:q] @@ -541,8 +552,8 @@ Spree::Admin::ReportsController.class_eval do @report_types = REPORT_TYPES[:products_and_inventory] @report = OpenFoodNetwork::ProductsAndInventoryReport.new spree_current_user, params - @table = @report.table - @header = @report.header + #@table = @report.table + #@header = @report.header end def render_report (header, table, create_csv, csv_file_name) @@ -575,6 +586,7 @@ Spree::Admin::ReportsController.class_eval do :bulk_coop => {:name => "Bulk Co-Op", :description => "Reports for Bulk Co-Op orders"}, :payments => {:name => "Payment Reports", :description => "Reports for Payments"}, :orders_and_fulfillment => {:name => "Orders & Fulfillment Reports", :description => ''}, + :customers => {:name => "Customers", :description => 'Customer details'}, :products_and_inventory => {:name => "Products & Inventory", :description => ''} } if spree_current_user.has_spree_role? 'admin' diff --git a/app/views/spree/admin/reports/customers.html.haml b/app/views/spree/admin/reports/customers.html.haml new file mode 100644 index 0000000000..d2a1a7be57 --- /dev/null +++ b/app/views/spree/admin/reports/customers.html.haml @@ -0,0 +1,8 @@ += form_for @search, :url => spree.customers_admin_reports_path do |s| + + %br + = button t(:search) + +%br +%br +%table#listing_customers.index diff --git a/app/views/spree/admin/reports/products_and_inventory.html.haml b/app/views/spree/admin/reports/products_and_inventory.html.haml index 57cfaf4b65..23228ffb71 100644 --- a/app/views/spree/admin/reports/products_and_inventory.html.haml +++ b/app/views/spree/admin/reports/products_and_inventory.html.haml @@ -33,13 +33,13 @@ %table#listing_products.index %thead %tr{'data-hook' => "products_header"} - - @header.each do |heading| + - @report.header.each do |heading| %th=heading %tbody - - @table.each do |row| + - @report.table.each do |row| %tr - row.each do |column| %td= column - - if @table.empty? + - if @report.table.empty? %tr %td{:colspan => "2"}= t(:none) diff --git a/config/routes.rb b/config/routes.rb index 269afec0a9..d96ba028e9 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -58,6 +58,7 @@ Spree::Core::Engine.routes.prepend do match '/admin/reports/orders_and_fulfillment' => 'admin/reports#orders_and_fulfillment', :as => "orders_and_fulfillment_admin_reports", :via => [:get, :post] match '/admin/products/bulk_edit' => 'admin/products#bulk_edit', :as => "bulk_edit_admin_products" match '/admin/reports/products_and_inventory' => 'admin/reports#products_and_inventory', :as => "products_and_inventory_admin_reports", :via => [:get, :post] + match '/admin/reports/customers' => 'admin/reports#customers', :as => "customers_admin_reports", :via => [:get, :post] namespace :api, :defaults => { :format => 'json' } do diff --git a/spec/controllers/spree/admin/reports_controller_spec.rb b/spec/controllers/spree/admin/reports_controller_spec.rb index cd87122d46..af4268a8e0 100644 --- a/spec/controllers/spree/admin/reports_controller_spec.rb +++ b/spec/controllers/spree/admin/reports_controller_spec.rb @@ -190,10 +190,55 @@ describe Spree::Admin::ReportsController do OpenFoodNetwork::ProductsAndInventoryReport.should_receive(:new) .with(user, {"test"=>"foo", "controller"=>"spree/admin/reports", "action"=>"products_and_inventory"}) .and_return(report = double(:report)) - report.stub(:table).and_return {} - report.stub(:header).and_return [] spree_get :products_and_inventory, :test => "foo" assigns(:report).should == report end end + + context "My Customers" do + let(:user) do + user = create(:user) + user.spree_roles << Spree::Role.find_or_create_by_name!('admin') + user + end + before do + controller.stub spree_current_user: user + end + + it "should have report types for customers" do + Spree::Admin::ReportsController::REPORT_TYPES[:customers].should == [ + ["Mailing List", :mailing_list], + ["Addresses", :addresses] + ] + end + + it "should build distributors for the current user" do + spree_get :customers + assigns(:distributors).should == [d1, d2, d3] + end + + it "builds suppliers for the current user" do + spree_get :customers + assigns(:suppliers).should == [s1, s2, s3] + end + + it "builds order cycles for the current user" do + spree_get :customers + assigns(:order_cycles).should == [ocB, ocA] + end + + it "assigns report types" do + spree_get :customers + assigns(:report_types).should == Spree::Admin::ReportsController::REPORT_TYPES[:customers] + end + + it "creates a CustomersReport" do + OpenFoodNetwork::CustomersReport.should_receive(:new) + .with(user, {"test"=>"foo", "controller"=>"spree/admin/reports", "action"=>"customers"}) + .and_return(report = double(:report)) + spree_get :customers, :test => "foo" + assigns(:report).should == report + end + end + end