diff --git a/app/models/admin/reports/duplicate_approved_claims.rb b/app/models/admin/reports/duplicate_approved_claims.rb index 6a5c28a56..33af136a6 100644 --- a/app/models/admin/reports/duplicate_approved_claims.rb +++ b/app/models/admin/reports/duplicate_approved_claims.rb @@ -46,23 +46,12 @@ def scope def duplicates_by_eligibility ActiveRecord::Base.connection.execute( - <<~SQL - WITH eligibilities AS (#{eligibilities_cte}) - SELECT claims.id - FROM claims - JOIN eligibilities - ON claims.eligibility_id = eligibilities.id - WHERE claims.academic_year = '#{academic_year}' - SQL + Policies::POLICIES.map do |policy| + policy_with_claimable_policies(policy) + end.compact.join("\nUNION\n") ).map { |row| row["id"] } end - def eligibilities_cte - Policies::POLICIES.map do |policy| - policy_with_claimable_policies(policy) - end.compact.join("\nUNION\n") - end - def policy_with_claimable_policies(policy) left_table = policy::Eligibility.table_name @@ -81,7 +70,7 @@ def policy_with_claimable_policies(policy) ) <<~SQL - SELECT #{left_table}.id + SELECT claims.id FROM #{left_table} JOIN #{right_table} #{right_table_alias} ON #{left_table}.id != #{right_table_alias}.id @@ -90,7 +79,9 @@ def policy_with_claimable_policies(policy) JOIN claims other_claims ON other_claims.eligibility_id = #{right_table_alias}.id JOIN decisions ON claims.id = decisions.claim_id JOIN decisions other_decisions ON other_claims.id = other_decisions.claim_id - WHERE decisions.result = 0 + WHERE claims.academic_year = '#{academic_year}' + AND other_claims.academic_year = '#{academic_year}' + AND decisions.result = 0 AND other_decisions.result = 0 SQL end.join("\nUNION\n")