Class: Report::SalesRepRanking::SalesRepRanking

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

Overview

Service object: sales rep ranking.

Defined Under Namespace

Classes: Result

Class Method Summary collapse

Class Method Details

.get_data(start_date, end_date, report_groupings, sales_rep_ids, company_id) ⇒ Object

Get data.

Parameters:

  • start_date (Date)
  • end_date (Date)
  • report_groupings (Object)
  • sales_rep_ids (Array<Integer>)
  • company_id (Integer)

Returns:

  • (Object)


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
# File 'app/services/report/sales_rep_ranking/sales_rep_ranking.rb', line 74

def self.get_data(start_date, end_date, report_groupings, sales_rep_ids, company_id)
  report_groupings_sql = report_groupings.empty? ? "" : " and vsf.report_grouping in (#{report_groupings.map { |rg| "'#{rg.sub("'", "''")}'" }.join(',')})"
  sales_rep_ids_sql = sales_rep_ids.blank? ? "" : " and primary_sr_id in (#{sales_rep_ids.map(&:to_i).join(',')})"
  company_id_sql = company_id.zero? ? " " : " and vsf.company_id = #{company_id} "

  sql = <<-SQL.squish
    select primary_sr_id as sales_rep_id,full_name as sales_rep,month,
        (row_number() over (PARTITION BY month ORDER BY sum(quantity * consolidated_exchange_rate * discounted_price) desc))::int as ranking,
        (sum(quantity * consolidated_exchange_rate * discounted_price))::numeric as sales_value
    from view_sales_facts vsf
    inner join parties e on vsf.primary_sr_id = e.id and type = 'Employee' and inactive = false
    inner join analytic_date_time_dimensions dt on vsf.gl_date = dt.date
    where gl_date between '#{start_date}' and '#{end_date}'
    #{report_groupings_sql}
    #{sales_rep_ids_sql}
    #{company_id_sql}
    group by primary_sr_id,full_name,month
    union all
    select primary_sr_id as sales_rep_id,full_name as sales_rep,999 as month,
        (row_number() over (ORDER BY sum(quantity * consolidated_exchange_rate * discounted_price) desc))::int as ranking,
        (sum(quantity * consolidated_exchange_rate * discounted_price))::numeric as sales_value
    from view_sales_facts vsf
    inner join parties e on vsf.primary_sr_id = e.id and type = 'Employee' and inactive = false
    where gl_date between '#{start_date}' and '#{end_date}'
    #{report_groupings_sql}
    #{sales_rep_ids_sql}
    #{company_id_sql}
    group by primary_sr_id,full_name
  SQL

  ActiveRecord::Base.lease_connection.execute(sql).to_a.map(&:symbolize_keys)
end

.total_report(options = {}) ⇒ Result

Returns sales rep ranking data.

Parameters:

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

    report filter options

Options Hash (options):

  • period1_gteq (Date)

    start of the reporting period

  • period1_lteq (Date)

    end of the reporting period

  • report_groupings (Array<String>)

    report groupings to include (blank entries ignored)

  • primary_sales_rep_ids (Array<String>)

    sales reps to include (blank entries ignored)

  • company_ids (Array<String>, String)

    company country scope (blank means all)

Returns:

  • (Result)

    sales rep ranking data



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'app/services/report/sales_rep_ranking/sales_rep_ranking.rb', line 25

def self.total_report(options = {})
  start_date = options[:period1_gteq]
  end_date = options[:period1_lteq]
  end_date.beginning_of_month
  report_groupings = options[:report_groupings].filter_map(&:presence)
  sales_rep_ids = options[:primary_sales_rep_ids].filter_map(&:presence)
  company_id = if options[:company_ids].present?
                 Company.sales_companies.find_by(country_iso3: options[:company_ids])&.id || 0
               else
                 0
               end

  data = get_data(start_date, end_date, report_groupings, sales_rep_ids, company_id)
  months = []
  sales_reps = []
  data.map { |r| months << r[:month] if r[:month] <= 12 }
  months = months.uniq.sort
  data.map { |r| sales_reps << r if r[:month] == end_date.month }
  info = { r1: 0, m1: 0, r2: 0, m2: 0, r3: 0, m3: 0, r4: 0, m4: 0, r5: 0, m5: 0, r6: 0, m6: 0, r7: 0, m7: 0, r8: 0, m8: 0, r9: 0, m9: 0, r10: 0, m10: 0, r11: 0, m11: 0, r12: 0, m12: 0, rt: 0, t: 0 }
  sales_reps_info = sales_reps.map { |s| s.merge(info) }
  months.each do |m|
    sales_reps_info.each do |sr|
      data.each do |d|
        sr[:sales_rep_id] == d[:sales_rep_id] && d[:month] == m ? sr[:"r#{m}"] = d[:ranking].to_f : ''
        sr[:sales_rep_id] == d[:sales_rep_id] && d[:month] == m ? sr[:"m#{m}"] = d[:sales_value].to_f : ''
      end
    end
  end

  sales_reps_info.each do |sr|
    data.each do |d|
      sr[:sales_rep_id] == d[:sales_rep_id] && d[:month] == 999 ? sr[:rt] = d[:ranking].to_f : ''
      sr[:sales_rep_id] == d[:sales_rep_id] && d[:month] == 999 ? sr[:t] = d[:sales_value].to_f : ''
    end
  end

  Result.new(success: true,
             data: sales_reps_info,
             start_date: start_date,
             end_date: end_date)
end