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
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
|
# File 'app/services/report/kpi_call/kpi_call.rb', line 34
def self.get_data(start_time, end_time, departments_sql, employee_ids_sql)
sql = <<-SQL
with kpis_limits as (
select
sum(case when kpi_code = 2 then tier2 else 0 end) as inbounds_min,sum(case when kpi_code = 2 then tier1 else 0 end) as inbounds_pts,
sum(case when kpi_code = 3 then tier2 else 0 end) as outbounds_min,sum(case when kpi_code = 3 then tier1 else 0 end) as outbounds_pts,
sum(case when kpi_code = 4 then tier2 else 0 end) as activities_min,sum(case when kpi_code = 4 then tier1 else 0 end) as activities_pts,
sum(case when kpi_code = 5 then tier2 else 0 end) as sms_min,sum(case when kpi_code = 5 then tier1 else 0 end) as sms_pts,
sum(case when kpi_code = 6 then tier2 else 0 end) as looms_min,sum(case when kpi_code = 6 then tier1 else 0 end) as looms_pts,
sum(case when kpi_code = 7 then tier2 else 0 end) as call_blocks_min,sum(case when kpi_code = 7 then tier1 else 0 end) as call_blocks_pts,
sum(case when kpi_code = 8 then tier2 else 0 end) as emails_min,sum(case when kpi_code = 8 then tier1 else 0 end) as emails_pts,
sum(case when kpi_code = 9 then tier2 else 0 end) as missed_calls_threshold,sum(case when kpi_code = 9 then tier1 else 0 end) as missed_calls_pts
from kpis k
inner join (
select id,max(kpi_date) as kpi_date
from kpis
group by id
) mkc on k.id = mkc.id and k.kpi_date = mkc.kpi_date
), kpi_data as (
select
employee_id,employee_name,department,sum(business_day) as business_day,(sum(business_day) - sum(day_off)) as days_worked,round(sum(credits_value)::numeric,2) as credits,
sum(time_avbl) as time_avbl,
case when sum(business_day) = 0 then 0 else sum(time_avbl) / sum(business_day) end as time_avbl_avg,
sum(inbounds) as inbounds,
sum(outbounds) as outbounds,
sum(missed) as missed,
sum(activities) as activities,
sum(sms) as sms,
sum(looms) as looms,
sum(call_block) as call_blocks,
sum(emails) as emails
from view_kpis_time_on_tasks e
where date between '#{start_time}' and '#{end_time}'
#{departments_sql}
#{employee_ids_sql}
group by employee_id, employee_name, department
)
select
employee_id,employee_name,department,business_day,days_worked,credits,time_avbl,time_avbl_avg,
inbounds,(case when (inbounds_min * (days_worked - credits)) = 0 then 0 else (inbounds / (inbounds_min * (days_worked - credits))) end * inbounds_pts) as inbounds_wpi,inbounds_pts,
outbounds,(case when (outbounds_min * (days_worked - credits)) = 0 then 0 else (outbounds / (outbounds_min * (days_worked - credits))) end * outbounds_pts) as outbounds_wpi,outbounds_pts,
activities,(case when (activities_min * (days_worked - credits)) = 0 then 0 else (activities / (activities_min * (days_worked - credits))) end * activities_pts) as activities_wpi,activities_pts,
sms,(case when (sms_min * (days_worked - credits)) = 0 then 0 else (sms / (sms_min * (days_worked - credits))) end * sms_pts) as sms_wpi,sms_pts,
looms,(case when (looms_min * (days_worked - credits)) = 0 then 0 else (looms / (looms_min * (days_worked - credits))) end * looms_pts) as looms_wpi,looms_pts,
call_blocks,(case when (call_blocks_min * (days_worked - credits)) = 0 then 0 else (call_blocks / (call_blocks_min * (days_worked - credits))) end * call_blocks_pts) as call_blocks_wpi,call_blocks_pts,
emails,(case when (emails_min * (days_worked - credits)) = 0 then 0 else (emails / (emails_min * (days_worked - credits))) end * emails_pts) as emails_wpi,emails_pts,
missed,
round(case when inbounds = 0 then 0 else (missed::numeric / inbounds::numeric) * 100 end, 1) as missed_calls_pct,
case when (days_worked - credits) <= 0 then 0 when time_avbl = 0 then 0 when inbounds = 0 then missed_calls_pts when (missed::numeric / inbounds::numeric) * 100 <= missed_calls_threshold then missed_calls_pts else 0 end as missed_calls_wpi,
missed_calls_pts
from kpi_data kd, kpis_limits kl
order by department, employee_name;
SQL
results = ActiveRecord::Base.connection.execute(sql).to_a.map { |r| r.symbolize_keys }
end
|