ministryofjustice/Claim-for-Crown-Court-Defence

View on GitHub
app/services/stats/management_information/queries/lgfs/lf2_redetermination_query.rb

Summary

Maintainability
A
0 mins
Test Coverage
A
100%
# frozen_string_literal: true

# https://docs.google.com/spreadsheets/d/1LgECNzbhOJ0MPS-2jbsXVyvtS2Q2TnjDQ4o42tyhU_g/edit#gid=1744254165
# Where scheme = LGFS
# And Where submission type = redetermination
# And Where originally submitted = DATE specified for this lookup *
# And Where disk evidence = no
# And Where claim total < 20,000
#

Dir.glob(File.join(__dir__, '..', 'base_count_query.rb')).each { |f| require_dependency f }

module Stats
  module ManagementInformation
    module Queries
      module LGFS
        class LF2RedeterminationQuery < BaseCountQuery
          acts_as_scheme :lgfs

          private

          # OPTIMIZE: this is the sames as AF2RedeterminationQuery
          def query
            <<~SQL
              WITH days AS (
                SELECT day::date
                FROM generate_series('#{@start_at}', '#{@end_at}', '1 day'::interval) day
              ),
              journeys AS (
                #{journeys_query}
              )
              SELECT count(j.*), date_trunc('day', d.day) as day
              FROM days d
              LEFT OUTER JOIN journeys j
                ON date_trunc('day', j.#{@date_column_filter}) = d.day
                AND j.scheme = 'LGFS'
                AND j.journey -> 0 ->> 'to' = 'redetermination'
                AND NOT j.disk_evidence
                AND j.claim_total::float < 20000.00
              GROUP BY day
            SQL
          end
        end
      end
    end
  end
end