malawi_hiv_program_reports 1.0.17 → 1.0.19

Sign up to get free protection for your applications and to get access to all the features.
@@ -19,7 +19,9 @@ module MalawiHivProgramReports
19
19
  end
20
20
 
21
21
  def find_report
22
- drop_temporary_tables
22
+ drop_temporary_tables unless @location
23
+ delete_patients_in_temp_table if @location
24
+
23
25
  create_temp_earliest_start_date unless temp_eartliest_start_date_exists?
24
26
  init_report
25
27
  build_cohort_tables
@@ -62,26 +64,55 @@ module MalawiHivProgramReports
62
64
  }
63
65
  end
64
66
 
67
+ def delete_patients_in_temp_table
68
+ if ActiveRecord::Base.connection.table_exists?('temp_tb_screened')
69
+ execute_action("DELETE FROM temp_tb_screened WHERE site_id = #{@location}")
70
+ end
71
+ if ActiveRecord::Base.connection.table_exists?('temp_tb_confirmed_and_on_treatment')
72
+ execute_action("DELETE FROM temp_tb_confirmed_and_on_treatment WHERE site_id = #{@location}")
73
+ end
74
+ end
75
+
65
76
  def drop_temporary_tables
66
- execute_action('DROP TABLE IF EXISTS temp_tb_screened;')
67
- execute_action('DROP TABLE IF EXISTS temp_tb_confirmed_and_on_treatment;')
77
+ execute_action("DROP TABLE IF EXISTS temp_tb_screened;")
78
+ execute_action("DROP TABLE IF EXISTS temp_tb_confirmed_and_on_treatment;")
68
79
  end
69
80
 
70
81
  def build_cohort_tables
71
82
  return unless rebuild_outcome || @occupation.present?
72
83
 
73
- cohort_builder = MalawiHivProgramReports::Moh::CohortBuilder.new(outcomes_definition: 'pepfar',
74
- location: @location)
75
- cohort_builder.init_temporary_tables(start_date, end_date, @occupation)
84
+ cohort_builder = MalawiHivProgramReports::Moh::CumulativeCohort.new(
85
+ start_date: start_date,
86
+ end_date: end_date,
87
+ locations: [@location.to_s],
88
+ rebuild: rebuild_outcome
89
+ ).find_report
76
90
  end
77
91
 
78
92
  def process_tb_screening
79
93
  execute_action(create_temp_tb_screened_query)
94
+ execute_action(insert_temp_tb_screened_query)
95
+ end
96
+
97
+ def create_temp_tb_screened_query
98
+ <<~SQL
99
+ CREATE TABLE IF NOT EXISTS temp_tb_screened(
100
+ patient_id INT(11) NOT NULL,
101
+ gender VARCHAR(255) NULL,
102
+ screened_date DATE NULL,
103
+ enrollment_date DATE NULL,
104
+ age_group VARCHAR(255) NULL,
105
+ tb_status VARCHAR(255) NULL,
106
+ screening_methods VARCHAR(255) DEFAULT NULL,
107
+ site_id INT(11) DEFAULT 1,
108
+ PRIMARY KEY (patient_id, site_id)
109
+ )
110
+ SQL
80
111
  end
81
112
 
82
- def create_temp_tb_screened_query
113
+ def insert_temp_tb_screened_query
83
114
  <<~SQL
84
- CREATE TABLE temp_tb_screened AS
115
+ INSERT INTO temp_tb_screened
85
116
  SELECT
86
117
  o.person_id as patient_id,
87
118
  LEFT(current_obs.gender, 1) AS gender, MAX(o.obs_datetime) AS screened_date,
@@ -89,18 +120,19 @@ module MalawiHivProgramReports
89
120
  disaggregated_age_group(current_obs.birthdate, DATE('#{end_date.to_date}')) AS age_group,
90
121
  cn.name AS tb_status,
91
122
  GROUP_CONCAT(DISTINCT vcn.name) AS screening_methods
