Any additions made to a Spree project should be stored in

vendor/extensions/site/

When Rails is up and running, code placed here will be rolled into Spree. If Spree is updated in the future then custom code shouldn’t be affected if it’s placed here.

Controller

The controller for the new report will be stored in

vendor/extensions/site/controllers/admin/

The controller code is based upon the reports controller found in the Spree code base here

app/controllers/admin/reports_controller.rb

Then create a new method, something like this:

 1 class Admin::ReportsController < Admin::BaseController
 2   before_filter :load_data  
 3   
 4   AVAILABLE_REPORTS = {
 5     :sales_total => {:name => "Sales Total", :description => "Sales Total For All Orders"},
 6     :backorders_total => {:name => "Backorders Total", :description => "Current Backorders Total"}
 7   }
 8 
 9   def index
10     @reports = AVAILABLE_REPORTS
11   end
12 
13   def sales_total
14     @search = Order.searchlogic(params[:search])
15     @search.checkout_complete = true
16     #set order by to default or form result
17     @search.order ||= "descend_by_created_at"
18     
19     @orders = @search.find(:all)    
20   
21     @item_total = @search.sum(:item_total)
22     @charge_total = @search.sum(:adjustment_total)
23     @credit_total = @search.sum(:credit_total)
24     @sales_total = @search.sum(:total)
25   end
26 
27   def backorders_total
28     @products = Product.all.map { |p| if p.master.on_backorder > 0 then p end }.compact
29   end
30 
31   private 
32   def load_data
33   end  
34 
35 end

View

The view for the report will be stored in

vendor/extensions/site/app/views/admin/reports/

This view should be based upon the reports view found in the Spree code base

vendor/extensions/theme_default/app/views/admin/reports/sales_total.html.erb

Here’s a custom report view (written in HAML which is included in Spree) called

vendor/extensions/site/app/views/admin/reports/backorders_total.html.haml
 1 %h1= t("sales_totals")
 2 %table.admin-report
 3   %thead
 4     %tr
 5       %th= t("name")
 6       %th= t("amount")
 7   %tbody
 8     - @products.each do |product|
 9       %tr
10         %th(scope="row")= product.name
11         %td(align="right")= product.variant.on_backorder

View index

You’ll also need to override the default reports index view

vendor/extensions/theme_default/app/views/admin/reports/index.html.erb

by creating a new

vendor/extensions/site/app/views/admin/reports/index/html.erb

like this

 1 %h1= t("listing_reports")
 2 %table.index
 3   %thead
 4     %tr
 5       %th= t("name")
 6       %th= t("description")
 7   %tbody
 8     - @reports.each do |key, value|
 9       %tr
10         %td= link_to t(value[:name].downcase.gsub(" ","_")), send("#{key}_admin_reports_url".to_sym)
11         %td= t(value[:description].downcase.gsub(" ","_"))

Route

You’ll need to add some routing information for the new report into

vendor/extensions/site/config/routes.rb

and it should look a bit like this

1 ActionController::Routing::Routes.draw do |map|
2    map.backorders_total_admin_reports '/admin/reports/backorders_total', :controller => 'admin/reports', :action => 'backorders_total'
3 end

Locale

As Spree can be configured for many different languages, not just English, we need to add phrase lookups for our new report into

vendor/extensions/site/config/locales/en-US.yml

Ours will look like this

1 ---
2 en-US:
3   backorders_total: "Backorders Total"
4   current_backorders_total: "Current Backorders Total"
blog comments powered by Disqus