diff --git a/app/controllers/spree/admin/reports_controller_decorator.rb b/app/controllers/spree/admin/reports_controller_decorator.rb index 06ba786101..0da4a37ace 100644 --- a/app/controllers/spree/admin/reports_controller_decorator.rb +++ b/app/controllers/spree/admin/reports_controller_decorator.rb @@ -1,5 +1,6 @@ require 'csv' 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/model_class_from_controller_name' @@ -535,7 +536,16 @@ Spree::Admin::ReportsController.class_eval do end def products_and_inventory + my_distributors = Enterprise.is_distributor.managed_by(spree_current_user) + my_suppliers = Enterprise.is_primary_producer.managed_by(spree_current_user) + distributors_of_my_products = Enterprise.with_distributed_products_outer.merge(Spree::Product.in_any_supplier(my_suppliers)) + @distributors = my_distributors | distributors_of_my_products + suppliers_of_products_I_distribute = my_distributors.map { |d| Spree::Product.in_distributor(d) }.flatten.map(&:supplier).uniq + @suppliers = my_suppliers | suppliers_of_products_I_distribute + @order_cycles = OrderCycle.active_or_complete.accessible_by(spree_current_user).order('orders_close_at DESC') + @report_types = REPORT_TYPES[:products_and_inventory] + @report = OpenFoodNetwork::ProductsAndInventoryReport.new params end def render_report (header, table, create_csv, csv_file_name) diff --git a/app/views/spree/admin/reports/_products_and_inventory_description.html.haml b/app/views/spree/admin/reports/_products_and_inventory_description.html.haml new file mode 100644 index 0000000000..f662298e3e --- /dev/null +++ b/app/views/spree/admin/reports/_products_and_inventory_description.html.haml @@ -0,0 +1,4 @@ +%ul{style: "margin-left: 12pt"} + - report_types.each do |report_type| + %li + = link_to report_type[0], "#{products_and_inventory_admin_reports_url}?report_type=#{report_type[1]}" diff --git a/app/views/spree/admin/reports/products_and_inventory.html.haml b/app/views/spree/admin/reports/products_and_inventory.html.haml new file mode 100644 index 0000000000..3fdef7127e --- /dev/null +++ b/app/views/spree/admin/reports/products_and_inventory.html.haml @@ -0,0 +1,45 @@ += form_for @search, :url => spree.products_and_inventory_admin_reports_url do |f| + %br + = label_tag nil, "Distributor: " + = select_tag(:distributor_id, + options_from_collection_for_select(@distributors, :id, :name, params[:distributor_id]), + :include_blank => true) + + %br + = label_tag nil, "Supplier: " + = select_tag(:supplier_id, + options_from_collection_for_select(@suppliers, :id, :name, params[:supplier_id]), + :include_blank => true) + + %br + = label_tag nil, "Order Cycle: " + - order_cycles_select = @order_cycles.collect {|oc| [ "#{oc.name}   (#{oc.orders_open_at.to_s(:short)} - #{oc.orders_close_at.to_s(:short)})".html_safe, oc.id ] } + = select_tag(:order_cycle_id, + options_for_select(order_cycles_select, params[:order_cycle_id]), + include_blank: true) + + %br + = label_tag nil, "Report Type: " + = select_tag(:report_type, options_for_select(@report_types, @report_type)) + + %br + %br + = check_box_tag :csv + = label_tag :csv, "Download as csv" + %br + = button t(:search) +%br +%br +%table#listing_products.index + %thead + %tr{'data-hook' => "products_header"} + - @header.each do |heading| + %th=heading + %tbody + - @table.each do |row| + %tr + - row.each do |column| + %td= column + - if @table.empty? + %tr + %td{:colspan => "2"}= t(:none) diff --git a/spec/controllers/spree/admin/reports_controller_spec.rb b/spec/controllers/spree/admin/reports_controller_spec.rb index 743b72cb4e..1a03ea386b 100644 --- a/spec/controllers/spree/admin/reports_controller_spec.rb +++ b/spec/controllers/spree/admin/reports_controller_spec.rb @@ -165,4 +165,43 @@ describe Spree::Admin::ReportsController do end end end + + context "Products & Inventory" 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 build distributors for the current user" do + spree_get :products_and_inventory + assigns(:distributors).should == [d1, d2, d3] + end + + it "builds suppliers for the current user" do + spree_get :products_and_inventory + assigns(:suppliers).should == [s1, s2, s3] + end + + it "builds order cycles for the current user" do + spree_get :products_and_inventory + assigns(:order_cycles).should == [ocB, ocA] + end + + it "assigns report types" do + spree_get :products_and_inventory + assigns(:report_types).should == Spree::Admin::ReportsController::REPORT_TYPES[:products_and_inventory] + end + + it "creates a ProductAndInventoryReport" do + OpenFoodNetwork::ProductsAndInventoryReport.should_receive(:new) + .with({"test"=>"foo", "controller"=>"spree/admin/reports", "action"=>"products_and_inventory"}) + .and_return({}) + spree_get :products_and_inventory, :test => "foo" + assigns(:report).should == {} + end + end end