Class: Report::SalesRepRanking::SalesRepRanking

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

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



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'app/services/report/sales_rep_ranking/sales_rep_ranking.rb', line 50

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

  sql = <<-SQL
    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

  results = ActiveRecord::Base.connection.execute(sql).to_a.map{ |r| r.symbolize_keys }
end

.total_report(options = {}) ⇒ Object



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
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'app/services/report/sales_rep_ranking/sales_rep_ranking.rb', line 8

def self.total_report(options = {})
  start_date = options[:period1_gteq]
  end_date = options[:period1_lteq]
  current_month_start_date = end_date.beginning_of_month
  report_groupings = options[:report_groupings].map(&:presence).compact
  sales_rep_ids = options[:primary_sales_rep_ids].map(&:presence).compact
  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}".to_sym] = d[:ranking].to_f : ''
        sr[:sales_rep_id] == d[:sales_rep_id] && d[:month] == m ? sr["m#{m}".to_sym] = 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