Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CAPT 1797 ops queries alternative implementation #3472

Draft
wants to merge 5 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions app/controllers/admin/reports_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
module Admin
class ReportsController < BaseAdminController
before_action :ensure_service_operator

def index
end

def show
respond_to do |format|
format.csv do
send_data(report.to_csv, filename: report.filename)
end
end
end

private

def report
@report ||= case params[:name]
when "fe-approved-claims-with-failing-provider-verification"
Reports::FeApprovedClaimsWithFailingProviderVerification.new
when "approved-claims-failing-qualification-task"
Reports::ApprovedClaimsFailingQualificationTask.new
else
raise ActiveRecord::RecordNotFound
end
end
end
end
121 changes: 121 additions & 0 deletions app/models/admin/reports/approved_claims_failing_qualification_task.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
module Admin
module Reports
class ApprovedClaimsFailingQualificationTask
HEADERS = [
"Claim reference",
"Teacher reference number",
"Policy",
"Status",
"Decision date",
"Decision agent",
"Qualification",
"ITT start year",
"ITT subject",
"ITT subjects",
"ITT start date",
"QTS award date",
"Qualification name"
]

def filename
"approved_claims_failing_qualification_task.csv"
end

def to_csv
CSV.generate(
row_sep: "\r\n",
write_headers: true,
headers: HEADERS
) do |csv|
rows.each { |row| csv << row }
end
end

private

def rows
scope.map(&ClaimPresenter.method(:new)).map(&:to_a)
end

def scope
Claim
.approved
.where(academic_year: AcademicYear.current)
.joins(:tasks)
.merge(Task.where(name: "qualifications", passed: false))
.includes(:eligibility, decisions: :created_by)
end

class ClaimPresenter
include Admin::ClaimsHelper

def initialize(claim)
@claim = claim
end

def to_a
[
claim.reference,
claim.eligibility.teacher_reference_number,
I18n.t("#{claim.policy.locale_key}.policy_acronym"),
status(claim),
I18n.l(approval.created_at.to_date, format: :day_month_year),
approval.created_by.full_name,
qualification,
itt_academic_year&.to_s,
eligible_itt_subject,
dqt_teacher_record.itt_subjects.join(", "),
I18n.l(dqt_teacher_record.itt_start_date, format: :day_month_year),
I18n.l(dqt_teacher_record.qts_award_date, format: :day_month_year),
dqt_teacher_record.qualification_name
]
end

private

attr_reader :claim

def approval
@approval ||= claim.decisions.reject(&:undone).last
end

# StudentLoans doesn't have an eligible_itt_subject
def eligible_itt_subject
claim.eligibility.try(:eligible_itt_subject)
end

# StudentLoans doesn't have an itt_academic_year
def itt_academic_year
claim.eligibility.try(:itt_academic_year)
end

# StudentLoans doesn't have a qualification
def qualification
claim.eligibility.try(:qualification)
end

def itt_subjects
dqt_teacher_record&.itt_subjects
end

def itt_start_date
dqt_teacher_record&.itt_start_date
end

def qts_award_date
dqt_teacher_record&.qts_award_date
end

def qualification_name
dqt_teacher_record&.qualification_name
end

def dqt_teacher_record
@dqt_teacher_record ||= if claim.has_dqt_record?
Dqt::Teacher.new(claim.dqt_teacher_status)
end
end
end
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
module Admin
module Reports
class FeApprovedClaimsWithFailingProviderVerification
HEADERS = [
"Claim reference",
"Full name",
"Claim amount",
"Claim status",
"Decision date",
"Decision agent",
"Contract of employment",
"Teaching responsibilities",
"First 5 years of teaching",
"One full term",
"Timetabled teaching hours",
"Age range taught",
"Subject",
"Course",
"2.5 hours weekly teaching",
"Performance",
"Disciplinary"
]

def filename
"fe_approved_claims_with_failing_provider_verification.csv"
end

def to_csv
CSV.generate(
row_sep: "\r\n",
write_headers: true,
headers: HEADERS
) do |csv|
rows.each { |row| csv << row }
end
end

private

def rows
scope.map(&ClaimPresenter.method(:new)).map(&:to_a)
end

def scope
Claim
.by_policy(Policies::FurtherEducationPayments)
.approved
.joins(:tasks)
.merge(Task.where(name: "provider_verification", passed: false))
.includes(:eligibility, decisions: :created_by)
end

class ClaimPresenter
include Admin::ClaimsHelper
include ActionView::Helpers::NumberHelper

def initialize(claim)
@claim = claim
end

def to_a
[
claim.reference,
claim.full_name,
number_to_currency(claim.award_amount, precision: 0),
status(claim),
approval_date,
approval.created_by.full_name,
present_assertion("contract_type"),
present_assertion("teaching_responsibilities"),
present_assertion("further_education_teaching_start_year"),
present_assertion("taught_at_least_one_term"),
present_assertion("teaching_hours_per_week"),
present_assertion("half_teaching_hours"),
present_assertion("subjects_taught"),
"??", # FIXME RL: not sure what courses should be
present_assertion("teaching_hours_per_week_next_term"),
present_assertion("subject_to_formal_performance_action"),
present_assertion("subject_to_disciplinary_action")
]
end

