Module: EmrOhspInterface::EmrOhspInterfaceService

Defined in:
app/services/emr_ohsp_interface/emr_ohsp_interface_service.rb

Class Method Summary collapse

Class Method Details

.calculate_age(dob) ⇒ Object

Age calculator



610
611
612
# File 'app/services/emr_ohsp_interface/emr_ohsp_interface_service.rb', line 610

def calculate_age(dob)
  age = ((Date.today-dob.to_date).to_i)/365 rescue 0
end

.disaggregate(disaggregate_key, concept_ids, start_date, end_date, type) ⇒ Object



421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
# File 'app/services/emr_ohsp_interface/emr_ohsp_interface_service.rb', line 421

def disaggregate(disaggregate_key, concept_ids, start_date, end_date, type)
  options = {"ids"=>nil}
  data = Encounter.where('encounter_datetime BETWEEN ? AND ?
  AND encounter_type = ? AND value_coded IN (?)
  AND concept_id IN(6543, 6542)',
  start_date.to_date.strftime('%Y-%m-%d 00:00:00'),
  end_date.to_date.strftime('%Y-%m-%d 23:59:59'),type.id,concept_ids).\
  joins('INNER JOIN obs ON obs.encounter_id = encounter.encounter_id
  INNER JOIN person p ON p.person_id = encounter.patient_id').\
  select('encounter.encounter_type, obs.value_coded, p.*')

  if disaggregate_key == "less"
  options["ids"] = data.select{|record| calculate_age(record["birthdate"]) < 5 }.\
  collect{|record| record["person_id"]}
  else 
    if disaggregate_key == "greater"
      options["ids"] = data.select{|record| calculate_age(record["birthdate"]) >= 5 }.\
      collect{|record| record["person_id"]}
    end
  end

  options
end

.generate_hmis_15_report(start_date = nil, end_date = nil) ⇒ Object



264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
# File 'app/services/emr_ohsp_interface/emr_ohsp_interface_service.rb', line 264

def generate_hmis_15_report(start_date=nil,end_date=nil)

  diag_map = settings["hmis_15_map"]
    
  #pull the data
  type = EncounterType.find_by_name 'Outpatient diagnosis'
  collection = {}
    
  special_indicators = ["Malaria - new cases (under 5)",
    "Malaria - new cases (5 & over)",
    "HIV confirmed positive (15-49 years) new cases",
    "Diarrhoea non - bloody -new cases (under5)",
    "Malnutrition - new case (under 5)",
    "Acute respiratory infections - new cases (U5)"
  ]
    
  diag_map.each do |key,value|
    options = {"ids"=>nil}
    concept_ids = ConceptName.where(name: value).collect{|cn| cn.concept_id}
    
    if !special_indicators.include?(key)
      data = Encounter.where('encounter_datetime BETWEEN ? AND ?
      AND encounter_type = ? AND value_coded IN (?)
      AND concept_id IN(6543, 6542)',
      start_date.to_date.strftime('%Y-%m-%d 00:00:00'),
      end_date.to_date.strftime('%Y-%m-%d 23:59:59'),type.id,concept_ids).\
      joins('INNER JOIN obs ON obs.encounter_id = encounter.encounter_id
      INNER JOIN person p ON p.person_id = encounter.patient_id').\
      select('encounter.encounter_type, obs.value_coded, p.*')
    
      # #under_five
      # under_five = data.select{|record| calculate_age(record["birthdate"]) < 5}.\
      #             collect{|record| record.person_id}
      # options["<5yrs"] = under_five
      # #above 5 years
      # over_five = data.select{|record| calculate_age(record["birthdate"]) >=5 }.\
      #             collect{|record| record.person_id}
    
      # options[">=5yrs"] =  over_five
    
      all = data.collect{|record| record.person_id}
    
    
      options["ids"] = all
    
      collection[key] = options
    else
      if key.eql?("Malaria - new cases (under 5)")
        data = Encounter.where('encounter_datetime BETWEEN ? AND ?
        AND encounter_type = ? AND value_coded IN (?)
        AND concept_id IN(6543, 6542)',
        start_date.to_date.strftime('%Y-%m-%d 00:00:00'),
        end_date.to_date.strftime('%Y-%m-%d 23:59:59'),type.id,concept_ids).\
        joins('INNER JOIN obs ON obs.encounter_id = encounter.encounter_id
        INNER JOIN person p ON p.person_id = encounter.patient_id').\
        select('encounter.encounter_type, obs.value_coded, p.*')
    
        under_five = data.select{|record| calculate_age(record["birthdate"]) < 5 }.\
                      collect{|record| record["person_id"]}
    
        options["ids"] = under_five
    
        collection[key] = options
      end
    
      if key.eql?("Malaria - new cases (5 & over)")
        data = Encounter.where('encounter_datetime BETWEEN ? AND ?
        AND encounter_type = ? AND value_coded IN (?)
        AND concept_id IN(6543, 6542)',
        start_date.to_date.strftime('%Y-%m-%d 00:00:00'),
        end_date.to_date.strftime('%Y-%m-%d 23:59:59'),type.id,concept_ids).\
        joins('INNER JOIN obs ON obs.encounter_id = encounter.encounter_id
        INNER JOIN person p ON p.person_id = encounter.patient_id').\
        select('encounter.encounter_type, obs.value_coded, p.*')
    
        over_and_five = data.select{|record| calculate_age(record["birthdate"])  >= 5 }.\
                      collect{|record| record["person_id"]}
    
        options["ids"] = over_and_five
    
        collection[key] = options
      end
    
      if key.eql?("HIV confirmed positive (15-49 years) new cases")
        data =  ActiveRecord::Base.connection.select_all(
          "SELECT * FROM temp_earliest_start_date
            WHERE date_enrolled BETWEEN '#{start_date}' AND '#{end_date}'
            AND date_enrolled = earliest_start_date
            GROUP BY patient_id" )
    
        over_and_15_49 = data.select{|record| calculate_age(record["birthdate"])  >= 15 && calculate_age(record["birthdate"]) <=49 }.\
               collect{|record| record["patient_id"]}
    
        options["ids"] = over_and_15_49
    
        collection[key] = options
      end

      if key.eql?("Diarrhoea non - bloody -new cases (under5)")
        data = Encounter.where('encounter_datetime BETWEEN ? AND ?
        AND encounter_type = ? AND value_coded IN (?)
        AND concept_id IN(6543, 6542)',
        start_date.to_date.strftime('%Y-%m-%d 00:00:00'),
        end_date.to_date.strftime('%Y-%m-%d 23:59:59'),type.id,concept_ids).\
        joins('INNER JOIN obs ON obs.encounter_id = encounter.encounter_id
        INNER JOIN person p ON p.person_id = encounter.patient_id').\
        select('encounter.encounter_type, obs.value_coded, p.*')
    
        under_five = data.select{|record| calculate_age(record["birthdate"]) < 5 }.\
                      collect{|record| record["person_id"]}
    
        options["ids"] = under_five

        collection[key] = options
      end

      if key.eql?("Malnutrition - new case (under 5)")
        data = Encounter.where('encounter_datetime BETWEEN ? AND ?
        AND encounter_type = ? AND value_coded IN (?)
        AND concept_id IN(6543, 6542)',
        start_date.to_date.strftime('%Y-%m-%d 00:00:00'),
        end_date.to_date.strftime('%Y-%m-%d 23:59:59'),type.id,concept_ids).\
        joins('INNER JOIN obs ON obs.encounter_id = encounter.encounter_id
        INNER JOIN person p ON p.person_id = encounter.patient_id').\
        select('encounter.encounter_type, obs.value_coded, p.*')
    
        under_five = data.select{|record| calculate_age(record["birthdate"]) < 5 }.\
                      collect{|record| record["person_id"]}
    
        options["ids"] = under_five

        collection[key] = options
      end

      if key.eql?("Acute respiratory infections - new cases (U5)")
        data = Encounter.where('encounter_datetime BETWEEN ? AND ?
        AND encounter_type = ? AND value_coded IN (?)
        AND concept_id IN(6543, 6542)',
        start_date.to_date.strftime('%Y-%m-%d 00:00:00'),
        end_date.to_date.strftime('%Y-%m-%d 23:59:59'),type.id,concept_ids).\
        joins('INNER JOIN obs ON obs.encounter_id = encounter.encounter_id
        INNER JOIN person p ON p.person_id = encounter.patient_id').\
        select('encounter.encounter_type, obs.value_coded, p.*')
    
        under_five = data.select{|record| calculate_age(record["birthdate"]) < 5 }.\
                      collect{|record| record["person_id"]}
    
        options["ids"] = under_five

        collection[key] = options
      end

  end
  end
   collection
end

.generate_hmis_17_report(start_date = nil, end_date = nil) ⇒ Object



445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
# File 'app/services/emr_ohsp_interface/emr_ohsp_interface_service.rb', line 445

def generate_hmis_17_report(start_date=nil,end_date=nil)
  diag_map = settings["hmis_17_map"]
  collection = {}
  special_indicators = [
    "Referals from other institutions",
    "OPD total attendance",
    "Referal to other institutions",
    "Malaria 5 years and older - new",
    "HIV/AIDS - new"
  ]
  special_under_five_indicators = [
    "Measles under five years - new",
    "Pneumonia under 5 years- new",
    "Dysentery under 5 years - new",
    "Diarrhoea non - bloody -new cases (under5)",
    "Malaria under 5 years - new",
    "Acute respiratory infections U5 - new"
  ]

  
  reg_data = registration_report(start_date,end_date)
 
  data =Observation.where(Arel.sql("obs_datetime BETWEEN '#{start_date.to_date.strftime('%Y-%m-%d 00:00:00')}' AND '#{end_date.to_date.strftime('%Y-%m-%d 23:59:59')}' AND c.voided = 0 AND obs.concept_id IN (6543, 6542) "))\
                    .joins(Arel.sql('INNER JOIN concept_name c ON c.concept_id = obs.value_coded
    INNER JOIN person p ON p.person_id = obs.person_id'))\
                    .pluck(Arel.sql("c.name, CASE WHEN  (SELECT timestampdiff(year, birthdate, '#{end_date.to_date.strftime('%Y-%m-%d')}')) >= 5 THEN 'more_than_5'
    ELSE 'less_than_5' END AS age_group,p.person_id")).group_by(&:shift)


    diag_map.each do |key, value|
      collection[key] = { "ids" => [] }
      if key.eql?("OPD total attendance")
        collection[key]["ids"] = reg_data.map { |item| item[1] }
      else
        if key.eql?("Referals from other institutions")
          reg_data = reg_data.rows.group_by(&:shift)
          collection[key]["ids"]= reg_data['Referral'] ? reg_data['Referral'].flatten : []
        end
      end
      data.each do |phrase, counts|
        next unless value.include?(phrase)
      
        counts.each do |label, count|
          if !key.eql?("Malaria 5 years and older - new") && !special_under_five_indicators.include?(key)
            collection[key]["ids"] << count
          else
            if ((special_under_five_indicators.include?(key) && label.eql?("less_than_5")) ||
                (key.eql?("Malaria 5 years and older - new") && label.eql?("more_than_5")))
              collection[key]["ids"] << count
            end
          end
        end
      end
    end
    collection
end

.generate_monthly_idsr_report(request = nil, start_date = nil, end_date = nil) ⇒ Object

idsr monthly report



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
# File 'app/services/emr_ohsp_interface/emr_ohsp_interface_service.rb', line 106

def generate_monthly_idsr_report(request=nil,start_date=nil,end_date=nil)
  diag_map = settings["monthly_idsr_map"]
  epi_month = months_generator.first.first.strip
  start_date = months_generator.first.last[1].split("to").first.strip if start_date.nil?
  end_date =  months_generator.first.last[1].split("to").last.strip if end_date.nil?
  type = EncounterType.find_by_name 'Outpatient diagnosis'
  collection = {}

  special_indicators = ["Malaria in Pregnancy",
                        "HIV New Initiated on ART",
                        "Diarrhoea In Under 5",
                        "Malnutrition In Under 5",
                        "Underweight Newborns < 2500g in Under 5 Cases",
                        "Severe Pneumonia in under 5 cases"]

  diag_map.each do |key,value|
    options = {"<5yrs"=>nil,">=5yrs"=>nil}
    concept_ids = ConceptName.where(name: value).collect{|cn| cn.concept_id}
    if !special_indicators.include?(key)
        data = Encounter.where('encounter_datetime BETWEEN ? AND ?
        AND encounter_type = ? AND value_coded IN (?)
        AND concept_id IN(6543, 6542)',
        start_date.to_date.strftime('%Y-%m-%d 00:00:00'),
        end_date.to_date.strftime('%Y-%m-%d 23:59:59'),type.id,concept_ids).\
        joins('INNER JOIN obs ON obs.encounter_id = encounter.encounter_id
        INNER JOIN person p ON p.person_id = encounter.patient_id').\
        select('encounter.encounter_type, obs.value_coded, p.*')

        #under_five
        under_five = data.select{|record| calculate_age(record["birthdate"]) < 5}.\
                    collect{|record| record.person_id}.uniq
        options["<5yrs"] = under_five
        #above 5 years
        over_five = data.select{|record| calculate_age(record["birthdate"]) >=5 }.\
                    collect{|record| record.person_id}.uniq

        options[">=5yrs"] =  over_five

        collection[key] = options
    else
      if key.eql?("Malaria in Pregnancy")
        mal_patient_id = Encounter.where('encounter_datetime BETWEEN ? AND ?
        AND encounter_type = ? AND value_coded IN (?)
        AND concept_id IN(6543, 6542)',
        start_date.to_date.strftime('%Y-%m-%d 00:00:00'),
        end_date.to_date.strftime('%Y-%m-%d 23:59:59'),type.id,concept_ids).\
        joins('INNER JOIN obs ON obs.encounter_id = encounter.encounter_id
        INNER JOIN person p ON p.person_id = encounter.patient_id').\
        select('encounter.encounter_type, obs.value_coded, p.*')

        mal_patient_id=   mal_patient_id.collect{|record| record.person_id}
        #find those that are pregnant
        preg = Observation.where(["concept_id = 6131 AND obs_datetime
                                   BETWEEN ? AND ? AND person_id IN(?)
                                    AND value_coded =1065",
                                    start_date.to_date.strftime('%Y-%m-%d 00:00:00'),
                                    end_date.to_date.strftime('%Y-%m-%d 23:59:59'),mal_patient_id ])

         options[">=5yrs"] =   preg.collect{|record| record.person_id} rescue 0
         collection[key] = options
      end

      if key.eql?("HIV New Initiated on ART")
       data =  ActiveRecord::Base.connection.select_all(
                  "SELECT * FROM temp_earliest_start_date
                      WHERE date_enrolled BETWEEN '#{start_date}' AND '#{end_date}'
                      AND date_enrolled = earliest_start_date
                       GROUP BY patient_id" )

        under_five = data.select{|record| calculate_age(record["birthdate"]) < 5 }.\
                       collect{|record| record["patient_id"]}

        over_five = data.select{|record| calculate_age(record["birthdate"]) >=5 }.\
                       collect{|record| record["patient_id"]}

        options["<5yrs"] = under_five
        options[">=5yrs"] =  over_five

        collection[key] = options
      end

      if key.eql?("Diarrhoea In Under 5")
        data = Encounter.where('encounter_datetime BETWEEN ? AND ?
        AND encounter_type = ? AND value_coded IN (?)
        AND concept_id IN(6543, 6542)',
        start_date.to_date.strftime('%Y-%m-%d 00:00:00'),
        end_date.to_date.strftime('%Y-%m-%d 23:59:59'),type.id,concept_ids).\
        joins('INNER JOIN obs ON obs.encounter_id = encounter.encounter_id
        INNER JOIN person p ON p.person_id = encounter.patient_id').\
        select('encounter.encounter_type, obs.value_coded, p.*')

        #under_five
        under_five = data.select{|record| calculate_age(record["birthdate"]) < 5}.\
                    collect{|record| record.person_id}
        options["<5yrs"] = under_five
        collection[key] = options
      end


      if key.eql?("Malnutrition In Under 5")
        data = Encounter.where('encounter_datetime BETWEEN ? AND ?
        AND encounter_type = ? AND value_coded IN (?)
        AND concept_id IN(6543, 6542)',
        start_date.to_date.strftime('%Y-%m-%d 00:00:00'),
        end_date.to_date.strftime('%Y-%m-%d 23:59:59'),type.id,concept_ids).\
        joins('INNER JOIN obs ON obs.encounter_id = encounter.encounter_id
        INNER JOIN person p ON p.person_id = encounter.patient_id').\
        select('encounter.encounter_type, obs.value_coded, p.*')

        #under_five
        under_five = data.select{|record| calculate_age(record["birthdate"]) < 5}.\
                    collect{|record| record.person_id}
        options["<5yrs"] = under_five
        collection[key] = options
      end


      if key.eql?("Underweight Newborns < 2500g in Under 5 Cases")
        data = Encounter.where('encounter_datetime BETWEEN ? AND ?
        AND encounter_type = ? AND value_coded IN (?)
        AND concept_id IN(6543, 6542)',
        start_date.to_date.strftime('%Y-%m-%d 00:00:00'),
        end_date.to_date.strftime('%Y-%m-%d 23:59:59'),type.id,concept_ids).\
        joins('INNER JOIN obs ON obs.encounter_id = encounter.encounter_id
        INNER JOIN person p ON p.person_id = encounter.patient_id').\
        select('encounter.encounter_type, obs.value_coded, p.*')

        #under_five
        under_five = data.select{|record| calculate_age(record["birthdate"]) < 5}.\
                    collect{|record| record.person_id}
        options["<5yrs"] = under_five
        collection[key] = options
      end

      if key.eql?("Severe Pneumonia in under 5 cases")
        data = Encounter.where('encounter_datetime BETWEEN ? AND ?
        AND encounter_type = ? AND value_coded IN (?)
        AND concept_id IN(6543, 6542)',
        start_date.to_date.strftime('%Y-%m-%d 00:00:00'),
        end_date.to_date.strftime('%Y-%m-%d 23:59:59'),type.id,concept_ids).\
        joins('INNER JOIN obs ON obs.encounter_id = encounter.encounter_id
        INNER JOIN person p ON p.person_id = encounter.patient_id').\
        select('encounter.encounter_type, obs.value_coded, p.*')

        #under_five
        under_five = data.select{|record| calculate_age(record["birthdate"]) < 5}.\
                    collect{|record| record.person_id}
        options["<5yrs"] = under_five
        collection[key] = options
      end
    end
  end
    if request == nil
     response = send_data(collection,"monthly")
    end
  return collection
end

.generate_notifiable_disease_conditions_report(start_date = nil, end_date = nil) ⇒ Object



519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
# File 'app/services/emr_ohsp_interface/emr_ohsp_interface_service.rb', line 519

def generate_notifiable_disease_conditions_report(start_date=nil,end_date=nil)
  diag_map = settings["notifiable_disease_conditions"]

  start_date = Date.today.strftime("%Y-%m-%d") if start_date.nil?
  end_date = Date.today.strftime("%Y-%m-%d") if end_date.nil?

  type = EncounterType.find_by_name 'Outpatient diagnosis'
  collection = {}
  concept_name_for_sms_portal = {}

  diag_map.each do |key,value|
    options = {"<5yrs"=>nil,">=5yrs"=>nil}
    concept_ids = ConceptName.where(name: value).collect{|cn| cn.concept_id}

    data = Encounter.where('encounter_datetime BETWEEN ? AND ?
    AND encounter_type = ? AND value_coded IN (?)
    AND concept_id IN(6543, 6542)',
    start_date.to_date.strftime('%Y-%m-%d 00:00:00'),
    end_date.to_date.strftime('%Y-%m-%d 23:59:59'),type.id,concept_ids).\
    joins('INNER JOIN obs ON obs.encounter_id = encounter.encounter_id
    INNER JOIN person p ON p.person_id = encounter.patient_id').\
    select('encounter.encounter_type, obs.value_coded, p.*')

    #under_five
    under_five = data.select{|record| calculate_age(record["birthdate"]) < 5}.\
                collect{|record| record.person_id}
    options["<5yrs"] = under_five
    #above 5 years
    over_five = data.select{|record| calculate_age(record["birthdate"]) >=5 }.\
                collect{|record| record.person_id}

    options[">=5yrs"] =  over_five

    collection[key] = options

    concept_name_for_sms_portal[key] = concept_ids
  end
  send_data_to_sms_portal(collection, concept_name_for_sms_portal)
  return collection
end

.generate_weekly_idsr_report(request = nil, start_date = nil, end_date = nil) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'app/services/emr_ohsp_interface/emr_ohsp_interface_service.rb', line 61

def generate_weekly_idsr_report(request=nil,start_date=nil,end_date=nil)

  diag_map = settings["weekly_idsr_map"]

  epi_week = weeks_generator.last.first.strip
  start_date = weeks_generator.last.last.split("to")[0].strip if start_date.nil?
  end_date = weeks_generator.last.last.split("to")[1].strip if end_date.nil?

  #pull the data
  type = EncounterType.find_by_name 'Outpatient diagnosis'
  collection = {}

  diag_map.each do |key,value|
    options = {"<5yrs"=>nil,">=5yrs"=>nil}
    concept_ids = ConceptName.where(name: value).collect{|cn| cn.concept_id}

    data = Encounter.where('encounter_datetime BETWEEN ? AND ?
    AND encounter_type = ? AND value_coded IN (?)
    AND concept_id IN(6543, 6542)',
    start_date.to_date.strftime('%Y-%m-%d 00:00:00'),
    end_date.to_date.strftime('%Y-%m-%d 23:59:59'),type.id,concept_ids).\
    joins('INNER JOIN obs ON obs.encounter_id = encounter.encounter_id
    INNER JOIN person p ON p.person_id = encounter.patient_id').\
    select('encounter.encounter_type, obs.value_coded, p.*')

    #under_five
    under_five = data.select{|record| calculate_age(record["birthdate"]) < 5}.\
                collect{|record| record.person_id}
    options["<5yrs"] = under_five
    #above 5 years
    over_five = data.select{|record| calculate_age(record["birthdate"]) >=5 }.\
                collect{|record| record.person_id}

    options[">=5yrs"] =  over_five

    collection[key] = options
  end
    if request == nil
     response = send_data(collection,"weekly")
    end
    
  return collection
end

.get_data_set_id(type) ⇒ Object



51
52
53
54
55
56
57
58
59
# File 'app/services/emr_ohsp_interface/emr_ohsp_interface_service.rb', line 51

def get_data_set_id(type)
  if type == "weekly"
    file = File.open(Rails.root.join("db","idsr_metadata","idsr_weekly_ohsp_ids.csv"))
  else
    file = File.open(Rails.root.join("db","idsr_metadata","idsr_monthly_ohsp_ids.csv"))
  end
  data = CSV.parse(file,headers: true)
  data_set_id = data.first["Data Set ID"]
end

.get_ohsp_de_ids(de, type) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'app/services/emr_ohsp_interface/emr_ohsp_interface_service.rb', line 26

def get_ohsp_de_ids(de,type)
  #this method returns an array ohsp report line ids
  result = []
  #["waoQ016uOz1", "r1AT49VBKqg", "FPN4D0s6K3m", "zE8k2BtValu"]
  #  ds,              de_id     ,  <5yrs       ,  >=5yrs
  puts de
  if type == "weekly"
  file = File.open(Rails.root.join("db","idsr_metadata","idsr_weekly_ohsp_ids.csv"))
  else
  file = File.open(Rails.root.join("db","idsr_metadata","idsr_monthly_ohsp_ids.csv"))
  end
  data = CSV.parse(file,headers: true)
  row = data.select{|row| row["Data Element Name"].strip.downcase.eql?(de.downcase.strip)}
  ohsp_ds_id = row[0]["Data Set ID"]
  result << ohsp_ds_id
  ohsp_de_id = row[0]["UID"]
  result << ohsp_de_id
  option1 = row[0]["<5Yrs"]
  result << option1
  option2 = row[0][">=5Yrs"]
  result << option2

  return result
end

.get_ohsp_facility_idObject



18
19
20
21
22
23
24
# File 'app/services/emr_ohsp_interface/emr_ohsp_interface_service.rb', line 18

def get_ohsp_facility_id
  file = File.open(Rails.root.join("db","idsr_metadata","emr_ohsp_facility_map.csv"))
  data = CSV.parse(file,headers: true)
  emr_facility_id = Location.current_health_center.id
  facility = data.select{|row| row["EMR_Facility_ID"].to_i == emr_facility_id}
  ohsp_id = facility[0]["OrgUnit ID"]
end

.months_generatorObject

helper menthod



561
562
563
564
565
566
567
568
569
570
571
572
# File 'app/services/emr_ohsp_interface/emr_ohsp_interface_service.rb', line 561

def months_generator
    months = Hash.new
    count = 1
    curr_date = Date.today
    while count < 13 do
        curr_date = curr_date - 1.month
        months[curr_date.strftime("%Y%m")] = [curr_date.strftime("%B-%Y"),\
                                  (curr_date.beginning_of_month.to_s+" to " + curr_date.end_of_month.to_s)]
        count +=  1
    end
    return months.to_a
end

.registration_report(start_date = nil, end_date = nil) ⇒ Object



502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
# File 'app/services/emr_ohsp_interface/emr_ohsp_interface_service.rb', line 502

def registration_report(start_date=nil,end_date=nil)
  ActiveRecord::Base.connection.select_all <<~SQL
  SELECT
  MIN(IFNULL(c.name, 'Unidentified')) AS visit_type,
    obs.person_id
    FROM `encounter`
    LEFT JOIN obs ON obs.encounter_id = encounter.encounter_id AND obs.voided = 0
    LEFT JOIN concept_name c ON c.concept_id = obs.value_coded 
    AND c.name IN ('New patient','Revisiting','Referral') AND c.voided = 0
    WHERE
        encounter.voided = 0
        AND  DATE(encounter_datetime) BETWEEN '#{start_date}' AND '#{end_date}'
        AND encounter.program_id = 14 -- OPD program
    GROUP BY encounter.patient_id, DATE(encounter_datetime);
  SQL
end

.send_data(data, type) ⇒ Object



614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
# File 'app/services/emr_ohsp_interface/emr_ohsp_interface_service.rb', line 614

def send_data(data,type)
  # method used to post data to the server
  #prepare payload here
  conn = server_config['ohsp']
  payload = {
    "dataSet" =>get_data_set_id(type),
    "period"=>(type.eql?("weekly") ? weeks_generator.last[0] : months_generator.first[0]),
    "orgUnit"=> get_ohsp_facility_id,
    "dataValues"=> []
  }
   special = ["Severe Pneumonia in under 5 cases","Malaria in Pregnancy",
             "Underweight Newborns < 2500g in Under 5 Cases","Diarrhoea In Under 5"]

  data.each do |key,value|
    if !special.include?(key)
        option1 =  {"dataElement"=>get_ohsp_de_ids(key,type)[1],
                    "categoryOptionCombo"=> get_ohsp_de_ids(key,type)[2],
                    "value"=>value["<5yrs"].size } rescue {}

        option2 = {"dataElement"=>get_ohsp_de_ids(key,type)[1],
                    "categoryOptionCombo"=> get_ohsp_de_ids(key,type)[3],
                    "value"=>value[">=5yrs"].size} rescue {}

      #fill data values array
        payload["dataValues"] << option1
        payload["dataValues"] << option2
    else
        case key
          when special[0]
            option1 =  {"dataElement"=>get_ohsp_de_ids(key,type)[1],
                        "categoryOptionCombo"=> get_ohsp_de_ids(key,type)[2],
                        "value"=>value["<5yrs"].size } rescue {}

            payload["dataValues"] << option1
          when special[1]
            option2 = {"dataElement"=>get_ohsp_de_ids(key,type)[1],
                        "categoryOptionCombo"=> get_ohsp_de_ids(key,type)[3],
                        "value"=>value[">=5yrs"].size } rescue {}

            payload["dataValues"] << option2
          when special[2]
            option1 =  {"dataElement"=>get_ohsp_de_ids(key,type)[1],
                        "categoryOptionCombo"=> get_ohsp_de_ids(key,type)[2],
                        "value"=>value["<5yrs"].size } rescue {}

            payload["dataValues"] << option1
          when special[3]
            option1 =  {"dataElement"=>get_ohsp_de_ids(key,type)[1],
                        "categoryOptionCombo"=> get_ohsp_de_ids(key,type)[2],
                        "value"=>value["<5yrs"].size} rescue {}

            payload["dataValues"] << option1
        end
    end
  end

  puts "now sending these values: #{payload.to_json}"
  url = "#{conn["url"]}/api/dataValueSets"
  puts url
  puts "pushing #{type} IDSR Reports"
  send = RestClient::Request.execute(method: :post,
                                      url: url,
                                      headers:{'Content-Type'=> 'application/json'},
                                      payload: payload.to_json,
                                      #headers: {accept: :json},
                                      user: conn["username"],
                                      password: conn["password"])

  puts send
end

.send_data_to_sms_portal(data, concept_name_collection) ⇒ Object



685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
# File 'app/services/emr_ohsp_interface/emr_ohsp_interface_service.rb', line 685

def send_data_to_sms_portal(data, concept_name_collection)
conn2 = server_config['idsr_sms']
data = data.select {|k,v| v.select {|kk,vv| vv.length > 0}.length > 0}
payload = {
  "email"=> conn2["username"],
  "password" => conn2["password"],
  "emr_facility_id" => Location.current_health_center.id,
  "emr_facility_name" => Location.current_health_center.name,
  "payload" => data,
  "concept_name_collection" => concept_name_collection
}
      
     
      
begin
  response = RestClient::Request.execute(method: :post,
    url: conn2["url"],
    headers:{'Content-Type'=> 'application/json'},
    payload: payload.to_json
  )
rescue RestClient::ExceptionWithResponse => res
  if res.class == RestClient::Forbidden
    puts "error: #{res.class}"
  end
end
      
if response.class != NilClass
  if response.code == 200
    puts "success: #{response}"
  end
end

end

.server_configObject



14
15
16
# File 'app/services/emr_ohsp_interface/emr_ohsp_interface_service.rb', line 14

def server_config
  config =YAML.load_file("#{Rails.root}/config/application.yml")
end

.settingsObject



9
10
11
12
# File 'app/services/emr_ohsp_interface/emr_ohsp_interface_service.rb', line 9

def settings
  file = File.read(Rails.root.join("db","idsr_metadata","idsr_ohsp_settings.json"))
  config = JSON.parse(file)
end

.weeks_generatorObject

helper menthod



575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
# File 'app/services/emr_ohsp_interface/emr_ohsp_interface_service.rb', line 575

def weeks_generator

  weeks = Hash.new
  first_day = (Date.today - (11).month).at_beginning_of_month
  wk_of_first_day = first_day.cweek

  if wk_of_first_day > 1
    wk = first_day.prev_year.year.to_s+"W"+wk_of_first_day.to_s
    dates = "#{(first_day-first_day.wday+1).to_s} to #{((first_day-first_day.wday+1)+6).to_s}"
    weeks[wk] = dates
  end

  #get the firt monday of the year
  while !first_day.monday? do
    first_day = first_day+1
  end
  first_monday = first_day
  #generate week numbers and date ranges

  while first_monday <= Date.today do
      wk = (first_monday.year).to_s+"W"+(first_monday.cweek).to_s
      dates =  "#{first_monday.to_s} to #{(first_monday+6).to_s}"
      #add to the hash
      weeks[wk] = dates
      #step by week
      first_monday += 7
  end
#remove the last week
this_wk = (Date.today.year).to_s+"W"+(Date.today.cweek).to_s
weeks = weeks.delete_if{|key,value| key==this_wk}

return weeks.to_a
end