Skip to content

Instantly share code, notes, and snippets.

@wrburgess
Last active February 23, 2019 05:50
Show Gist options
  • Save wrburgess/4676144 to your computer and use it in GitHub Desktop.
Save wrburgess/4676144 to your computer and use it in GitHub Desktop.
How to add or extend Reports and Reporting on Spree

How to add or extend Reports and Reporting on Spree

Note: This pertains to Spree 1.2 or 1.3 as best we know

Override the Spree::ReportsController

See attached reports_controller.rb file

Add a view for your report

See attached promotions.html.erb file

Add new routes for your report

See attached routes.rb file

Add new translations to your app

See attached en.yml file

Restart your dev server

Routing may need to be updated

# config/locales/en.yml
en:
...
promotions: "Promotions"
promotions_report_description: "Promotions description"
<!-- app/views/spree/admin/reports/promotions.html.erb -->
<% content_for :page_title do %>
<%= t(:promotions) %>
<% end %>
<%= @example %>
<!-- report page content here -->
# app/controllers/spree/admin/reports_controller.rb
module Spree
module Admin
class ReportsController < Spree::Admin::BaseController
respond_to :html
AVAILABLE_REPORTS = {
:sales_total => { :name => I18n.t(:sales_total), :description => I18n.t(:sales_total_description) },
:promotions => { :name => I18n.t(:promotions), :description => I18n.t(:promotion_report_description) } #<= added
}
def index
@reports = AVAILABLE_REPORTS
respond_with(@reports)
end
def sales_total
params[:q] = {} unless params[:q]
if params[:q][:created_at_gt].blank?
params[:q][:created_at_gt] = Time.zone.now.beginning_of_month
else
params[:q][:created_at_gt] = Time.zone.parse(params[:q][:created_at_gt]).beginning_of_day rescue Time.zone.now.beginning_of_month
end
if params[:q] && !params[:q][:created_at_lt].blank?
params[:q][:created_at_lt] = Time.zone.parse(params[:q][:created_at_lt]).end_of_day rescue ""
end
if params[:q].delete(:completed_at_not_null) == "1"
params[:q][:completed_at_not_null] = true
else
params[:q][:completed_at_not_null] = false
end
params[:q][:s] ||= "created_at desc"
@search = Order.complete.ransack(params[:q])
@orders = @search.result
@totals = {}
@orders.each do |order|
@totals[order.currency] = { :item_total => ::Money.new(0, order.currency), :adjustment_total => ::Money.new(0, order.currency), :sales_total => ::Money.new(0, order.currency) } unless @totals[order.currency]
@totals[order.currency][:item_total] += order.display_item_total.money
@totals[order.currency][:adjustment_total] += order.display_adjustment_total.money
@totals[order.currency][:sales_total] += order.display_total.money
end
end
def promotions #<= added
@example = "Example"
end
end
end
end
# config/routes.rb
Peelstar::Application.routes.draw do
...
mount Spree::Core::Engine, at: 'store'
...
AppName::Core::Engine.routes.prepend do
root to: redirect("/")
namespace :admin do
...
resources :reports, :only => [:index, :show] do # <= add this block
collection do
get :sales_total
post :sales_total
get :promotions
post :promotions
end
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment