emr_ohsp_interface 2.2.5 → 3.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
| @@ -1,4 +1,4 @@ | |
| 1 | 
            -
            require  | 
| 1 | 
            +
            require 'emr_ohsp_interface/version'
         | 
| 2 2 |  | 
| 3 3 | 
             
            module EmrOhspInterface
         | 
| 4 4 | 
             
              module EmrOhspInterfaceService
         | 
| @@ -9,7 +9,7 @@ module EmrOhspInterface | |
| 9 9 | 
             
                  include EmrOhspInterface::Utils
         | 
| 10 10 |  | 
| 11 11 | 
             
                  def settings
         | 
| 12 | 
            -
                    file = File.read(Rails.root.join( | 
| 12 | 
            +
                    file = File.read(Rails.root.join('db', 'idsr_metadata', 'idsr_ohsp_settings.json'))
         | 
| 13 13 | 
             
                    config = JSON.parse(file)
         | 
| 14 14 | 
             
                  end
         | 
| 15 15 |  | 
| @@ -18,96 +18,95 @@ module EmrOhspInterface | |
| 18 18 | 
             
                  end
         | 
| 19 19 |  | 
| 20 20 | 
             
                  def get_ohsp_facility_id
         | 
| 21 | 
            -
                    file = File.open(Rails.root.join( | 
| 22 | 
            -
                    data = CSV.parse(file,headers: true)
         | 
| 21 | 
            +
                    file = File.open(Rails.root.join('db', 'idsr_metadata', 'emr_ohsp_facility_map.csv'))
         | 
| 22 | 
            +
                    data = CSV.parse(file, headers: true)
         | 
| 23 23 | 
             
                    emr_facility_id = Location.current_health_center.id
         | 
| 24 | 
            -
                    facility = data.select{|row| row[ | 
| 25 | 
            -
                    ohsp_id = facility[0][ | 
| 24 | 
            +
                    facility = data.select { |row| row['EMR_Facility_ID'].to_i == emr_facility_id }
         | 
| 25 | 
            +
                    ohsp_id = facility[0]['OrgUnit ID']
         | 
| 26 26 | 
             
                  end
         | 
| 27 27 |  | 
| 28 | 
            -
                  def get_ohsp_de_ids(de,type)
         | 
| 29 | 
            -
                    #this method returns an array ohsp report line ids
         | 
| 28 | 
            +
                  def get_ohsp_de_ids(de, type)
         | 
| 29 | 
            +
                    # this method returns an array ohsp report line ids
         | 
| 30 30 | 
             
                    result = []
         | 
| 31 | 
            -
                    #["waoQ016uOz1", "r1AT49VBKqg", "FPN4D0s6K3m", "zE8k2BtValu"]
         | 
| 31 | 
            +
                    # ["waoQ016uOz1", "r1AT49VBKqg", "FPN4D0s6K3m", "zE8k2BtValu"]
         | 
| 32 32 | 
             
                    #  ds,              de_id     ,  <5yrs       ,  >=5yrs
         | 
| 33 33 | 
             
                    puts de
         | 
| 34 | 
            -
                    if type ==  | 
| 35 | 
            -
                    file = File.open(Rails.root.join( | 
| 34 | 
            +
                    if type == 'weekly'
         | 
| 35 | 
            +
                    file = File.open(Rails.root.join('db', 'idsr_metadata', 'idsr_weekly_ohsp_ids.csv'))
         | 
| 36 36 | 
             
                    else
         | 
| 37 | 
            -
                    file = File.open(Rails.root.join( | 
| 37 | 
            +
                    file = File.open(Rails.root.join('db', 'idsr_metadata', 'idsr_monthly_ohsp_ids.csv'))
         | 
| 38 38 | 
             
                    end
         | 
| 39 | 
            -
                    data = CSV.parse(file,headers: true)
         | 
| 40 | 
            -
                    row = data.select{|row| row[ | 
| 41 | 
            -
                    ohsp_ds_id = row[0][ | 
| 39 | 
            +
                    data = CSV.parse(file, headers: true)
         | 
| 40 | 
            +
                    row = data.select { |row| row['Data Element Name'].strip.downcase.eql?(de.downcase.strip) }
         | 
| 41 | 
            +
                    ohsp_ds_id = row[0]['Data Set ID']
         | 
| 42 42 | 
             
                    result << ohsp_ds_id
         | 
| 43 | 
            -
                    ohsp_de_id = row[0][ | 
| 43 | 
            +
                    ohsp_de_id = row[0]['UID']
         | 
| 44 44 | 
             
                    result << ohsp_de_id
         | 
| 45 | 
            -
                    option1 = row[0][ | 
| 45 | 
            +
                    option1 = row[0]['<5Yrs']
         | 
| 46 46 | 
             
                    result << option1
         | 
| 47 | 
            -
                    option2 = row[0][ | 
| 47 | 
            +
                    option2 = row[0]['>=5Yrs']
         | 
| 48 48 | 
             
                    result << option2
         | 
| 49 49 |  | 
| 50 50 | 
             
                    return result
         | 
| 51 51 | 
             
                  end
         | 
| 52 52 |  | 
| 53 53 | 
             
                  def get_data_set_id(type)
         | 
| 54 | 
            -
                    if type ==  | 
| 55 | 
            -
                      file = File.open(Rails.root.join( | 
| 54 | 
            +
                    if type == 'weekly'
         | 
| 55 | 
            +
                      file = File.open(Rails.root.join('db', 'idsr_metadata', 'idsr_weekly_ohsp_ids.csv'))
         | 
| 56 56 | 
             
                    else
         | 
| 57 | 
            -
                      file = File.open(Rails.root.join( | 
| 57 | 
            +
                      file = File.open(Rails.root.join('db', 'idsr_metadata', 'idsr_monthly_ohsp_ids.csv'))
         | 
| 58 58 | 
             
                    end
         | 
| 59 | 
            -
                    data = CSV.parse(file,headers: true)
         | 
| 60 | 
            -
                    data_set_id = data.first[ | 
| 59 | 
            +
                    data = CSV.parse(file, headers: true)
         | 
| 60 | 
            +
                    data_set_id = data.first['Data Set ID']
         | 
| 61 61 | 
             
                  end
         | 
| 62 62 |  | 
| 63 | 
            -
                  def generate_weekly_idsr_report(request=nil,start_date=nil,end_date=nil)
         | 
| 63 | 
            +
                  def generate_weekly_idsr_report(request=nil, start_date=nil, end_date=nil)
         | 
| 64 64 |  | 
| 65 | 
            -
                    diag_map = settings[ | 
| 65 | 
            +
                    diag_map = settings['weekly_idsr_map']
         | 
| 66 | 
            +
                    site_id = Location.current.location_id
         | 
| 66 67 |  | 
| 67 68 | 
             
                    epi_week = weeks_generator.last.first.strip
         | 
| 68 | 
            -
                    start_date = weeks_generator.last.last.split( | 
| 69 | 
            -
                    end_date = weeks_generator.last.last.split( | 
| 69 | 
            +
                    start_date = weeks_generator.last.last.split('to')[0].strip if start_date.nil?
         | 
| 70 | 
            +
                    end_date = weeks_generator.last.last.split('to')[1].strip if end_date.nil?
         | 
| 70 71 |  | 
| 71 | 
            -
                    #pull the data
         | 
| 72 | 
            +
                    # pull the data
         | 
| 72 73 | 
             
                    type = EncounterType.find_by_name 'Outpatient diagnosis'
         | 
| 73 74 | 
             
                    collection = {}
         | 
| 74 75 |  | 
| 75 | 
            -
                    diag_map.each do |key,value|
         | 
| 76 | 
            -
                      options = { | 
| 77 | 
            -
                      concept_ids = ConceptName.where(name: value).collect{|cn| cn.concept_id}
         | 
| 76 | 
            +
                    diag_map.each do |key, value|
         | 
| 77 | 
            +
                      options = {'<5yrs'=>nil, '>=5yrs'=>nil}
         | 
| 78 | 
            +
                      concept_ids = ConceptName.where(name: value).collect { |cn| cn.concept_id }
         | 
| 78 79 |  | 
| 79 80 | 
             
                      data = Encounter.where('encounter_datetime BETWEEN ? AND ?
         | 
| 80 81 | 
             
                      AND encounter_type = ? AND value_coded IN (?)
         | 
| 81 | 
            -
                      AND concept_id IN(6543, 6542)',
         | 
| 82 | 
            -
             | 
| 83 | 
            -
             | 
| 84 | 
            -
             | 
| 85 | 
            -
                      INNER JOIN person p ON p.person_id = encounter.patient_id') | 
| 86 | 
            -
             | 
| 87 | 
            -
             | 
| 88 | 
            -
                      #under_five
         | 
| 89 | 
            -
                      under_five = data.select{|record| calculate_age(record[ | 
| 90 | 
            -
             | 
| 91 | 
            -
                      options[ | 
| 92 | 
            -
                      #above 5 years
         | 
| 93 | 
            -
                      over_five = data.select{|record| calculate_age(record[ | 
| 94 | 
            -
             | 
| 95 | 
            -
             | 
| 96 | 
            -
                      options[ | 
| 82 | 
            +
                      AND concept_id IN(6543, 6542) AND encounter.site_id = ?',
         | 
| 83 | 
            +
                                             start_date.to_date.strftime('%Y-%m-%d 00:00:00'),
         | 
| 84 | 
            +
                                             end_date.to_date.strftime('%Y-%m-%d 23:59:59'), type.id, concept_ids, site_id)
         | 
| 85 | 
            +
                                      .joins('INNER JOIN obs ON obs.encounter_id = encounter.encounter_id
         | 
| 86 | 
            +
                      INNER JOIN person p ON p.person_id = encounter.patient_id')
         | 
| 87 | 
            +
                                      .select('encounter.encounter_type, obs.value_coded, p.*')
         | 
| 88 | 
            +
             | 
| 89 | 
            +
                      # under_five
         | 
| 90 | 
            +
                      under_five = data.select { |record| calculate_age(record['birthdate']) < 5 }
         | 
| 91 | 
            +
                                       .collect { |record| record.person_id }
         | 
| 92 | 
            +
                      options['<5yrs'] = under_five
         | 
| 93 | 
            +
                      # above 5 years
         | 
| 94 | 
            +
                      over_five = data.select { |record| calculate_age(record['birthdate']) >=5 }
         | 
| 95 | 
            +
                                      .collect { |record| record.person_id }
         | 
| 96 | 
            +
             | 
| 97 | 
            +
                      options['>=5yrs'] = over_five
         | 
| 97 98 |  | 
| 98 99 | 
             
                      collection[key] = options
         | 
| 99 100 | 
             
                    end
         | 
| 100 | 
            -
                      if request == nil
         | 
| 101 | 
            -
                       response = send_data(collection,"weekly")
         | 
| 102 | 
            -
                      end
         | 
| 101 | 
            +
                      response = send_data(collection, 'weekly') if request == nil
         | 
| 103 102 |  | 
| 104 103 | 
             
                    return collection
         | 
| 105 104 | 
             
                  end
         | 
| 106 105 |  | 
| 107 | 
            -
                  def generate_quarterly_idsr_report(request=nil,start_date=nil,end_date=nil)
         | 
| 106 | 
            +
                  def generate_quarterly_idsr_report(request=nil, start_date=nil, end_date=nil)
         | 
| 108 107 | 
             
                    epi_month = quarters_generator.first.first.strip
         | 
| 109 | 
            -
                    start_date = quarters_generator.first.last[1].split( | 
| 110 | 
            -
                    end_date = | 
| 108 | 
            +
                    start_date = quarters_generator.first.last[1].split('to').first.strip if start_date.nil?
         | 
| 109 | 
            +
                    end_date = quarters_generator.first.last[1].split('to').last.strip if end_date.nil?
         | 
| 111 110 | 
             
                    indicators = [
         | 
| 112 111 | 
             
                      'Diabetes Mellitus',
         | 
| 113 112 | 
             
                      'Cervical Cancer',
         | 
| @@ -123,11 +122,11 @@ module EmrOhspInterface | |
| 123 122 | 
             
                      'Psychosis'
         | 
| 124 123 | 
             
                    ]
         | 
| 125 124 |  | 
| 126 | 
            -
                    diagnosis_concepts = ['Primary Diagnosis','Secondary Diagnosis']
         | 
| 127 | 
            -
                    encounters = ['OUTPATIENT DIAGNOSIS','ADMISSION DIAGNOSIS']
         | 
| 125 | 
            +
                    diagnosis_concepts = ['Primary Diagnosis', 'Secondary Diagnosis']
         | 
| 126 | 
            +
                    encounters = ['OUTPATIENT DIAGNOSIS', 'ADMISSION DIAGNOSIS']
         | 
| 128 127 |  | 
| 129 128 | 
             
                    report_struct = indicators.each_with_object({}) do |indicator, report|
         | 
| 130 | 
            -
                      report[indicator | 
| 129 | 
            +
                      report[indicator] = ['<5 yrs', '>=5 yrs'].each_with_object({}) do |group, sub_report|
         | 
| 131 130 | 
             
                        sub_report[group] = {
         | 
| 132 131 | 
             
                          outpatient_cases: [],
         | 
| 133 132 | 
             
                          inpatient_cases: [],
         | 
| @@ -138,6 +137,14 @@ module EmrOhspInterface | |
| 138 137 | 
             
                      end
         | 
| 139 138 | 
             
                    end
         | 
| 140 139 |  | 
| 140 | 
            +
                    admitted_patient_died = proc do |patient|
         | 
| 141 | 
            +
                      visit_type = patient['visit_type']
         | 
| 142 | 
            +
                      dead = patient['dead']
         | 
| 143 | 
            +
             | 
| 144 | 
            +
                      visit_type == 'ADMISSION DIAGNOSIS' && dead
         | 
| 145 | 
            +
                    end
         | 
| 146 | 
            +
             | 
| 147 | 
            +
             | 
| 141 148 | 
             
                    diagonised = ActiveRecord::Base.connection.select_all <<~SQL
         | 
| 142 149 | 
             
                      SELECT
         | 
| 143 150 | 
             
                        e.patient_id,
         | 
| @@ -159,358 +166,315 @@ module EmrOhspInterface | |
| 159 166 | 
             
                        AND DATE(e.encounter_datetime) < '#{end_date}'
         | 
| 160 167 | 
             
                        AND obs.concept_id IN (#{ConceptName.where(name: diagnosis_concepts).pluck(:concept_id).join(',')})
         | 
| 161 168 | 
             
                        AND obs.value_coded IN (#{ConceptName.where(name: indicators).pluck(:concept_id).join(',')})
         | 
| 169 | 
            +
                        AND e.site_id = #{Location.current.location_id}
         | 
| 162 170 | 
             
                      GROUP BY
         | 
| 163 | 
            -
                        p.person_id | 
| 171 | 
            +
                        p.person_id
         | 
| 164 172 | 
             
                    SQL
         | 
| 165 173 |  | 
| 166 174 | 
             
                    malaria_tests = lab_results(test_types: ['Malaria Screening'], start_date: start_date, end_date: end_date)
         | 
| 167 175 |  | 
| 168 | 
            -
                    tested_patient_ids = malaria_tests.map{|patient| patient['patient_id']}
         | 
| 176 | 
            +
                    tested_patient_ids = malaria_tests.map { |patient| patient['patient_id'] }
         | 
| 169 177 |  | 
| 170 | 
            -
                     | 
| 171 | 
            -
                      diagnosis = patient['diagnosis']&.downcase
         | 
| 172 | 
            -
                      visit_type = patient['visit_type']
         | 
| 178 | 
            +
                    tested_positive = lambda do |patient|
         | 
| 173 179 | 
             
                      patient_id = patient['patient_id']
         | 
| 174 | 
            -
                       | 
| 175 | 
            -
             | 
| 176 | 
            -
                      five_plus = '>=5 yrs'
         | 
| 177 | 
            -
                      less_than_5 = '<5 yrs'
         | 
| 178 | 
            -
             | 
| 179 | 
            -
                      age_group = birthdate > 5.years.ago ? less_than_5 : five_plus
         | 
| 180 | 
            +
                      return false unless tested_patient_ids.include?(patient_id)
         | 
| 180 181 |  | 
| 181 | 
            -
                       | 
| 182 | 
            -
                       | 
| 183 | 
            -
                      report_struct[diagnosis][age_group][:tested_malaria] << patient_id if tested_patient_ids.include?(patient_id)
         | 
| 184 | 
            -
                      report_struct[diagnosis][age_group][:tested_positive_malaria] << patient_id if tested_positive(tested_patient_ids, patient)
         | 
| 185 | 
            -
                      report_struct[diagnosis][age_group][:inpatient_cases_death] << patient_id if admitted_patient_died(patient)
         | 
| 182 | 
            +
                      results = malaria_tests.find { |test| test['patient_id'] == patient_id }['results']
         | 
| 183 | 
            +
                      ['positive', 'parasites seen'].include?(results)
         | 
| 186 184 | 
             
                    end
         | 
| 187 | 
            -
             | 
| 188 | 
            -
             | 
| 189 | 
            -
             | 
| 190 | 
            -
             | 
| 191 | 
            -
             | 
| 192 | 
            -
                  def tested_positive(ids, patient)
         | 
| 193 | 
            -
                    patient_id = patient['patient_id']
         | 
| 194 | 
            -
                    return false unless ids.include?(patient_id)
         | 
| 185 | 
            +
                      diagonised.each do |patient|
         | 
| 186 | 
            +
                        diagnosis = patient['diagnosis'].titleize
         | 
| 187 | 
            +
                        visit_type = patient['visit_type']
         | 
| 188 | 
            +
                        patient_id = patient['patient_id']
         | 
| 189 | 
            +
                        birthdate = patient['birthdate']
         | 
| 195 190 |  | 
| 196 | 
            -
             | 
| 197 | 
            -
             | 
| 198 | 
            -
                  end
         | 
| 199 | 
            -
             | 
| 200 | 
            -
                  def admitted_patient_died(patient)
         | 
| 201 | 
            -
                    visit_type = patient['visit_type']
         | 
| 202 | 
            -
                    dead = patient['dead']
         | 
| 191 | 
            +
                        five_plus = '>=5 yrs'
         | 
| 192 | 
            +
                        less_than_5 = '<5 yrs'
         | 
| 203 193 |  | 
| 204 | 
            -
             | 
| 194 | 
            +
                        age_group = birthdate > 5.years.ago ? less_than_5 : five_plus
         | 
| 195 | 
            +
                      
         | 
| 196 | 
            +
                        report_struct[diagnosis][age_group][:outpatient_cases] << patient_id if visit_type == 'OUTPATIENT DIAGNOSIS'
         | 
| 197 | 
            +
                        report_struct[diagnosis][age_group][:inpatient_cases] << patient_id if visit_type == 'ADMISSION DIAGNOSIS'
         | 
| 198 | 
            +
                        if tested_patient_ids.include?(patient_id)
         | 
| 199 | 
            +
                          report_struct[diagnosis][age_group][:tested_malaria] << patient_id
         | 
| 200 | 
            +
                        end
         | 
| 201 | 
            +
                        report_struct[diagnosis][age_group][:tested_positive_malaria] << patient_id if tested_positive.call(patient)
         | 
| 202 | 
            +
                        if admitted_patient_died.call(patient)
         | 
| 203 | 
            +
                          report_struct[diagnosis][age_group][:inpatient_cases_death] << patient_id
         | 
| 204 | 
            +
                        end
         | 
| 205 | 
            +
                      end
         | 
| 206 | 
            +
                    report_struct
         | 
| 205 207 | 
             
                  end
         | 
| 206 208 |  | 
| 207 | 
            -
                  #idsr monthly report
         | 
| 208 | 
            -
                  def generate_monthly_idsr_report(request=nil,start_date=nil,end_date=nil)
         | 
| 209 | 
            -
                    diag_map = settings[ | 
| 209 | 
            +
                  # idsr monthly report
         | 
| 210 | 
            +
                  def generate_monthly_idsr_report(request=nil, start_date=nil, end_date=nil)
         | 
| 211 | 
            +
                    diag_map = settings['monthly_idsr_map']
         | 
| 210 212 | 
             
                    epi_month = months_generator.first.first.strip
         | 
| 211 | 
            -
                    start_date = months_generator.first.last[1].split( | 
| 212 | 
            -
                    end_date = | 
| 213 | 
            +
                    start_date = months_generator.first.last[1].split('to').first.strip if start_date.nil?
         | 
| 214 | 
            +
                    end_date = months_generator.first.last[1].split('to').last.strip if end_date.nil?
         | 
| 213 215 | 
             
                    type = EncounterType.find_by_name 'Outpatient diagnosis'
         | 
| 214 216 | 
             
                    collection = {}
         | 
| 215 217 |  | 
| 216 | 
            -
                    special_indicators = [ | 
| 217 | 
            -
                                           | 
| 218 | 
            -
                                           | 
| 219 | 
            -
                                           | 
| 220 | 
            -
                                           | 
| 221 | 
            -
                                           | 
| 218 | 
            +
                    special_indicators = ['Malaria in Pregnancy',
         | 
| 219 | 
            +
                                          'HIV New Initiated on ART',
         | 
| 220 | 
            +
                                          'Diarrhoea In Under 5',
         | 
| 221 | 
            +
                                          'Malnutrition In Under 5',
         | 
| 222 | 
            +
                                          'Underweight Newborns < 2500g in Under 5 Cases',
         | 
| 223 | 
            +
                                          'Severe Pneumonia in under 5 cases']
         | 
| 222 224 |  | 
| 223 | 
            -
                    diag_map.each do |key,value|
         | 
| 224 | 
            -
                      options = { | 
| 225 | 
            -
                      concept_ids = ConceptName.where(name: value).collect{|cn| cn.concept_id}
         | 
| 225 | 
            +
                    diag_map.each do |key, value|
         | 
| 226 | 
            +
                      options = {'<5yrs'=>nil, '>=5yrs'=>nil}
         | 
| 227 | 
            +
                      concept_ids = ConceptName.where(name: value).collect { |cn| cn.concept_id }
         | 
| 226 228 | 
             
                      if !special_indicators.include?(key)
         | 
| 227 229 | 
             
                          data = Encounter.where('encounter_datetime BETWEEN ? AND ?
         | 
| 228 230 | 
             
                          AND encounter_type = ? AND value_coded IN (?)
         | 
| 229 | 
            -
                          AND concept_id IN(6543, 6542)',
         | 
| 230 | 
            -
             | 
| 231 | 
            -
             | 
| 232 | 
            -
             | 
| 233 | 
            -
             | 
| 234 | 
            -
                           | 
| 235 | 
            -
             | 
| 236 | 
            -
             | 
| 237 | 
            -
                          under_five | 
| 238 | 
            -
             | 
| 239 | 
            -
             | 
| 240 | 
            -
                           | 
| 241 | 
            -
                           | 
| 242 | 
            -
             | 
| 243 | 
            -
             | 
| 244 | 
            -
             | 
| 231 | 
            +
                          AND concept_id IN(6543, 6542) AND encounter.site_id = ?',
         | 
| 232 | 
            +
                                                 start_date.to_date.strftime('%Y-%m-%d 00:00:00'),
         | 
| 233 | 
            +
                                                 end_date.to_date.strftime('%Y-%m-%d 23:59:59'), type.id, concept_ids,
         | 
| 234 | 
            +
                                                 Location.current.location_id)
         | 
| 235 | 
            +
                                          .joins('INNER JOIN obs ON obs.encounter_id = encounter.encounter_id
         | 
| 236 | 
            +
                          INNER JOIN person p ON p.person_id = encounter.patient_id')
         | 
| 237 | 
            +
                                          .select('encounter.encounter_type, obs.value_coded, p.*')
         | 
| 238 | 
            +
             | 
| 239 | 
            +
                          # under_five
         | 
| 240 | 
            +
                          under_five = data.select { |record| calculate_age(record['birthdate']) < 5 }\
         | 
| 241 | 
            +
                                           .collect { |record| record.person_id }.uniq
         | 
| 242 | 
            +
                          options['<5yrs'] = under_five
         | 
| 243 | 
            +
                          # above 5 years
         | 
| 244 | 
            +
                          over_five = data.select { |record| calculate_age(record['birthdate']) >=5 }\
         | 
| 245 | 
            +
                                          .collect { |record| record.person_id }.uniq
         | 
| 246 | 
            +
             | 
| 247 | 
            +
                          options['>=5yrs'] = over_five
         | 
| 245 248 |  | 
| 246 249 | 
             
                          collection[key] = options
         | 
| 247 250 | 
             
                      else
         | 
| 248 | 
            -
                        if key.eql?( | 
| 251 | 
            +
                        if key.eql?('Malaria in Pregnancy')
         | 
| 249 252 | 
             
                          mal_patient_id = Encounter.where('encounter_datetime BETWEEN ? AND ?
         | 
| 250 253 | 
             
                          AND encounter_type = ? AND value_coded IN (?)
         | 
| 251 | 
            -
                          AND concept_id IN(6543, 6542)',
         | 
| 252 | 
            -
             | 
| 253 | 
            -
             | 
| 254 | 
            -
             | 
| 255 | 
            -
             | 
| 256 | 
            -
                           | 
| 257 | 
            -
             | 
| 258 | 
            -
             | 
| 259 | 
            -
                           | 
| 254 | 
            +
                          AND concept_id IN(6543, 6542) AND encounter.site_id = ?',
         | 
| 255 | 
            +
                                                           start_date.to_date.strftime('%Y-%m-%d 00:00:00'),
         | 
| 256 | 
            +
                                                           end_date.to_date.strftime('%Y-%m-%d 23:59:59'), type.id, concept_ids,
         | 
| 257 | 
            +
                                                           Location.current.location_id)
         | 
| 258 | 
            +
                                                    .joins('INNER JOIN obs ON obs.encounter_id = encounter.encounter_id
         | 
| 259 | 
            +
                          INNER JOIN person p ON p.person_id = encounter.patient_id')\
         | 
| 260 | 
            +
                                                    .select('encounter.encounter_type, obs.value_coded, p.*')
         | 
| 261 | 
            +
             | 
| 262 | 
            +
                          mal_patient_id=   mal_patient_id.collect { |record| record.person_id }
         | 
| 263 | 
            +
                          # find those that are pregnant
         | 
| 260 264 | 
             
                          preg = Observation.where(["concept_id = 6131 AND obs_datetime
         | 
| 261 265 | 
             
                                                     BETWEEN ? AND ? AND person_id IN(?)
         | 
| 262 266 | 
             
                                                      AND value_coded =1065",
         | 
| 263 | 
            -
             | 
| 264 | 
            -
             | 
| 267 | 
            +
                                                    start_date.to_date.strftime('%Y-%m-%d 00:00:00'),
         | 
| 268 | 
            +
                                                    end_date.to_date.strftime('%Y-%m-%d 23:59:59'), mal_patient_id ])
         | 
| 265 269 |  | 
| 266 | 
            -
                           options[ | 
| 270 | 
            +
                           options['>=5yrs'] = preg.collect { |record| record.person_id } rescue 0
         | 
| 267 271 | 
             
                           collection[key] = options
         | 
| 268 272 | 
             
                        end
         | 
| 269 273 |  | 
| 270 | 
            -
                        if key.eql?( | 
| 271 | 
            -
                         data = | 
| 274 | 
            +
                        if key.eql?('HIV New Initiated on ART')
         | 
| 275 | 
            +
                         data = ActiveRecord::Base.connection.select_all(
         | 
| 272 276 | 
             
                                    "SELECT * FROM temp_earliest_start_date
         | 
| 273 277 | 
             
                                        WHERE date_enrolled BETWEEN '#{start_date}' AND '#{end_date}'
         | 
| 274 278 | 
             
                                        AND date_enrolled = earliest_start_date
         | 
| 275 | 
            -
             | 
| 279 | 
            +
                                        AND site_id = #{Location.current.location_id}
         | 
| 280 | 
            +
                                         GROUP BY patient_id" ).to_a
         | 
| 276 281 |  | 
| 277 | 
            -
                          under_five = data.select{|record| calculate_age(record[ | 
| 278 | 
            -
             | 
| 282 | 
            +
                          under_five = data.select { |record| calculate_age(record['birthdate']) < 5 }\
         | 
| 283 | 
            +
                                           .collect { |record| record['patient_id'] }
         | 
| 279 284 |  | 
| 280 | 
            -
                          over_five = data.select{|record| calculate_age(record[ | 
| 281 | 
            -
             | 
| 285 | 
            +
                          over_five = data.select { |record| calculate_age(record['birthdate']) >=5 }\
         | 
| 286 | 
            +
                                          .collect { |record| record['patient_id'] }
         | 
| 282 287 |  | 
| 283 | 
            -
                          options[ | 
| 284 | 
            -
                          options[ | 
| 288 | 
            +
                          options['<5yrs'] = under_five
         | 
| 289 | 
            +
                          options['>=5yrs'] = over_five
         | 
| 285 290 |  | 
| 286 291 | 
             
                          collection[key] = options
         | 
| 287 292 | 
             
                        end
         | 
| 288 293 |  | 
| 289 | 
            -
                        if key.eql?( | 
| 290 | 
            -
                          data =  | 
| 291 | 
            -
             | 
| 292 | 
            -
             | 
| 293 | 
            -
             | 
| 294 | 
            -
             | 
| 295 | 
            -
                           | 
| 296 | 
            -
                           | 
| 297 | 
            -
             | 
| 298 | 
            -
             | 
| 299 | 
            -
                          #under_five
         | 
| 300 | 
            -
                          under_five = data.select{|record| calculate_age(record["birthdate"]) < 5}.\
         | 
| 301 | 
            -
                                      collect{|record| record.person_id}
         | 
| 302 | 
            -
                          options["<5yrs"] = under_five
         | 
| 294 | 
            +
                        if key.eql?('Diarrhoea In Under 5')
         | 
| 295 | 
            +
                          data = fetch_encounter_data(start_date.to_date.strftime('%Y-%m-%d 00:00:00'),
         | 
| 296 | 
            +
                                                    end_date.to_date.strftime('%Y-%m-%d 23:59:59'),
         | 
| 297 | 
            +
                                                    type.id,
         | 
| 298 | 
            +
                                                    concept_ids)
         | 
| 299 | 
            +
             | 
| 300 | 
            +
                          # under_five
         | 
| 301 | 
            +
                          under_five = data.select { |record| calculate_age(record['birthdate']) < 5 }\
         | 
| 302 | 
            +
                                           .collect { |record| record.person_id }
         | 
| 303 | 
            +
                          options['<5yrs'] = under_five
         | 
| 303 304 | 
             
                          collection[key] = options
         | 
| 304 305 | 
             
                        end
         | 
| 305 306 |  | 
| 306 307 |  | 
| 307 | 
            -
                        if key.eql?( | 
| 308 | 
            -
                          data =  | 
| 309 | 
            -
             | 
| 310 | 
            -
             | 
| 311 | 
            -
             | 
| 312 | 
            -
             | 
| 313 | 
            -
                           | 
| 314 | 
            -
                           | 
| 315 | 
            -
             | 
| 316 | 
            -
             | 
| 317 | 
            -
                          #under_five
         | 
| 318 | 
            -
                          under_five = data.select{|record| calculate_age(record["birthdate"]) < 5}.\
         | 
| 319 | 
            -
                                      collect{|record| record.person_id}
         | 
| 320 | 
            -
                          options["<5yrs"] = under_five
         | 
| 308 | 
            +
                        if key.eql?('Malnutrition In Under 5')
         | 
| 309 | 
            +
                          data = fetch_encounter_data(start_date.to_date.strftime('%Y-%m-%d 00:00:00'),
         | 
| 310 | 
            +
                                                    end_date.to_date.strftime('%Y-%m-%d 23:59:59'),
         | 
| 311 | 
            +
                                                    type.id,
         | 
| 312 | 
            +
                                                    concept_ids)
         | 
| 313 | 
            +
             | 
| 314 | 
            +
                          # under_five
         | 
| 315 | 
            +
                          under_five = data.select { |record| calculate_age(record['birthdate']) < 5 }\
         | 
| 316 | 
            +
                                           .collect { |record| record.person_id }
         | 
| 317 | 
            +
                          options['<5yrs'] = under_five
         | 
| 321 318 | 
             
                          collection[key] = options
         | 
| 322 319 | 
             
                        end
         | 
| 323 320 |  | 
| 324 321 |  | 
| 325 | 
            -
                        if key.eql?( | 
| 326 | 
            -
                          data =  | 
| 327 | 
            -
             | 
| 328 | 
            -
             | 
| 329 | 
            -
             | 
| 330 | 
            -
             | 
| 331 | 
            -
                           | 
| 332 | 
            -
                           | 
| 333 | 
            -
             | 
| 334 | 
            -
             | 
| 335 | 
            -
                          #under_five
         | 
| 336 | 
            -
                          under_five = data.select{|record| calculate_age(record["birthdate"]) < 5}.\
         | 
| 337 | 
            -
                                      collect{|record| record.person_id}
         | 
| 338 | 
            -
                          options["<5yrs"] = under_five
         | 
| 322 | 
            +
                        if key.eql?('Underweight Newborns < 2500g in Under 5 Cases')
         | 
| 323 | 
            +
                          data = fetch_encounter_data(start_date.to_date.strftime('%Y-%m-%d 00:00:00'),
         | 
| 324 | 
            +
                                                    end_date.to_date.strftime('%Y-%m-%d 23:59:59'),
         | 
| 325 | 
            +
                                                    type.id,
         | 
| 326 | 
            +
                                                    concept_ids)
         | 
| 327 | 
            +
             | 
| 328 | 
            +
                          # under_five
         | 
| 329 | 
            +
                          under_five = data.select { |record| calculate_age(record['birthdate']) < 5 }\
         | 
| 330 | 
            +
                                           .collect { |record| record.person_id }
         | 
| 331 | 
            +
                          options['<5yrs'] = under_five
         | 
| 339 332 | 
             
                          collection[key] = options
         | 
| 340 333 | 
             
                        end
         | 
| 341 334 |  | 
| 342 | 
            -
                        if key.eql?( | 
| 343 | 
            -
                          data =  | 
| 344 | 
            -
             | 
| 345 | 
            -
             | 
| 346 | 
            -
             | 
| 347 | 
            -
             | 
| 348 | 
            -
                           | 
| 349 | 
            -
                           | 
| 350 | 
            -
             | 
| 351 | 
            -
             | 
| 352 | 
            -
                          #under_five
         | 
| 353 | 
            -
                          under_five = data.select{|record| calculate_age(record["birthdate"]) < 5}.\
         | 
| 354 | 
            -
                                      collect{|record| record.person_id}
         | 
| 355 | 
            -
                          options["<5yrs"] = under_five
         | 
| 335 | 
            +
                        if key.eql?('Severe Pneumonia in under 5 cases')
         | 
| 336 | 
            +
                          data = fetch_encounter_data(start_date.to_date.strftime('%Y-%m-%d 00:00:00'),
         | 
| 337 | 
            +
                                                    end_date.to_date.strftime('%Y-%m-%d 23:59:59'),
         | 
| 338 | 
            +
                                                    type.id,
         | 
| 339 | 
            +
                                                    concept_ids)
         | 
| 340 | 
            +
             | 
| 341 | 
            +
                          # under_five
         | 
| 342 | 
            +
                          under_five = data.select { |record| calculate_age(record['birthdate']) < 5 }\
         | 
| 343 | 
            +
                                           .collect { |record| record.person_id }
         | 
| 344 | 
            +
                          options['<5yrs'] = under_five
         | 
| 356 345 | 
             
                          collection[key] = options
         | 
| 357 346 | 
             
                        end
         | 
| 358 347 | 
             
                      end
         | 
| 359 348 | 
             
                    end
         | 
| 360 | 
            -
                      if request == nil
         | 
| 361 | 
            -
                       response = send_data(collection,"monthly")
         | 
| 362 | 
            -
                      end
         | 
| 349 | 
            +
                      response = send_data(collection, 'monthly') if request == nil
         | 
| 363 350 | 
             
                    return collection
         | 
| 364 351 | 
             
                  end
         | 
| 365 352 |  | 
| 366 | 
            -
                  def  | 
| 353 | 
            +
                  def fetch_encounter_data(start_date, end_date, type_id, concept_ids)
         | 
| 354 | 
            +
                    Encounter.where('encounter_datetime BETWEEN ? AND ? 
         | 
| 355 | 
            +
                                    AND encounter_type = ? 
         | 
| 356 | 
            +
                                    AND value_coded IN (?)
         | 
| 357 | 
            +
                                    AND concept_id IN(6543, 6542) 
         | 
| 358 | 
            +
                                    AND encounter.site_id = ?',
         | 
| 359 | 
            +
                                    start_date, end_date, type_id, concept_ids, Location.current.location_id)
         | 
| 360 | 
            +
                              .joins('INNER JOIN obs ON obs.encounter_id = encounter.encounter_id
         | 
| 361 | 
            +
                                      INNER JOIN person p ON p.person_id = encounter.patient_id')
         | 
| 362 | 
            +
                              .select('encounter.encounter_type, obs.value_coded, p.*')
         | 
| 363 | 
            +
                  end
         | 
| 364 | 
            +
             | 
| 365 | 
            +
                  def generate_hmis_15_report(start_date=nil, end_date=nil)
         | 
| 367 366 |  | 
| 368 | 
            -
                    diag_map = settings[ | 
| 367 | 
            +
                    diag_map = settings['hmis_15_map']
         | 
| 369 368 |  | 
| 370 | 
            -
                    #pull the data
         | 
| 369 | 
            +
                    # pull the data
         | 
| 371 370 | 
             
                    type = EncounterType.find_by_name 'Outpatient diagnosis'
         | 
| 372 371 | 
             
                    collection = {}
         | 
| 373 372 |  | 
| 374 | 
            -
                    special_indicators = [ | 
| 375 | 
            -
             | 
| 376 | 
            -
             | 
| 377 | 
            -
             | 
| 378 | 
            -
             | 
| 379 | 
            -
             | 
| 380 | 
            -
                    ]
         | 
| 373 | 
            +
                    special_indicators = ['Malaria - new cases (under 5)',
         | 
| 374 | 
            +
                                          'Malaria - new cases (5 & over)',
         | 
| 375 | 
            +
                                          'HIV confirmed positive (15-49 years) new cases',
         | 
| 376 | 
            +
                                          'Diarrhoea non - bloody -new cases (under5)',
         | 
| 377 | 
            +
                                          'Malnutrition - new case (under 5)',
         | 
| 378 | 
            +
                                          'Acute respiratory infections - new cases (U5)']
         | 
| 381 379 |  | 
| 382 | 
            -
                    diag_map.each do |key,value|
         | 
| 383 | 
            -
                      options = { | 
| 384 | 
            -
                      concept_ids = ConceptName.where(name: value).collect{|cn| cn.concept_id}
         | 
| 380 | 
            +
                    diag_map.each do |key, value|
         | 
| 381 | 
            +
                      options = {'ids'=>nil}
         | 
| 382 | 
            +
                      concept_ids = ConceptName.where(name: value).collect { |cn| cn.concept_id }
         | 
| 385 383 |  | 
| 386 384 | 
             
                      if !special_indicators.include?(key)
         | 
| 387 | 
            -
                        data =  | 
| 388 | 
            -
             | 
| 389 | 
            -
             | 
| 390 | 
            -
             | 
| 391 | 
            -
             | 
| 392 | 
            -
                         | 
| 393 | 
            -
                        INNER JOIN person p ON p.person_id = encounter.patient_id').\
         | 
| 394 | 
            -
                        select('encounter.encounter_type, obs.value_coded, p.*')
         | 
| 395 | 
            -
                
         | 
| 396 | 
            -
                        # #under_five
         | 
| 397 | 
            -
                        # under_five = data.select{|record| calculate_age(record["birthdate"]) < 5}.\
         | 
| 398 | 
            -
                        #             collect{|record| record.person_id}
         | 
| 399 | 
            -
                        # options["<5yrs"] = under_five
         | 
| 400 | 
            -
                        # #above 5 years
         | 
| 401 | 
            -
                        # over_five = data.select{|record| calculate_age(record["birthdate"]) >=5 }.\
         | 
| 402 | 
            -
                        #             collect{|record| record.person_id}
         | 
| 385 | 
            +
                        data = fetch_encounter_data(start_date.to_date.strftime('%Y-%m-%d 00:00:00'),
         | 
| 386 | 
            +
                                                    end_date.to_date.strftime('%Y-%m-%d 23:59:59'),
         | 
| 387 | 
            +
                                                    type.id,
         | 
| 388 | 
            +
                                                    concept_ids)
         | 
| 389 | 
            +
                          
         | 
| 390 | 
            +
                        all = data.collect { |record| record.person_id }
         | 
| 403 391 |  | 
| 404 | 
            -
                        # options[">=5yrs"] =  over_five
         | 
| 405 392 |  | 
| 406 | 
            -
                         | 
| 407 | 
            -
                
         | 
| 408 | 
            -
                
         | 
| 409 | 
            -
                        options["ids"] = all
         | 
| 393 | 
            +
                        options['ids'] = all
         | 
| 410 394 |  | 
| 411 395 | 
             
                        collection[key] = options
         | 
| 412 396 | 
             
                      else
         | 
| 413 | 
            -
                        if key.eql?( | 
| 414 | 
            -
                          data =  | 
| 415 | 
            -
             | 
| 416 | 
            -
             | 
| 417 | 
            -
             | 
| 418 | 
            -
                          end_date.to_date.strftime('%Y-%m-%d 23:59:59'),type.id,concept_ids).\
         | 
| 419 | 
            -
                          joins('INNER JOIN obs ON obs.encounter_id = encounter.encounter_id
         | 
| 420 | 
            -
                          INNER JOIN person p ON p.person_id = encounter.patient_id').\
         | 
| 421 | 
            -
                          select('encounter.encounter_type, obs.value_coded, p.*')
         | 
| 397 | 
            +
                        if key.eql?('Malaria - new cases (under 5)')
         | 
| 398 | 
            +
                          data = fetch_encounter_data(start_date.to_date.strftime('%Y-%m-%d 00:00:00'),
         | 
| 399 | 
            +
                                                      end_date.to_date.strftime('%Y-%m-%d 23:59:59'),
         | 
| 400 | 
            +
                                                      type.id,
         | 
| 401 | 
            +
                                                      concept_ids)
         | 
| 422 402 |  | 
| 423 | 
            -
                          under_five = data.select{|record| calculate_age(record[ | 
| 424 | 
            -
             | 
| 403 | 
            +
                          under_five = data.select { |record| calculate_age(record['birthdate']) < 5 }\
         | 
| 404 | 
            +
                                           .collect { |record| record['person_id'] }
         | 
| 425 405 |  | 
| 426 | 
            -
                          options[ | 
| 406 | 
            +
                          options['ids'] = under_five
         | 
| 427 407 |  | 
| 428 408 | 
             
                          collection[key] = options
         | 
| 429 409 | 
             
                        end
         | 
| 430 410 |  | 
| 431 | 
            -
                        if key.eql?( | 
| 432 | 
            -
                          data =  | 
| 433 | 
            -
             | 
| 434 | 
            -
             | 
| 435 | 
            -
             | 
| 436 | 
            -
                          end_date.to_date.strftime('%Y-%m-%d 23:59:59'),type.id,concept_ids).\
         | 
| 437 | 
            -
                          joins('INNER JOIN obs ON obs.encounter_id = encounter.encounter_id
         | 
| 438 | 
            -
                          INNER JOIN person p ON p.person_id = encounter.patient_id').\
         | 
| 439 | 
            -
                          select('encounter.encounter_type, obs.value_coded, p.*')
         | 
| 411 | 
            +
                        if key.eql?('Malaria - new cases (5 & over)')
         | 
| 412 | 
            +
                          data = fetch_encounter_data(start_date.to_date.strftime('%Y-%m-%d 00:00:00'),
         | 
| 413 | 
            +
                                                      end_date.to_date.strftime('%Y-%m-%d 23:59:59'),
         | 
| 414 | 
            +
                                                      type.id,
         | 
| 415 | 
            +
                                                      concept_ids)
         | 
| 440 416 |  | 
| 441 | 
            -
                          over_and_five = data.select{|record| calculate_age(record[ | 
| 442 | 
            -
             | 
| 417 | 
            +
                          over_and_five = data.select { |record| calculate_age(record['birthdate']) >= 5 }\
         | 
| 418 | 
            +
                                              .collect { |record| record['person_id'] }
         | 
| 443 419 |  | 
| 444 | 
            -
                          options[ | 
| 420 | 
            +
                          options['ids'] = over_and_five
         | 
| 445 421 |  | 
| 446 422 | 
             
                          collection[key] = options
         | 
| 447 423 | 
             
                        end
         | 
| 448 424 |  | 
| 449 | 
            -
                        if key.eql?( | 
| 450 | 
            -
                          data = | 
| 425 | 
            +
                        if key.eql?('HIV confirmed positive (15-49 years) new cases')
         | 
| 426 | 
            +
                          data = ActiveRecord::Base.connection.select_all(
         | 
| 451 427 | 
             
                            "SELECT * FROM temp_earliest_start_date
         | 
| 452 428 | 
             
                              WHERE date_enrolled BETWEEN '#{start_date}' AND '#{end_date}'
         | 
| 453 429 | 
             
                              AND date_enrolled = earliest_start_date
         | 
| 454 | 
            -
                               | 
| 430 | 
            +
                              AND site_id = #{Location.current.location_id}
         | 
| 431 | 
            +
                              GROUP BY patient_id" ).to_a
         | 
| 455 432 |  | 
| 456 | 
            -
                          over_and_15_49 = data.select{|record| calculate_age(record[ | 
| 457 | 
            -
             | 
| 433 | 
            +
                          over_and_15_49 = data.select { |record| calculate_age(record['birthdate']) >= 15 && calculate_age(record['birthdate']) <=49 }\
         | 
| 434 | 
            +
                                               .collect { |record| record['patient_id'] }
         | 
| 458 435 |  | 
| 459 | 
            -
                          options[ | 
| 436 | 
            +
                          options['ids'] = over_and_15_49
         | 
| 460 437 |  | 
| 461 438 | 
             
                          collection[key] = options
         | 
| 462 439 | 
             
                        end
         | 
| 463 440 |  | 
| 464 | 
            -
                        if key.eql?( | 
| 465 | 
            -
                          data =  | 
| 466 | 
            -
             | 
| 467 | 
            -
             | 
| 468 | 
            -
             | 
| 469 | 
            -
                          end_date.to_date.strftime('%Y-%m-%d 23:59:59'),type.id,concept_ids).\
         | 
| 470 | 
            -
                          joins('INNER JOIN obs ON obs.encounter_id = encounter.encounter_id
         | 
| 471 | 
            -
                          INNER JOIN person p ON p.person_id = encounter.patient_id').\
         | 
| 472 | 
            -
                          select('encounter.encounter_type, obs.value_coded, p.*')
         | 
| 441 | 
            +
                        if key.eql?('Diarrhoea non - bloody -new cases (under5)')
         | 
| 442 | 
            +
                          data = fetch_encounter_data(start_date.to_date.strftime('%Y-%m-%d 00:00:00'),
         | 
| 443 | 
            +
                                                      end_date.to_date.strftime('%Y-%m-%d 23:59:59'),
         | 
| 444 | 
            +
                                                      type.id,
         | 
| 445 | 
            +
                                                      concept_ids)
         | 
| 473 446 |  | 
| 474 | 
            -
                          under_five = data.select{|record| calculate_age(record[ | 
| 475 | 
            -
             | 
| 447 | 
            +
                          under_five = data.select { |record| calculate_age(record['birthdate']) < 5 }\
         | 
| 448 | 
            +
                                           .collect { |record| record['person_id'] }
         | 
| 476 449 |  | 
| 477 | 
            -
                          options[ | 
| 450 | 
            +
                          options['ids'] = under_five
         | 
| 478 451 |  | 
| 479 452 | 
             
                          collection[key] = options
         | 
| 480 453 | 
             
                        end
         | 
| 481 454 |  | 
| 482 | 
            -
                        if key.eql?( | 
| 483 | 
            -
                          data =  | 
| 484 | 
            -
             | 
| 485 | 
            -
             | 
| 486 | 
            -
             | 
| 487 | 
            -
                          end_date.to_date.strftime('%Y-%m-%d 23:59:59'),type.id,concept_ids).\
         | 
| 488 | 
            -
                          joins('INNER JOIN obs ON obs.encounter_id = encounter.encounter_id
         | 
| 489 | 
            -
                          INNER JOIN person p ON p.person_id = encounter.patient_id').\
         | 
| 490 | 
            -
                          select('encounter.encounter_type, obs.value_coded, p.*')
         | 
| 455 | 
            +
                        if key.eql?('Malnutrition - new case (under 5)')
         | 
| 456 | 
            +
                          data = fetch_encounter_data(start_date.to_date.strftime('%Y-%m-%d 00:00:00'),
         | 
| 457 | 
            +
                                                      end_date.to_date.strftime('%Y-%m-%d 23:59:59'),
         | 
| 458 | 
            +
                                                      type.id,
         | 
| 459 | 
            +
                                                      concept_ids)
         | 
| 491 460 |  | 
| 492 | 
            -
                          under_five = data.select{|record| calculate_age(record[ | 
| 493 | 
            -
             | 
| 461 | 
            +
                          under_five = data.select { |record| calculate_age(record['birthdate']) < 5 }\
         | 
| 462 | 
            +
                                           .collect { |record| record['person_id'] }
         | 
| 494 463 |  | 
| 495 | 
            -
                          options[ | 
| 464 | 
            +
                          options['ids'] = under_five
         | 
| 496 465 |  | 
| 497 466 | 
             
                          collection[key] = options
         | 
| 498 467 | 
             
                        end
         | 
| 499 468 |  | 
| 500 | 
            -
                        if key.eql?( | 
| 501 | 
            -
                          data =  | 
| 502 | 
            -
             | 
| 503 | 
            -
             | 
| 504 | 
            -
             | 
| 505 | 
            -
                           | 
| 506 | 
            -
             | 
| 507 | 
            -
                          INNER JOIN person p ON p.person_id = encounter.patient_id').\
         | 
| 508 | 
            -
                          select('encounter.encounter_type, obs.value_coded, p.*')
         | 
| 509 | 
            -
                
         | 
| 510 | 
            -
                          under_five = data.select{|record| calculate_age(record["birthdate"]) < 5 }.\
         | 
| 511 | 
            -
                                        collect{|record| record["person_id"]}
         | 
| 469 | 
            +
                        if key.eql?('Acute respiratory infections - new cases (U5)')
         | 
| 470 | 
            +
                          data = fetch_encounter_data(start_date.to_date.strftime('%Y-%m-%d 00:00:00'),
         | 
| 471 | 
            +
                                                      end_date.to_date.strftime('%Y-%m-%d 23:59:59'),
         | 
| 472 | 
            +
                                                      type.id,
         | 
| 473 | 
            +
                                                      concept_ids)
         | 
| 474 | 
            +
                          under_five = data.select { |record| calculate_age(record['birthdate']) < 5 }\
         | 
| 475 | 
            +
                                           .collect { |record| record['person_id'] }
         | 
| 512 476 |  | 
| 513 | 
            -
                          options[ | 
| 477 | 
            +
                          options['ids'] = under_five
         | 
| 514 478 |  | 
| 515 479 | 
             
                          collection[key] = options
         | 
| 516 480 | 
             
                        end
         | 
| @@ -521,96 +485,96 @@ module EmrOhspInterface | |
| 521 485 | 
             
                  end
         | 
| 522 486 |  | 
| 523 487 | 
             
                  def disaggregate(disaggregate_key, concept_ids, start_date, end_date, type)
         | 
| 524 | 
            -
                    options = { | 
| 488 | 
            +
                    options = {'ids'=>nil}
         | 
| 525 489 | 
             
                    data = Encounter.where('encounter_datetime BETWEEN ? AND ?
         | 
| 526 490 | 
             
                    AND encounter_type = ? AND value_coded IN (?)
         | 
| 527 491 | 
             
                    AND concept_id IN(6543, 6542)',
         | 
| 528 | 
            -
             | 
| 529 | 
            -
             | 
| 530 | 
            -
             | 
| 531 | 
            -
                    INNER JOIN person p ON p.person_id = encounter.patient_id') | 
| 532 | 
            -
             | 
| 533 | 
            -
             | 
| 534 | 
            -
                    if disaggregate_key ==  | 
| 535 | 
            -
                    options[ | 
| 536 | 
            -
             | 
| 492 | 
            +
                                           start_date.to_date.strftime('%Y-%m-%d 00:00:00'),
         | 
| 493 | 
            +
                                           end_date.to_date.strftime('%Y-%m-%d 23:59:59'), type.id, concept_ids)\
         | 
| 494 | 
            +
                                    .joins('INNER JOIN obs ON obs.encounter_id = encounter.encounter_id
         | 
| 495 | 
            +
                    INNER JOIN person p ON p.person_id = encounter.patient_id')\
         | 
| 496 | 
            +
                                    .select('encounter.encounter_type, obs.value_coded, p.*')
         | 
| 497 | 
            +
             | 
| 498 | 
            +
                    if disaggregate_key == 'less'
         | 
| 499 | 
            +
                    options['ids'] = data.select { |record| calculate_age(record['birthdate']) < 5 }\
         | 
| 500 | 
            +
                                         .collect { |record| record['person_id'] }
         | 
| 537 501 | 
             
                    else 
         | 
| 538 | 
            -
                      if disaggregate_key ==  | 
| 539 | 
            -
                        options[ | 
| 540 | 
            -
             | 
| 502 | 
            +
                      if disaggregate_key == 'greater'
         | 
| 503 | 
            +
                        options['ids'] = data.select { |record| calculate_age(record['birthdate']) >= 5 }\
         | 
| 504 | 
            +
                                             .collect { |record| record['person_id'] }
         | 
| 541 505 | 
             
                      end
         | 
| 542 506 | 
             
                    end
         | 
| 543 507 |  | 
| 544 508 | 
             
                    options
         | 
| 545 509 | 
             
                  end
         | 
| 546 510 |  | 
| 547 | 
            -
                  def generate_hmis_17_report(start_date=nil,end_date=nil)
         | 
| 511 | 
            +
                  def generate_hmis_17_report(start_date=nil, end_date=nil)
         | 
| 548 512 |  | 
| 549 | 
            -
                    diag_map = settings[ | 
| 513 | 
            +
                    diag_map = settings['hmis_17_map']
         | 
| 550 514 |  | 
| 551 | 
            -
                    #pull the data
         | 
| 515 | 
            +
                    # pull the data
         | 
| 552 516 | 
             
                    type = EncounterType.find_by_name 'Outpatient diagnosis'
         | 
| 553 517 | 
             
                    collection = {}
         | 
| 554 518 |  | 
| 555 519 | 
             
                    special_indicators = [
         | 
| 556 | 
            -
                       | 
| 557 | 
            -
                       | 
| 558 | 
            -
                       | 
| 559 | 
            -
                       | 
| 560 | 
            -
                       | 
| 520 | 
            +
                      'Referals from other institutions',
         | 
| 521 | 
            +
                      'OPD total attendance',
         | 
| 522 | 
            +
                      'Referal to other institutions',
         | 
| 523 | 
            +
                      'Malaria 5 years and older - new',
         | 
| 524 | 
            +
                      'HIV/AIDS - new'
         | 
| 561 525 | 
             
                    ]
         | 
| 562 526 |  | 
| 563 527 | 
             
                    special_under_five_indicators = [
         | 
| 564 | 
            -
                       | 
| 565 | 
            -
                       | 
| 566 | 
            -
                       | 
| 567 | 
            -
                       | 
| 568 | 
            -
                       | 
| 528 | 
            +
                      'Measles under five years - new',
         | 
| 529 | 
            +
                      'Pneumonia under 5 years- new',
         | 
| 530 | 
            +
                      'Dysentery under 5 years - new',
         | 
| 531 | 
            +
                      'Diarrhoea non - bloody -new cases (under5)',
         | 
| 532 | 
            +
                      'Malaria under 5 years - new'
         | 
| 569 533 | 
             
                    ]
         | 
| 570 534 |  | 
| 571 | 
            -
                    diag_map.each do |key,value|
         | 
| 572 | 
            -
                      options = { | 
| 573 | 
            -
                      concept_ids = ConceptName.where(name: value).collect{|cn| cn.concept_id}
         | 
| 535 | 
            +
                    diag_map.each do |key, value|
         | 
| 536 | 
            +
                      options = {'ids'=>nil}
         | 
| 537 | 
            +
                      concept_ids = ConceptName.where(name: value).collect { |cn| cn.concept_id }
         | 
| 574 538 |  | 
| 575 539 | 
             
                      if !special_indicators.include?(key) && !special_under_five_indicators.include?(key)
         | 
| 576 540 | 
             
                        data = Encounter.where('encounter_datetime BETWEEN ? AND ?
         | 
| 577 541 | 
             
                        AND encounter_type = ? AND value_coded IN (?)
         | 
| 578 542 | 
             
                        AND concept_id IN(6543, 6542)',
         | 
| 579 | 
            -
             | 
| 580 | 
            -
             | 
| 581 | 
            -
             | 
| 582 | 
            -
                        INNER JOIN person p ON p.person_id = encounter.patient_id') | 
| 583 | 
            -
             | 
| 543 | 
            +
                                               start_date.to_date.strftime('%Y-%m-%d 00:00:00'),
         | 
| 544 | 
            +
                                               end_date.to_date.strftime('%Y-%m-%d 23:59:59'), type.id, concept_ids)\
         | 
| 545 | 
            +
                                        .joins('INNER JOIN obs ON obs.encounter_id = encounter.encounter_id
         | 
| 546 | 
            +
                        INNER JOIN person p ON p.person_id = encounter.patient_id')\
         | 
| 547 | 
            +
                                        .select('encounter.encounter_type, obs.value_coded, p.*')
         | 
| 584 548 |  | 
| 585 | 
            -
                        all = data.collect{|record| record.person_id}
         | 
| 549 | 
            +
                        all = data.collect { |record| record.person_id }
         | 
| 586 550 |  | 
| 587 551 |  | 
| 588 | 
            -
                        options[ | 
| 552 | 
            +
                        options['ids'] = all
         | 
| 589 553 |  | 
| 590 554 | 
             
                        collection[key] = options
         | 
| 591 555 | 
             
                      else
         | 
| 592 | 
            -
                        if key.eql?( | 
| 556 | 
            +
                        if key.eql?('Referals from other institutions') 
         | 
| 593 557 | 
             
                          _type = EncounterType.find_by_name 'PATIENT REGISTRATION'
         | 
| 594 558 | 
             
                          visit_type = ConceptName.find_by_name 'Type of visit'
         | 
| 595 559 |  | 
| 596 560 | 
             
                          data = Encounter.where('encounter_datetime BETWEEN ? AND ?
         | 
| 597 561 | 
             
                          AND encounter_type = ? AND value_coded IS NOT NULL
         | 
| 598 562 | 
             
                          AND obs.concept_id = ?', start_date.to_date.strftime('%Y-%m-%d 00:00:00'),
         | 
| 599 | 
            -
             | 
| 600 | 
            -
             | 
| 563 | 
            +
                                                 end_date.to_date.strftime('%Y-%m-%d 23:59:59'), _type.id, visit_type.concept_id)\
         | 
| 564 | 
            +
                                          .joins('INNER JOIN obs ON obs.encounter_id = encounter.encounter_id
         | 
| 601 565 | 
             
                          INNER JOIN person p ON p.person_id = encounter.patient_id
         | 
| 602 | 
            -
                          INNER JOIN concept_name c ON c.concept_id = 6541') | 
| 603 | 
            -
             | 
| 604 | 
            -
             | 
| 566 | 
            +
                          INNER JOIN concept_name c ON c.concept_id = 6541')\
         | 
| 567 | 
            +
                                          .select('encounter.encounter_type, obs.value_coded, obs.obs_datetime, p.*, c.name visit_type')\
         | 
| 568 | 
            +
                                          .group('p.person_id, encounter.encounter_id')
         | 
| 605 569 |  | 
| 606 | 
            -
                          all = data.collect{|record| record.person_id}
         | 
| 570 | 
            +
                          all = data.collect { |record| record.person_id }
         | 
| 607 571 |  | 
| 608 | 
            -
                          options[ | 
| 572 | 
            +
                          options['ids'] = all
         | 
| 609 573 |  | 
| 610 574 | 
             
                          collection[key] = options
         | 
| 611 575 | 
             
                        end
         | 
| 612 576 |  | 
| 613 | 
            -
                        if key.eql?( | 
| 577 | 
            +
                        if key.eql?('OPD total attendance')
         | 
| 614 578 | 
             
                          programID = Program.find_by_name 'OPD Program'
         | 
| 615 579 | 
             
                          data = Encounter.find_by_sql(
         | 
| 616 580 | 
             
                            "SELECT patient_id, DATE_FORMAT(encounter_datetime,'%Y-%m-%d') enc_date
         | 
| @@ -620,55 +584,55 @@ module EmrOhspInterface | |
| 620 584 | 
             
                              AND '" + end_date.to_date.strftime('%Y-%m-%d 23:59:59') + "'
         | 
| 621 585 | 
             
                              AND program_id ='" + programID.program_id.to_s + "'
         | 
| 622 586 | 
             
                            GROUP BY enc_date"
         | 
| 623 | 
            -
                          ).map{|e| e. patient_id}
         | 
| 587 | 
            +
                          ).map { |e| e. patient_id }
         | 
| 624 588 |  | 
| 625 | 
            -
                          options[ | 
| 589 | 
            +
                          options['ids'] = data
         | 
| 626 590 | 
             
                          collection[key] = options
         | 
| 627 591 | 
             
                        end
         | 
| 628 592 |  | 
| 629 | 
            -
                        if key.eql?( | 
| 593 | 
            +
                        if key.eql?('Referal to other institutions')
         | 
| 630 594 | 
             
                          data = Observation.where("obs_datetime BETWEEN ? AND ?
         | 
| 631 | 
            -
                          AND concept_id = ?",start_date.to_date.strftime('%Y-%m-%d 00:00:00'),
         | 
| 632 | 
            -
             | 
| 633 | 
            -
             | 
| 634 | 
            -
             | 
| 635 | 
            -
                          all = data.collect{|record| record.person_id}
         | 
| 636 | 
            -
                          options[ | 
| 595 | 
            +
                          AND concept_id = ?", start_date.to_date.strftime('%Y-%m-%d 00:00:00'),
         | 
| 596 | 
            +
                                                   end_date.to_date.strftime('%Y-%m-%d 23:59:59'), '7414')\
         | 
| 597 | 
            +
                                            .joins('LEFT JOIN location l ON l.location_id = obs.value_text')\
         | 
| 598 | 
            +
                                            .select('obs.person_id').order('obs_datetime DESC')
         | 
| 599 | 
            +
                          all = data.collect { |record| record.person_id }
         | 
| 600 | 
            +
                          options['ids'] = all
         | 
| 637 601 | 
             
                          collection[key] = options
         | 
| 638 602 | 
             
                        end
         | 
| 639 603 |  | 
| 640 | 
            -
                        if key.eql?( | 
| 641 | 
            -
                          data = | 
| 604 | 
            +
                        if key.eql?('HIV/AIDS - new')
         | 
| 605 | 
            +
                          data = ActiveRecord::Base.connection.select_all(
         | 
| 642 606 | 
             
                            "SELECT * FROM temp_earliest_start_date
         | 
| 643 607 | 
             
                            WHERE date_enrolled BETWEEN '#{start_date}' AND '#{end_date}'
         | 
| 644 608 | 
             
                            AND date_enrolled = earliest_start_date
         | 
| 645 | 
            -
                            GROUP BY patient_id" )
         | 
| 646 | 
            -
                          all = data.collect{|record| record[ | 
| 647 | 
            -
                          options[ | 
| 609 | 
            +
                            GROUP BY patient_id" ).to_hash
         | 
| 610 | 
            +
                          all = data.collect { |record| record['patient_id'] }
         | 
| 611 | 
            +
                          options['ids'] = all
         | 
| 648 612 | 
             
                          collection[key] = options
         | 
| 649 613 | 
             
                        end
         | 
| 650 614 |  | 
| 651 | 
            -
                        if key.eql?( | 
| 652 | 
            -
                          collection[key] = disaggregate('less',concept_ids, start_date, end_date, type)
         | 
| 615 | 
            +
                        if key.eql?('Measles under five years - new')
         | 
| 616 | 
            +
                          collection[key] = disaggregate('less', concept_ids, start_date, end_date, type)
         | 
| 653 617 | 
             
                        end
         | 
| 654 618 |  | 
| 655 | 
            -
                        if key.eql?( | 
| 619 | 
            +
                        if key.eql?('Pneumonia under 5 years- new')
         | 
| 656 620 | 
             
                          collection[key] = disaggregate('less', concept_ids, start_date, end_date, type)
         | 
| 657 621 | 
             
                        end
         | 
| 658 622 |  | 
| 659 | 
            -
                        if key.eql?( | 
| 623 | 
            +
                        if key.eql?('Malaria under 5 years - new')
         | 
| 660 624 | 
             
                          collection[key] = disaggregate('less', concept_ids, start_date, end_date, type)
         | 
| 661 625 | 
             
                        end
         | 
| 662 626 |  | 
| 663 | 
            -
                        if key.eql?( | 
| 664 | 
            -
                          collection[key] = disaggregate('greater',concept_ids, start_date, end_date, type)
         | 
| 627 | 
            +
                        if key.eql?('Malaria 5 years and older - new')
         | 
| 628 | 
            +
                          collection[key] = disaggregate('greater', concept_ids, start_date, end_date, type)
         | 
| 665 629 | 
             
                        end
         | 
| 666 630 |  | 
| 667 | 
            -
                        if key.eql?( | 
| 631 | 
            +
                        if key.eql?('Dysentery under 5 years - new')
         | 
| 668 632 | 
             
                          collection[key] = disaggregate('less', concept_ids, start_date, end_date, type)
         | 
| 669 633 | 
             
                        end
         | 
| 670 634 |  | 
| 671 | 
            -
                        if key.eql?( | 
| 635 | 
            +
                        if key.eql?('Diarrhoea non - bloody -new cases (under5)')
         | 
| 672 636 | 
             
                          collection[key] = disaggregate('less', concept_ids, start_date, end_date, type)
         | 
| 673 637 | 
             
                        end
         | 
| 674 638 |  | 
| @@ -679,38 +643,38 @@ module EmrOhspInterface | |
| 679 643 |  | 
| 680 644 | 
             
                  end
         | 
| 681 645 |  | 
| 682 | 
            -
                  def generate_notifiable_disease_conditions_report(start_date=nil,end_date=nil)
         | 
| 683 | 
            -
                    diag_map = settings[ | 
| 646 | 
            +
                  def generate_notifiable_disease_conditions_report(start_date=nil, end_date=nil)
         | 
| 647 | 
            +
                    diag_map = settings['notifiable_disease_conditions']
         | 
| 684 648 |  | 
| 685 | 
            -
                    start_date = Date.today.strftime( | 
| 686 | 
            -
                    end_date = Date.today.strftime( | 
| 649 | 
            +
                    start_date = Date.today.strftime('%Y-%m-%d') if start_date.nil?
         | 
| 650 | 
            +
                    end_date = Date.today.strftime('%Y-%m-%d') if end_date.nil?
         | 
| 687 651 |  | 
| 688 652 | 
             
                    type = EncounterType.find_by_name 'Outpatient diagnosis'
         | 
| 689 653 | 
             
                    collection = {}
         | 
| 690 654 | 
             
                    concept_name_for_sms_portal = {}
         | 
| 691 655 |  | 
| 692 | 
            -
                    diag_map.each do |key,value|
         | 
| 693 | 
            -
                      options = { | 
| 694 | 
            -
                      concept_ids = ConceptName.where(name: value).collect{|cn| cn.concept_id}
         | 
| 656 | 
            +
                    diag_map.each do |key, value|
         | 
| 657 | 
            +
                      options = {'<5yrs'=>nil, '>=5yrs'=>nil}
         | 
| 658 | 
            +
                      concept_ids = ConceptName.where(name: value).collect { |cn| cn.concept_id }
         | 
| 695 659 |  | 
| 696 660 | 
             
                      data = Encounter.where('encounter_datetime BETWEEN ? AND ?
         | 
| 697 661 | 
             
                      AND encounter_type = ? AND value_coded IN (?)
         | 
| 698 662 | 
             
                      AND concept_id IN(6543, 6542)',
         | 
| 699 | 
            -
             | 
| 700 | 
            -
             | 
| 701 | 
            -
             | 
| 702 | 
            -
                      INNER JOIN person p ON p.person_id = encounter.patient_id') | 
| 703 | 
            -
             | 
| 704 | 
            -
             | 
| 705 | 
            -
                      #under_five
         | 
| 706 | 
            -
                      under_five = data.select{|record| calculate_age(record[ | 
| 707 | 
            -
             | 
| 708 | 
            -
                      options[ | 
| 709 | 
            -
                      #above 5 years
         | 
| 710 | 
            -
                      over_five = data.select{|record| calculate_age(record[ | 
| 711 | 
            -
             | 
| 712 | 
            -
             | 
| 713 | 
            -
                      options[ | 
| 663 | 
            +
                                             start_date.to_date.strftime('%Y-%m-%d 00:00:00'),
         | 
| 664 | 
            +
                                             end_date.to_date.strftime('%Y-%m-%d 23:59:59'), type.id, concept_ids)\
         | 
| 665 | 
            +
                                      .joins('INNER JOIN obs ON obs.encounter_id = encounter.encounter_id
         | 
| 666 | 
            +
                      INNER JOIN person p ON p.person_id = encounter.patient_id')\
         | 
| 667 | 
            +
                                      .select('encounter.encounter_type, obs.value_coded, p.*')
         | 
| 668 | 
            +
             | 
| 669 | 
            +
                      # under_five
         | 
| 670 | 
            +
                      under_five = data.select { |record| calculate_age(record['birthdate']) < 5 }\
         | 
| 671 | 
            +
                                       .collect { |record| record.person_id }
         | 
| 672 | 
            +
                      options['<5yrs'] = under_five
         | 
| 673 | 
            +
                      # above 5 years
         | 
| 674 | 
            +
                      over_five = data.select { |record| calculate_age(record['birthdate']) >=5 }\
         | 
| 675 | 
            +
                                      .collect { |record| record.person_id }
         | 
| 676 | 
            +
             | 
| 677 | 
            +
                      options['>=5yrs'] = over_five
         | 
| 714 678 |  | 
| 715 679 | 
             
                      collection[key] = options
         | 
| 716 680 |  | 
| @@ -727,9 +691,9 @@ module EmrOhspInterface | |
| 727 691 | 
             
                      curr_date = Date.today
         | 
| 728 692 | 
             
                      while count < 13 do
         | 
| 729 693 | 
             
                          curr_date = curr_date - 1.month
         | 
| 730 | 
            -
                          months[curr_date.strftime( | 
| 731 | 
            -
             | 
| 732 | 
            -
                          count += | 
| 694 | 
            +
                          months[curr_date.strftime('%Y%m')] = [curr_date.strftime('%B-%Y'),\
         | 
| 695 | 
            +
                                                                (curr_date.beginning_of_month.to_s+' to ' + curr_date.end_of_month.to_s)]
         | 
| 696 | 
            +
                          count += 1
         | 
| 733 697 | 
             
                      end
         | 
| 734 698 | 
             
                      return months.to_a
         | 
| 735 699 | 
             
                  end
         | 
| @@ -744,7 +708,7 @@ module EmrOhspInterface | |
| 744 708 | 
             
                    init_quarter = Date.today.beginning_of_year - 2.years
         | 
| 745 709 |  | 
| 746 710 | 
             
                    while init_quarter <= Date.today do
         | 
| 747 | 
            -
                      quarter = init_quarter.strftime( | 
| 711 | 
            +
                      quarter = init_quarter.strftime('%Y')+' Q'+to_quarter.call(init_quarter).to_s
         | 
| 748 712 | 
             
                      dates = "#{(init_quarter.beginning_of_quarter).to_s} to #{(init_quarter.end_of_quarter).to_s}"
         | 
| 749 713 | 
             
                      quarters[quarter] = dates
         | 
| 750 714 | 
             
                      init_quarter = init_quarter + 3.months
         | 
| @@ -761,90 +725,90 @@ module EmrOhspInterface | |
| 761 725 | 
             
                    wk_of_first_day = first_day.cweek
         | 
| 762 726 |  | 
| 763 727 | 
             
                    if wk_of_first_day > 1
         | 
| 764 | 
            -
                      wk = first_day.prev_year.year.to_s+ | 
| 728 | 
            +
                      wk = first_day.prev_year.year.to_s+'W'+wk_of_first_day.to_s
         | 
| 765 729 | 
             
                      dates = "#{(first_day-first_day.wday+1).to_s} to #{((first_day-first_day.wday+1)+6).to_s}"
         | 
| 766 730 | 
             
                      weeks[wk] = dates
         | 
| 767 731 | 
             
                    end
         | 
| 768 732 |  | 
| 769 | 
            -
                    #get the firt monday of the year
         | 
| 733 | 
            +
                    # get the firt monday of the year
         | 
| 770 734 | 
             
                    while !first_day.monday? do
         | 
| 771 735 | 
             
                      first_day = first_day+1
         | 
| 772 736 | 
             
                    end
         | 
| 773 737 | 
             
                    first_monday = first_day
         | 
| 774 | 
            -
                    #generate week numbers and date ranges
         | 
| 738 | 
            +
                    # generate week numbers and date ranges
         | 
| 775 739 |  | 
| 776 740 | 
             
                    while first_monday <= Date.today do
         | 
| 777 | 
            -
                        wk = (first_monday.year).to_s+ | 
| 778 | 
            -
                        dates = | 
| 779 | 
            -
                        #add to the hash
         | 
| 741 | 
            +
                        wk = (first_monday.year).to_s+'W'+(first_monday.cweek).to_s
         | 
| 742 | 
            +
                        dates = "#{first_monday.to_s} to #{(first_monday+6).to_s}"
         | 
| 743 | 
            +
                        # add to the hash
         | 
| 780 744 | 
             
                        weeks[wk] = dates
         | 
| 781 | 
            -
                        #step by week
         | 
| 745 | 
            +
                        # step by week
         | 
| 782 746 | 
             
                        first_monday += 7
         | 
| 783 747 | 
             
                    end
         | 
| 784 | 
            -
                  #remove the last week
         | 
| 785 | 
            -
                  this_wk = (Date.today.year).to_s+ | 
| 786 | 
            -
                  weeks = weeks.delete_if{|key,value| key==this_wk}
         | 
| 748 | 
            +
                  # remove the last week
         | 
| 749 | 
            +
                  this_wk = (Date.today.year).to_s+'W'+(Date.today.cweek).to_s
         | 
| 750 | 
            +
                  weeks = weeks.delete_if { |key, value| key==this_wk }
         | 
| 787 751 |  | 
| 788 752 | 
             
                  return weeks.to_a
         | 
| 789 753 | 
             
                  end
         | 
| 790 754 |  | 
| 791 | 
            -
                  #Age calculator
         | 
| 755 | 
            +
                  # Age calculator
         | 
| 792 756 | 
             
                  def calculate_age(dob)
         | 
| 793 757 | 
             
                    age = ((Date.today-dob.to_date).to_i)/365 rescue 0
         | 
| 794 758 | 
             
                  end
         | 
| 795 759 |  | 
| 796 | 
            -
                  def send_data(data,type)
         | 
| 760 | 
            +
                  def send_data(data, type)
         | 
| 797 761 | 
             
                    # method used to post data to the server
         | 
| 798 | 
            -
                    #prepare payload here
         | 
| 762 | 
            +
                    # prepare payload here
         | 
| 799 763 | 
             
                    conn = server_config['ohsp']
         | 
| 800 764 | 
             
                    payload = {
         | 
| 801 | 
            -
                       | 
| 802 | 
            -
                       | 
| 803 | 
            -
                       | 
| 804 | 
            -
                       | 
| 765 | 
            +
                      'dataSet' =>get_data_set_id(type),
         | 
| 766 | 
            +
                      'period'=>(type.eql?('weekly') ? weeks_generator.last[0] : months_generator.first[0]),
         | 
| 767 | 
            +
                      'orgUnit'=> get_ohsp_facility_id,
         | 
| 768 | 
            +
                      'dataValues'=> []
         | 
| 805 769 | 
             
                    }
         | 
| 806 | 
            -
                     special = [ | 
| 807 | 
            -
             | 
| 770 | 
            +
                     special = ['Severe Pneumonia in under 5 cases', 'Malaria in Pregnancy',
         | 
| 771 | 
            +
                                'Underweight Newborns < 2500g in Under 5 Cases', 'Diarrhoea In Under 5']
         | 
| 808 772 |  | 
| 809 | 
            -
                    data.each do |key,value|
         | 
| 773 | 
            +
                    data.each do |key, value|
         | 
| 810 774 | 
             
                      if !special.include?(key)
         | 
| 811 | 
            -
                          option1 = | 
| 812 | 
            -
                                       | 
| 813 | 
            -
                                       | 
| 775 | 
            +
                          option1 = {'dataElement'=>get_ohsp_de_ids(key, type)[1],
         | 
| 776 | 
            +
                                      'categoryOptionCombo'=> get_ohsp_de_ids(key, type)[2],
         | 
| 777 | 
            +
                                      'value'=>value['<5yrs'].size } rescue {}
         | 
| 814 778 |  | 
| 815 | 
            -
                          option2 = { | 
| 816 | 
            -
                                       | 
| 817 | 
            -
                                       | 
| 779 | 
            +
                          option2 = {'dataElement'=>get_ohsp_de_ids(key, type)[1],
         | 
| 780 | 
            +
                                      'categoryOptionCombo'=> get_ohsp_de_ids(key, type)[3],
         | 
| 781 | 
            +
                                      'value'=>value['>=5yrs'].size} rescue {}
         | 
| 818 782 |  | 
| 819 | 
            -
                        #fill data values array
         | 
| 820 | 
            -
                          payload[ | 
| 821 | 
            -
                          payload[ | 
| 783 | 
            +
                        # fill data values array
         | 
| 784 | 
            +
                          payload['dataValues'] << option1
         | 
| 785 | 
            +
                          payload['dataValues'] << option2
         | 
| 822 786 | 
             
                      else
         | 
| 823 787 | 
             
                          case key
         | 
| 824 788 | 
             
                            when special[0]
         | 
| 825 | 
            -
                              option1 = | 
| 826 | 
            -
                                           | 
| 827 | 
            -
                                           | 
| 789 | 
            +
                              option1 = {'dataElement'=>get_ohsp_de_ids(key, type)[1],
         | 
| 790 | 
            +
                                          'categoryOptionCombo'=> get_ohsp_de_ids(key, type)[2],
         | 
| 791 | 
            +
                                          'value'=>value['<5yrs'].size } rescue {}
         | 
| 828 792 |  | 
| 829 | 
            -
                              payload[ | 
| 793 | 
            +
                              payload['dataValues'] << option1
         | 
| 830 794 | 
             
                            when special[1]
         | 
| 831 | 
            -
                              option2 = { | 
| 832 | 
            -
                                           | 
| 833 | 
            -
                                           | 
| 795 | 
            +
                              option2 = {'dataElement'=>get_ohsp_de_ids(key, type)[1],
         | 
| 796 | 
            +
                                          'categoryOptionCombo'=> get_ohsp_de_ids(key, type)[3],
         | 
| 797 | 
            +
                                          'value'=>value['>=5yrs'].size } rescue {}
         | 
| 834 798 |  | 
| 835 | 
            -
                              payload[ | 
| 799 | 
            +
                              payload['dataValues'] << option2
         | 
| 836 800 | 
             
                            when special[2]
         | 
| 837 | 
            -
                              option1 = | 
| 838 | 
            -
                                           | 
| 839 | 
            -
                                           | 
| 801 | 
            +
                              option1 = {'dataElement'=>get_ohsp_de_ids(key, type)[1],
         | 
| 802 | 
            +
                                          'categoryOptionCombo'=> get_ohsp_de_ids(key, type)[2],
         | 
| 803 | 
            +
                                          'value'=>value['<5yrs'].size } rescue {}
         | 
| 840 804 |  | 
| 841 | 
            -
                              payload[ | 
| 805 | 
            +
                              payload['dataValues'] << option1
         | 
| 842 806 | 
             
                            when special[3]
         | 
| 843 | 
            -
                              option1 = | 
| 844 | 
            -
                                           | 
| 845 | 
            -
                                           | 
| 807 | 
            +
                              option1 = {'dataElement'=>get_ohsp_de_ids(key, type)[1],
         | 
| 808 | 
            +
                                          'categoryOptionCombo'=> get_ohsp_de_ids(key, type)[2],
         | 
| 809 | 
            +
                                          'value'=>value['<5yrs'].size} rescue {}
         | 
| 846 810 |  | 
| 847 | 
            -
                              payload[ | 
| 811 | 
            +
                              payload['dataValues'] << option1
         | 
| 848 812 | 
             
                          end
         | 
| 849 813 | 
             
                      end
         | 
| 850 814 | 
             
                    end
         | 
| @@ -854,51 +818,47 @@ module EmrOhspInterface | |
| 854 818 | 
             
                    puts url
         | 
| 855 819 | 
             
                    puts "pushing #{type} IDSR Reports"
         | 
| 856 820 | 
             
                    send = RestClient::Request.execute(method: :post,
         | 
| 857 | 
            -
             | 
| 858 | 
            -
             | 
| 859 | 
            -
             | 
| 860 | 
            -
                                                        #headers: {accept: :json},
         | 
| 861 | 
            -
             | 
| 862 | 
            -
             | 
| 821 | 
            +
                                                       url: url,
         | 
| 822 | 
            +
                                                       headers:{'Content-Type'=> 'application/json'},
         | 
| 823 | 
            +
                                                       payload: payload.to_json,
         | 
| 824 | 
            +
                                                        # headers: {accept: :json},
         | 
| 825 | 
            +
                                                       user: conn['username'],
         | 
| 826 | 
            +
                                                       password: conn['password'])
         | 
| 863 827 |  | 
| 864 828 | 
             
                    puts send
         | 
| 865 829 | 
             
                  end
         | 
| 866 830 |  | 
| 867 831 | 
             
                  def send_data_to_sms_portal(data, concept_name_collection)
         | 
| 868 832 | 
             
                    conn2 = server_config['idsr_sms']
         | 
| 869 | 
            -
                    data = data.select {|k,v| v.select {|kk,vv| vv.length > 0}.length > 0}
         | 
| 833 | 
            +
                    data = data.select { |k, v| v.select { |kk, vv| vv.length > 0 }.length > 0 }
         | 
| 870 834 | 
             
                    payload = {
         | 
| 871 | 
            -
                       | 
| 872 | 
            -
                       | 
| 873 | 
            -
                       | 
| 874 | 
            -
                       | 
| 875 | 
            -
                       | 
| 876 | 
            -
                       | 
| 835 | 
            +
                      'email'=> conn2['username'],
         | 
| 836 | 
            +
                      'password' => conn2['password'],
         | 
| 837 | 
            +
                      'emr_facility_id' => Location.current_health_center.id,
         | 
| 838 | 
            +
                      'emr_facility_name' => Location.current_health_center.name,
         | 
| 839 | 
            +
                      'payload' => data,
         | 
| 840 | 
            +
                      'concept_name_collection' => concept_name_collection
         | 
| 877 841 | 
             
                    }
         | 
| 878 842 |  | 
| 879 843 |  | 
| 880 844 |  | 
| 881 845 | 
             
                    begin
         | 
| 882 846 | 
             
                      response = RestClient::Request.execute(method: :post,
         | 
| 883 | 
            -
             | 
| 884 | 
            -
             | 
| 885 | 
            -
             | 
| 847 | 
            +
                                                             url: conn2['url'],
         | 
| 848 | 
            +
                                                             headers:{'Content-Type'=> 'application/json'},
         | 
| 849 | 
            +
                                                             payload: payload.to_json
         | 
| 886 850 | 
             
                      )
         | 
| 887 851 | 
             
                    rescue RestClient::ExceptionWithResponse => res
         | 
| 888 | 
            -
                      if res.class == RestClient::Forbidden
         | 
| 889 | 
            -
                        puts "error: #{res.class}"
         | 
| 890 | 
            -
                      end
         | 
| 852 | 
            +
                      puts "error: #{res.class}" if res.class == RestClient::Forbidden
         | 
| 891 853 | 
             
                    end
         | 
| 892 854 |  | 
| 893 855 | 
             
                    if response.class != NilClass
         | 
| 894 | 
            -
                      if response.code == 200
         | 
| 895 | 
            -
                        puts "success: #{response}"
         | 
| 896 | 
            -
                      end
         | 
| 856 | 
            +
                      puts "success: #{response}" if response.code == 200
         | 
| 897 857 | 
             
                    end
         | 
| 898 858 |  | 
| 899 | 
            -
             | 
| 859 | 
            +
                  end
         | 
| 900 860 |  | 
| 901 861 | 
             
                end
         | 
| 902 862 | 
             
              end
         | 
| 903 863 |  | 
| 904 | 
            -
            end
         | 
| 864 | 
            +
            end
         |