private

attr_reader :claim

def approval_date
I18n.l(approval.created_at.to_date, format: :day_month_year)
end

def approval
@approval ||= claim.decisions.reject(&:undone).last
end

def present_assertion(name)
case claim.eligibility.verification_assertion(name)
when true then "Yes"
when false then "No"
else "N/A" # fixed and variable contracts have different assertions
end
end
end
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
module AutomatedChecks
module ClaimVerifiers
class DuplicateClaimsCheck
def initialize(claim:)
@claim = claim
end

def perform
matching_attribute_finder.matching_claims.each do |existing_claim|
if existing_claim.created_at < claim.created_at
original_claim = existing_claim
duplicate_claim = claim
else
original_claim = claim
duplicate_claim = existing_claim
end

unless Claims::ClaimDuplicate.exists?(original_claim: original_claim, duplicate_claim: duplicate_claim)
Claims::ClaimDuplicate.create!(
original_claim: original_claim,
duplicate_claim: duplicate_claim,
matching_attributes: matching_attribute_finder.matching_attributes(existing_claim)
)
end
end
end

private

attr_reader :claim

def matching_attribute_finder
@matching_attribute_finder ||= Claim::MatchingAttributeFinder.new(claim)
end
end
end
end
20 changes: 20 additions & 0 deletions app/models/claim.rb
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,26 @@ class Claim < ApplicationRecord
inverse_of: :assigned_claims,
optional: true

has_many :claim_duplicates_as_original_claim,
class_name: "Claims::ClaimDuplicate",
foreign_key: :original_claim_id,
dependent: :destroy

has_many :claim_duplicates_as_duplicate_claim,
class_name: "Claims::ClaimDuplicate",
foreign_key: :duplicate_claim_id,
dependent: :destroy

has_many :duplicates,
through: :claim_duplicates_as_original_claim,
source: :duplicate_claim,
class_name: "Claim"

has_many :originals,
through: :claim_duplicates_as_duplicate_claim,
source: :original_claim,
class_name: "Claim"

enum :payroll_gender, {
dont_know: 0,
female: 1,
Expand Down
5 changes: 5 additions & 0 deletions app/models/claims.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module Claims
def self.table_name_prefix
"claims_"
end
end
28 changes: 28 additions & 0 deletions app/models/claims/claim_duplicate.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
module Claims
class ClaimDuplicate < ApplicationRecord
belongs_to :original_claim, class_name: "Claim"
belongs_to :duplicate_claim, class_name: "Claim"

validates :duplicate_claim, uniqueness: {
scope: :original_claim,
message: "has already been registered as a duplicate"
}
validate :claims_are_not_the_same, if: -> { original_claim && duplicate_claim }
validate :original_claim_is_older, if: -> { original_claim && duplicate_claim }
validates :matching_attributes, presence: true

private

def claims_are_not_the_same
return unless original_claim == duplicate_claim

errors.add(:duplicate_claim, "can't be the same as the original claim")
end

def original_claim_is_older
return unless original_claim.created_at > duplicate_claim.created_at

errors.add(:original_claim, "must be older than the duplicate claim")
end
end
end
3 changes: 2 additions & 1 deletion app/models/policies/early_career_payments.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ module EarlyCareerPayments
AutomatedChecks::ClaimVerifiers::CensusSubjectsTaught,
AutomatedChecks::ClaimVerifiers::Employment,
AutomatedChecks::ClaimVerifiers::StudentLoanPlan,
AutomatedChecks::ClaimVerifiers::FraudRisk
AutomatedChecks::ClaimVerifiers::FraudRisk,
AutomatedChecks::ClaimVerifiers::DuplicateClaimsCheck
].freeze

POLICY_START_YEAR = AcademicYear.new(2021).freeze
Expand Down
3 changes: 2 additions & 1 deletion app/models/policies/early_years_payments.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ module EarlyYearsPayments

VERIFIERS = [
AutomatedChecks::ClaimVerifiers::StudentLoanPlan,
AutomatedChecks::ClaimVerifiers::EarlyYearsPayments::Identity
AutomatedChecks::ClaimVerifiers::EarlyYearsPayments::Identity,
AutomatedChecks::ClaimVerifiers::DuplicateClaimsCheck
]

# Attributes to delete from claims submitted before the current academic
Expand Down
3 changes: 2 additions & 1 deletion app/models/policies/further_education_payments.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ module FurtherEducationPayments
AutomatedChecks::ClaimVerifiers::ProviderVerification,
AutomatedChecks::ClaimVerifiers::Employment,
AutomatedChecks::ClaimVerifiers::StudentLoanPlan,
AutomatedChecks::ClaimVerifiers::FraudRisk
AutomatedChecks::ClaimVerifiers::FraudRisk,
AutomatedChecks::ClaimVerifiers::DuplicateClaimsCheck
]

# Options shown to admins when rejecting a claim
Expand Down
Loading
Loading