Class: MalawiHivProgramReports::Moh::CohortDisaggregatedAdditions

Inherits:
Object
  • Object
show all
Defined in:
app/services/malawi_hiv_program_reports/moh/cohort_disaggregated_additions.rb

Overview

This class is used to add additional cohort disaggregated data rubocop:disable Metrics/ClassLength

Constant Summary collapse

COHORT_REGIMENS =
%w[
  0P 2P 4PP 4PA 9PP 9PA 11PP 11PA 12PP 12PA 14PP 14PA 15PP 15PA 16P 17PP 17PA
  4A 5A 6A 7A 8A 9A 10A 11A 12A 13A 14A 15A 16A 17A
].freeze

Instance Method Summary collapse

Constructor Details

#initialize(start_date:, end_date:, gender:, age_group:) ⇒ CohortDisaggregatedAdditions

Returns a new instance of CohortDisaggregatedAdditions.



13
14
15
16
17
18
# File 'app/services/malawi_hiv_program_reports/moh/cohort_disaggregated_additions.rb', line 13

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

Instance Method Details

#clients_given_iptObject



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'app/services/malawi_hiv_program_reports/moh/cohort_disaggregated_additions.rb', line 43

def clients_given_ipt
  return female_clients_given_ipt('FP') if @gender == 'pregnant'
  return female_clients_given_ipt('FNP') if @gender == 'fnp'
  return female_clients_given_ipt('FBf') if @gender == 'breastfeeding'

  gender = @gender.first.upcase

  patient_ids = []
  results = ActiveRecord::Base.connection.select_all <<~SQL
    SELECT
      e.patient_id, disaggregated_age_group(e.birthdate, DATE('#{@end_date}')) age_group
    FROM temp_earliest_start_date e
    INNER JOIN temp_patient_outcomes USING(patient_id)
    WHERE cum_outcome = 'On antiretrovirals' AND LEFT(gender,1) = '#{gender}'
    GROUP BY e.patient_id HAVING  age_group = '#{@age_group}';
  SQL

  (results || []).each do |row|
    patient_ids << row['patient_id'].to_i
  end

  return [] if patient_ids.blank?

  given_ipt(patient_ids)
end

#disaggregated_regimen_distributionObject



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'app/services/malawi_hiv_program_reports/moh/cohort_disaggregated_additions.rb', line 69

def disaggregated_regimen_distribution
  additional_sql = ''
  additional_for_women_sql = ''
  gender = @gender.first.upcase

  if @age_group == 'All'
    case @gender.upcase
    when 'FP'
      additional_for_women_sql = 'INNER JOIN temp_disaggregated t ON t.patient_id = e.patient_id'
      additional_for_women_sql += " AND t.maternal_status = 'FP' "
    when 'FBF'
      additional_for_women_sql = 'INNER JOIN temp_disaggregated t ON t.patient_id = e.patient_id'
      additional_for_women_sql += " AND t.maternal_status = 'Fbf' "
    when 'FNP'
      additional_for_women_sql = 'INNER JOIN temp_disaggregated t ON t.patient_id = e.patient_id'
      additional_for_women_sql += " AND t.maternal_status = 'FNP' "
    end
  else
    additional_sql = "HAVING age_group = '#{@age_group}'"
  end

  patients = ActiveRecord::Base.connection.select_all <<~SQL
    SELECT
      e.patient_id,  disaggregated_age_group(e.birthdate, DATE("#{@end_date.to_date}")) age_group
    FROM temp_earliest_start_date e
    INNER JOIN temp_patient_outcomes o ON  o.patient_id = e.patient_id
    #{additional_for_women_sql}
    WHERE LEFT(gender,1) = '#{gender}' AND o.cum_outcome = 'On antiretrovirals'
    AND DATE(date_enrolled) <= '#{@end_date.to_date}'
    GROUP BY e.patient_id #{additional_sql};
  SQL

  return {} if patients.blank?

  patient_ids = patients.map { |p| p['patient_id'].to_i }
  data = {}

  patient_ids.each do |patient_id|
    regimen_data = ActiveRecord::Base.connection.select_one <<~SQL
      SELECT patient_current_regimen(#{patient_id}, DATE('#{@end_date.to_date}')) regimen;
    SQL

    regimen = (COHORT_REGIMENS.include? regimen_data['regimen']) ? regimen_data['regimen'] : 'N/A'
    data[regimen] = [] if data[regimen].blank?
    data[regimen] << patient_id
  end

  data
end

#screened_for_tbObject



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'app/services/malawi_hiv_program_reports/moh/cohort_disaggregated_additions.rb', line 20

def screened_for_tb
  return screened_for_tb_female_client('FP') if @gender == 'pregnant'
  return screened_for_tb_female_client('FNP') if @gender == 'fnp'
  return screened_for_tb_female_client('FBf') if @gender == 'breastfeeding'

  gender = @gender.first.upcase
  results = ActiveRecord::Base.connection.select_all <<~SQL
    SELECT
      e.patient_id, disaggregated_age_group(e.birthdate, DATE('#{@end_date}')) age_group
    FROM temp_earliest_start_date e
    INNER JOIN temp_patient_outcomes USING(patient_id)
    WHERE cum_outcome = 'On antiretrovirals' AND LEFT(gender,1) = '#{gender}'
    GROUP BY e.patient_id HAVING  age_group = '#{@age_group}';
  SQL

  patient_ids = []
  (results || []).each do |r|
    patient_ids << r['patient_id'].to_i
  end

  tb_screened(patient_ids)
end