malawi_hiv_program_reports 1.0.23 → 1.0.24

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 13a308e891673d2c03b82b355cbd1cb30acf9562bd4710e90b975563a7b9a5d0
4
- data.tar.gz: 5a347a6889a9bf4cedd109e83df9146c21265b38e4aab1999ca1db3dca5f7fd6
3
+ metadata.gz: 6a9d1e889940aaf8994a6ec4460478b967f0271d4f02319f6fa5de3979ba1dee
4
+ data.tar.gz: 9429bd54ed65107f2cb2834723b368ddb6685eee25aac8d937b203532e264290
5
5
  SHA512:
6
- metadata.gz: f918d797106fafc2c0ea27b5f529dbee2403583be90428e679ec27b5d2bd4bc97a541c5ee369cf7122ec9b52d63be1bb01331833e40a7c7e8ebbc950d612d319
7
- data.tar.gz: 882a731d6f7f538e3d4cbd81fe9052ac54b15dd5b304b49d27abb097876de56daf9d4e6fcbbe5310fd69fa9003fdb3e01834071e7732afcf292ea56f895e11da
6
+ metadata.gz: 39e251a0d4e9cfa9d9cff5b7b8c86698c54127a06e4cccbd6fc737ae011f93707276a2da6d7f5c93656573b5a0564f8a93847b294cc21e2aff7b02c7a0064ade
7
+ data.tar.gz: dfe33da4f7a2dff0ef62eb6c9eba6e60ffed4eaa977a3344b142b9fe329e14de1aaf76501ad2816f415b127dce72c0e4144d1f7b7c9273d736ce3c6fabdfbea0
@@ -15,7 +15,7 @@ module MalawiHivProgramReports
15
15
  @min_age = min_age
16
16
  @max_age = max_age
17
17
  @org = org
18
- @initialize_tables = (initialize_tables == 'true')
18
+ @initialize_tables = (initialize_tables.to_s == "true")
19
19
  @occupation = kwargs[:occupation]
20
20
  @location = kwargs[:location]
21
21
  @format = kwargs[:format] || 'poc'
@@ -260,7 +260,7 @@ module MalawiHivProgramReports
260
260
  AND LENGTH(identifier) > 0 AND i.voided = 0
261
261
  WHERE s.concept_set = #{arv_concept_set} AND o.voided = 0