92
- FROM obs o
123
+ #{@location ? ", #{@location}" : 1} AS site_id
124
+ FROM obs #{current_partition} o
93
125
  INNER JOIN (
94
126
  SELECT o.person_id, MAX(o.obs_datetime) AS obs_datetime, tesd.earliest_start_date, tesd.gender, tesd.birthdate
95
- FROM obs o
96
- INNER JOIN temp_earliest_start_date tesd ON tesd.patient_id = o.person_id
127
+ FROM obs #{current_partition} o
128
+ INNER JOIN cdr_temp_cohort_members tesd ON tesd.patient_id = o.person_id
97
129
  WHERE o.concept_id = #{::ConceptName.find_by_name('TB status').concept_id}
98
130
  AND o.value_coded IN (SELECT concept_id FROM concept_name WHERE name IN ('TB Suspected', 'TB NOT suspected') AND voided = 0)
99
131
  AND o.voided = 0 AND o.obs_datetime BETWEEN '#{start_date}' AND '#{end_date}' #{site_manager(operator: 'AND', column: 'o.site_id', location: @location)}
100
132
  GROUP BY o.person_id
101
133
  ) current_obs ON current_obs.person_id = o.person_id AND current_obs.obs_datetime = o.obs_datetime
102
134
  INNER JOIN concept_name cn ON cn.concept_id = o.value_coded AND cn.voided = 0
103
- LEFT JOIN obs screen_method ON screen_method.concept_id = #{::ConceptName.find_by_name('TB screening method used').concept_id}
135
+ LEFT JOIN obs #{current_partition} screen_method ON screen_method.concept_id = #{::ConceptName.find_by_name('TB screening method used').concept_id}
104
136
  AND screen_method.voided = 0 #{site_manager(operator: 'AND', column: 'screen_method.site_id', location: @location)}
105
137
  AND screen_method.person_id = o.person_id#{' '}
106
138
  AND DATE(screen_method.obs_datetime) = DATE(current_obs.obs_datetime)
@@ -114,22 +146,42 @@ module MalawiHivProgramReports
114
146
  end
115
147
 
116
148
  def temp_eartliest_start_date_exists?
117
- ActiveRecord::Base.connection.table_exists?('temp_earliest_start_date')
149
+ ActiveRecord::Base.connection.table_exists?('cdr_temp_cohort_members')
118
150
  end
119
151
 
120
152
  def create_temp_earliest_start_date
121
- cohort_builder = MalawiHivProgramReports::Moh::CohortBuilder.new(outcomes_definition: 'pepfar',
122
- location: @location)
123
- cohort_builder.init_temporary_tables(start_date, end_date, @occupation)
153
+ MalawiHivProgramReports::Moh::CumulativeCohort.new(
154
+ start_date: start_date,
155
+ end_date: end_date,
156
+ locations: [@location],
157
+ rebuild: rebuild_outcome
158
+ ).find_report
124
159
  end
125
160
 
126
161
  def process_tb_confirmed_and_on_treatment
127
162
  execute_action(create_temp_tb_confirmed_query)
163
+ execute_action(insert_temp_tb_confirmed_query)
128
164
  end
129
165
 
130
166
  def create_temp_tb_confirmed_query
131
167
  <<~SQL
132
- CREATE TABLE temp_tb_confirmed_and_on_treatment AS
168
+ CREATE TABLE IF NOT EXISTS temp_tb_confirmed_and_on_treatment(
169
+ patient_id INT(11) NOT NULL,
170
+ gender VARCHAR(255) NULL,
171
+ age_group VARCHAR(255) NOT NULL,
172
+ tb_confirmed_date DATE NULL,
173
+ has_tb_confirmed_date BOOLEAN NOT NULL,
174
+ enrollment_date DATE NULL,
175
+ prev_reading DATE,
176
+ site_id INT(11) DEFAULT 1,
177
+ PRIMARY KEY (patient_id, site_id)
178
+ )
179
+ SQL
180
+ end
181
+
182
+ def insert_temp_tb_confirmed_query
183
+ <<~SQL
184
+ INSERT INTO temp_tb_confirmed_and_on_treatment
133
185
  SELECT
134
186
  o.person_id as patient_id,
135
187
  LEFT(p.gender, 1) AS gender,
@@ -141,17 +193,18 @@ module MalawiHivProgramReports
141
193
  END AS has_tb_confirmed_date,
142
194
  tesd.earliest_start_date as enrollment_date,
143
195
  prev.tb_confirmed_date prev_reading
