Class: Report::CustomerPerformance

Inherits:
Object
  • Object
show all
Defined in:
app/services/report/customer_performance.rb

Overview

Command pattern to encapsulate parameters necessary to run a customer performance report

Defined Under Namespace

Classes: CustomerPerformanceResult, PeriodData, Result

Constant Summary collapse

GROUPINGS =

Groupings.

{
  'Buying Group' => :buying_group_name,
  'Customer' => :customer_identity,
  'Profile' => :profile_name,
  'Primary Rep' => :primary_sales_rep_id,
  'Report Grouping' => :report_grouping,
  'State/Province' => :state_code
}.freeze
VALID_GROUPING =

Valid grouping.

GROUPINGS.values

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ CustomerPerformance

Returns a new instance of CustomerPerformance.

Parameters:

  • options (Hash) (defaults to: {})

    report filter options

Options Hash (options):

  • periods (Array<Range<Date>>)

    date ranges to report on (defaults to the current year)

  • grouping (Symbol)

    grouping dimension: :report_grouping, :buying_group, :profile, or :customer

  • company_ids (Array<Integer>)

    companies to include

  • buying_group_ids (Array<Integer>)

    buying groups to include

  • profile_ids (Array<Integer>)

    profiles to include

  • report_groupings (Array<String>)

    report groupings to include

  • state_codes (Array<String>)

    state codes to include

  • primary_sales_rep_ids (Array<Integer>)

    primary sales reps to include

Raises:

  • (ArgumentError)


41
42
43
44
45
46
# File 'app/services/report/customer_performance.rb', line 41

def initialize(options = {})
  @options = options
  @options[:periods] ||= [Date.current.all_year] # default range
  @options[:grouping] ||= :report_grouping # valid options are :buying_group or :profile or :customer
  raise ArgumentError, "Invalid grouping specified, must be one of #{VALID_GROUPING.join(', ')}" unless valid_grouping?
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



4
5
6
# File 'app/services/report/customer_performance.rb', line 4

def options
  @options
end

Instance Method Details

#groupingObject

Grouping.

Returns:

  • (Object)


50
51
52
# File 'app/services/report/customer_performance.rb', line 50

def grouping
  @options[:grouping]
end

#performObject

Perform.

Returns:

  • (Object)


62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'app/services/report/customer_performance.rb', line 62

def perform
  grouped_results = []

  sql_select = <<-EOS
    #{grouping} as group_identifier,
    max(company_id) as company_id,
    max(customer_id) as customer_id,
    max(customer_name) as customer_name,
    max(report_grouping) as report_grouping,
    max(buying_group_id) as buying_group_id,
    max(buying_group_name) as buying_group_name,
    max(profile_id) as profile_id,
    max(profile_name) as profile_name,
    max(primary_sales_rep_id) as primary_sales_rep_id,
    max(primary_sales_rep_full_name) as primary_sales_rep_full_name,
    max(state_code) as state_code,
    sum(revenue_consolidated) as total_sales_revenue_consolidated,
    count(id) as total_sales_count
  EOS

  # Summary
  summaries = periods.map { |p| Report::CustomerPerformance::PeriodData.new(period: p, sales_revenue: BigDecimal('0.0'), sales_count: 0) }

  periods.each_with_index do |period, index|
    # Run a query for sales for that period
    rel = ViewSalesByCustomer.where(gl_date: period)
                             .where("#{grouping} IS NOT NULL")
                             .group(grouping)
                             .select(sql_select)
                             .order(grouping)
    rel = rel.where(company_id: @options[:company_ids]) if @options[:company_ids].present?
    rel = rel.where(buying_group_id: @options[:buying_group_ids]) if @options[:buying_group_ids].present?
    rel = rel.where(profile_id: @options[:profile_ids]) if @options[:profile_ids].present?
    rel = rel.where(report_grouping: @options[:report_groupings]) if @options[:report_groupings].present?
    rel = rel.where(state_code: @options[:state_codes]) if @options[:state_codes].present?
    rel = rel.where(primary_sales_rep_id: @options[:primary_sales_rep_ids]) if @options[:primary_sales_rep_ids].present?
    # Loop through our result and build a hash for each group
    rel.each do |row|
      # See if we have this result already otherwise initialize it
      cpr = grouped_results.find { |r| r.group_identifier.to_s == row.group_identifier.to_s }
      if cpr.nil?
        # Initialize a new result instance, feeding it the periods
        cpr = Report::CustomerPerformance::CustomerPerformanceResult.build(grouping, row.group_identifier, periods)

        # Populate filter fields
        if grouping == :customer_identity
          cpr.customer_name = row.customer_name
          cpr.customer_id = row.customer_id
        end

        if grouping == :primary_sales_rep_id
          cpr.primary_sales_rep_ids = [row.primary_sales_rep_id]
        elsif @options[:primary_sales_rep_ids].present?
          cpr.primary_sales_rep_ids = @options[:primary_sales_rep_ids]
        end

        if grouping == :buying_group_name
          cpr.buying_group_ids = [row.buying_group_id]
        elsif @options[:buying_group_ids].present?
          cpr.buying_group_ids = @options[:buying_group_ids]
        end

        if grouping == :profile_name
          cpr.profile_ids = [row.profile_id]
        elsif @options[:profile_ids].present?
          cpr.profile_ids = @options[:profile_ids]
        end

        if grouping == :report_grouping
          cpr.report_groupings = [row.group_identifier]
        elsif @options[:report_groupings].present?
          cpr.report_groupings = @options[:report_groupings]
        end

        if grouping == :state_code
          cpr.state_codes = [row.group_identifier]
        elsif @options[:state_codes].present?
          cpr.state_codes = @options[:state_codes]
        end

        # Company id is only a filter
        cpr.company_ids = @options[:company_ids].presence

        grouped_results << cpr
      end

      # Update period index
      cpr.periods[index].attributes = { sales_revenue: row.total_sales_revenue_consolidated, sales_count: row.total_sales_count }
      summary_period_data = summaries[index]
      summary_period_data.sales_revenue += row.total_sales_revenue_consolidated
      summary_period_data.sales_count += row.total_sales_count
    end
  end
  Result.new(
    success: true,
    periods: periods,
    grouped_results: grouped_results,
    summaries: summaries
  )
end

#periodsObject

Periods.

Returns:

  • (Object)


56
57
58
# File 'app/services/report/customer_performance.rb', line 56

def periods
  @options[:periods]
end