From 5d465f9fcd392697d27d7a0892670380026c220c Mon Sep 17 00:00:00 2001 From: Richard Lynch Date: Thu, 10 Oct 2024 12:14:50 +0100 Subject: [PATCH] Refactor query Refactors the CTE to build a generic eligibilities table we can join to. Now when we add a new policy the payroll query should automatically pick it up. Ideally we'd rename the "student_loan_repayment_amount" column in the DB but that's quite involved, so for the time being we define an 'award_amount_column' column on the policy which returns the name of the DB column we need to check to get the award amount. --- app/models/base_policy.rb | 4 ++ app/models/payroll_run.rb | 56 +++++++++------------------- app/models/policies/student_loans.rb | 4 ++ 3 files changed, 26 insertions(+), 38 deletions(-) diff --git a/app/models/base_policy.rb b/app/models/base_policy.rb index d4442b602e..6d43ac0388 100644 --- a/app/models/base_policy.rb +++ b/app/models/base_policy.rb @@ -66,4 +66,8 @@ def approvable?(claim) def decision_deadline_date(claim) (claim.submitted_at + Claim::DECISION_DEADLINE).to_date end + + def award_amount_column + "award_amount" + end end diff --git a/app/models/payroll_run.rb b/app/models/payroll_run.rb index d39dc999ff..5dd92201ac 100644 --- a/app/models/payroll_run.rb +++ b/app/models/payroll_run.rb @@ -73,50 +73,30 @@ def payments_count end def line_items(policy, filter: :all) - sql = <<~SQL - WITH claims_with_award_amount AS ( + eligibilities_cte = "WITH eligibilities AS(" + eligibilities_cte += Policies::POLICIES.map do |policy| + <<~SQL SELECT - claims.*, - CASE - WHEN early_career_payments_eligibilities.id IS NOT NULL - THEN early_career_payments_eligibilities.award_amount - WHEN further_education_payments_eligibilities.id IS NOT NULL - THEN further_education_payments_eligibilities.award_amount - WHEN international_relocation_payments_eligibilities.id IS NOT NULL - THEN international_relocation_payments_eligibilities.award_amount - WHEN levelling_up_premium_payments_eligibilities.id IS NOT NULL - THEN levelling_up_premium_payments_eligibilities.award_amount - WHEN student_loans_eligibilities.id IS NOT NULL - THEN student_loans_eligibilities.student_loan_repayment_amount - END AS award_amount - - FROM claims - LEFT JOIN early_career_payments_eligibilities - ON claims.eligibility_id = early_career_payments_eligibilities.id - AND claims.eligibility_type = 'Policies::EarlyCareerPayments::Eligibility' - LEFT JOIN further_education_payments_eligibilities - ON claims.eligibility_id = further_education_payments_eligibilities.id - AND claims.eligibility_type = 'Policies::FurtherEducationPayments::Eligibility' - LEFT JOIN international_relocation_payments_eligibilities - ON claims.eligibility_id = international_relocation_payments_eligibilities.id - AND claims.eligibility_type = 'Policies::InternationalRelocationPayments::Eligibility' - LEFT JOIN levelling_up_premium_payments_eligibilities - ON claims.eligibility_id = levelling_up_premium_payments_eligibilities.id - AND claims.eligibility_type = 'Policies::LevellingUpPremiumPayments::Eligibility' - LEFT JOIN student_loans_eligibilities - ON claims.eligibility_id = student_loans_eligibilities.id - AND claims.eligibility_type = 'Policies::StudentLoans::Eligibility' - ) + id, + #{policy.award_amount_column} AS award_amount, + '#{policy::Eligibility}' AS eligibility_type + FROM #{policy::Eligibility.table_name} + SQL + end.join(" UNION ALL ") + eligibilities_cte += ")" + sql = <<~SQL + #{eligibilities_cte} SELECT /* A topup is always paid in different payment/payroll_run than the main claim was */ - COALESCE(topups.award_amount, claims.award_amount) AS award_amount + COALESCE(topups.award_amount, eligibilities.award_amount) AS award_amount FROM payments JOIN claim_payments ON claim_payments.payment_id = payments.id - JOIN claims_with_award_amount AS claims - ON claims.id = claim_payments.claim_id - LEFT JOIN topups - ON topups.claim_id = claims.id + JOIN claims ON claims.id = claim_payments.claim_id + JOIN eligibilities + ON claims.eligibility_id = eligibilities.id + AND claims.eligibility_type = eligibilities.eligibility_type + LEFT JOIN topups ON topups.claim_id = claims.id WHERE payments.payroll_run_id = '#{id}' SQL diff --git a/app/models/policies/student_loans.rb b/app/models/policies/student_loans.rb index 83454eaaa4..16862dba0f 100644 --- a/app/models/policies/student_loans.rb +++ b/app/models/policies/student_loans.rb @@ -153,5 +153,9 @@ def current_financial_year(format = :default) def payroll_file_name "TSLR" end + + def award_amount_column + "student_loan_repayment_amount" + end end end