144
- FROM obs o
145
- INNER JOIN temp_earliest_start_date tesd ON tesd.patient_id = o.person_id #{site_manager(operator: 'AND', column: 'tesd.site_id', location: @location)}
146
- INNER JOIN person p ON p.person_id = o.person_id AND p.voided = 0 #{site_manager(operator: 'AND', column: 'p.site_id', location: @location)}
147
- LEFT JOIN obs tcd ON tcd.concept_id = #{::ConceptName.find_by_name('TB treatment start date').concept_id}
196
+ #{@location ? ", #{@location}" : ''} AS site_id
197
+ FROM obs #{current_partition} o
198
+ INNER JOIN cdr_temp_cohort_members tesd ON tesd.patient_id = o.person_id #{site_manager(operator: 'AND', column: 'tesd.site_id', location: @location)}
199
+ INNER JOIN person #{current_partition} p ON p.person_id = o.person_id AND p.voided = 0 #{site_manager(operator: 'AND', column: 'p.site_id', location: @location)}
200
+ LEFT JOIN obs #{current_partition} tcd ON tcd.concept_id = #{::ConceptName.find_by_name('TB treatment start date').concept_id}
148
201
  AND tcd.voided = 0 AND tcd.person_id = o.person_id #{site_manager(operator: 'AND', column: 'tcd.site_id', location: @location)}
