Sha256: 48f35bcba2181869bf281dc3374fc2131996a1a33566783c16b88d557a80b904

Contents?: true

Size: 1.46 KB

Versions: 44

Compression:

Stored size: 1.46 KB

Contents

# frozen_string_literal: true

# Reports on all patients that were on treatment in given time period
# regardless of whether they have a terminal state or not within the
# period.
module MalawiHivProgramReports
  module Clinic
    class PatientsOnTreatment
      attr_reader :start_date, :end_date

      def initialize(start_date:, end_date:)
        @start_date = start_date
        @end_date = end_date
      end

      # Returns patients that were on treatment within the given time period.
      def self.within(start_date, end_date)
        sql_conditions =
          <<~SQL
            ((start_date >= :start_date AND start_date <= :end_date)
              OR (end_date >= :start_date AND end_date <= :end_date)
              OR (start_date < :start_date AND end_date IS NULL))
            AND state = :state
          SQL

        on_arvs = ::PatientState.where(sql_conditions, start_date:,
                                                       end_date:,
                                                       state: ::ArtService::Constants::States::ON_ANTIRETROVIRALS)

        ::PatientProgram.select('DISTINCT patient_program.patient_id')
                        .joins(:patient_states)
                        .merge(on_arvs)
                        .where(program_id: ::ArtService::Constants::PROGRAM_ID)
      end

      # We an interface to satisfy, let's be good citizens
      def find_report
        within(start_date, end_date).map(&:patient_id)
      end
    end
  end
end

Version data entries

44 entries across 44 versions & 1 rubygems

Version Path
malawi_hiv_program_reports-1.1.18 app/services/malawi_hiv_program_reports/clinic/patients_on_treatment.rb
malawi_hiv_program_reports-1.1.17 app/services/malawi_hiv_program_reports/clinic/patients_on_treatment.rb
malawi_hiv_program_reports-1.1.16 app/services/malawi_hiv_program_reports/clinic/patients_on_treatment.rb
malawi_hiv_program_reports-1.1.15 app/services/malawi_hiv_program_reports/clinic/patients_on_treatment.rb
malawi_hiv_program_reports-1.1.14 app/services/malawi_hiv_program_reports/clinic/patients_on_treatment.rb
malawi_hiv_program_reports-1.1.13 app/services/malawi_hiv_program_reports/clinic/patients_on_treatment.rb
malawi_hiv_program_reports-1.1.12 app/services/malawi_hiv_program_reports/clinic/patients_on_treatment.rb
malawi_hiv_program_reports-1.1.11 app/services/malawi_hiv_program_reports/clinic/patients_on_treatment.rb
malawi_hiv_program_reports-1.1.10 app/services/malawi_hiv_program_reports/clinic/patients_on_treatment.rb
malawi_hiv_program_reports-1.1.9 app/services/malawi_hiv_program_reports/clinic/patients_on_treatment.rb
malawi_hiv_program_reports-1.1.8 app/services/malawi_hiv_program_reports/clinic/patients_on_treatment.rb
malawi_hiv_program_reports-1.1.7 app/services/malawi_hiv_program_reports/clinic/patients_on_treatment.rb
malawi_hiv_program_reports-1.1.6 app/services/malawi_hiv_program_reports/clinic/patients_on_treatment.rb
malawi_hiv_program_reports-1.1.5 app/services/malawi_hiv_program_reports/clinic/patients_on_treatment.rb
malawi_hiv_program_reports-1.1.4 app/services/malawi_hiv_program_reports/clinic/patients_on_treatment.rb
malawi_hiv_program_reports-1.1.3 app/services/malawi_hiv_program_reports/clinic/patients_on_treatment.rb
malawi_hiv_program_reports-1.1.2 app/services/malawi_hiv_program_reports/clinic/patients_on_treatment.rb
malawi_hiv_program_reports-1.1.1 app/services/malawi_hiv_program_reports/clinic/patients_on_treatment.rb
malawi_hiv_program_reports-1.1.0 app/services/malawi_hiv_program_reports/clinic/patients_on_treatment.rb
malawi_hiv_program_reports-1.0.27 app/services/malawi_hiv_program_reports/clinic/patients_on_treatment.rb