Class: Report::OpportunitiesReport::OpportunitiesReport

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

Defined Under Namespace

Classes: Result

Class Method Summary collapse

Class Method Details

.amazonObject



559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
# File 'app/services/report/opportunities_report/opportunities_report.rb', line 559

def self.amazon
  sql = <<-SQL
      with tmp_customers as (
          select id,full_name from parties where id in (2385944,4267467,2741053,2742665,5768174,5322434)
      ), tmp_product_lines as (
          select id,trim(split_part(lineage_expanded,'>',1)) as group1,case when trim(split_part(lineage_expanded,'>',2)) = '' then 'N/A' else trim(split_part(lineage_expanded,'>',2)) end as group2
          from product_lines
      ), sales_amazon as (
          select customer_id,primary_product_line_id,asin,sum(previous_value)::numeric as previous_sales,sum(current_value)::numeric as current_sales,sum(last_week_value)::numeric as last_week_sales
          from (
              select customer_id,primary_product_line_id,coalesce(amazon_asin,'') as asin,upc,
              case when to_char(gl_date, 'YYYY')::int = to_char(date_trunc('year', now()), 'YYYY')::int - 1 then sum((quantity * (consolidated_exchange_rate * discounted_price))) else 0 end as previous_value,
              case when to_char(gl_date, 'YYYY')::int = to_char(date_trunc('year', now()), 'YYYY')::int then sum((quantity * (consolidated_exchange_rate * discounted_price))) else 0 end as current_value,
              case when gl_date between (current_date - INTERVAL '7 day')::date and (current_date - INTERVAL '1 day')::date then sum((quantity * (consolidated_exchange_rate * discounted_price))) else 0 end as last_week_value
              from view_sales_facts
              where (gl_date between date_trunc('year', now())::date and current_date::date or gl_date between (date_trunc('year', now())::date - INTERVAL '1 year') and (current_date - INTERVAL '1 year')::date )
              and customer_id in (2385944,4267467,2741053,2742665,5768174,5322434,92281)
              and order_id not in (919564,919565,919591,919592,919593)
              group by customer_id,primary_product_line_id,amazon_asin,upc,gl_date
          )a
          group by customer_id,primary_product_line_id,asin
      )
      select 'CN' || customer_id::varchar as code,full_name,group1,group2,asin,sum(previous_sales) as previous_sales,sum(current_sales) as current_sales,
      case when sum(previous_sales) = 0 and sum(current_sales) = 0 then 0
      when sum(previous_sales) = 0 then 100 else ((sum(current_sales) - sum(previous_sales)) / sum(previous_sales)) * 100 end as variation,
      sum(last_week_sales) as last_week_sales,case when sum(current_sales) = 0 then 0 else (sum(last_week_sales) / sum(current_sales)) * 100 end var_week
      from sales_amazon sf
      inner join tmp_customers tc on sf.customer_id = tc.id
      inner join tmp_product_lines pl on pl.id = sf.primary_product_line_id
      group by customer_id,full_name,group1,group2,asin
      order by customer_id,full_name,group1,7 desc;
  SQL

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

.list_of_opportunities_over_5k(start_date, end_date) ⇒ Object



231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
# File 'app/services/report/opportunities_report/opportunities_report.rb', line 231

def self.list_of_opportunities_over_5k(start_date,end_date)
  sql = <<-SQL
      select date,opportunity_id,opp_ref,opp_state,vof.report_grouping,open_activity,
      sr.full_name as sales_rep,lr.full_name as local_rep,t1.full_name as tech1,t2.full_name as tech2,
      consolidated_value,group1 as product_line
      from view_opportunities_facts vof
      left join parties sr on vof.primary_sr_id = sr.id
      left join parties lr on vof.local_sr_id = lr.id
      left join parties t1 on vof.technical_sr_id = t1.id
      left join parties t2 on vof.technical_sr_sec_id = t2.id
      where date between '#{start_date}' and '#{end_date}'
      and consolidated_value >= 5000
      order by opp_state desc
  SQL

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

.opportunities(date_start, period, amount, within_miles, origin_lat, origin_lng) ⇒ Object



49
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
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
# File 'app/services/report/opportunities_report/opportunities_report.rb', line 49

