Class: CustomerStatisticsReport

Inherits:
Report
  • Object
show all
Defined in:
app/reports/customer_statistics_report.rb

Instance Attribute Summary

Attributes inherited from Report

#options, #results

Instance Method Summary collapse

Methods inherited from Report

build, parameters, parse_date, #report_key

Constructor Details

#initialize(options = nil) ⇒ CustomerStatisticsReport

Returns a new instance of CustomerStatisticsReport.



3
4
5
6
# File 'app/reports/customer_statistics_report.rb', line 3

def initialize(options = nil)
  options[:year] = (options[:year] || Date.current.year).to_i
  super(options)
end

Instance Method Details

#performObject



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'app/reports/customer_statistics_report.rb', line 8

def perform
  sql_select = <<-EOS
    date_part('month', shipped_date) as month, 
    currency, count(distinct customer_id) as number_of_customers, 
    sum(total - COALESCE(shipping_cost,0) - COALESCE(shipping_coupon,0) - COALESCE(tax_total,0)) as order_sum, 
    avg(total - COALESCE(shipping_cost,0) - COALESCE(shipping_coupon,0) - COALESCE(tax_total,0)) as order_avg, 
    count(id) as order_count
  EOS
  data = Order.where("date_part('year', shipped_date) = ?", @options[:year]).
                   where("total > 0").
                   where(:state => 'invoiced')
  data = data.where(:currency => @options[:currency]) if @options[:currency]
  data = data.where("COALESCE(primary_sales_rep_id,secondary_sales_rep_id,local_sales_rep_id) = ?",@options[:sales_rep_id]) if @options[:sales_rep_id]

  data = data.select(sql_select).
             group("date_part('month', shipped_date), currency")


  @results = {}
  @results[:data] = data
  @results
end