diff --git a/app/controllers/admin/reports_controller.rb b/app/controllers/admin/reports_controller.rb new file mode 100644 index 0000000000..86efbdfeb5 --- /dev/null +++ b/app/controllers/admin/reports_controller.rb @@ -0,0 +1,18 @@ +module Admin + class ReportsController < BaseAdminController + before_action :ensure_service_operator + + def index + @reports = Report.all + end + + def show + respond_to do |format| + format.csv { + report = Report.find(params[:id]) + send_data report.csv, filename: "#{report.name.parameterize(separator: "_")}_#{report.created_at.iso8601}.csv" + } + end + end + end +end diff --git a/app/jobs/reports_job.rb b/app/jobs/reports_job.rb new file mode 100644 index 0000000000..ae8a05102e --- /dev/null +++ b/app/jobs/reports_job.rb @@ -0,0 +1,9 @@ +class ReportsJob < CronJob + self.cron_expression = "0 6 * * 2#2" # second Tuesday of the month + + def perform + Rails.logger.info "Generating Ops reports" + + Report.create!(name: Reports::DuplicateClaims::NAME, csv: Reports::DuplicateClaims.new.to_csv) + end +end diff --git a/app/models/report.rb b/app/models/report.rb new file mode 100644 index 0000000000..1dfff22ffa --- /dev/null +++ b/app/models/report.rb @@ -0,0 +1,2 @@ +class Report < ApplicationRecord +end diff --git a/app/models/reports/duplicate_claims.rb b/app/models/reports/duplicate_claims.rb new file mode 100644 index 0000000000..1d3610ad54 --- /dev/null +++ b/app/models/reports/duplicate_claims.rb @@ -0,0 +1,47 @@ +require "csv" +require "excel_utils" + +module Reports + class DuplicateClaims + include Admin::ClaimsHelper + + NAME = "Duplicate Approved Claims" + HEADERS = [ + "Claim reference", + "Teacher reference number", + "Full name", + "Policy name", + "Claim amount", + "Claim status", + "Decision date", + "Decision agent" + ].freeze + + def initialize + @claims = Claim.approved.select { |claim| Claim::MatchingAttributeFinder.new(claim).matching_claims.any? } + end + + def to_csv + CSV.generate(write_headers: true, headers: HEADERS) do |csv| + @claims.each do |claim| + csv << row( + claim.reference, + claim.eligibility.teacher_reference_number, + claim.full_name, + claim.policy, + claim.award_amount, + status(claim), + claim.latest_decision.created_at, + claim.latest_decision.created_by.full_name + ) + end + end + end + + private + + def row(*entries) + entries.map { |entry| ExcelUtils.escape_formulas(entry) } + end + end +end diff --git a/app/models/reports/failed_provider_check_claims.rb b/app/models/reports/failed_provider_check_claims.rb new file mode 100644 index 0000000000..b295ba0a66 --- /dev/null +++ b/app/models/reports/failed_provider_check_claims.rb @@ -0,0 +1,8 @@ +require "csv" +require "excel_utils" + +module Reports + class FailedProviderCheckClaims + NAME = "Claims with failed provider check" + end +end diff --git a/app/models/reports/failed_qualification_claims.rb b/app/models/reports/failed_qualification_claims.rb new file mode 100644 index 0000000000..c5125cf232 --- /dev/null +++ b/app/models/reports/failed_qualification_claims.rb @@ -0,0 +1,39 @@ +require "csv" +require "excel_utils" + +module Reports + class FailedQualificationClaims + NAME = "Claims with failed qualification status" + HEADERS = [ + "Claim reference", + "Teacher reference number", + "Policy name", + "Decision date", + "Decision agent" + ].freeze + + def initialize + @claims = Claim.approved.select { |claim| Claim::MatchingAttributeFinder.new(claim).matching_claims.any? } + end + + def to_csv + CSV.generate(write_headers: true, headers: HEADERS) do |csv| + @claims.each do |claim| + csv << row( + claim.reference, + claim.eligibility.teacher_reference_number, + claim.policy, + claim.latest_decision.created_at, + claim.latest_decision.created_by.full_name + ) + end + end + end + + private + + def row(*entries) + entries.map { |entry| ExcelUtils.escape_formulas(entry) } + end + end +end diff --git a/app/views/admin/reports/index.html.erb b/app/views/admin/reports/index.html.erb new file mode 100644 index 0000000000..1167efca71 --- /dev/null +++ b/app/views/admin/reports/index.html.erb @@ -0,0 +1,30 @@ +<% content_for(:page_title) { page_title("Download Reports") } %> + +<% content_for :back_link do %> + <%= govuk_back_link href: admin_root_path %> +<% end %> + +
Name | +Created At | +
---|---|
<%= govuk_link_to report.name, admin_report_path(report, format: :csv) %> | +<%= l(report.created_at) %> | +