Add basic action for enterprise fee summary

This commit is contained in:
Kristina Lim
2018-10-09 14:36:56 +08:00
committed by luisramos0
parent b7439d2574
commit ec81e4221f
7 changed files with 109 additions and 0 deletions

View File

@@ -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

View File

@@ -0,0 +1 @@
= render "filters"

View File

@@ -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

View File

@@ -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'

View File

@@ -0,0 +1,5 @@
module OpenFoodNetwork
module Reports
class UnsupportedReportFormatException < StandardError; end
end
end

View File

@@ -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