Class: MalawiHivProgramReports::Clinic::AppointmentsReport

Inherits:
Object
  • Object
show all
Includes:
Utils::CommonSqlQueryUtils
Defined in:
app/services/malawi_hiv_program_reports/clinic/appointments_report.rb

Constant Summary collapse

ENCOUNTER_NAMES =
[
  'VITALS', 'HIV STAGING',
  'APPOINTMENT', 'HIV CLINIC REGISTRATION',
  'ART_FOLLOWUP', 'TREATMENT', 'UPDATE OUTCOME',
  'HIV RECEPTION', 'LAB', 'HIV CLINIC CONSULTATION',
  'DISPENSING', 'LAB ORDERS', 'ART ADHERENCE',
  'GIVE LAB RESULTS', 'CERVICAL CANCER SCREENING',
  'HYPERTENSION MANAGEMENT', 'FAST TRACK ASSESMENT'
].freeze
HIV_ENCOUNTERS =
::EncounterType.where('name IN(?)', ENCOUNTER_NAMES).map(&:id)

Instance Method Summary collapse

Methods included from Utils::CommonSqlQueryUtils

#current_occupation_query, #external_client_query, #occupation_filter, #partition_by_site, #process_occupation

Constructor Details

#initialize(start_date:, end_date:, **kwargs) ⇒ AppointmentsReport

Returns a new instance of AppointmentsReport.



23
24
25
26
27
# File 'app/services/malawi_hiv_program_reports/clinic/appointments_report.rb', line 23

def initialize(start_date:, end_date:, **kwargs)
  @start_date = start_date
  @end_date = end_date
  @occupation = kwargs[:occupation]
end

Instance Method Details

#missed_appointmentsObject

rubocop:disable Metrics/MethodLength rubocop:disable Metrics/AbcSize



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'app/services/malawi_hiv_program_reports/clinic/appointments_report.rb', line 31

def missed_appointments
  appointments = ::Observation.joins(:encounter)
                              .joins("LEFT JOIN (#{current_occupation_query} )AS a ON a.person_id = obs.person_id")
                              .merge(appointment_encounters)
                              .where.not(person_id: referral_patients.select(:person_id))
                              .where(concept: ::ConceptName.where(name: 'Appointment date').select(:concept_id))
                              .where('value_datetime BETWEEN ? AND ? AND encounter.program_id = ?',
                                     @start_date.strftime('%Y-%m-%d 00:00:00'),
                                     @end_date.strftime('%Y-%m-%d 23:59:59'), 1)
                              .where(occupation_filter(occupation: @occupation, field_name: 'value',
                                                       table_name: 'a', include_clause: false).to_s)
                              .group(:person_id)

  appointments.each_with_object([]) do |appointment, patients|
    patient = missed_appointment?(appointment)

    patients << patient unless patient.blank?
  end
end

#patient_visit_listObject

rubocop:disable Metrics/MethodLength rubocop:disable Metrics/AbcSize rubocop:disable Metrics/PerceivedComplexity rubocop:disable Metrics/CyclomaticComplexity



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'app/services/malawi_hiv_program_reports/clinic/appointments_report.rb', line 99

def patient_visit_list
  yes_concept = ::ConceptName.find_by_name('YES').concept_id
  hiv_reception_breakdown = {}

  (patient_visits || []).each do |v|
    # visit_date = v['obs_datetime'].to_date
    visit_type = v['name']
    ans_given = v['value_coded'].to_i == yes_concept
    patient_id = v['patient_id'].to_i
    patient_present = (visit_type.match(/patient/i) && ans_given ? true : false)
    guardian_present = (visit_type.match(/person/i) && ans_given ? true : false)

    if hiv_reception_breakdown[patient_id].blank?
      demographics = client_data(patient_id)
      hiv_reception_breakdown[patient_id] = {
        patient_present: false, guardian_present: false,
        given_name: demographics['given_name'],
        family_name: demographics['family_name'],
        gender: demographics['gender'],
        birthdate: demographics['birthdate'],
        arv_number: demographics['arv_number']
      }
    end

    hiv_reception_breakdown[patient_id][:patient_present] = patient_present if /patient/i.match?(visit_type)
    hiv_reception_breakdown[patient_id][:guardian_present] = guardian_present if /person/i.match?(visit_type)
  end

  hiv_reception_breakdown
end

#patient_visit_typesObject

rubocop:disable Metrics/MethodLength rubocop:disable Metrics/AbcSize rubocop:disable Metrics/PerceivedComplexity rubocop:disable Metrics/CyclomaticComplexity



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'app/services/malawi_hiv_program_reports/clinic/appointments_report.rb', line 57

def patient_visit_types
  yes_concept = ::ConceptName.find_by_name('YES').concept_id
  hiv_reception_breakdown = {}

  (patient_visits || []).each do |v|
    visit_date = v['obs_datetime'].to_date
    visit_type = v['name']
    ans_given = v['value_coded'].to_i == yes_concept
    patient_id = v['patient_id'].to_i
    patient_present = (visit_type.match(/patient/i) && ans_given ? true : false)
    guardian_present = (visit_type.match(/person/i) && ans_given ? true : false)

    if hiv_reception_breakdown[visit_date].blank?
      hiv_reception_breakdown[visit_date] = {}
      hiv_reception_breakdown[visit_date][patient_id] = {
        patient_present: 0, guardian_present: 0
      }
    elsif hiv_reception_breakdown[visit_date][patient_id].blank?
      hiv_reception_breakdown[visit_date][patient_id] = {
        patient_present: false, guardian_present: false
      }
    end

    if /patient/i.match?(visit_type)
      hiv_reception_breakdown[visit_date][patient_id][:patient_present] = patient_present
    end
    if /person/i.match?(visit_type)
      hiv_reception_breakdown[visit_date][patient_id][:guardian_present] = guardian_present
    end
  end

  hiv_reception_breakdown
end