Class: MalawiHivProgramReports::Clinic::IptCoverage

Inherits:
Object
  • Object
show all
Defined in:
app/services/malawi_hiv_program_reports/clinic/ipt_coverage.rb

Instance Method Summary collapse

Constructor Details

#initialize(start_date:, end_date:) ⇒ IptCoverage

Returns a new instance of IptCoverage.



11
12
13
14
# File 'app/services/malawi_hiv_program_reports/clinic/ipt_coverage.rb', line 11

def initialize(start_date:, end_date:)
  @start_date = start_date.to_date.strftime('%Y-%m-%d 00:00:00')
  @end_date = end_date.to_date.strftime('%Y-%m-%d 23:59:59')
end

Instance Method Details

#dataObject



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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'app/services/malawi_hiv_program_reports/clinic/ipt_coverage.rb', line 16

def data
  patient_ids = []
  patients = {}

  (on_art_in_reporting_period || []).each do |data|
    patient_ids << data['patient_id'].to_i
  end

  return patients if patient_ids.blank?

  data = ipt_dispensations(patient_ids)

  (data || []).each do |record|
    patient_id = record['patient_id'].to_i
    age_group = record['age_group']
    gender = record['gender']

    gender = if gender.blank?
               'Unknown'
             else
               (/F/i.match?(gender) ? 'Female' : 'Male')
             end

    patients[age_group] = {} if patients[age_group].blank?
    patients[age_group][gender] = {} if patients[age_group][gender].blank?
    patients[age_group][gender][patient_id] = 0 if patients[age_group][gender][patient_id].blank?

    prescription_info = ActiveRecord::Base.connection.select_one <<-SQL
      SELECT TIMESTAMPDIFF(day, DATE('#{record['start_date']}'), DATE('#{record['auto_expire_date']}')) days;
    SQL

    next if prescription_info['days'].blank?

    patients[age_group][gender][patient_id] += prescription_info['days'].to_i
  end

  age_groups = {}

  (patients || {}).each do |keys, values|
    pats = values
    (pats || {}).each do |sex, ids|
      (ids || {}).each do |patient_id, count|
        next unless count >= 168

        age_groups[keys] = {} if age_groups[keys].blank?
        age_groups[keys][sex] = [] if age_groups[keys][sex].blank?
        age_groups[keys][sex] << patient_id
      end
    end
  end

  age_groups
end