262
262
  AND DATE(o.start_date) = (
263
- SELECT DATE(MAX(t.start_date)) FROM orders t
263
+ SELECT DATE(MAX(t.start_date)) FROM orders #{current_partition} t
264
264
  INNER JOIN drug_order #{current_partition} t2 ON t2.order_id = t.order_id
265
265
  INNER JOIN drug t3 ON t3.drug_id = t2.drug_inventory_id
266
266
  INNER JOIN concept_set t4 ON t4.concept_id = t3.concept_id
@@ -289,7 +289,6 @@ module MalawiHivProgramReports
289
289
  INNER JOIN moh_regimen_ingredient i ON r.regimen_id = i.regimen_id
290
290
  AND r.regimen_index = #{regimen_index}
291
291
  INNER JOIN moh_regimen_doses d ON i.dose_id = d.dose_id #{weight_sql}
292
- #{site_manager(operator: 'AND', column: 'r.site_id', location: @location)}
293
292
  GROUP BY min_weight, max_weight, drug_inventory_id;
294
293
  SQL
295
294
 
@@ -62,7 +62,6 @@ module MalawiHivProgramReports
62
62
  #{function_manager(function: 'disaggregated_age_group', location: @location, args: "p.birthdate::date, '#{end_date.to_date}::date'")} age_group
63
63
  FROM person #{current_partition} p
64
64
  WHERE #{in_manager(column: 'p.person_id', values: patient_ids)}
65
- #{site_manager(operator: 'AND', column: 'p.site_id', location: @location)}
66
65
  GROUP BY p.person_id #{group_by_columns('p.birthdate, p.gender')}
67
66
  SQL
68
67
 
@@ -119,14 +118,10 @@ module MalawiHivProgramReports
119
118
  #{function_manager(function: 'date_antiretrovirals_started', location: @location, args: "p.patient_id::int, MIN(s.start_date)::date, #{@location}::int")} AS earliest_start_date
120
119
  FROM patient_program #{current_partition} p
121
120
  INNER JOIN person #{current_partition} pe ON pe.person_id = p.patient_id AND pe.voided = 0
122
- #{site_manager(operator: 'AND', column: 'pe.site_id', location: @location)}
123
121
  INNER JOIN patient_state #{current_partition} s ON p.patient_program_id = s.patient_program_id AND s.voided = 0 AND s.state = 7
124
- #{site_manager(operator: 'AND', column: 's.site_id', location: @location)}
125
122
  LEFT JOIN (#{current_occupation_query}) a ON a.person_id = p.patient_id
126
- #{site_manager(operator: 'AND', column: 'a.site_id', location: @location)}
127
123
  WHERE p.program_id = 1 #{%w[Military Civilian].include?(@occupation) ? 'AND' : ''} #{occupation_filter(occupation: @occupation, field_name: 'value', table_name: 'a', include_clause: false)}
128
124
  AND DATE(s.start_date) < '#{start_date.to_date}'
129
- #{site_manager(operator: 'AND', column: 'p.site_id', location: @location)}
130
125
  AND #{function_manager(function: 'pepfar_patient_outcome', location: @location, args: "p.patient_id::int, '#{start_date.to_date}'::date, #{@location}::int")} = 'On antiretrovirals'
131
126
  AND pe.person_id NOT IN (#{drug_refills_and_external_consultation_list})
132
127
  GROUP BY p.patient_id
@@ -144,15 +139,11 @@ module MalawiHivProgramReports
144
139
  #{function_manager(function: 'date_antiretrovirals_started', location: @location, args: "p.patient_id::int, MIN(s.start_date)::date, #{@location}::int")} AS earliest_start_date
145
140
  FROM patient_program #{current_partition} p
146
141
  INNER JOIN person #{current_partition} pe ON pe.person_id = p.patient_id AND pe.voided = 0
147
- #{site_manager(operator: 'AND', column: 'pe.site_id', location: @location)}
148
142
  INNER JOIN patient_state #{current_partition} s ON p.patient_program_id = s.patient_program_id AND s.voided = 0 AND s.state = 7
149
- #{site_manager(operator: 'AND', column: 's.site_id', location: @location)}
150
143
  LEFT JOIN (#{current_occupation_query}) a ON a.person_id = p.patient_id
151
- #{site_manager(operator: 'AND', column: 'a.site_id', location: @location)}
152
144
  WHERE p.program_id = 1 #{%w[Military Civilian].include?(@occupation) ? 'AND' : ''} #{occupation_filter(occupation: @occupation, field_name: 'value', table_name: 'a', include_clause: false)}
153
145
  AND DATE(s.start_date) BETWEEN DATE('#{start_date}') AND DATE('#{end_date}')
154
146
  AND pe.person_id NOT IN (#{drug_refills_and_external_consultation_list})
155
- #{site_manager(operator: 'AND', column: 'p.site_id', location: @location)}
156
147
  GROUP BY p.patient_id
157
148
  HAVING date_enrolled IS NOT NULL AND date_enrolled BETWEEN DATE('#{start_date}') AND DATE('#{end_date}');
158
149
  SQL
@@ -166,7 +166,7 @@ module MalawiHivProgramReports
166
166
  DATE(MIN(pregnant_or_breastfeeding.obs_datetime)) AS maternal_status_date
167
167
  FROM cdr_temp_cohort_members pp
168
168
  INNER JOIN person #{current_partition} pe ON pe.person_id = pp.patient_id AND pe.voided = 0
169
- #{site_manager(operator: 'AND', column: 'pe.site_id', location: @location)}
169
+ #{site_manager(operator: 'AND', column: 'pp.site_id', location: @location)}
170
170
  LEFT JOIN (
171
171
  SELECT max(o.obs_datetime) AS obs_datetime, o.person_id
172
172
  FROM obs #{current_partition} o
@@ -176,24 +176,20 @@ module MalawiHivProgramReports
176
176
  AND pp.voided = 0
177
177
  INNER JOIN patient_state #{current_partition} ps ON ps.patient_program_id = pp.patient_program_id AND ps.voided = 0 AND ps.state = 7 AND ps.start_date <= DATE('#{end_date}')
178
178
  WHERE o.concept_id = #{concept_name('CD4 count').concept_id} AND o.voided = 0
179
- #{site_manager(operator: 'AND', column: 'o.site_id', location: @location)}#{' '}
180
179
  AND o.obs_datetime <= '#{end_date}' AND o.obs_datetime >= '#{start_date}'
181
180
  GROUP BY o.person_id
182
181
  ) current_cd4 ON current_cd4.person_id = pp.patient_id
183
182
  LEFT JOIN obs #{current_partition} o ON o.person_id = pp.patient_id AND o.concept_id = #{concept_name('CD4 count').concept_id} AND o.voided = 0 AND o.obs_datetime = current_cd4.obs_datetime
184
- #{site_manager(operator: 'AND', column: 'o.site_id', location: @location)}
185
183
  LEFT JOIN obs #{current_partition} transfer_in ON transfer_in.person_id = pp.patient_id
186
184
  AND transfer_in.concept_id = #{concept_name('Ever registered at ART clinic').concept_id}
187
185
  AND transfer_in.voided = 0
188
186
  AND transfer_in.value_coded = #{concept_name('Yes').concept_id}
189
187
  AND transfer_in.obs_datetime <= '#{end_date}'
190
188
  AND transfer_in.obs_datetime >= '#{start_date}'
191
- #{site_manager(operator: 'AND', column: 'transfer_in.site_id', location: @location)}
192
189
  LEFT JOIN obs #{current_partition} pregnant_or_breastfeeding ON pregnant_or_breastfeeding.person_id = pp.patient_id
193
190
  AND pregnant_or_breastfeeding.concept_id IN (SELECT concept_id FROM concept_name WHERE name IN ('Breast feeding?', 'Breast feeding', 'Breastfeeding', 'Is patient pregnant?', 'patient pregnant') AND voided = 0)
194
191
  AND pregnant_or_breastfeeding.voided = 0
195
192
  AND pregnant_or_breastfeeding.value_coded = #{concept_name('Yes').concept_id}
196
- #{site_manager(operator: 'AND', column: 'pregnant_or_breastfeeding.site_id', location: @location)}
197
193
  LEFT JOIN concept_name preg_or_breast ON preg_or_breast.concept_id = pregnant_or_breastfeeding.concept_id AND preg_or_breast.voided = 0
198
194
  WHERE pp.date_enrolled <= '#{end_date}' AND pp.date_enrolled >= '#{start_date}'
199
195
  GROUP BY pp.patient_id
@@ -93,8 +93,8 @@ module MalawiHivProgramReports
93
93
 
94
94
  def process_age_group_report(age_group, gender, age_group_report)
95
95
  {
96
- age_group: age_group,
97
- gender: gender,
96
+ age_group:,
97
+ gender:,
98
98
  cd4_less_than_200: age_group_report[:cd4_less_than_200],
99
99
  cd4_greater_than_or_equal_to_200: age_group_report[:cd4_greater_than_or_equal_to_200],
100
100
  unknown_cd4_count: age_group_report[:unknown_cd4_count],
@@ -122,7 +122,7 @@ module MalawiHivProgramReports
122
122
 
123
123
  def tx_rtt
124
124
  ActiveRecord::Base.connection.select_all <<~SQL
125
- SELECT patient_program.patient_id,
125
+ SELECT patient_program.patient_id,
126
126
  #{function_manager(function: 'disaggregated_age_group', location: @location, args: "person.birthdate::date, #{@end_date}::date")} AS age_group,
127
127
  person.gender,
128
128
  IF(
@@ -165,7 +165,6 @@ module MalawiHivProgramReports
165
165
  END cd4_count_group
166
166
  FROM patient_program #{current_partition}
167
167
  INNER JOIN person #{current_partition} ON person.person_id = patient_program.patient_id
168
- #{site_manager(operator: 'AND', column: 'person.site_id', location: @location)}
169
168
  /* Select patients that were on treatment before start of reporting period */
170
169
  INNER JOIN patient_state #{current_partition} AS patient_ever_on_treatment
171
170
  ON patient_ever_on_treatment.patient_program_id = patient_program.patient_program_id
@@ -186,7 +185,6 @@ module MalawiHivProgramReports
186
185
  ON date_of_last_patient_state_before_quarter.patient_program_id = patient_program.patient_program_id
187
186
  LEFT JOIN patient_state #{current_partition} AS patient_state_at_start_of_quarter
188
187
  ON patient_state_at_start_of_quarter.patient_program_id = date_of_last_patient_state_before_quarter.patient_program_id
189
- #{site_manager(operator: 'AND', column: 'patient_state_at_start_of_quarter.site_id', location: @location)}
190
188
  AND patient_state_at_start_of_quarter.date_created = date_of_last_patient_state_before_quarter.date_created
191
189
  AND patient_state_at_start_of_quarter.state IN (6, 12) /* 2: TO, 6: Tx Stopped, 12: Defaulted */
192
190
  /* Select patients who received ART within the reporting period. */
@@ -200,7 +198,7 @@ module MalawiHivProgramReports
200
198
  INNER JOIN drug_order #{current_partition}
201
199
  ON drug_order.order_id = orders.order_id
202
200
  AND drug_order.quantity > 0
203
- AND drug_order.drug_inventory_id IN (SELECT DISTINCT drug_id FROM arv_drug #{current_partition})
201
+ AND drug_order.drug_inventory_id IN (SELECT DISTINCT drug_id FROM arv_drug)
204
202
  WHERE encounter.voided = 0
205
203
  AND encounter.program_id = 1
206
204
  AND DATE(encounter.encounter_datetime) BETWEEN DATE(#{start_date}) AND DATE(#{end_date})
@@ -220,7 +218,7 @@ module MalawiHivProgramReports
220
218
  ON date_of_last_patient_state_in_quarter.patient_program_id = patient_program.patient_program_id
221
219
 
222
220
  /*Not sure why Walter had this section in but I believe its not neccessary*/
223
- /*INNER JOIN patient_state #{current_partition} AS patient_state_at_end_of_quarter
221
+ /*INNER JOIN patient_state AS patient_state_at_end_of_quarter
224
222
  ON patient_state_at_end_of_quarter.patient_program_id = patient_program.patient_program_id
225
223
  AND patient_state_at_end_of_quarter.date_created = date_of_last_patient_state_before_quarter.date_created
226
224
  AND patient_state_at_end_of_quarter.state = 7*/
@@ -235,13 +233,12 @@ module MalawiHivProgramReports
235
233
  INNER JOIN orders #{current_partition}
236
234
  ON orders.encounter_id = encounter.encounter_id
237
235
  AND orders.voided = 0
238
- #{site_manager(operator: 'AND', column: 'orders.site_id', location: @location)}
239
236
  AND DATE(orders.start_date) BETWEEN DATE(#{start_date}) AND DATE(#{end_date})
240
237
  AND DATE(orders.auto_expire_date) >= (DATE(#{end_date}) - INTERVAL 30 DAY)
241
238
  INNER JOIN drug_order #{current_partition}
242
239
  ON drug_order.order_id = orders.order_id
243
240
  AND drug_order.quantity > 0
244
- AND drug_order.drug_inventory_id IN (SELECT DISTINCT drug_id FROM arv_drug #{current_partition})
241
+ AND drug_order.drug_inventory_id IN (SELECT DISTINCT drug_id FROM arv_drug)
245
242
  WHERE encounter.program_id = 1
246
243
  AND DATE(encounter.encounter_datetime) BETWEEN DATE(#{start_date}) AND DATE(#{end_date})
247
244
  AND encounter.voided = 0
@@ -251,19 +248,17 @@ module MalawiHivProgramReports
251
248
  SELECT max(o.obs_datetime) AS obs_datetime, o.person_id
252
249
  FROM obs #{current_partition} o
253
250
  INNER JOIN concept_name cn ON cn.concept_id = o.concept_id AND cn.name = 'CD4 count'
254
- #{site_manager(operator: 'AND', column: 'o.site_id', location: @location)}
255
- WHERE o.concept_id = #{concept('CD4 count').concept_id} AND o.voided = 0
251
+ WHERE o.concept_id = #{concept_name('CD4 count').concept_id} AND o.voided = 0
256
252
  AND o.obs_datetime <= #{end_date} AND o.obs_datetime >= #{start_date}
257
253
  GROUP BY o.person_id
258
254
  ) current_cd4 ON current_cd4.person_id = patient_program.patient_id
259
- LEFT JOIN obs #{current_partition} cd4_result ON cd4_result.person_id = patient_program.patient_id AND cd4_result.concept_id = #{concept('CD4 count').concept_id} AND cd4_result.voided = 0
260
- #{site_manager(operator: 'AND', column: 'cd4_result.site_id', location: @location)}
255
+ LEFT JOIN obs #{current_partition} cd4_result ON cd4_result.person_id = patient_program.patient_id AND cd4_result.concept_id = #{concept_name('CD4 count').concept_id} AND cd4_result.voided = 0
261
256
  # Not sure why we are matching the two dates but the result was excluding cd4 results for patients.
262
257
  # AND cd4_result.obs_datetime = current_cd4.obs_datetime
263
258
  LEFT JOIN (#{current_occupation_query}) a ON a.person_id = patient_program.patient_id
264
259
  WHERE patient_program.program_id = 1 #{%w[Military Civilian].include?(@occupation) ? 'AND' : ''} #{occupation_filter(occupation: @occupation, field_name: 'value', table_name: 'a', include_clause: false)}
265
- /* Ensure that the patients retrieved, did not receive ART within 28 days
266
- before the start of the reporting period */
260
+ /* Ensure that the patients retrieved, did not receive ART within 28 days
261
+ before the start of the reporting period */
267
262
  AND patient_program.patient_id NOT IN (
268
263
  SELECT DISTINCT orders.patient_id
269
264
  FROM orders #{current_partition}
@@ -272,15 +267,13 @@ module MalawiHivProgramReports
272
267
  INNER JOIN patient_program #{current_partition}
273
268
  ON patient_program.patient_id = orders.patient_id
274
269
  AND patient_program.program_id = 1
275
- #{site_manager(operator: 'AND', column: 'orders.site_id', location: @location)}
276
270
  WHERE ((DATE(orders.start_date )BETWEEN (DATE(#{start_date}) - INTERVAL 30 DAY) AND DATE(#{start_date}))
277
271
  OR (DATE(orders.auto_expire_date) BETWEEN (DATE(#{start_date}) - INTERVAL 30 DAY) AND DATE(#{start_date})))
278
272
  AND orders.voided = 0
279
273
  )
280
274
  GROUP BY patient_program.patient_id
281
275
  HAVING initial_outcome IN ('Defaulted', 'Treatment stopped')
282
- AND final_outcome = 'On antiretrovirals'
283
- AND initial_outcome_date IS NOT NULL;
276
+ AND final_outcome = 'On antiretrovirals';
284
277
  SQL
285
278
  end
286
279
  end
@@ -81,7 +81,7 @@ module MalawiHivProgramReports
81
81
  def build_cohort_tables
82
82
  return unless rebuild_outcome || @occupation.present?
83
83
 
84
- cohort_builder = MalawiHivProgramReports::Moh::CumulativeCohort.new(
84
+ MalawiHivProgramReports::Moh::CumulativeCohort.new(
85
85
  start_date: start_date,
86
86
  end_date: end_date,
87
87
  locations: [@location.to_s],
@@ -126,19 +126,20 @@ module MalawiHivProgramReports
126
126
  SELECT o.person_id, MAX(o.obs_datetime) AS obs_datetime, tesd.earliest_start_date, tesd.gender, tesd.birthdate
127
127
  FROM obs #{current_partition} o
128
128
  INNER JOIN cdr_temp_cohort_members tesd ON tesd.patient_id = o.person_id
129
+ #{site_manager(operator: 'AND', column: 'tesd.site_id', location: @location)}
129
130
  WHERE o.concept_id = #{::ConceptName.find_by_name('TB status').concept_id}
130
131
  AND o.value_coded IN (SELECT concept_id FROM concept_name WHERE name IN ('TB Suspected', 'TB NOT suspected') AND voided = 0)
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)}
132
+ AND o.voided = 0 AND o.obs_datetime BETWEEN '#{start_date}' AND '#{end_date}'
132
133
  GROUP BY o.person_id
133
134
  ) current_obs ON current_obs.person_id = o.person_id AND current_obs.obs_datetime = o.obs_datetime
134
135
  INNER JOIN concept_name cn ON cn.concept_id = o.value_coded AND cn.voided = 0
135
136
  LEFT JOIN obs #{current_partition} screen_method ON screen_method.concept_id = #{::ConceptName.find_by_name('TB screening method used').concept_id}
136
- AND screen_method.voided = 0 #{site_manager(operator: 'AND', column: 'screen_method.site_id', location: @location)}
137
- AND screen_method.person_id = o.person_id#{' '}
137
+ AND screen_method.voided = 0
138
+ AND screen_method.person_id = o.person_id
138
139
  AND DATE(screen_method.obs_datetime) = DATE(current_obs.obs_datetime)
139
140
  LEFT JOIN concept_name vcn ON vcn.concept_id = screen_method.value_coded AND vcn.voided = 0 AND vcn.name IN ('CXR', 'MWRD')
140
141
  WHERE o.concept_id = #{::ConceptName.find_by_name('TB status').concept_id}
141
- AND o.voided = 0 #{site_manager(operator: 'AND', column: 'o.site_id', location: @location)}
142
+ AND o.voided = 0
142
143
  AND o.value_coded IN (SELECT concept_id FROM concept_name WHERE name IN ('TB Suspected', 'TB NOT suspected') AND voided = 0)
143
144
  AND o.obs_datetime BETWEEN '#{start_date}' AND '#{end_date}'
144
145
  GROUP BY o.person_id
@@ -195,26 +196,27 @@ module MalawiHivProgramReports
195
196
  prev.tb_confirmed_date prev_reading
196
197
  #{@location ? ", #{@location}" : ''} AS site_id
197
198
  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)}
199
+ INNER JOIN cdr_temp_cohort_members tesd ON tesd.patient_id = o.person_id
200
+ #{site_manager(operator: 'AND', column: 'tesd.site_id', location: @location)}
201
+ INNER JOIN person #{current_partition} p ON p.person_id = o.person_id AND p.voided = 0
200
202
  LEFT JOIN obs #{current_partition} tcd ON tcd.concept_id = #{::ConceptName.find_by_name('TB treatment start date').concept_id}
201
- AND tcd.voided = 0 AND tcd.person_id = o.person_id #{site_manager(operator: 'AND', column: 'tcd.site_id', location: @location)}
203
+ AND tcd.voided = 0 AND tcd.person_id = o.person_id
202
204
  LEFT JOIN (
203
205
  SELECT
204
206
  o.person_id,
205
207
  COALESCE(MAX(tcd.value_datetime),MAX(o.obs_datetime)) AS tb_confirmed_date
206
208
  FROM obs #{current_partition} o
207
209
  LEFT JOIN obs #{current_partition} tcd ON tcd.concept_id = #{::ConceptName.find_by_name('TB treatment start date').concept_id}
208
- AND tcd.voided = 0 AND tcd.person_id = o.person_id #{site_manager(operator: 'AND', column: 'tcd.site_id', location: @location)}
210
+ AND tcd.voided = 0 AND tcd.person_id = o.person_id
209
211
  WHERE o.concept_id = #{::ConceptName.find_by_name('TB status').concept_id}
210
212
  AND o.value_coded = #{::ConceptName.find_by_name('Confirmed TB on treatment').concept_id}
211
- AND o.voided = 0 #{site_manager(operator: 'AND', column: 'o.site_id', location: @location)}
213
+ AND o.voided = 0
212
214
  AND o.obs_datetime <= '#{start_date}'
213
215
  GROUP BY o.person_id
214
216
  ) prev ON prev.person_id = o.person_id
215
217
  WHERE o.concept_id = #{::ConceptName.find_by_name('TB status').concept_id}
216
218
  AND o.value_coded = #{::ConceptName.find_by_name('Confirmed TB on treatment').concept_id}
217
- AND o.voided = 0 #{site_manager(operator: 'AND', column: 'o.site_id', location: @location)}
219
+ AND o.voided = 0
218
220
  AND o.obs_datetime BETWEEN '#{start_date}' AND '#{end_date}'
219
221
  GROUP BY o.person_id
220
222
  SQL
@@ -236,8 +238,10 @@ module MalawiHivProgramReports
236
238
  <<~SQL
237
239
  SELECT tpo.patient_id, LEFT(tesd.gender, 1) AS gender, disaggregated_age_group(tesd.birthdate, DATE('#{end_date.to_date}')) age_group
238
240
  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)}
240
- WHERE tpo.cum_outcome = 'On antiretrovirals' #{site_manager(operator: 'AND', column: 'tpo.site_id', location: @location)}
241
+ INNER JOIN cdr_temp_cohort_members tesd ON tesd.patient_id = tpo.patient_id
242
+ #{site_manager(operator: 'AND', column: 'tesd.site_id', location: @location)}
243
+ #{site_manager(operator: 'AND', column: 'tpo.site_id', location: @location)}
244
+ WHERE tpo.cum_outcome = 'On antiretrovirals'
241
245
  SQL
242
246
  end
243
247
 
@@ -94,30 +94,27 @@ module MalawiHivProgramReports
94
94
  external_concept = ::ConceptName.find_by_name('External consultation').concept_id
95
95
  hiv_clinic_registration_id = ::EncounterType.find_by_name('HIV CLINIC REGISTRATION').encounter_type_id
96
96
 
97
- ActiveRecord::Base.connection.select_all("
97
+ res = ActiveRecord::Base.connection.select_all <<~SQL
98
98
  SELECT p.person_id patient_id
99
99
  FROM person #{current_partition} p
100
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
- #{site_manager(operator: 'AND', column: 'pp.site_id', location: @location)}
102
101
  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
- #{site_manager(operator: 'AND', column: 'ps.site_id', location: @location)}
104
102
  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
- #{site_manager(operator: 'AND', column: 'hiv_registration.site_id', location: @location)}
106
103
  LEFT JOIN (
107
104
  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
- #{site_manager(operator: 'AND', column: 'obs.site_id', location: @location)}
109
105
  ) AS new_patient ON p.person_id = new_patient.person_id
110
106
  LEFT JOIN (
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)}
107
+ 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: '+')}
112
108
  ) AS refill ON p.person_id = refill.person_id
113
109
  LEFT JOIN (
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)}
110
+ 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: '+')}
115
111
  ) AS external ON p.person_id = external.person_id
116
112
  WHERE (refill.value_coded IS NOT NULL OR external.value_coded IS NOT NULL)
117
113
  AND NOT (hiv_registration.encounter_id IS NOT NULL OR new_patient.value_coded IS NOT NULL)
118
- #{site_manager(operator: 'AND', column: 'p.site_id', location: @location)}
119
114
  GROUP BY p.person_id
120
- ORDER BY hiv_registration.encounter_datetime DESC, refill.obs_datetime DESC, external.obs_datetime DESC;").each do |record|
115
+ ORDER BY hiv_registration.encounter_datetime DESC, refill.obs_datetime DESC, external.obs_datetime DESC
116
+ SQL
117
+ res.each do |record|
121
118
  to_remove << record['patient_id'].to_i
122
119
  end
123
120
  to_remove.join(',')
@@ -46,13 +46,12 @@ module MalawiHivProgramReports
46
46
  ActiveRecord::Base.connection.adapter_name.downcase
47
47
  <<~SQL
48
48
  SELECT a.person_id, a.value, a.site_id
49
- FROM person_attribute a
50
- LEFT OUTER JOIN person_attribute b
51
- ON a.person_attribute_id = b.person_attribute_id #{site_manager(operator: 'AND', column: 'b.site_id', location: @location)}
49
+ FROM person_attribute #{current_partition} a
50
+ LEFT OUTER JOIN person_attribute #{current_partition} b
51
+ ON a.person_attribute_id = b.person_attribute_id
52
52
  AND a.date_created < b.date_created
53
- AND b.voided = 0 #{site_manager(operator: 'AND', column: 'a.site_id', location: @location)}
53
+ AND b.voided = 0
54
54
  WHERE b.person_attribute_id IS NULL AND a.person_attribute_type_id = 13 AND a.voided = 0
55
- #{site_manager(operator: 'AND', column: 'a.site_id', location: @location)}
56
55
  SQL
57
56
  end
58
57
 
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module MalawiHivProgramReports
4
- VERSION = '1.0.23'
4
+ VERSION = '1.0.24'
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: malawi_hiv_program_reports
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.23
4
+ version: 1.0.24
5
5
  platform: ruby
6
6
  authors:
7
7
  - Roy Chanunkha