Class: MalawiHivProgramReports::Cohort::Regimens

Inherits:
Object
  • Object
show all
Includes:
Adapters::Moh::Custom, Utils::ModelUtils
Defined in:
app/services/malawi_hiv_program_reports/cohort/regimens.rb

Instance Method Summary collapse

Methods included from Adapters::Moh::Custom

#cast_manager, #current_partition, #exe_create_drill_down_table, #exe_temp_cohort_members_table, #exe_temp_order_details_table, #exe_temp_other_patient_types, #exe_temp_register_start_date_table, #exe_tmp_patient_table, #function_manager, #group_by_columns, #in_manager, #interval_manager, #min_filt, #site_manager, #timestampdiff_manager

Methods included from Utils::ModelUtils

#concept, #concept_id_to_name, #concept_name, #concept_name_to_id, #drug, #encounter_type, #global_property, #order_type, #patient_identifier_type, #program, #report_type, #user_property

Instance Method Details

#arv_drugs_concept_setObject



47
48
49
50
# File 'app/services/malawi_hiv_program_reports/cohort/regimens.rb', line 47

def arv_drugs_concept_set
  @arv_drugs_concept_set ||= ::ConceptSet.where(set: concept_name('Antiretroviral drugs'))
                                         .select(:concept_id)
end

#patient_regimens(date, location) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'app/services/malawi_hiv_program_reports/cohort/regimens.rb', line 9

def patient_regimens(date, location)
  @location = location
  @adapter = ActiveRecord::Base.connection.adapter_name.downcase
  ActiveRecord::Base.connection.quote(date)

  ActiveRecord::Base.connection.select_all <<~SQL
    SELECT prescriptions.patient_id,
           regimens.name AS regimen_category,
           prescriptions.drugs,
           prescriptions.prescription_date
    FROM (
      SELECT orders.patient_id,
            #{@adapter == 'mysql2' ? 'GROUP_CONCAT(DISTINCT drug_order.drug_inventory_id ORDER BY drug_order.drug_inventory_id ASC) AS drugs,' : "(SELECT STRING_AGG(drug_inventory_id::VARCHAR, ',') FROM (SELECT DISTINCT drug_order.drug_inventory_id FROM drug_order ORDER BY drug_order.drug_inventory_id ASC) AS subquery) AS drugs,"}
            DATE(tmdo.start_date) prescription_date
      FROM temp_patient_outcomes AS outcomes
      INNER JOIN tmp_max_drug_orders tmdo ON tmdo.patient_id = outcomes.patient_id AND outcomes.cum_outcome = 'On antiretrovirals' #{site_manager(operator: 'AND', column: 'tmdo.site_id', location: @location)}
      INNER JOIN orders
        ON orders.patient_id = outcomes.patient_id
        AND orders.concept_id IN (#{arv_drugs_concept_set.to_sql})
        AND orders.voided = 0 #{site_manager(operator: 'AND', column: 'orders.site_id', location: @location)}
        AND DATE(orders.start_date) = DATE(tmdo.start_date)
      INNER JOIN drug_order
        ON drug_order.order_id = orders.order_id AND drug_order.quantity > 0 #{site_manager(operator: 'AND', column: 'drug_order.site_id', location: @location)}
      /* Only select drugs prescribed on the last prescription day */
      GROUP BY orders.patient_id #{@adapter == 'mysql2' ? '' : ', recent_prescription.prescription_date'}
    ) AS prescriptions
    LEFT JOIN (
      SELECT #{@adapter == 'mysql2' ? 'GROUP_CONCAT(drug.drug_id ORDER BY drug.drug_id ASC) AS drugs,' : "(SELECT STRING_AGG(drug_id::VARCHAR, ',') FROM (SELECT drug.drug_id FROM drug ORDER BY drug.drug_id ASC) AS subquery) AS drugs,"}
            regimen_name.name AS name
      FROM moh_regimen_combination AS combo
        INNER JOIN moh_regimen_combination_drug AS drug USING (regimen_combination_id)
        INNER JOIN moh_regimen_name AS regimen_name USING (regimen_name_id)
      GROUP BY combo.regimen_combination_id, regimen_name.name#{' '}
    ) AS regimens
      ON regimens.drugs = prescriptions.drugs
  SQL
end