malawi_hiv_program_reports 1.0.13 → 1.0.14
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/app/services/malawi_hiv_program_reports/arv_refill_periods.rb +12 -2
- data/app/services/malawi_hiv_program_reports/moh/cohort_builder.rb +2 -2
- data/app/services/malawi_hiv_program_reports/pepfar/tx_ml.rb +3 -1
- data/app/services/malawi_hiv_program_reports/pepfar/tx_tb.rb +21 -13
- data/app/services/malawi_hiv_program_reports/pepfar/utils.rb +12 -5
- data/app/services/malawi_hiv_program_reports/pepfar/viral_load_coverage2.rb +17 -3
- data/lib/malawi_hiv_program_reports/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8c1d535116ce1283de2cbbfd8e32f29f7c67ca4a3ffab22cf91de0fe3396199b
|
4
|
+
data.tar.gz: f094051dd235262b875743e63b059ed4449d5d966b13dbfd4bb7c4fac16d0a29
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7e7c15a56f50c1923fadbda4fcd7b1711989d50d5d32ca6c7f9cb9862694248194d2a9d7589538d10d894d65654af74e0d260f3ea0e7fc3754ee5238c4002092
|
7
|
+
data.tar.gz: 5864f57e5cb41ccb861ef52f474c6ea4d08b105089fd84dd6322adaaaf7603a4492109d4c19f6c8c9f149ca1bdac5405280ab37e7430b5976ad5a5c27135a6ed
|
@@ -94,15 +94,19 @@ module MalawiHivProgramReports
|
|
94
94
|
TIMESTAMPDIFF(day, DATE(o.start_date), DATE(o.auto_expire_date)) prescribed_days
|
95
95
|
FROM orders o
|
96
96
|
INNER JOIN drug_order od ON od.order_id = o.order_id
|
97
|
+
#{site_manager(operator: 'AND', column: 'od.site_id', location: @location)}
|
97
98
|
INNER JOIN drug d ON d.drug_id = od.drug_inventory_id
|
98
99
|
INNER JOIN concept_set s ON s.concept_id = d.concept_id
|
99
100
|
INNER JOIN person p ON p.person_id = o.patient_id
|
101
|
+
#{site_manager(operator: 'AND', column: 'p.site_id', location: @location)}
|
100
102
|
INNER JOIN encounter e ON e.patient_id = p.person_id
|
103
|
+
#{site_manager(operator: 'AND', column: 'e.site_id', location: @location)}
|
101
104
|
WHERE s.concept_set = #{arv_concept_set} AND o.voided = 0
|
102
105
|
#{site_manager(operator: 'AND', column: 'o.site_id', location: @location)}
|
103
106
|
AND DATE(o.start_date) = (
|
104
107
|
SELECT DATE(MAX(t.start_date)) FROM orders t
|
105
108
|
INNER JOIN drug_order t2 ON t2.order_id = t.order_id
|
109
|
+
#{site_manager(operator: 'AND', column: 't2.site_id', location: @location)}
|
106
110
|
INNER JOIN drug t3 ON t3.drug_id = t2.drug_inventory_id
|
107
111
|
INNER JOIN concept_set t4 ON t4.concept_id = t3.concept_id
|
108
112
|
WHERE t.patient_id = #{patient_id}
|
@@ -179,7 +183,9 @@ module MalawiHivProgramReports
|
|
179
183
|
def get_weight(patient_id)
|
180
184
|
concept_id = ::ConceptName.find_by_name('Weight (Kg)').concept_id
|
181
185
|
weight_details = ::Observation.where("person_id = ? AND concept_id = ?
|
182
|
-
AND obs_datetime <= ?
|
186
|
+
AND obs_datetime <= ?
|
187
|
+
#{site_manager(operator: 'AND', column: 'site_id', location: @location)}
|
188
|
+
AND ( CAST(value_numeric as DECIMAL(4,1)) > 0 OR
|
183
189
|
CAST(value_text as DECIMAL(4,1)) > 0)", patient_id,
|
184
190
|
concept_id, @end_date).order('obs_datetime DESC, date_created DESC')
|
185
191
|
|
@@ -216,19 +222,23 @@ module MalawiHivProgramReports
|
|
216
222
|
TIMESTAMPDIFF(day, DATE(o.start_date), DATE(o.auto_expire_date)) prescribed_days
|
217
223
|
FROM orders o
|
218
224
|
INNER JOIN drug_order od ON od.order_id = o.order_id
|
225
|
+
#{site_manager(operator: 'AND', column: 'od.site_id', location: @location)}
|
219
226
|
INNER JOIN drug d ON d.drug_id = od.drug_inventory_id
|
220
227
|
INNER JOIN concept_set s ON s.concept_id = d.concept_id
|
221
228
|
INNER JOIN person p ON p.person_id = o.patient_id
|
229
|
+
#{site_manager(operator: 'AND', column: 'p.site_id', location: @location)}
|
222
230
|
INNER JOIN encounter e ON e.patient_id = p.person_id
|
231
|
+
#{site_manager(operator: 'AND', column: 'e.site_id', location: @location)}
|
223
232
|
AND e.program_id = #{program_id}
|
224
|
-
#{site_manager(operator: 'AND', column: 'o.site_id', location: @location)}
|
225
233
|
LEFT JOIN patient_identifier i ON i.patient_id = o.patient_id
|
226
234
|
AND i.identifier_type = #{identifier_type}
|
227
235
|
AND LENGTH(identifier) > 0 AND i.voided = 0
|
228
236
|
WHERE s.concept_set = #{arv_concept_set} AND o.voided = 0
|
237
|
+
#{site_manager(operator: 'AND', column: 'o.site_id', location: @location)}
|
229
238
|
AND DATE(o.start_date) = (
|
230
239
|
SELECT DATE(MAX(t.start_date)) FROM orders t
|
231
240
|
INNER JOIN drug_order t2 ON t2.order_id = t.order_id
|
241
|
+
#{site_manager(operator: 'AND', column: 't2.site_id', location: @location)}
|
232
242
|
INNER JOIN drug t3 ON t3.drug_id = t2.drug_inventory_id
|
233
243
|
INNER JOIN concept_set t4 ON t4.concept_id = t3.concept_id
|
234
244
|
WHERE t.patient_id = #{patient_id}
|
@@ -1004,7 +1004,7 @@ module MalawiHivProgramReports
|
|
1004
1004
|
|
1005
1005
|
ActiveRecord::Base.connection.execute <<~SQL
|
1006
1006
|
INSERT INTO temp_patient_tb_status
|
1007
|
-
SELECT e.person_id,
|
1007
|
+
SELECT e.person_id, e.site_id, obs.value_coded
|
1008
1008
|
FROM temp_latest_tb_status e
|
1009
1009
|
INNER JOIN obs ON obs.person_id = e.person_id AND obs.voided = 0
|
1010
1010
|
AND obs.concept_id = 7459 #{site_manager(operator: 'AND', column: 'e.site_id', location: @location)}
|
@@ -1921,7 +1921,7 @@ module MalawiHivProgramReports
|
|
1921
1921
|
FROM obs o
|
1922
1922
|
WHERE o.concept_id IN (6131,1755,7972,7563)
|
1923
1923
|
AND o.value_coded IN (1065,1755)
|
1924
|
-
AND o.voided = 0 #{site_manager(operator: 'AND', column: 'o.
|
1924
|
+
AND o.voided = 0 #{site_manager(operator: 'AND', column: 'o.site_id', location: @location)}
|
1925
1925
|
AND o.obs_datetime >= '#{start_date}' AND o.obs_datetime < '#{end_date}' + INTERVAL 1 DAY;
|
1926
1926
|
SQL
|
1927
1927
|
ActiveRecord::Base.connection.execute 'CREATE INDEX fre_person ON temp_pregnant_obs(person_id);'
|
@@ -123,6 +123,7 @@ module MalawiHivProgramReports
|
|
123
123
|
INNER JOIN patient_state s ON p.patient_program_id = s.patient_program_id AND s.voided = 0 AND s.state = 7
|
124
124
|
#{site_manager(operator: 'AND', column: 's.site_id', location: @location)}
|
125
125
|
LEFT JOIN (#{current_occupation_query}) a ON a.person_id = p.patient_id
|
126
|
+
#{site_manager(operator: 'AND', column: 'a.site_id', location: @location)}
|
126
127
|
WHERE p.program_id = 1 #{%w[Military Civilian].include?(@occupation) ? 'AND' : ''} #{occupation_filter(occupation: @occupation, field_name: 'value', table_name: 'a', include_clause: false)}
|
127
128
|
AND DATE(s.start_date) < '#{start_date.to_date}'
|
128
129
|
#{site_manager(operator: 'AND', column: 'p.site_id', location: @location)}
|
@@ -147,6 +148,7 @@ module MalawiHivProgramReports
|
|
147
148
|
INNER JOIN patient_state s ON p.patient_program_id = s.patient_program_id AND s.voided = 0 AND s.state = 7
|
148
149
|
#{site_manager(operator: 'AND', column: 's.site_id', location: @location)}
|
149
150
|
LEFT JOIN (#{current_occupation_query}) a ON a.person_id = p.patient_id
|
151
|
+
#{site_manager(operator: 'AND', column: 'a.site_id', location: @location)}
|
150
152
|
WHERE p.program_id = 1 #{%w[Military Civilian].include?(@occupation) ? 'AND' : ''} #{occupation_filter(occupation: @occupation, field_name: 'value', table_name: 'a', include_clause: false)}
|
151
153
|
AND DATE(s.start_date) BETWEEN DATE('#{start_date}') AND DATE('#{end_date}')
|
152
154
|
AND pe.person_id NOT IN (#{drug_refills_and_external_consultation_list})
|
@@ -158,7 +160,7 @@ module MalawiHivProgramReports
|
|
158
160
|
|
159
161
|
def defaulter_period(patient_id, earliest_start_date)
|
160
162
|
defaulter_date = ActiveRecord::Base.connection.select_one <<~SQL
|
161
|
-
SELECT #{function_manager(function: 'current_pepfar_defaulter_date', location: @location, args: "#{patient_id}::int, '#{end_date}'::date, #{@location}::int")}
|
163
|
+
SELECT #{function_manager(function: 'current_pepfar_defaulter_date', location: @location, args: "#{patient_id}::int, '#{end_date}'::date, #{@location}::int")} def_date;
|
162
164
|
SQL
|
163
165
|
|
164
166
|
defaulter_date = begin
|
@@ -8,6 +8,7 @@ module MalawiHivProgramReports
|
|
8
8
|
attr_accessor :start_date, :end_date, :report, :rebuild_outcome
|
9
9
|
|
10
10
|
include Utils
|
11
|
+
include MalawiHivProgramReports::Adapters::Moh::Custom
|
11
12
|
|
12
13
|
def initialize(start_date:, end_date:, **kwargs)
|
13
14
|
@start_date = start_date.to_date.strftime('%Y-%m-%d 00:00:00')
|
@@ -69,7 +70,8 @@ module MalawiHivProgramReports
|
|
69
70
|
def build_cohort_tables
|
70
71
|
return unless rebuild_outcome || @occupation.present?
|
71
72
|
|
72
|
-
cohort_builder = MalawiHivProgramReports::Moh::CohortBuilder.new(outcomes_definition: 'pepfar',
|
73
|
+
cohort_builder = MalawiHivProgramReports::Moh::CohortBuilder.new(outcomes_definition: 'pepfar',
|
74
|
+
location: @location)
|
73
75
|
cohort_builder.init_temporary_tables(start_date, end_date, @occupation)
|
74
76
|
end
|
75
77
|
|
@@ -94,14 +96,17 @@ module MalawiHivProgramReports
|
|
94
96
|
INNER JOIN temp_earliest_start_date tesd ON tesd.patient_id = o.person_id
|
95
97
|
WHERE o.concept_id = #{::ConceptName.find_by_name('TB status').concept_id}
|
96
98
|
AND o.value_coded IN (SELECT concept_id FROM concept_name WHERE name IN ('TB Suspected', 'TB NOT suspected') AND voided = 0)
|
97
|
-
AND o.voided = 0 AND o.obs_datetime BETWEEN '#{start_date}' AND '#{end_date}'
|
99
|
+
AND o.voided = 0 AND o.obs_datetime BETWEEN '#{start_date}' AND '#{end_date}' #{site_manager(operator: 'AND', column: 'o.site_id', location: @location)}
|
98
100
|
GROUP BY o.person_id
|
99
101
|
) current_obs ON current_obs.person_id = o.person_id AND current_obs.obs_datetime = o.obs_datetime
|
100
102
|
INNER JOIN concept_name cn ON cn.concept_id = o.value_coded AND cn.voided = 0
|
101
|
-
LEFT JOIN obs screen_method ON screen_method.concept_id = #{::ConceptName.find_by_name('TB screening method used').concept_id}
|
103
|
+
LEFT JOIN obs screen_method ON screen_method.concept_id = #{::ConceptName.find_by_name('TB screening method used').concept_id}
|
104
|
+
AND screen_method.voided = 0 #{site_manager(operator: 'AND', column: 'screen_method.site_id', location: @location)}
|
105
|
+
AND screen_method.person_id = o.person_id#{' '}
|
106
|
+
AND DATE(screen_method.obs_datetime) = DATE(current_obs.obs_datetime)
|
102
107
|
LEFT JOIN concept_name vcn ON vcn.concept_id = screen_method.value_coded AND vcn.voided = 0 AND vcn.name IN ('CXR', 'MWRD')
|
103
108
|
WHERE o.concept_id = #{::ConceptName.find_by_name('TB status').concept_id}
|
104
|
-
AND o.voided = 0
|
109
|
+
AND o.voided = 0 #{site_manager(operator: 'AND', column: 'o.site_id', location: @location)}
|
105
110
|
AND o.value_coded IN (SELECT concept_id FROM concept_name WHERE name IN ('TB Suspected', 'TB NOT suspected') AND voided = 0)
|
106
111
|
AND o.obs_datetime BETWEEN '#{start_date}' AND '#{end_date}'
|
107
112
|
GROUP BY o.person_id
|
@@ -113,7 +118,8 @@ module MalawiHivProgramReports
|
|
113
118
|
end
|
114
119
|
|
115
120
|
def create_temp_earliest_start_date
|
116
|
-
cohort_builder = MalawiHivProgramReports::Moh::CohortBuilder.new(outcomes_definition: 'pepfar',
|
121
|
+
cohort_builder = MalawiHivProgramReports::Moh::CohortBuilder.new(outcomes_definition: 'pepfar',
|
122
|
+
location: @location)
|
117
123
|
cohort_builder.init_temporary_tables(start_date, end_date, @occupation)
|
118
124
|
end
|
119
125
|
|
@@ -136,24 +142,26 @@ module MalawiHivProgramReports
|
|
136
142
|
tesd.earliest_start_date as enrollment_date,
|
137
143
|
prev.tb_confirmed_date prev_reading
|
138
144
|
FROM obs o
|
139
|
-
INNER JOIN temp_earliest_start_date tesd ON tesd.patient_id = o.person_id
|
140
|
-
INNER JOIN person p ON p.person_id = o.person_id AND p.voided = 0
|
141
|
-
LEFT JOIN obs tcd ON tcd.concept_id = #{::ConceptName.find_by_name('TB treatment start date').concept_id}
|
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}
|
148
|
+
AND tcd.voided = 0 AND tcd.person_id = o.person_id #{site_manager(operator: 'AND', column: 'tcd.site_id', location: @location)}
|
142
149
|
LEFT JOIN (
|
143
150
|
SELECT
|
144
151
|
o.person_id,
|
145
152
|
COALESCE(MAX(tcd.value_datetime),MAX(o.obs_datetime)) AS tb_confirmed_date
|
146
153
|
FROM obs o
|
147
|
-
LEFT JOIN obs tcd ON tcd.concept_id = #{::ConceptName.find_by_name('TB treatment start date').concept_id}
|
154
|
+
LEFT JOIN obs tcd ON tcd.concept_id = #{::ConceptName.find_by_name('TB treatment start date').concept_id}
|
155
|
+
AND tcd.voided = 0 AND tcd.person_id = o.person_id #{site_manager(operator: 'AND', column: 'tcd.site_id', location: @location)}
|
148
156
|
WHERE o.concept_id = #{::ConceptName.find_by_name('TB status').concept_id}
|
149
157
|
AND o.value_coded = #{::ConceptName.find_by_name('Confirmed TB on treatment').concept_id}
|
150
|
-
AND o.voided = 0
|
158
|
+
AND o.voided = 0 #{site_manager(operator: 'AND', column: 'o.site_id', location: @location)}
|
151
159
|
AND o.obs_datetime <= '#{start_date}'
|
152
160
|
GROUP BY o.person_id
|
153
161
|
) prev ON prev.person_id = o.person_id
|
154
162
|
WHERE o.concept_id = #{::ConceptName.find_by_name('TB status').concept_id}
|
155
163
|
AND o.value_coded = #{::ConceptName.find_by_name('Confirmed TB on treatment').concept_id}
|
156
|
-
AND o.voided = 0
|
164
|
+
AND o.voided = 0 #{site_manager(operator: 'AND', column: 'o.site_id', location: @location)}
|
157
165
|
AND o.obs_datetime BETWEEN '#{start_date}' AND '#{end_date}'
|
158
166
|
GROUP BY o.person_id
|
159
167
|
SQL
|
@@ -175,8 +183,8 @@ module MalawiHivProgramReports
|
|
175
183
|
<<~SQL
|
176
184
|
SELECT tpo.patient_id, LEFT(tesd.gender, 1) AS gender, disaggregated_age_group(tesd.birthdate, DATE('#{end_date.to_date}')) age_group
|
177
185
|
FROM temp_patient_outcomes tpo
|
178
|
-
INNER JOIN temp_earliest_start_date tesd ON tesd.patient_id = tpo.patient_id
|
179
|
-
WHERE tpo.cum_outcome = 'On antiretrovirals'
|
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)}
|
187
|
+
WHERE tpo.cum_outcome = 'On antiretrovirals' #{site_manager(operator: 'AND', column: 'tpo.site_id', location: @location)}
|
180
188
|
SQL
|
181
189
|
end
|
182
190
|
|
@@ -98,14 +98,21 @@ module MalawiHivProgramReports
|
|
98
98
|
SELECT p.person_id patient_id
|
99
99
|
FROM person p
|
100
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
|
101
|
-
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
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
|
103
|
+
#{site_manager(operator: 'AND', column: 'ps.site_id', location: @location)}
|
103
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
105
|
#{site_manager(operator: 'AND', column: 'hiv_registration.site_id', location: @location)}
|
105
|
-
LEFT JOIN (
|
106
|
-
|
107
|
-
|
108
|
-
|
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: '+')}
|
108
|
+
#{site_manager(operator: 'AND', column: 'obs.site_id', location: @location)}
|
109
|
+
) AS new_patient ON p.person_id = new_patient.person_id
|
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)}
|
112
|
+
) AS refill ON p.person_id = refill.person_id
|
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)}
|
115
|
+
) AS external ON p.person_id = external.person_id
|
109
116
|
WHERE (refill.value_coded IS NOT NULL OR external.value_coded IS NOT NULL)
|
110
117
|
AND NOT (hiv_registration.encounter_id IS NOT NULL OR new_patient.value_coded IS NOT NULL)
|
111
118
|
#{site_manager(operator: 'AND', column: 'p.site_id', location: @location)}
|
@@ -126,18 +126,21 @@ module MalawiHivProgramReports
|
|
126
126
|
SELECT o.person_id, o.value_coded
|
127
127
|
FROM obs o
|
128
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})
|
129
|
+
#{site_manager(operator: 'AND', column: 'e.site_id', location: @location)}
|
129
130
|
INNER JOIN person p ON o.person_id = e.patient_id AND LEFT(p.gender, 1) = 'F'
|
130
|
-
#{site_manager(operator: 'AND', column: '
|
131
|
+
#{site_manager(operator: 'AND', column: 'p.site_id', location: @location)}
|
131
132
|
INNER JOIN (
|
132
133
|
SELECT person_id, MAX(obs_datetime) AS obs_datetime
|
133
134
|
FROM obs
|
134
135
|
INNER JOIN encounter ON encounter.encounter_id = obs.encounter_id AND encounter.encounter_type IN (#{encounter_types.to_sql}) AND encounter.voided = 0
|
136
|
+
#{site_manager(operator: 'AND', column: 'encounter.site_id', location: @location)}
|
135
137
|
WHERE obs.concept_id IN (#{pregnant_concepts.to_sql})
|
136
138
|
AND obs.obs_datetime BETWEEN DATE(#{ActiveRecord::Base.connection.quote(start_date)}) AND DATE(#{ActiveRecord::Base.connection.quote(end_date)}) + INTERVAL 1 DAY
|
137
139
|
AND obs.voided = 0
|
138
140
|
#{site_manager(operator: 'AND', column: 'obs.site_id', location: @location)}
|
139
141
|
GROUP BY person_id
|
140
142
|
) AS max_obs ON max_obs.person_id = o.person_id AND max_obs.obs_datetime = o.obs_datetime
|
143
|
+
#{site_manager(operator: 'AND', column: 'o.site_id', location: @location)}
|
141
144
|
WHERE o.concept_id IN (#{pregnant_concepts.to_sql})
|
142
145
|
AND o.voided = 0
|
143
146
|
AND o.value_coded IN (#{yes_concepts.join(',')})
|
@@ -152,18 +155,21 @@ module MalawiHivProgramReports
|
|
152
155
|
SELECT o.person_id, o.value_coded
|
153
156
|
FROM obs o
|
154
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})
|
158
|
+
#{site_manager(operator: 'AND', column: 'e.site_id', location: @location)}
|
155
159
|
INNER JOIN person p ON o.person_id = e.patient_id AND LEFT(p.gender, 1) = 'F'
|
156
|
-
#{site_manager(operator: 'AND', column: '
|
160
|
+
#{site_manager(operator: 'AND', column: 'p.site_id', location: @location)}
|
157
161
|
INNER JOIN (
|
158
162
|
SELECT person_id, MAX(obs_datetime) AS obs_datetime
|
159
163
|
FROM obs
|
160
164
|
INNER JOIN encounter ON encounter.encounter_id = obs.encounter_id AND encounter.encounter_type IN (#{encounter_types.to_sql}) AND encounter.voided = 0
|
165
|
+
#{site_manager(operator: 'AND', column: 'encounter.site_id', location: @location)}
|
161
166
|
WHERE obs.concept_id IN (#{breast_feeding_concepts.to_sql})
|
162
167
|
AND obs.obs_datetime BETWEEN DATE(#{ActiveRecord::Base.connection.quote(start_date)}) AND DATE(#{ActiveRecord::Base.connection.quote(end_date)}) + INTERVAL 1 DAY
|
163
168
|
AND obs.voided = 0
|
164
169
|
#{site_manager(operator: 'AND', column: 'obs.site_id', location: @location)}
|
165
170
|
GROUP BY person_id
|
166
171
|
) AS max_obs ON max_obs.person_id = o.person_id AND max_obs.obs_datetime = o.obs_datetime
|
172
|
+
#{site_manager(operator: 'AND', column: 'o.site_id', location: @location)}
|
167
173
|
WHERE o.concept_id IN (#{breast_feeding_concepts.to_sql})
|
168
174
|
AND o.voided = 0
|
169
175
|
AND o.value_coded IN (#{yes_concepts.join(',')})
|
@@ -285,16 +291,21 @@ module MalawiHivProgramReports
|
|
285
291
|
INNER JOIN person p ON p.person_id = ab.patient_id AND p.voided = 0
|
286
292
|
#{site_manager(operator: 'AND', column: 'p.site_id', location: @location)}
|
287
293
|
INNER JOIN drug_order dor ON dor.order_id = ab.order_id AND dor.quantity > 0
|
294
|
+
#{site_manager(operator: 'AND', column: 'dor.site_id', location: @location)}
|
288
295
|
INNER JOIN arv_drug ad ON dor.drug_inventory_id = ad.drug_id
|
289
296
|
INNER JOIN patient_program pp ON pp.patient_id = ab.patient_id AND pp.voided = 0 AND pp.program_id = 1
|
297
|
+
#{site_manager(operator: 'AND', column: 'pp.site_id', location: @location)}
|
290
298
|
INNER JOIN (
|
291
|
-
SELECT a.patient_program_id, a.state, a.start_date, a.end_date
|
299
|
+
SELECT a.site_id, a.patient_program_id, a.state, a.start_date, a.end_date
|
292
300
|
FROM patient_state a
|
293
301
|
LEFT OUTER JOIN patient_state b ON a.patient_program_id = b.patient_program_id
|
302
|
+
#{site_manager(operator: 'AND', column: 'b.site_id', location: @location)}
|
294
303
|
AND a.start_date < b.start_date
|
304
|
+
#{site_manager(operator: 'AND', column: 'a.site_id', location: @location)}
|
295
305
|
AND b.voided = 0
|
296
306
|
WHERE b.patient_program_id IS NULL AND a.end_date IS NULL AND a.voided = 0
|
297
307
|
) current_state ON current_state.patient_program_id = pp.patient_program_id
|
308
|
+
#{site_manager(operator: 'AND', column: 'current_state.site_id', location: @location)}
|
298
309
|
LEFT OUTER JOIN orders b ON ab.patient_id = b.patient_id
|
299
310
|
AND ab.order_id = b.order_id
|
300
311
|
AND ab.auto_expire_date < b.auto_expire_date
|
@@ -368,6 +379,7 @@ module MalawiHivProgramReports
|
|
368
379
|
ON reason_for_test.order_id = orders.order_id
|
369
380
|
AND reason_for_test.concept_id IN (SELECT concept_id FROM concept_name WHERE name LIKE 'Reason for test' AND voided = 0)
|
370
381
|
AND reason_for_test.voided = 0
|
382
|
+
#{site_manager(operator: 'AND', column: 'reason_for_test.site_id', location: @location)}
|
371
383
|
LEFT JOIN concept_name AS reason_for_test_value
|
372
384
|
ON reason_for_test_value.concept_id = reason_for_test.value_coded
|
373
385
|
AND reason_for_test_value.voided = 0
|
@@ -376,6 +388,7 @@ module MalawiHivProgramReports
|
|
376
388
|
AND result.concept_id IN (SELECT concept_id FROM concept_name WHERE name LIKE 'HIV Viral load' AND voided = 0)
|
377
389
|
AND result.voided = 0
|
378
390
|
AND (result.value_text IS NOT NULL OR result.value_numeric IS NOT NULL)
|
391
|
+
#{site_manager(operator: 'AND', column: 'result.site_id', location: @location)}
|
379
392
|
INNER JOIN (
|
380
393
|
/* Get the latest order dates for each patient */
|
381
394
|
SELECT orders.patient_id, MAX(orders.start_date) AS start_date
|
@@ -404,6 +417,7 @@ module MalawiHivProgramReports
|
|
404
417
|
AND orders.start_date >= DATE(#{ActiveRecord::Base.connection.quote(start_date)}) - INTERVAL 12 MONTH
|
405
418
|
AND orders.voided = 0
|
406
419
|
AND orders.patient_id IN (#{clients.push(0).join(',')})
|
420
|
+
#{site_manager(operator: 'AND', column: 'orders.site_id', location: @location)}
|
407
421
|
GROUP BY orders.patient_id
|
408
422
|
SQL
|
409
423
|
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.
|
4
|
+
version: 1.0.14
|
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-03-
|
11
|
+
date: 2024-03-27 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|