Class: EducationForm::CreateDailyFiscalYearToDateReport

Inherits:
Object
  • Object
show all
Includes:
Sidekiq::Job
Defined in:
app/sidekiq/education_form/create_daily_fiscal_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
OCTOBER =
10

Instance Method Summary collapse

Constructor Details

#initialize(date = yesterday) ⇒ CreateDailyFiscalYearToDateReport

use yesterday as the date for the daily job otherwise we will miss applications that are submitted after the report is run



26
27
28
# File 'app/sidekiq/education_form/create_daily_fiscal_year_to_date_report.rb', line 26

def initialize(date = yesterday)
  @date = date
end

Instance Method Details

#beginning_of_fiscal_yearObject



34
35
36
37
# File 'app/sidekiq/education_form/create_daily_fiscal_year_to_date_report.rb', line 34

def beginning_of_fiscal_year
  # The beginning of the federal fiscal year is October 1st
  Date.new(fiscal_year - 1, OCTOBER)
end

#build_region_submission(application_types, form_type, relation) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'app/sidekiq/education_form/create_daily_fiscal_year_to_date_report.rb', line 84

def build_region_submission(application_types, form_type, relation)
  region_submissions = {}

  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

  region_submissions
end

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



47
48
49
50
51
52
53
54
55
56
57
# File 'app/sidekiq/education_form/create_daily_fiscal_year_to_date_report.rb', line 47

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



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

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|
      next if region_excluded(fiscal_year, region)

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

      form_submissions[region] = build_region_submission(application_types, form_type, relation)
    end

    submissions[form_type] = form_submissions
  end

  submissions
end

#convert_submissions_to_csv_arrayObject



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

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|
    next if region_excluded(fiscal_year, 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



223
224
225
226
227
228
229
230
231
# File 'app/sidekiq/education_form/create_daily_fiscal_year_to_date_report.rb', line 223

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



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'app/sidekiq/education_form/create_daily_fiscal_year_to_date_report.rb', line 137

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



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'app/sidekiq/education_form/create_daily_fiscal_year_to_date_report.rb', line 98

def create_csv_header
  csv_array = []
  num_form_types = FORM_TYPES.size

  @ranges = {
    day: @date.all_day,
    year: beginning_of_fiscal_year..@date.end_of_day
  }

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

  csv_array << ["Submitted Vets.gov Applications - Report FYTD #{fiscal_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



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'app/sidekiq/education_form/create_daily_fiscal_year_to_date_report.rb', line 119

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



162
163
164
165
166
167
168
169
170
171
172
# File 'app/sidekiq/education_form/create_daily_fiscal_year_to_date_report.rb', line 162

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

#fiscal_yearObject



39
40
41
42
43
44
45
# File 'app/sidekiq/education_form/create_daily_fiscal_year_to_date_report.rb', line 39

def fiscal_year
  if @date.month < OCTOBER
    @date.year
  else
    @date.year + 1
  end
end

#generate_csvObject



233
234
235
236
237
238
239
240
241
242
243
244
245
# File 'app/sidekiq/education_form/create_daily_fiscal_year_to_date_report.rb', line 233

def generate_csv
  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

  filename
end

#get_totals_hash_with_form_typesObject



174
175
176
177
178
179
180
181
182
# File 'app/sidekiq/education_form/create_daily_fiscal_year_to_date_report.rb', line 174

def get_totals_hash_with_form_types
  totals = {}

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

  totals
end

#performObject



247
248
249
250
251
252
# File 'app/sidekiq/education_form/create_daily_fiscal_year_to_date_report.rb', line 247

def perform
  filename = generate_csv
  return unless FeatureFlipper.send_edu_report_email?

  YearToDateReportMailer.build(filename).deliver_now
end

#region_excluded(fiscal_year, region) ⇒ Object



214
215
216
217
218
219
220
221
# File 'app/sidekiq/education_form/create_daily_fiscal_year_to_date_report.rb', line 214

def region_excluded(fiscal_year, region)
  # Atlanta is to be excluded from FYTD reports after the 2017 fiscal year
  return true if fiscal_year > 2017 && region == :southern
  # St. Louis is to be excluded from FYTD reports after the 2020 fiscal year
  return true if fiscal_year > 2020 && region == :central

  false
end

#show_individual_benefits(form_type) ⇒ Object



59
60
61
# File 'app/sidekiq/education_form/create_daily_fiscal_year_to_date_report.rb', line 59

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

#yesterdayObject



30
31
32
# File 'app/sidekiq/education_form/create_daily_fiscal_year_to_date_report.rb', line 30

def yesterday
  Time.zone.today - 1.day
end