Class: Spree::AdvancedReport::IncrementReport

Inherits:
Spree::AdvancedReport show all
Defined in:
lib/spree/advanced_report/increment_report.rb

Direct Known Subclasses

Count, Profit, Revenue, Units

Defined Under Namespace

Classes: Count, Profit, Revenue, Units

Constant Summary collapse

INCREMENTS =
I18n.t("adv_report.increments").map(&:to_sym)

Instance Attribute Summary collapse

Attributes inherited from Spree::AdvancedReport

#data, #date_text, #orders, #params, #product, #product_in_taxon, #product_text, #ruportdata, #search, #taxon, #taxon_text, #unfiltered_params

Instance Method Summary collapse

Methods inherited from Spree::AdvancedReport

#date_range, #description, #download_url, #name, #order_count, #profit, #revenue, #units

Constructor Details

#initialize(params) ⇒ IncrementReport

Returns a new instance of IncrementReport.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/spree/advanced_report/increment_report.rb', line 5

def initialize(params)
  super(params)

  self.increments = INCREMENTS 
  self.ruportdata = INCREMENTS.inject({}) { |h, inc| h[inc] = Table(%w[key display value]); h }
  self.data = INCREMENTS.inject({}) { |h, inc| h[inc] = {}; h }

  self.dates = {
    I18n.t("adv_report.daily").downcase.to_sym => {
      :date_bucket => "%F",
      :date_display => "%m-%d-%Y",
      :header_display => I18n.t("adv_report.daily"),
    },
    I18n.t("adv_report.weekly").downcase.to_sym => {
      :header_display => I18n.t("adv_report.weekly")
    },
    I18n.t("adv_report.monthly").downcase.to_sym => {
      :date_bucket => "%Y-%m",
      :date_display => "%B %Y",
      :header_display => I18n.t("adv_report.monthly"),
    },
    I18n.t("adv_report.quarterly").downcase.to_sym => {
      :header_display => I18n.t("adv_report.quarterly")
    },
    I18n.t("adv_report.yearly").downcase.to_sym => {
      :date_bucket => "%Y",
      :date_display => "%Y",
      :header_display => I18n.t("adv_report.yearly"),
    }
  }
end

Instance Attribute Details

#all_dataObject

Returns the value of attribute all_data.



3
4
5
# File 'lib/spree/advanced_report/increment_report.rb', line 3

def all_data
  @all_data
end

#datesObject

Returns the value of attribute dates.



3
4
5
# File 'lib/spree/advanced_report/increment_report.rb', line 3

def dates
  @dates
end

#incrementsObject

Returns the value of attribute increments.



3
4
5
# File 'lib/spree/advanced_report/increment_report.rb', line 3

def increments
  @increments
end

#totalObject

Returns the value of attribute total.



3
4
5
# File 'lib/spree/advanced_report/increment_report.rb', line 3

def total
  @total
end

Instance Method Details

#format_totalObject



75
76
77
# File 'lib/spree/advanced_report/increment_report.rb', line 75

def format_total
  self.total 
end

#generate_ruport_dataObject



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/spree/advanced_report/increment_report.rb', line 37

def generate_ruport_data
  self.all_data = Table(%w[increment key display value]) 
  INCREMENTS.each do |inc|
    data[inc].each { |k,v| ruportdata[inc] << { "key" => k, "display" => v[:display], "value" => v[:value] } }
    ruportdata[inc].data.each do |p|
      self.all_data << { "increment" => inc.to_s.capitalize, "key" => p.data["key"], "display" => p.data["display"], "value" => p.data["value"] }
    end
    Rails.logger.info "THE DATES #{self.dates} : #{inc}"
    ruportdata[inc].sort_rows_by!(["key"])
    ruportdata[inc].remove_column("key")
    ruportdata[inc].rename_column("display", dates[inc][:header_display])
    ruportdata[inc].rename_column("value", self.name)
  end
  self.all_data.sort_rows_by!(["key"])
  self.all_data.remove_column("key")
  self.all_data = Grouping(self.all_data, :by => "increment") 
end

#get_bucket(type, completed_at) ⇒ Object



55
56
57
58
59
60
61
62
# File 'lib/spree/advanced_report/increment_report.rb', line 55

def get_bucket(type, completed_at)
  if type == I18n.t("adv_report.weekly").downcase.to_sym
    return completed_at.beginning_of_week.strftime("%Y-%m-%d")
  elsif type == I18n.t("adv_report.quarterly").downcase.to_sym
    return completed_at.beginning_of_quarter.strftime("%Y-%m")
  end
  completed_at.strftime(dates[type][:date_bucket])
end

#get_display(type, completed_at) ⇒ Object



64
65
66
67
68
69
70
71
72
73
# File 'lib/spree/advanced_report/increment_report.rb', line 64

def get_display(type, completed_at)
  if type == I18n.t("adv_report.weekly").downcase.to_sym
    #funny business
    #next_week = completed_at + 7
    return completed_at.beginning_of_week.strftime("%m-%d-%Y") # + ' - ' + next_week.beginning_of_week.strftime("%m-%d-%Y")
  elsif type == I18n.t("adv_report.quarterly").downcase.to_sym
    return completed_at.strftime("%Y") + ' Q' + (completed_at.beginning_of_quarter.strftime("%m").to_i/3 + 1).to_s
  end
  completed_at.strftime(dates[type][:date_display])
end