Class: EducationForm::CreateDailyYearToDateReport

Inherits:
Object
  • Object
show all
Includes:
Sidekiq::Job
Defined in:
app/sidekiq/education_form/create_daily_year_to_date_report.rb

Constant Summary collapse

TOTALS_HASH =
{
  yearly: 0,
  daily_submitted: 0,
  daily_processed: 0
}.freeze
FORM_TYPES =
EducationBenefitsClaim::FORM_TYPES
FORM_TYPE_HEADERS =
EducationBenefitsClaim.form_headers(FORM_TYPES).map do |form_header|
  [form_header, '', '']
end.flatten.freeze

Instance Method Summary collapse

Instance Method Details

#build_submission_relation(range_type, region, form_type, status) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
# File 'app/sidekiq/education_form/create_daily_year_to_date_report.rb', line 20

def build_submission_relation(range_type, region, form_type, status)
  range = @ranges[range_type]
  relation = EducationBenefitsSubmission.where(
    created_at: range,
    region: region.to_s,
    form_type:
  )
  relation = relation.where(status: 'processed') if status == :processed

  relation
end

#calculate_submissions(range_type: :year, status: :processed) ⇒ Object



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
# File 'app/sidekiq/education_form/create_daily_year_to_date_report.rb', line 36

def calculate_submissions(range_type: :year, status: :processed)
  submissions = {}
  application_types = EducationBenefitsClaim::APPLICATION_TYPES

  FORM_TYPES.each do |form_type|
    form_submissions = {}

    EducationFacility::REGIONS.each do |region|
      region_submissions = {}

      relation = build_submission_relation(range_type, region, form_type, status)

      if show_individual_benefits(form_type)
        application_types.each do |application_type|
          region_submissions[application_type] = relation.where(application_type => true).count
        end
      else
        region_submissions[:all] = relation.count
      end

      form_submissions[region] = region_submissions
    end

    submissions[form_type] = form_submissions
  end

  submissions
end

#convert_submissions_to_csv_arrayObject



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
# File 'app/sidekiq/education_form/create_daily_year_to_date_report.rb', line 151

def convert_submissions_to_csv_array
  submissions_csv_array = []
  submissions = {
    yearly: calculate_submissions,
    daily_submitted: calculate_submissions(range_type: :day, status: :submitted),
    daily_processed: calculate_submissions(range_type: :day, status: :processed)
  }
  grand_totals = get_totals_hash_with_form_types

  EducationFacility::REGIONS.each do |region|
    submissions_total = get_totals_hash_with_form_types

    submissions_csv_array += create_csv_data_block(region, submissions, submissions_total)

    submissions_csv_array << create_totals_row(['', 'TOTAL'], submissions_total)

    submissions_total.each do |form_type, form_submissions|
      form_submissions.each do |range_type, total|
        grand_totals[form_type][range_type] += total
      end
    end
  end

  submissions_csv_array << create_totals_row(['ALL RPOS TOTAL', ''], grand_totals)

  submissions_csv_array
end

#create_csv_arrayObject



179
180
181
182
183
184
185
186
187
# File 'app/sidekiq/education_form/create_daily_year_to_date_report.rb', line 179

def create_csv_array
  csv_array = []

  csv_array += create_csv_header
  csv_array += convert_submissions_to_csv_array
  csv_array << ['', ''] + FORM_TYPE_HEADERS

  csv_array
end

#create_csv_data_block(region, submissions, submissions_total) ⇒ Object



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'app/sidekiq/education_form/create_daily_year_to_date_report.rb', line 104

def create_csv_data_block(region, submissions, submissions_total)
  csv_array = []
  application_types = EducationBenefitsClaim::APPLICATION_TYPES

  application_types.each_with_index do |application_type, i|
    on_last_index = i == (application_types.size - 1)
    row = [
      i.zero? ? EducationFacility::RPO_NAMES[region] : '',
      application_type.humanize(capitalize: false)
    ]

    row += create_data_row(
      on_last_index,
      application_type,
      region,
      submissions,
      submissions_total
    )

    csv_array << row
  end

  csv_array
end

#create_csv_headerObject



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'app/sidekiq/education_form/create_daily_year_to_date_report.rb', line 65

def create_csv_header
  csv_array = []
  num_form_types = FORM_TYPES.size

  @ranges = {}
  %i[day year].each do |range_type|
    @ranges[range_type] = @date.public_send("beginning_of_#{range_type}")..@date.end_of_day
  end

  ranges_header = [@ranges[:year].to_s, '', @ranges[:day].to_s]
   = ['', 'Submitted', 'Sent to Spool File']

  csv_array << ["Submitted Vets.gov Applications - Report FYTD #{@date.year} as of #{@date}"]
  csv_array << ['', '', 'DOCUMENT TYPE']
  csv_array << ['RPO', 'BENEFIT TYPE'] + FORM_TYPE_HEADERS
  csv_array << ['', ''] + ranges_header * num_form_types
  csv_array << ['', ''] +  * num_form_types

  csv_array
end

#create_data_row(on_last_index, application_type, region, submissions, submissions_total) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'app/sidekiq/education_form/create_daily_year_to_date_report.rb', line 86

def create_data_row(on_last_index, application_type, region, submissions, submissions_total)
  row = []

  FORM_TYPES.each do |form_type|
    next row += ['', '', ''] if !show_individual_benefits(form_type) && !on_last_index

    TOTALS_HASH.each_key do |range_type|
      application_type_key = show_individual_benefits(form_type) ? application_type : :all
      num_submissions = submissions[range_type][form_type][region][application_type_key]
      row << num_submissions

      submissions_total[form_type][range_type] += num_submissions
    end
  end

  row
end

#create_totals_row(text_rows, totals) ⇒ Object



129
130
131
132
133
134
135
136
137
138
139
# File 'app/sidekiq/education_form/create_daily_year_to_date_report.rb', line 129

def create_totals_row(text_rows, totals)
  row = text_rows.clone

  FORM_TYPES.each do |form_type|
    TOTALS_HASH.each_key do |range_type|
      row << totals[form_type][range_type]
    end
  end

  row
end

#get_totals_hash_with_form_typesObject



141
142
143
144
145
146
147
148
149
# File 'app/sidekiq/education_form/create_daily_year_to_date_report.rb', line 141

def get_totals_hash_with_form_types
  totals = {}

  FORM_TYPES.each do |form_type|
    totals[form_type] = TOTALS_HASH.dup
  end

  totals
end

#performObject



189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
# File 'app/sidekiq/education_form/create_daily_year_to_date_report.rb', line 189

def perform
  # use yesterday as the date otherwise we will miss applications that are submitted after the report is run
  @date = Time.zone.today - 1.day
  folder = 'tmp/daily_reports'
  FileUtils.mkdir_p(folder)
  filename = "#{folder}/#{@date}.csv"

  CSV.open(filename, 'wb') do |csv|
    create_csv_array.each do |row|
      csv << row
    end
  end

  return unless FeatureFlipper.send_edu_report_email?

  YearToDateReportMailer.build(filename).deliver_now
end

#show_individual_benefits(form_type) ⇒ Object



32
33
34
# File 'app/sidekiq/education_form/create_daily_year_to_date_report.rb', line 32

def show_individual_benefits(form_type)
  %w[1990n 0993].exclude?(form_type)
end