149
202
  LEFT JOIN (
150
203
  SELECT
151
204
  o.person_id,
152
205
  COALESCE(MAX(tcd.value_datetime),MAX(o.obs_datetime)) AS tb_confirmed_date
153
- FROM obs o
154
- LEFT JOIN obs tcd ON tcd.concept_id = #{::ConceptName.find_by_name('TB treatment start date').concept_id}
206
+ FROM obs #{current_partition} o
207
+ LEFT JOIN obs #{current_partition} tcd ON tcd.concept_id = #{::ConceptName.find_by_name('TB treatment start date').concept_id}
155
208
  AND tcd.voided = 0 AND tcd.person_id = o.person_id #{site_manager(operator: 'AND', column: 'tcd.site_id', location: @location)}
156
209
  WHERE o.concept_id = #{::ConceptName.find_by_name('TB status').concept_id}
157
210
  AND o.value_coded = #{::ConceptName.find_by_name('Confirmed TB on treatment').concept_id}
@@ -182,8 +235,8 @@ module MalawiHivProgramReports
182
235
  def create_patients_alive_and_on_art_query
183
236
  <<~SQL
184
237
  SELECT tpo.patient_id, LEFT(tesd.gender, 1) AS gender, disaggregated_age_group(tesd.birthdate, DATE('#{end_date.to_date}')) age_group
185
- FROM temp_patient_outcomes tpo
186
- INNER JOIN temp_earliest_start_date tesd ON tesd.patient_id = tpo.patient_id #{site_manager(operator: 'AND', column: 'tesd.site_id', location: @location)}
238
+ FROM cdr_temp_patient_outcomes #{current_partition} tpo
239
+ INNER JOIN cdr_temp_cohort_members tesd ON tesd.patient_id = tpo.patient_id #{site_manager(operator: 'AND', column: 'tesd.site_id', location: @location)}
187
240
  WHERE tpo.cum_outcome = 'On antiretrovirals' #{site_manager(operator: 'AND', column: 'tpo.site_id', location: @location)}
188
241
  SQL
189
242
  end
@@ -269,6 +322,7 @@ module MalawiHivProgramReports
269
322
  <<~SQL
270
323
  SELECT tbs.patient_id, tbs.enrollment_date, LEFT(tbs.gender, 1) AS gender, tbs.age_group, tbs.tb_status, tbs.screened_date, tbs.screening_methods
271
324
  FROM temp_tb_screened tbs
325
+ #{site_manager(operator: 'WHERE', column: 'tbs.site_id', location: @location)}
272
326
  SQL
273
327
  end
274
328
 
@@ -277,6 +331,7 @@ module MalawiHivProgramReports
277
331
  SELECT t.patient_id, t.gender, t.age_group, t.enrollment_date, t.tb_confirmed_date
278
332
  FROM temp_tb_confirmed_and_on_treatment t
279
333
  WHERE t.tb_confirmed_date > '#{start_date}'
334
+ #{site_manager(operator: 'AND', column: 't.site_id', location: @location)}
280
335
  AND (t.has_tb_confirmed_date = TRUE OR t.prev_reading IS NULL OR TIMESTAMPDIFF(MONTH,t.prev_reading, t.tb_confirmed_date) > 6)
281
336
  SQL
282
337
  end
@@ -37,7 +37,7 @@ module MalawiHivProgramReports
37
37
  def pepfar_patient_drilldown_information(patients, current_date)
38
38
  raise ::ArgumentError, "current_date can't be nil" unless current_date
39
39
 
40
- ::Person.joins("LEFT JOIN patient_identifier ON patient_identifier.patient_id = person.person_id
40
+ ::Person.joins("LEFT JOIN patient_identifier #{current_partition} ON patient_identifier.patient_id = person.person_id
41
41
  AND patient_identifier.voided = 0
42
42
  AND patient_identifier.identifier_type IN (#{pepfar_patient_identifier_type.to_sql})")
43
43
  .where(person_id: patients)
@@ -96,22 +96,22 @@ module MalawiHivProgramReports
96
96
 
97
97
  ActiveRecord::Base.connection.select_all("
98
98
  SELECT p.person_id patient_id
99
- FROM person p
100
- INNER JOIN patient_program pp ON pp.patient_id = p.person_id AND pp.program_id = #{::Program.find_by_name('HIV PROGRAM').id} AND pp.voided = 0
99
+ FROM person #{current_partition} p
100
+ INNER JOIN patient_program #{current_partition} pp ON pp.patient_id = p.person_id AND pp.program_id = #{::Program.find_by_name('HIV PROGRAM').id} AND pp.voided = 0
101
101
  #{site_manager(operator: 'AND', column: 'pp.site_id', location: @location)}
102
- INNER JOIN patient_state ps ON ps.patient_program_id = pp.patient_program_id AND ps.state = 7 AND ps.start_date IS NOT NULL
102
+ INNER JOIN patient_state #{current_partition} ps ON ps.patient_program_id = pp.patient_program_id AND ps.state = 7 AND ps.start_date IS NOT NULL
103
103
  #{site_manager(operator: 'AND', column: 'ps.site_id', location: @location)}
104
- LEFT JOIN encounter as hiv_registration ON hiv_registration.patient_id = p.person_id AND hiv_registration.encounter_datetime < DATE(#{ActiveRecord::Base.connection.quote(end_date)}) AND hiv_registration.encounter_type = #{hiv_clinic_registration_id} AND hiv_registration.voided = 0
104
+ LEFT JOIN encounter #{current_partition} as hiv_registration ON hiv_registration.patient_id = p.person_id AND hiv_registration.encounter_datetime < DATE(#{ActiveRecord::Base.connection.quote(end_date)}) AND hiv_registration.encounter_type = #{hiv_clinic_registration_id} AND hiv_registration.voided = 0
105
105
  #{site_manager(operator: 'AND', column: 'hiv_registration.site_id', location: @location)}
106
106
  LEFT JOIN (
107
- SELECT * FROM obs WHERE concept_id = #{type_of_patient_concept} AND voided = 0 AND value_coded = #{new_patient_concept} AND obs_datetime < #{interval_manager(date: end_date, value: 1, interval: 'DAY', operator: '+')}
107
+ SELECT * FROM obs #{current_partition} WHERE concept_id = #{type_of_patient_concept} AND voided = 0 AND value_coded = #{new_patient_concept} AND obs_datetime < #{interval_manager(date: end_date, value: 1, interval: 'DAY', operator: '+')}
108
108
  #{site_manager(operator: 'AND', column: 'obs.site_id', location: @location)}
109
109
  ) AS new_patient ON p.person_id = new_patient.person_id
110
110
  LEFT JOIN (
111
- SELECT * FROM obs WHERE concept_id = #{type_of_patient_concept} AND voided = 0 AND value_coded = #{drug_refill_concept} AND obs_datetime < #{interval_manager(date: end_date, value: 1, interval: 'DAY', operator: '+')} #{site_manager(operator: 'AND', column: 'obs.site_id', location: @location)}
111
+ SELECT * FROM obs #{current_partition} WHERE concept_id = #{type_of_patient_concept} AND voided = 0 AND value_coded = #{drug_refill_concept} AND obs_datetime < #{interval_manager(date: end_date, value: 1, interval: 'DAY', operator: '+')} #{site_manager(operator: 'AND', column: 'obs.site_id', location: @location)}
112
112
  ) AS refill ON p.person_id = refill.person_id
113
113
  LEFT JOIN (
114
- SELECT * FROM obs WHERE concept_id = #{type_of_patient_concept} AND voided = 0 AND value_coded = #{external_concept} AND obs_datetime < #{interval_manager(date: end_date, value: 1, interval: 'DAY', operator: '+')} #{site_manager(operator: 'AND', column: 'obs.site_id', location: @location)}
114
+ SELECT * FROM obs #{current_partition} WHERE concept_id = #{type_of_patient_concept} AND voided = 0 AND value_coded = #{external_concept} AND obs_datetime < #{interval_manager(date: end_date, value: 1, interval: 'DAY', operator: '+')} #{site_manager(operator: 'AND', column: 'obs.site_id', location: @location)}
115
115
  ) AS external ON p.person_id = external.person_id
116
116
  WHERE (refill.value_coded IS NOT NULL OR external.value_coded IS NOT NULL)
117
117
  AND NOT (hiv_registration.encounter_id IS NOT NULL OR new_patient.value_coded IS NOT NULL)
@@ -124,15 +124,15 @@ module MalawiHivProgramReports
124
124
  def pregnant_women(patient_list)
125
125
  ActiveRecord::Base.connection.select_all <<~SQL
126
126
  SELECT o.person_id, o.value_coded
127
- FROM obs o
128
- INNER JOIN encounter e ON e.encounter_id = o.encounter_id AND e.voided = 0 AND e.encounter_type IN (#{encounter_types.to_sql})
127
+ FROM obs #{current_partition} o
128
+ INNER JOIN encounter #{current_partition} e ON e.encounter_id = o.encounter_id AND e.voided = 0 AND e.encounter_type IN (#{encounter_types.to_sql})
129
129
  #{site_manager(operator: 'AND', column: 'e.site_id', location: @location)}
130
- INNER JOIN person p ON o.person_id = e.patient_id AND LEFT(p.gender, 1) = 'F'
130
+ INNER JOIN person #{current_partition} p ON o.person_id = e.patient_id AND LEFT(p.gender, 1) = 'F'
131
131
  #{site_manager(operator: 'AND', column: 'p.site_id', location: @location)}
132
132
  INNER JOIN (
133
133
  SELECT person_id, MAX(obs_datetime) AS obs_datetime
134
- FROM obs
135
- INNER JOIN encounter ON encounter.encounter_id = obs.encounter_id AND encounter.encounter_type IN (#{encounter_types.to_sql}) AND encounter.voided = 0
134
+ FROM obs #{current_partition}
135
+ INNER JOIN encounter #{current_partition} ON encounter.encounter_id = obs.encounter_id AND encounter.encounter_type IN (#{encounter_types.to_sql}) AND encounter.voided = 0
136
136
  #{site_manager(operator: 'AND', column: 'encounter.site_id', location: @location)}
137
137
  WHERE obs.concept_id IN (#{pregnant_concepts.to_sql})
138
138
  AND obs.obs_datetime BETWEEN DATE(#{ActiveRecord::Base.connection.quote(start_date)}) AND DATE(#{ActiveRecord::Base.connection.quote(end_date)}) + INTERVAL 1 DAY
@@ -153,15 +153,15 @@ module MalawiHivProgramReports
153
153
  def breast_feeding(patient_list)
154
154
  ActiveRecord::Base.connection.select_all <<~SQL
155
155
  SELECT o.person_id, o.value_coded
156
- FROM obs o
157
- INNER JOIN encounter e ON e.encounter_id = o.encounter_id AND e.voided = 0 AND e.encounter_type IN (#{encounter_types.to_sql})
156
+ FROM obs #{current_partition} o
157
+ INNER JOIN encounter #{current_partition} e ON e.encounter_id = o.encounter_id AND e.voided = 0 AND e.encounter_type IN (#{encounter_types.to_sql})
158
158
  #{site_manager(operator: 'AND', column: 'e.site_id', location: @location)}
159
- INNER JOIN person p ON o.person_id = e.patient_id AND LEFT(p.gender, 1) = 'F'
159
+ INNER JOIN person #{current_partition} p ON o.person_id = e.patient_id AND LEFT(p.gender, 1) = 'F'
160
160
  #{site_manager(operator: 'AND', column: 'p.site_id', location: @location)}
161
161
  INNER JOIN (
162
162
  SELECT person_id, MAX(obs_datetime) AS obs_datetime
163
- FROM obs
164
- INNER JOIN encounter ON encounter.encounter_id = obs.encounter_id AND encounter.encounter_type IN (#{encounter_types.to_sql}) AND encounter.voided = 0
163
+ FROM obs #{current_partition}
164
+ INNER JOIN encounter #{current_partition} ON encounter.encounter_id = obs.encounter_id AND encounter.encounter_type IN (#{encounter_types.to_sql}) AND encounter.voided = 0
165
165
  #{site_manager(operator: 'AND', column: 'encounter.site_id', location: @location)}
166
166
  WHERE obs.concept_id IN (#{breast_feeding_concepts.to_sql})
167
167
  AND obs.obs_datetime BETWEEN DATE(#{ActiveRecord::Base.connection.quote(start_date)}) AND DATE(#{ActiveRecord::Base.connection.quote(end_date)}) + INTERVAL 1 DAY
@@ -198,8 +198,8 @@ module MalawiHivProgramReports
198
198
  ActiveRecord::Base.connection.select_all(
199
199
  <<~SQL
200
200
  SELECT tpo.patient_id, LEFT(tesd.gender, 1) AS gender, disaggregated_age_group(tesd.birthdate, DATE('#{end_date.to_date}')) age_group
201
- FROM temp_patient_outcomes tpo
202
- INNER JOIN temp_earliest_start_date tesd ON tesd.patient_id = tpo.patient_id
201
+ FROM temp_patient_outcomes #{current_partition} tpo
202
+ INNER JOIN temp_earliest_start_date #{current_partition} tesd ON tesd.patient_id = tpo.patient_id
203
203
  WHERE tpo.cum_outcome = 'On antiretrovirals'
204
204
  SQL
205
205
  )
@@ -267,10 +267,10 @@ module MalawiHivProgramReports
267
267
  @adverse_outcomes ||= ActiveRecord::Base.connection.select_all(
268
268
  <<~SQL
269
269
  SELECT pws.program_workflow_state_id state
270
- FROM program_workflow pw
271
- INNER JOIN concept_name pcn ON pcn.concept_id = pw.concept_id AND pcn.concept_name_type = 'FULLY_SPECIFIED' AND pcn.voided = 0
272
- INNER JOIN program_workflow_state pws ON pws.program_workflow_id = pw.program_workflow_id AND pws.retired = 0
273
- INNER JOIN concept_name cn ON cn.concept_id = pws.concept_id AND cn.concept_name_type = 'FULLY_SPECIFIED' AND cn.voided = 0
270
+ FROM program_workflow #{current_partition} pw
271
+ INNER JOIN concept_name #{current_partition} pcn ON pcn.concept_id = pw.concept_id AND pcn.concept_name_type = 'FULLY_SPECIFIED' AND pcn.voided = 0
272
+ INNER JOIN program_workflow_state #{current_partition} pws ON pws.program_workflow_id = pw.program_workflow_id AND pws.retired = 0
273
+ INNER JOIN concept_name #{current_partition} cn ON cn.concept_id = pws.concept_id AND cn.concept_name_type = 'FULLY_SPECIFIED' AND cn.voided = 0
274
274
  WHERE pw.program_id = 1 AND pw.retired = 0 AND pws.terminal = 1
275
275
  SQL
276
276
  ).map { |state| state['state'] }
@@ -287,18 +287,18 @@ module MalawiHivProgramReports
287
287
  current_state.state,
288
288
  current_state.start_date outcome_date,
289
289
  current_order.start_date vl_order_date
290
- FROM orders ab
291
- INNER JOIN person p ON p.person_id = ab.patient_id AND p.voided = 0
290
+ FROM orders #{current_partition} ab
291
+ INNER JOIN person #{current_partition} p ON p.person_id = ab.patient_id AND p.voided = 0
292
292
  #{site_manager(operator: 'AND', column: 'p.site_id', location: @location)}
293
- INNER JOIN drug_order dor ON dor.order_id = ab.order_id AND dor.quantity > 0
293
+ INNER JOIN drug_order #{current_partition} dor ON dor.order_id = ab.order_id AND dor.quantity > 0
294
294
  #{site_manager(operator: 'AND', column: 'dor.site_id', location: @location)}
295
- INNER JOIN arv_drug ad ON dor.drug_inventory_id = ad.drug_id
296
- INNER JOIN patient_program pp ON pp.patient_id = ab.patient_id AND pp.voided = 0 AND pp.program_id = 1
295
+ INNER JOIN arv_drug #{current_partition} ad ON dor.drug_inventory_id = ad.drug_id
296
+ INNER JOIN patient_program #{current_partition} pp ON pp.patient_id = ab.patient_id AND pp.voided = 0 AND pp.program_id = 1
297
297
  #{site_manager(operator: 'AND', column: 'pp.site_id', location: @location)}
298
298
  INNER JOIN (
299
299
  SELECT a.site_id, a.patient_program_id, a.state, a.start_date, a.end_date
300
- FROM patient_state a
301
- LEFT OUTER JOIN patient_state b ON a.patient_program_id = b.patient_program_id
300
+ FROM patient_state #{current_partition} a
301
+ LEFT OUTER JOIN patient_state #{current_partition} b ON a.patient_program_id = b.patient_program_id
302
302
  #{site_manager(operator: 'AND', column: 'b.site_id', location: @location)}
303
303
  AND a.start_date < b.start_date
304
304
  #{site_manager(operator: 'AND', column: 'a.site_id', location: @location)}
@@ -306,24 +306,24 @@ module MalawiHivProgramReports
306
306
  WHERE b.patient_program_id IS NULL AND a.end_date IS NULL AND a.voided = 0
307
307
  ) current_state ON current_state.patient_program_id = pp.patient_program_id
308
308
  #{site_manager(operator: 'AND', column: 'current_state.site_id', location: @location)}
309
- LEFT OUTER JOIN orders b ON ab.patient_id = b.patient_id
309
+ LEFT OUTER JOIN orders #{current_partition} b ON ab.patient_id = b.patient_id
310
310
  AND ab.order_id = b.order_id
311
311
  AND ab.auto_expire_date < b.auto_expire_date
312
312
  AND b.voided = 0 AND b.order_type_id = 1
313
313
  #{site_manager(operator: 'AND', column: 'b.site_id', location: @location)}
314
314
  LEFT JOIN (#{current_occupation_query}) a ON a.person_id = ab.patient_id
315
315
  #{site_manager(operator: 'AND', column: 'a.site_id', location: @location)}
316
- LEFT JOIN patient_identifier pid ON pid.patient_id = pp.patient_id AND pid.identifier_type IN (#{pepfar_patient_identifier_type.to_sql}) AND pid.voided = 0
316
+ LEFT JOIN patient_identifier #{current_partition} pid ON pid.patient_id = pp.patient_id AND pid.identifier_type IN (#{pepfar_patient_identifier_type.to_sql}) AND pid.voided = 0
317
317
  #{site_manager(operator: 'AND', column: 'pid.site_id', location: @location)}
318
318
  LEFT JOIN (
319
319
  SELECT ab.patient_id, MAX(ab.start_date) start_date
320
- FROM orders ab
321
- INNER JOIN concept_name
320
+ FROM orders #{current_partition} ab
321
+ INNER JOIN concept_name #{current_partition}
322
322
  ON concept_name.concept_id = ab.concept_id
323
323
  AND concept_name.name IN ('Blood', 'DBS (Free drop to DBS card)', 'DBS (Using capillary tube)', '50:50 Normal Plasma')
324
324
  AND concept_name.voided = 0
325
325
  #{site_manager(operator: 'AND', column: 'ab.site_id', location: @location)}
326
- LEFT OUTER JOIN orders b ON ab.patient_id = b.patient_id
326
+ LEFT OUTER JOIN orders #{current_partition} b ON ab.patient_id = b.patient_id
327
327
  AND ab.order_id = b.order_id
328
328
  AND ab.start_date < b.start_date
329
329
  AND b.voided = 0
@@ -364,40 +364,40 @@ module MalawiHivProgramReports
364
364
  COALESCE(reason_for_test_value.name, reason_for_test.value_text) AS reason_for_test,
365
365
  result.value_modifier AS result_modifier,
366
366
  COALESCE(result.value_numeric, result.value_text) AS result_value
367
- FROM orders
368
- INNER JOIN person patient ON patient.person_id = orders.patient_id AND patient.voided = 0
367
+ FROM orders #{current_partition}
368
+ INNER JOIN person #{current_partition} patient ON patient.person_id = orders.patient_id AND patient.voided = 0
369
369
  #{site_manager(operator: 'AND', column: 'patient.site_id', location: @location)}
370
- INNER JOIN order_type
370
+ INNER JOIN order_type #{current_partition}
371
371
  ON order_type.order_type_id = orders.order_type_id
372
372
  AND order_type.name = 'Lab'
373
373
  AND order_type.retired = 0
374
- INNER JOIN concept_name
374
+ INNER JOIN concept_name #{current_partition}
375
375
  ON concept_name.concept_id = orders.concept_id
376
376
  AND concept_name.name IN ('Blood', 'DBS (Free drop to DBS card)', 'DBS (Using capillary tube)', 'Plasma')
377
377
  AND concept_name.voided = 0
378
- LEFT JOIN obs AS reason_for_test
378
+ LEFT JOIN obs #{current_partition} AS reason_for_test
379
379
  ON reason_for_test.order_id = orders.order_id
380
- AND reason_for_test.concept_id IN (SELECT concept_id FROM concept_name WHERE name LIKE 'Reason for test' AND voided = 0)
380
+ AND reason_for_test.concept_id IN (SELECT concept_id FROM concept_name #{current_partition} WHERE name LIKE 'Reason for test' AND voided = 0)
381
381
  AND reason_for_test.voided = 0
382
382
  #{site_manager(operator: 'AND', column: 'reason_for_test.site_id', location: @location)}
383
- LEFT JOIN concept_name AS reason_for_test_value
383
+ LEFT JOIN concept_name #{current_partition} AS reason_for_test_value
384
384
  ON reason_for_test_value.concept_id = reason_for_test.value_coded
385
385
  AND reason_for_test_value.voided = 0
386
- LEFT JOIN obs AS result
386
+ LEFT JOIN obs #{current_partition} AS result
387
387
  ON result.order_id = orders.order_id
388
- AND result.concept_id IN (SELECT concept_id FROM concept_name WHERE name LIKE 'HIV Viral load' AND voided = 0)
388
+ AND result.concept_id IN (SELECT concept_id FROM concept_name #{current_partition} WHERE name LIKE 'HIV Viral load' AND voided = 0)
389
389
  AND result.voided = 0
390
390
  AND (result.value_text IS NOT NULL OR result.value_numeric IS NOT NULL)
391
391
  #{site_manager(operator: 'AND', column: 'result.site_id', location: @location)}
392
392
  INNER JOIN (
393
393
  /* Get the latest order dates for each patient */
394
394
  SELECT orders.patient_id, MAX(orders.start_date) AS start_date
395
- FROM orders
396
- INNER JOIN order_type
395
+ FROM orders #{current_partition}
396
+ INNER JOIN order_type #{current_partition}
397
397
  ON order_type.order_type_id = orders.order_type_id
398
398
  AND order_type.name = 'Lab'
399
399
  AND order_type.retired = 0
400
- INNER JOIN concept_name
400
+ INNER JOIN concept_name #{current_partition}
401
401
  ON concept_name.concept_id = orders.concept_id
402
402
  AND concept_name.name IN ('Blood', 'DBS (Free drop to DBS card)', 'DBS (Using capillary tube)', 'Plasma')
403
403
  AND concept_name.voided = 0
@@ -409,7 +409,7 @@ module MalawiHivProgramReports
409
409
  ) AS latest_patient_order_date
410
410
  ON latest_patient_order_date.patient_id = orders.patient_id
411
411
  AND latest_patient_order_date.start_date = orders.start_date
412
- LEFT JOIN patient_identifier
412
+ LEFT JOIN patient_identifier #{current_partition}
413
413
  ON patient_identifier.patient_id = orders.patient_id
414
414
  AND patient_identifier.identifier_type IN (#{pepfar_patient_identifier_type.to_sql})
415
415
  AND patient_identifier.voided = 0
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module MalawiHivProgramReports
4
- VERSION = '1.0.17'
4
+ VERSION = '1.0.19'
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: malawi_hiv_program_reports
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.17
4
+ version: 1.0.19
5
5
  platform: ruby
6
6
  authors:
7
7
  - Roy Chanunkha
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-04-17 00:00:00.000000000 Z
11
+ date: 2024-04-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -189,7 +189,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
189
189
  - !ruby/object:Gem::Version
190
190
  version: '0'
191
191
  requirements: []
192
- rubygems_version: 3.5.6
192
+ rubygems_version: 3.4.1
193
193
  signing_key:
194
194
  specification_version: 4
195
195
  summary: Malawi HIV PROGRAM Reports for Ruby on Rails