Class: Report::ActivityPerformance::Command

Inherits:
BaseCommand
  • Object
show all
Defined in:
app/services/report/activity_performance/command.rb

Instance Method Summary collapse

Instance Method Details

#executeObject



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
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
# File 'app/services/report/activity_performance/command.rb', line 11

def execute
  return unless valid?
  check_for_warnings
  rep_hash = {} # will be used to store our matrix
  datetime_range = date.beginning_of_day..date.end_of_day
  # Find all activities completed for a given day
  activities_completed = Activity.joins(:activity_type).
                                  joins(:closed_by).
                                  completed.
                                  where(completion_datetime: datetime_range).
                                  where("parties.type = 'Employee'")
                                  # where.not(activity_types: { priority: nil})
  # add a rep filter if specified
  if employee_ids.compact_blank.empty? && role_ids.compact_blank.empty?
    activities_completed
  elsif employee_ids.compact_blank.present?
    activities_completed = activities_completed.where(closed_by_id: employee_ids)
  elsif (eids = employee_ids_in_selected_roles).present?
    activities_completed = activities_completed.where(closed_by_id: eids)
  end

  # Group by a soup of keys using the new centralized tagging system
  # Uses subqueries to check for tag presence via taggings/tags join tables
  activities_completed_grouped_by_employee = activities_completed
    .where("(#{activity_type_not_tagged_with_sql('sales_call')} OR #{activity_type_not_tagged_with_sql('callblock')})")
    .group(:closed_by_id, :full_name, tag_category_case_sql)
    .count
  activities_completed_grouped_by_employee.merge!(
    activities_completed
      .where("(#{activity_type_tagged_with_sql('sales_call')} OR #{activity_type_tagged_with_sql('callblock')})")
      .group(:closed_by_id, :full_name, callblock_case_sql)
      .count
  )
  # Iterate our hash and build our results
  activities_completed_grouped_by_employee.each do |(employee_id, employee_name, tag),count|
    tag_position = ActivityType::TAGS_FOR_REPORTS.map{ |tr| tr[1] if tr[0] == tag }.uniq.compact.first
    r = rep_hash[employee_id] ||= Report::ActivityPerformance::Result.new(employee_id: employee_id, employee_name: employee_name)
    r.closed_by_tag[tag_position] += count
  end

  open_activity_sql = <<-EOS
    (target_datetime <= :open_date_end or open_date = :open_date) and activities.activity_result_type_id IS NULL
  EOS

  # Find all open activities for the given day, assigned.
  activities_open = Activity.joins(:activity_type).
                             joins(:assigned_resource).
                             joins("left join historical_open_activities hoa on hoa.activity_id = activities.id and  hoa.assigned_resource_id = activities.assigned_resource_id AND open_date = '#{date}'::date").
                             where(open_activity_sql, open_date: date, open_date_end: date.end_of_day)
                            #  where.not(activity_types: { priority: nil})
  # add a rep filter if specified
  if employee_ids.compact_blank.empty? && role_ids.compact_blank.empty?
    activities_open
  elsif employee_ids.compact_blank.present?
    activities_open = activities_open.where(assigned_resource_id: employee_ids)
  elsif (eids = employee_ids_in_selected_roles).present?
    activities_open = activities_open.where(assigned_resource_id: eids)
  end

  # Group by a soup of keys using the new centralized tagging system
  activities_open_grouped_by_employee = activities_open
    .where("(#{activity_type_not_tagged_with_sql('sales_call')} OR #{activity_type_not_tagged_with_sql('callblock')})")
    .group(:assigned_resource_id, :full_name, tag_category_case_sql)
    .count
  activities_open_grouped_by_employee.merge!(
    activities_open
      .where("(#{activity_type_tagged_with_sql('sales_call')} OR #{activity_type_tagged_with_sql('callblock')})")
      .group(:assigned_resource_id, :full_name, callblock_case_sql)
      .count
  )
  # Iterate our hash and build our results
  activities_open_grouped_by_employee.each do |(employee_id, employee_name, tag),count|
    tag_position = ActivityType::TAGS_FOR_REPORTS.map{ |tr| tr[1] if tr[0] == tag }.uniq.compact.first
    r = rep_hash[employee_id] ||= Report::ActivityPerformance::Result.new(employee_id: employee_id, employee_name: employee_name)
    r.open_by_tag[tag_position] += count
  end
  # Sort by rep name because its prettier and store results
  self.results = rep_hash.values.sort_by(&:employee_name)
end

#to_csvObject



91
92
93
# File 'app/services/report/activity_performance/command.rb', line 91

def to_csv
  return unless valid?
end