From ec81e4221f8df26dec04aa1e52f52956df80a1c5 Mon Sep 17 00:00:00 2001 From: Kristina Lim Date: Tue, 9 Oct 2018 14:36:56 +0800 Subject: [PATCH] Add basic action for enterprise fee summary --- ...nterprise_fee_summary_report_controller.rb | 52 +++++++++++++++++++ .../_filters.html.haml | 0 .../index.html.haml | 1 + config/locales/en.yml | 1 + config/routes/spree.rb | 1 + lib/open_food_network/reports.rb | 5 ++ ...rise_fee_summary_report_controller_spec.rb | 49 +++++++++++++++++ 7 files changed, 109 insertions(+) create mode 100644 app/controllers/spree/admin/reports/enterprise_fee_summary_report_controller.rb create mode 100644 app/views/spree/admin/reports/enterprise_fee_summary_report/_filters.html.haml create mode 100644 app/views/spree/admin/reports/enterprise_fee_summary_report/index.html.haml create mode 100644 lib/open_food_network/reports.rb create mode 100644 spec/controllers/spree/admin/reports/enterprise_fee_summary_report_controller_spec.rb diff --git a/app/controllers/spree/admin/reports/enterprise_fee_summary_report_controller.rb b/app/controllers/spree/admin/reports/enterprise_fee_summary_report_controller.rb new file mode 100644 index 0000000000..3f6f9e957e --- /dev/null +++ b/app/controllers/spree/admin/reports/enterprise_fee_summary_report_controller.rb @@ -0,0 +1,52 @@ +require "open_food_network/reports" +require "order_management/reports/enterprise_fee_summary/parameters" +require "order_management/reports/enterprise_fee_summary/report_service" +require "order_management/reports/enterprise_fee_summary/renderers/csv_renderer" + +module Spree + module Admin + module Reports + class EnterpriseFeeSummaryReportController < BaseController + def index + return render_report_form if params[:report].blank? + return respond_to_invalid_parameters unless report_parameters.valid? + + service = report_klass::ReportService.new(report_parameters, report_renderer_klass) + send_data service.render, filename: service.filename + end + + private + + def respond_to_invalid_parameters + flash[:error] = I18n.t("invalid_filter_parameters", scope: i18n_scope) + render_report_form + end + + def i18n_scope + "order_management.reports.enterprise_fee_summary" + end + + def render_report_form + render action: :index + end + + def report_klass + OrderManagement::Reports::EnterpriseFeeSummary + end + + def report_parameters + @report_parameters ||= report_klass::Parameters.new(params[:report]) + end + + def report_renderer_klass + case params[:report_format] + when "csv" + report_klass::Renderers::CsvRenderer + else + raise OpenFoodNetwork::Reports::UnsupportedReportFormatException + end + end + end + end + end +end diff --git a/app/views/spree/admin/reports/enterprise_fee_summary_report/_filters.html.haml b/app/views/spree/admin/reports/enterprise_fee_summary_report/_filters.html.haml new file mode 100644 index 0000000000..e69de29bb2 diff --git a/app/views/spree/admin/reports/enterprise_fee_summary_report/index.html.haml b/app/views/spree/admin/reports/enterprise_fee_summary_report/index.html.haml new file mode 100644 index 0000000000..790853ca1f --- /dev/null +++ b/app/views/spree/admin/reports/enterprise_fee_summary_report/index.html.haml @@ -0,0 +1 @@ += render "filters" diff --git a/config/locales/en.yml b/config/locales/en.yml index 119a0fe200..19e5295b8d 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -2700,6 +2700,7 @@ See the %{link} to find out more about %{sitename}'s features and to start using fee_calculated_on_transfer_through_name: "Fee Calc on Transfer Through" tax_category_name: "Tax Category" total_amount: "$$ SUM" + invalid_filter_parameters: "The filters you selected for this report are invalid." spree: # TODO: remove `email` key once we get to Spree 2.0 diff --git a/config/routes/spree.rb b/config/routes/spree.rb index 2e9dc0b5c8..ecba823fa1 100644 --- a/config/routes/spree.rb +++ b/config/routes/spree.rb @@ -25,6 +25,7 @@ Spree::Core::Engine.routes.prepend do 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] match '/admin/reports/xero_invoices' => 'admin/reports#xero_invoices', :as => "xero_invoices_admin_reports", :via => [:get, :post] + get "/admin/reports/enterprise_fee_summary", to: "admin/reports/enterprise_fee_summary_report#index", as: :enterprise_fee_summary_admin_reports match '/admin', :to => 'admin/overview#index', :as => :admin match '/admin/payment_methods/show_provider_preferences' => 'admin/payment_methods#show_provider_preferences', :via => :get put 'credit_cards/new_from_token', to: 'credit_cards#new_from_token' diff --git a/lib/open_food_network/reports.rb b/lib/open_food_network/reports.rb new file mode 100644 index 0000000000..5e6886b178 --- /dev/null +++ b/lib/open_food_network/reports.rb @@ -0,0 +1,5 @@ +module OpenFoodNetwork + module Reports + class UnsupportedReportFormatException < StandardError; end + end +end diff --git a/spec/controllers/spree/admin/reports/enterprise_fee_summary_report_controller_spec.rb b/spec/controllers/spree/admin/reports/enterprise_fee_summary_report_controller_spec.rb new file mode 100644 index 0000000000..7b058d233e --- /dev/null +++ b/spec/controllers/spree/admin/reports/enterprise_fee_summary_report_controller_spec.rb @@ -0,0 +1,49 @@ +require "spec_helper" + +describe Spree::Admin::Reports::EnterpriseFeeSummaryReportController, type: :controller do + let!(:admin) { create(:admin_user) } + + let(:current_user) { admin } + + before do + allow(controller).to receive(:spree_current_user) { admin } + end + + describe "#index" do + context "when there are no parameters" do + it "renders the report form" do + get :index + + expect(response).to be_success + expect(response).to render_template(view_template_path) + end + end + + context "when the parameters are valid" do + it "sends the generated report in the correct format" do + get :index, report: { start_at: "2018-10-09 07:30:00" }, report_format: "csv" + + expect(response).to be_success + expect(response.body).not_to be_blank + expect(response.header["Content-Type"]).to eq("text/csv") + end + end + + context "when the parameters are invalid" do + it "renders the report form with an error" do + get :index, report: { start_at: "invalid date" }, report_format: "csv" + + expect(flash[:error]).to eq(I18n.t("invalid_filter_parameters", scope: i18n_scope)) + expect(response).to render_template(view_template_path) + end + end + end + + def i18n_scope + "order_management.reports.enterprise_fee_summary" + end + + def view_template_path + "spree/admin/reports/enterprise_fee_summary_report/index" + end +end