def self.opportunities(date_start,period,amount,within_miles,origin_lat,origin_lng)
  period_sql_new_opp = period == 'daily' ? " and date = ('#{date_start}'::date - INTERVAL '1 day')::date " : " and date between ('#{date_start}'::date - INTERVAL '7 day')::date and '#{date_start}'::date "
  period_sql_existing_opp = period == 'daily' ? " and date = ('#{date_start}'::date - INTERVAL '2 day')::date " : " and date between (('#{date_start}'::date - INTERVAL '8 day')::date - INTERVAL '#{period}')::date and ('#{date_start}'::date - INTERVAL '8 day')::date "
  period_sql_view_opp = period == 'daily' ? " and created_at::date = ('#{date_start}'::date - INTERVAL '1 day')::date " : " and created_at::date between ('#{date_start}'::date - INTERVAL '#{period}')::date and '#{date_start}'::date "
  period_sql_open_act = period == 'daily' ? " and vo.created_at::date = ('#{date_start}'::date - INTERVAL '1 day')::date " : " and vo.created_at::date between ('#{date_start}'::date - INTERVAL '#{period}')::date and '#{date_start}'::date "

  sql = <<-SQL
    with new_opportunities as (
        select
        customer_id,primary_sr_id,opportunity_id,reference_number_lq,last_quote_id,date,opp_state,cast(consolidated_value as numeric) as opp_consolidated_value,
        opp_reception_type,group1 as product_line,report_grouping
        from view_opportunities_facts opf
        where order_size = 5
        and opp_state in ('interest','untracked','promised','quoting','instant_quoting','qualify','follow_up','cancelled','abandoned')
        #{period_sql_new_opp}
        and consolidated_value >= #{amount}

    ), existing_opportunities as (
        select customer_id,primary_sr_id,opportunity_id,reference_number_lq,last_quote_id,date,opp_state,cast(consolidated_value as numeric) as opp_consolidated_value,
        opp_reception_type,group1 as product_line,report_grouping
        from view_opportunities_facts opf
        where order_size = 5
        and opp_state in ('interest','untracked','promised','quoting','instant_quoting','qualify','follow_up','cancelled','abandoned')
        #{period_sql_existing_opp}
        and consolidated_value >= #{amount}

    ), opp_customer as (
        select distinct customer_id
        from (
        select * from new_opportunities
        union ALL
        select * from existing_opportunities
        )a

    ), email as (
        select party_id, case when category = 'email' then detail else null end as category_email
        from contact_points
        inner join opp_customer on party_id = customer_id
        where category = 'email' and position = 1 and party_id is not null

    ), phone as (
        select party_id, case when category = 'phone' then detail else null end as category_phone
        from contact_points
        inner join opp_customer on party_id = customer_id
        where category = 'phone' and position = 1 and party_id is not null

    ), addresses as (
        select party_id,lat,lng
        from addresses
        inner join opp_customer on party_id = customer_id
        where party_id is not null

    ), view_opp as (
        select id,customer_full_name,primary_sales_rep_id,primary_sales_rep_full_name,open_sales_activity,last_sales_activity_completed_on,last_activity_notes,next_sales_activity_id
        from view_opportunities
        where contactable = true
        and state in ('interest','untracked','promised','quoting','instant_quoting','qualify','follow_up','cancelled','abandoned')
        and value >= #{amount}
        #{period_sql_view_opp}

    ), open_activities as (
        select act.id,opportunity_id,task_type,full_name as assigned_full_name,notes,cast(target_datetime as date) as target_datetime
        from activities act
        inner join view_opportunities vo on act.id = vo.next_sales_activity_id
        left join activity_types act_ty on activity_type_id = act_ty.id
        left join parties pa on assigned_resource_id = pa.id
        where contactable = true
        and vo.state in ('interest','untracked','promised','quoting','instant_quoting','qualify','follow_up','cancelled','abandoned')
        and value >= #{amount}
        #{period_sql_open_act}

    ), previous as (
        select
        opf.opportunity_id as opp_id,
        reference_number_lq as reference_number,
        last_quote_id as quote_id,
        date as date,
        opp_state as state,
        opf.customer_id,
        tvo.customer_full_name as customer,
        te.category_email as email,
        tp.category_phone as phone,
        tvo.primary_sales_rep_full_name as sales_rep,
        opp_consolidated_value as value,
        opp_reception_type as reception_type,
        product_line,
        report_grouping,
        coalesce(tvo.last_activity_notes,'') as notes,
        tvo.open_sales_activity as open_act,
        task_type,
        assigned_full_name,
        coalesce(toa.notes, '') as notes_open_act,
        target_datetime
        from new_opportunities opf
        inner join view_opp tvo on opf.opportunity_id = tvo.id
        left join email te on opf.customer_id = te.party_id
        left join phone tp on opf.customer_id = tp.party_id
        left join open_activities toa on tvo.next_sales_activity_id = toa.id
        order by date desc

    ), result1 as (
        select (case when #{origin_lat} = 0 and #{origin_lng} = 0 then 0 else (ACOS(COS(#{origin_lat})*COS(#{origin_lng})*COS(RADIANS(a.lat))*COS(RADIANS(a.lng))+COS(#{origin_lat})*SIN(#{origin_lng})*COS(RADIANS(a.lat))*SIN(RADIANS(a.lng))+SIN(#{origin_lat})*SIN(RADIANS(a.lat)))*3963) end)::double precision as distance,
              'new'::varchar as new_existing,'open'::varchar as open_closed,p.*
        from previous p
        left join addresses a on p.customer_id = a.party_id
        where state in ('interest','untracked','promised','quoting','instant_quoting','qualify','follow_up')
        and (email is not null or phone is not null)

    ), result2 as (
        select (case when #{origin_lat} = 0 and #{origin_lng} = 0 then 0 else (ACOS(COS(#{origin_lat})*COS(#{origin_lng})*COS(RADIANS(a.lat))*COS(RADIANS(a.lng))+COS(#{origin_lat})*SIN(#{origin_lng})*COS(RADIANS(a.lat))*SIN(RADIANS(a.lng))+SIN(#{origin_lat})*SIN(RADIANS(a.lat)))*3963) end)::double precision as distance,
              'new'::varchar as new_existing,'closed'::varchar as open_closed,p.*
        from previous p
        left join addresses a on p.customer_id = a.party_id
        where state in ('cancelled','abandoned')
        and (email is not null or phone is not null)

    ), previous2 as (
        select
        opf.opportunity_id as opp_id,
        reference_number_lq as reference_number,
        last_quote_id as quote_id,
        date as date,
        opp_state as state,
        opf.customer_id,
        tvo.customer_full_name as customer,
        te.category_email as email,
        tp.category_phone as phone,
        tvo.primary_sales_rep_full_name as sales_rep,
        opp_consolidated_value as value,
        opp_reception_type as reception_type,
        product_line,
        report_grouping,
        coalesce(tvo.last_activity_notes,'') as notes,
        tvo.open_sales_activity as open_act,
        task_type,
        assigned_full_name,
        coalesce(toa.notes, '') as notes_open_act,
        target_datetime
        from existing_opportunities opf
        inner join view_opp tvo on opf.opportunity_id = tvo.id
        left join email te on opf.customer_id = te.party_id
        left join phone tp on opf.customer_id = tp.party_id
        left join open_activities toa on tvo.next_sales_activity_id = toa.id
        order by date desc

    ), result3 as (
        select (case when #{origin_lat} = 0 and #{origin_lng} = 0 then 0 else (ACOS(COS(#{origin_lat})*COS(#{origin_lng})*COS(RADIANS(a.lat))*COS(RADIANS(a.lng))+COS(#{origin_lat})*SIN(#{origin_lng})*COS(RADIANS(a.lat))*SIN(RADIANS(a.lng))+SIN(#{origin_lat})*SIN(RADIANS(a.lat)))*3963) end)::double precision as distance,
              'existing'::varchar as new_existing_c,'open'::varchar as open_closed_c,p2.*
        from previous2 p2
        left join addresses a on p2.customer_id = a.party_id
        where state in ('interest','untracked','promised','quoting','instant_quoting','qualify','follow_up')
        and (email is not null or phone is not null)

    ), result4 as (
        select (case when #{origin_lat} = 0 and #{origin_lng} = 0 then 0 else (ACOS(COS(#{origin_lat})*COS(#{origin_lng})*COS(RADIANS(a.lat))*COS(RADIANS(a.lng))+COS(#{origin_lat})*SIN(#{origin_lng})*COS(RADIANS(a.lat))*SIN(RADIANS(a.lng))+SIN(#{origin_lat})*SIN(RADIANS(a.lat)))*3963) end)::double precision as distance,
              'existing'::varchar as new_existing_c,'closed'::varchar as open_closed_c,p2.*
        from previous2 p2
        left join addresses a on p2.customer_id = a.party_id
        where state in ('cancelled','abandoned')
        and (email is not null or phone is not null)

    )
    select distinct *
    from result1
    where distance <= #{within_miles}
    union all
    select distinct *
    from result2
    where distance <= #{within_miles}
    union all
    select distinct *
    from result3
    where distance <= #{within_miles}
    union all
    select distinct *
    from result4
    where distance <= #{within_miles};
  SQL

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

.opportunities_by_product_line(start_date) ⇒ Object



537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
# File 'app/services/report/opportunities_report/opportunities_report.rb', line 537

def self.opportunities_by_product_line(start_date)
  sql = <<-SQL
      select group1 as product_line,count(*) as total_opps,sum(consolidated_value) as total_amount,
      sum(case when opp_state = 'follow_up' then 1 else 0 end) as follow_up_qty,
      sum(case when opp_state = 'follow_up' then consolidated_value else 0 end) as follow_up_amount,
      sum(case when opp_state = 'won' then 1 else 0 end) as won_qty,
      sum(case when opp_state = 'won' then consolidated_value else 0 end) as won_amount,
      sum(case when opp_state = 'lost' then 1 else 0 end) as lost_qty,
      sum(case when opp_state = 'lost' then consolidated_value else 0 end) as lost_amount
      from (
          select distinct group1,opportunity_id,consolidated_value,opp_state
          from view_opportunities_facts
          where group1 is not null
          and date >= '#{start_date}'

      )a group by group1

  SQL

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

.opportunities_report_for_local_reps(start_date) ⇒ Object



398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
# File 'app/services/report/opportunities_report/opportunities_report.rb', line 398

def self.opportunities_report_for_local_reps(start_date)
  sql = <<-SQL
      with range_amount as (
          select 1 as range_id, '0-499' as range_value
          union all
          select 2,'500-999'
          union all
          select 3,'1000-1499'
          union all
          select 4,'1500-1999'
          union all
          select 5,'2000-2999'
          union all
          select 6,'3000-4900'
          union all
          select 7,'5000-7499'
          union all
          select 8,'7500-9999'
          union all
          select 9,'10000-29999'
          union all
          select 10,'30000+'

      ), opps_data as (
          select distinct local_sr_id,opportunity_id,consolidated_value,opp_state,
          case when consolidated_value between 0 and 499.9999 then 1
              when consolidated_value between 500 and 999.9999 then 2
              when consolidated_value between 1000 and 1499.9999 then 3
              when consolidated_value between 1500 and 1999.9999 then 4
              when consolidated_value between 2000 and 2999.9999 then 5
              when consolidated_value between 3000 and 4999.9999 then 6
              when consolidated_value between 5000 and 7499.9999 then 7
              when consolidated_value between 7500 and 9999.9999 then 8
              when consolidated_value between 10000 and 29999.9999 then 9
              when consolidated_value >= 30000 then 10 else 0 end as range_id
          from view_opportunities_facts
          where local_sr_id in (6,92)
          and date >= '#{start_date}'

      ), opps_by_tech as (
          select local_sr_id,range_id,opp_state,count(*) as num_opps,sum(consolidated_value) as opps_amount
          from opps_data
          group by local_sr_id,range_id,opp_state

      )
      select ra.range_id,ra.range_value,
      sum(case when local_sr_id = 6 then num_opps end) as mg_local_qty,
      sum(case when local_sr_id = 6 then opps_amount end) as mg_local_amount,
      sum(case when local_sr_id = 6 and opp_state = 'won' then num_opps end) as mg_local_won_qty,
      sum(case when local_sr_id = 6 and opp_state = 'won' then opps_amount end) as mg_local_won_amount,
      sum(case when local_sr_id = 92 then num_opps end) as gj_local_qty,
      sum(case when local_sr_id = 92 then opps_amount end) as gj_local_amount,
      sum(case when local_sr_id = 92 and opp_state = 'won' then num_opps end) as gj_local_won_qty,
      sum(case when local_sr_id = 92 and opp_state = 'won' then opps_amount end) as gj_local_won_amount
      from range_amount ra
      left join opps_by_tech obc on ra.range_id = obc.range_id
      group by ra.range_id,ra.range_value
  SQL

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

.opportunities_report_for_tech_reps(start_date) ⇒ Object



460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
# File 'app/services/report/opportunities_report/opportunities_report.rb', line 460

def self.opportunities_report_for_tech_reps(start_date)
  sql = <<-SQL
      with range_amount as (
          select 1 as range_id, '0-499' as range_value
          union all
          select 2,'500-999'
          union all
          select 3,'1000-1499'
          union all
          select 4,'1500-1999'
          union all
          select 5,'2000-2999'
          union all
          select 6,'3000-4900'
          union all
          select 7,'5000-7499'
          union all
          select 8,'7500-9999'
          union all
          select 9,'10000-29999'
          union all
          select 10,'30000+'

      ), opps_data as (
          select distinct technical_sr_id,opportunity_id,consolidated_value,
          case when consolidated_value between 0 and 499.9999 then 1
              when consolidated_value between 500 and 999.9999 then 2
              when consolidated_value between 1000 and 1499.9999 then 3
              when consolidated_value between 1500 and 1999.9999 then 4
              when consolidated_value between 2000 and 2999.9999 then 5
              when consolidated_value between 3000 and 4999.9999 then 6
              when consolidated_value between 5000 and 7499.9999 then 7
              when consolidated_value between 7500 and 9999.9999 then 8
              when consolidated_value between 10000 and 29999.9999 then 9
              when consolidated_value >= 30000 then 10 else 0 end as range_id
          from view_opportunities_facts
          where technical_sr_id in (82,68,226197,19617800)
          and date >= '#{start_date}'
          union all
          select distinct technical_sr_sec_id,opportunity_id,consolidated_value,
          case when consolidated_value between 0 and 499.9999 then 1
              when consolidated_value between 500 and 999.9999 then 2
              when consolidated_value between 1000 and 1499.9999 then 3
              when consolidated_value between 1500 and 1999.9999 then 4
              when consolidated_value between 2000 and 2999.9999 then 5
              when consolidated_value between 3000 and 4999.9999 then 6
              when consolidated_value between 5000 and 7499.9999 then 7
              when consolidated_value between 7500 and 9999.9999 then 8
              when consolidated_value between 10000 and 29999.9999 then 9
              when consolidated_value >= 30000 then 10 else 0 end as range_id
          from view_opportunities_facts
          where technical_sr_sec_id in (82,68,226197,19617800)
          and date >= '#{start_date}'

      ), opps_by_tech as (
          select technical_sr_id,range_id,count(*) as num_opps,sum(consolidated_value) as opps_amount
          from opps_data
          group by technical_sr_id,range_id

      )
      select ra.range_id,ra.range_value,
      sum(case when technical_sr_id = 82 then num_opps end) as sr_tech_qty,
      sum(case when technical_sr_id = 82 then opps_amount end) as sr_tech_amount,
      sum(case when technical_sr_id = 68 then num_opps end) as cs_tech_qty,
      sum(case when technical_sr_id = 68 then opps_amount end) as cs_tech_amount,
      sum(case when technical_sr_id = 226197 then num_opps end) as ai_tech_qty,
      sum(case when technical_sr_id = 226197 then opps_amount end) as ai_tech_amount,
      sum(case when technical_sr_id = 19617800 then num_opps end) as jb_tech_qty,
      sum(case when technical_sr_id = 19617800 then opps_amount end) as jb_tech_amount
      from range_amount ra
      left join opps_by_tech obc on ra.range_id = obc.range_id
      group by ra.range_id,ra.range_value
  SQL

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

.result_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
# File 'app/services/report/opportunities_report/opportunities_report.rb', line 8

def self.result_report(options = {})
  date_start = options[:date_start].to_date
  period = options[:period][0]
  amount = options[:amount][0].to_i

  (options[:within_miles][0] == '' || options[:within_miles] == nil) ? within_miles = 0 : within_miles = options[:within_miles][0].to_i
  if Geocoder.coordinates(options[:within_miles_of]) == nil || options[:within_miles_of] == '' || options[:within_miles].empty?
    lat,lng = 0,0
  else
    lat,lng = Geocoder.coordinates(options[:within_miles_of])
  end
  origin_lat, origin_lng = Math.deg2rad(lat), Math.deg2rad(lng)
  (lat.zero? && lng.zero? && within_miles == 0) ? flag = 0 : flag = 1

  data_opp = opportunities(date_start,period,amount,within_miles,origin_lat,origin_lng)
  start_date = Date.current.months_ago(1).beginning_of_month
  end_date = Date.current.months_ago(1).end_of_month
  list_opps_over_5k = list_of_opportunities_over_5k(start_date,end_date)
  total_opportunities_report_for_primary_reps = total_opportunities_report_for_primary_reps(start_date.beginning_of_year)
  won_opportunities_report_for_primary_reps = won_opportunities_report_for_primary_reps(start_date.beginning_of_year)
  opportunities_report_for_local_reps = opportunities_report_for_local_reps(start_date.beginning_of_year)
  opportunities_report_for_tech_reps = opportunities_report_for_tech_reps(start_date.beginning_of_year)
  opportunities_by_product_line = opportunities_by_product_line(start_date.beginning_of_year)

  data_amazon = amazon

  Result.new(success: true,
             data_opp: data_opp,
             data_amazon: data_amazon,
             list_opps_over_5k: list_opps_over_5k,
             total_opportunities_report_for_primary_reps: total_opportunities_report_for_primary_reps,
             won_opportunities_report_for_primary_reps: won_opportunities_report_for_primary_reps,
             opportunities_report_for_local_reps: opportunities_report_for_local_reps,
             opportunities_report_for_tech_reps: opportunities_report_for_tech_reps,
             opportunities_by_product_line: opportunities_by_product_line,
             start_date: start_date,
             end_date: end_date,
             flag: flag)

end

.total_opportunities_report_for_primary_reps(start_date) ⇒ Object



249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
# File 'app/services/report/opportunities_report/opportunities_report.rb', line 249

def self.total_opportunities_report_for_primary_reps(start_date)
  sql = <<-SQL
      with range_amount as (
          select 1 as range_id, '0-499' as range_value
          union all
          select 2,'500-999'
          union all
          select 3,'1000-1499'
          union all
          select 4,'1500-1999'
          union all
          select 5,'2000-2999'
          union all
          select 6,'3000-4900'
          union all
          select 7,'5000-7499'
          union all
          select 8,'7500-9999'
          union all
          select 9,'10000-29999'
          union all
          select 10,'30000+'

      ), opps_data as (
          select distinct primary_sr_id,opportunity_id,consolidated_value,
          case when consolidated_value between 0 and 499.9999 then 1
              when consolidated_value between 500 and 999.9999 then 2
              when consolidated_value between 1000 and 1499.9999 then 3
              when consolidated_value between 1500 and 1999.9999 then 4
              when consolidated_value between 2000 and 2999.9999 then 5
              when consolidated_value between 3000 and 4999.9999 then 6
              when consolidated_value between 5000 and 7499.9999 then 7
              when consolidated_value between 7500 and 9999.9999 then 8
              when consolidated_value between 10000 and 29999.9999 then 9
              when consolidated_value >= 30000 then 10 else 0 end as range_id
          from view_opportunities_facts
          where primary_sr_id in (531953,531955,6855834,166896,38,16775767,7210095,8,6910334,6)
          and date >= '#{start_date}'

      ), opps_by_tech as (
          select primary_sr_id,range_id,count(*) as num_opps,sum(consolidated_value) as opps_amount
          from opps_data
          group by primary_sr_id,range_id

      )
      select ra.range_id,ra.range_value,
      sum(case when primary_sr_id = 531953 then num_opps end) as js_rep_qty,
      sum(case when primary_sr_id = 531953 then opps_amount end) as js_rep_amount,
      sum(case when primary_sr_id = 531955 then num_opps end) as pb_rep_qty,
      sum(case when primary_sr_id = 531955 then opps_amount end) as pb_rep_amount,
      sum(case when primary_sr_id = 6855834 then num_opps end) as pl_rep_qty,
      sum(case when primary_sr_id = 6855834 then opps_amount end) as pl_rep_amount,
      sum(case when primary_sr_id = 166896 then num_opps end) as tb_rep_qty,
      sum(case when primary_sr_id = 166896 then opps_amount end) as tb_rep_amount,
      sum(case when primary_sr_id = 38 then num_opps end) as jw_rep_qty,
      sum(case when primary_sr_id = 38 then opps_amount end) as jw_rep_amount,
      sum(case when primary_sr_id = 16775767 then num_opps end) as mk_rep_qty,
      sum(case when primary_sr_id = 16775767 then opps_amount end) as mk_rep_amount,
      sum(case when primary_sr_id = 7210095 then num_opps end) as lh_rep_qty,
      sum(case when primary_sr_id = 7210095 then opps_amount end) as lh_rep_amount,
      sum(case when primary_sr_id = 8 then num_opps end) as km_rep_qty,
      sum(case when primary_sr_id = 8 then opps_amount end) as km_rep_amount,
      sum(case when primary_sr_id = 6910334 then num_opps end) as la_rep_qty,
      sum(case when primary_sr_id = 6910334 then opps_amount end) as la_rep_amount,
      sum(case when primary_sr_id = 6 then num_opps end) as mg_rep_qty,
      sum(case when primary_sr_id = 6 then opps_amount end) as mg_rep_amount
      from range_amount ra
      left join opps_by_tech obc on ra.range_id = obc.range_id
      group by ra.range_id,ra.range_value
  SQL

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

.won_opportunities_report_for_primary_reps(start_date) ⇒ Object



323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
# File 'app/services/report/opportunities_report/opportunities_report.rb', line 323

def self.won_opportunities_report_for_primary_reps(start_date)
  sql = <<-SQL
      with range_amount as (
          select 1 as range_id, '0-499' as range_value
          union all
          select 2,'500-999'
          union all
          select 3,'1000-1499'
          union all
          select 4,'1500-1999'
          union all
          select 5,'2000-2999'
          union all
          select 6,'3000-4900'
          union all
          select 7,'5000-7499'
          union all
          select 8,'7500-9999'
          union all
          select 9,'10000-29999'
          union all
          select 10,'30000+'

      ), opps_data as (
          select distinct primary_sr_id,opportunity_id,consolidated_value,
          case when consolidated_value between 0 and 499.9999 then 1
              when consolidated_value between 500 and 999.9999 then 2
              when consolidated_value between 1000 and 1499.9999 then 3
              when consolidated_value between 1500 and 1999.9999 then 4
              when consolidated_value between 2000 and 2999.9999 then 5
              when consolidated_value between 3000 and 4999.9999 then 6
              when consolidated_value between 5000 and 7499.9999 then 7
              when consolidated_value between 7500 and 9999.9999 then 8
              when consolidated_value between 10000 and 29999.9999 then 9
              when consolidated_value >= 30000 then 10 else 0 end as range_id
          from view_opportunities_facts
          where primary_sr_id in (531953,531955,6855834,166896,38,16775767,7210095,8,6910334,6)
          and opp_state = 'won'
          and date >= '#{start_date}'

      ), opps_by_tech as (
          select primary_sr_id,range_id,count(*) as num_opps,sum(consolidated_value) as opps_amount
          from opps_data
          group by primary_sr_id,range_id

      )
      select ra.range_id,ra.range_value,
      sum(case when primary_sr_id = 531953 then num_opps end) as js_rep_qty,
      sum(case when primary_sr_id = 531953 then opps_amount end) as js_rep_amount,
      sum(case when primary_sr_id = 531955 then num_opps end) as pb_rep_qty,
      sum(case when primary_sr_id = 531955 then opps_amount end) as pb_rep_amount,
      sum(case when primary_sr_id = 6855834 then num_opps end) as pl_rep_qty,
      sum(case when primary_sr_id = 6855834 then opps_amount end) as pl_rep_amount,
      sum(case when primary_sr_id = 166896 then num_opps end) as tb_rep_qty,
      sum(case when primary_sr_id = 166896 then opps_amount end) as tb_rep_amount,
      sum(case when primary_sr_id = 38 then num_opps end) as jw_rep_qty,
      sum(case when primary_sr_id = 38 then opps_amount end) as jw_rep_amount,
      sum(case when primary_sr_id = 16775767 then num_opps end) as mk_rep_qty,
      sum(case when primary_sr_id = 16775767 then opps_amount end) as mk_rep_amount,
      sum(case when primary_sr_id = 7210095 then num_opps end) as lh_rep_qty,
      sum(case when primary_sr_id = 7210095 then opps_amount end) as lh_rep_amount,
      sum(case when primary_sr_id = 8 then num_opps end) as km_rep_qty,
      sum(case when primary_sr_id = 8 then opps_amount end) as km_rep_amount,
      sum(case when primary_sr_id = 6910334 then num_opps end) as la_rep_qty,
      sum(case when primary_sr_id = 6910334 then opps_amount end) as la_rep_amount,
      sum(case when primary_sr_id = 6 then num_opps end) as mg_rep_qty,
      sum(case when primary_sr_id = 6 then opps_amount end) as mg_rep_amount
      from range_amount ra
      left join opps_by_tech obc on ra.range_id = obc.range_id
      group by ra.range_id,ra.range_value
  SQL

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