-
-
Notifications
You must be signed in to change notification settings - Fork 572
Expand file tree
/
Copy pathannual_reports_controller.rb
More file actions
85 lines (68 loc) · 2.55 KB
/
annual_reports_controller.rb
File metadata and controls
85 lines (68 loc) · 2.55 KB
1
2
3
4
5
6
7
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
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
class Reports::AnnualReportsController < ApplicationController
before_action :validate_show_params, only: [:show, :recalculate]
before_action :validate_range_params, only: [:range]
def index
# 2813_update_annual_report -- changed to earliest_reporting_year
# so that we can do system tests and staging
@foundation_year = current_organization.earliest_reporting_year
@current_year = Time.current.year
@years = (@foundation_year...@current_year).to_a
@month_remaining_to_report = 12 - Time.current.month
end
def show
@year = year_param
@report = Reports.retrieve_report(organization: current_organization, year: @year)
respond_to do |format|
format.html
format.csv do
send_data Exports::ExportReportCSVService.new(reports: @report.all_reports).generate_csv,
filename: "NdbnAnnuals-#{@year}-#{Time.zone.today}.csv"
end
end
end
def recalculate
year = year_param
Reports.retrieve_report(organization: current_organization, year: year, recalculate: true)
redirect_to reports_annual_report_path(year), notice: "Recalculated annual report!"
end
def range
# Set range to be within valid reporting bounds
# Start year cannot be before org founding year
# End year cannot be after current year
year_start = [range_params[:year_start].to_i, current_organization.earliest_reporting_year].max
year_end = [range_params[:year_end].to_i, Time.current.year].min
# Sort years if out of order
year_start, year_end = [year_start, year_end].minmax
reports = get_range_report(year_start, year_end)
respond_to do |format|
format.csv do
send_data Exports::ExportReportCSVService.new(reports:).generate_csv(range: true),
filename: "NdbnAnnuals-#{year_start}-#{year_end}.csv"
end
end
end
private
def get_range_report(year_start, year_end)
(year_start..year_end).map do |year|
Reports.retrieve_report(organization: current_organization, year: year, recalculate: true)
rescue ActiveRecord::RecordInvalid => e
Rails.logger.error("Failed to retrieve annual report for year #{year}: #{e.message}")
nil
end.compact
end
def year_param
params.require(:year)
end
def range_params
params.permit(:year_start, :year_end)
end
def validate_show_params
not_found! unless year_param.to_i.positive?
end
def validate_range_params
not_found! unless range_params[:year_start] =~ year_regex && range_params[:year_end] =~ year_regex
end
def year_regex
/^\d{4}$/
end
end