Module: EmrOhspInterface::EmrLimsInterfaceService

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

Class Method Summary collapse

Class Method Details

.check_patient_number(patient_id) ⇒ Object



30
31
32
33
34
35
36
37
# File 'app/services/emr_ohsp_interface/emr_lims_interface_service.rb', line 30

def check_patient_number(patient_id)
    get_patient_id = query "SELECT patient_number FROM patients WHERE `external_patient_number` ='#{patient_id}'; "

    get_patient_id.each do |x|
      return patient_number = x['patient_number']
    end

end

.create_lab_order(lab_details, clinician_id) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'app/services/emr_ohsp_interface/emr_lims_interface_service.rb', line 51

def create_lab_order(lab_details, clinician_id)
  external_patient_number = get_patient_dentifier(lab_details[0][:patient_id])
  patient_number = check_patient_number(external_patient_number)
  time = Time.new
  date = time.strftime('%Y-%m-%d %H:%M:%S')
  lab_details.map do |order_params|
    if patient_number.blank?
      patient_number = get_patient_number() + 1

      person_data = Person.where('person_id= ?', order_params[:patient_id])[0]
      address_data = PersonAddress.where('person_id= ?', order_params[:patient_id])[0]
      name_data = PersonName.where('person_id= ?', order_params[:patient_id])[0]

      gender = person_data[:gender].match(/f/i) ? 1 : (person_data[:gender].match(/m/i) ? 0 : 2)
      create_patient(
        name_data[:given_name],
        name_data[:family_name],
        clinician_id[0][:requesting_clinician_id],
        address_data[:address1],
        gender,
        person_data[:birthdate],
        person_data[:birthdate_estimated],
        external_patient_number,
        patient_number,
        date
      )
    end
    create_visit(patient_number, date)
    create_specimens(
      clinician_id[0][:requesting_clinician_id],
      order_params[:specimen][:name],
      order_params[:accession_number]
    )
    specimen_id = get_specimen_id
    create_unsync_orders(date, specimen_id)

    create_test(specimen_id, order_params[:requesting_clinician], order_params[:tests][0][:name])
  end
end

.create_local_tracking_numberObject

lims tracking number ###############



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
# File 'app/services/emr_ohsp_interface/emr_lims_interface_service.rb', line 196

def create_local_tracking_number
  configs = YAML.load_file "#{Rails.root}/config/application.yml"
  site_code = configs['facility_code']
  file = JSON.parse(File.read("#{Rails.root}/public/tracker.json"))
  todate = Time.now.strftime('%Y%m%d')
  year = Time.now.strftime('%Y%m%d').to_s.slice(2..3)
  month = Time.now.strftime('%m')
  day = Time.now.strftime('%d')

  key = file.keys

  if todate > key[0]

    fi = {}
    fi[todate] = 1
    File.open("#{Rails.root}/public/tracker.json", 'w') do |f|
      f.write(fi.to_json)
    end

    value = '001'
    tracking_number = "X#{site_code}#{year}#{get_month(month)}#{get_day(day)}#{value}"

  else
    counter = file[todate]

    value = if counter.to_s.length == 1
              '00' + counter.to_s
            elsif counter.to_s.length == 2
              '0' + counter.to_s
            else
              begin
                  counter.to_s
              rescue StandardError
                '001'
                end
            end

    tracking_number = "X#{site_code}#{year}#{get_month(month)}#{get_day(day)}#{value}"

  end
  tracking_number
end

.create_patient(firstname, surname, user_id, address, gender, dob, dob_estimated, external_patient_number, patient_number, date) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
102
# File 'app/services/emr_ohsp_interface/emr_lims_interface_service.rb', line 91

def create_patient(
  firstname, surname,
  user_id, address,
  gender, dob,
  dob_estimated, external_patient_number,
  patient_number, date
)
  query(
    "INSERT INTO `patients` (`name`, `first_name_code`, `last_name_code`, `created_by`, `address`, `gender`, `patient_number`, `dob`, `dob_estimated`, `external_patient_number`, `created_at`, `updated_at`)
    VALUES ('#{firstname} #{surname}', SOUNDEX('#{firstname}'), SOUNDEX('#{surname}'), #{user_id}, '#{address}',#{gender},'#{patient_number}', '#{dob}', '#{dob_estimated}', '#{external_patient_number}', '#{date}', '#{date}')"
  )
end

.create_specimens(user_id, specimen_type, tracking_number) ⇒ Object



109
110
111
112
113
114
115
116
# File 'app/services/emr_ohsp_interface/emr_lims_interface_service.rb', line 109

def create_specimens(user_id, specimen_type, tracking_number)
  specimen_type_id = settings['lims_specimen_map'][specimen_type.to_s]
  accession_number = new_accession_number
  # prepare_next_tracking_number()
  # tracking_number = create_local_tracking_number()
  query(" INSERT INTO `specimens` (`specimen_type_id`, `accepted_by`, `priority`, `accession_number`, `tracking_number`)
  VALUES ('#{specimen_type_id}', '#{user_id}', 'Stat', '#{accession_number}', '#{tracking_number}')")
end

.create_test(specimen_id, requested_by, test_type) ⇒ Object



123
124
125
126
127
128
# File 'app/services/emr_ohsp_interface/emr_lims_interface_service.rb', line 123

def create_test(specimen_id, requested_by, test_type)
  visit_id = get_visit_id
  test_type_id = settings['lims_test_type_map'][test_type.to_s]
  query("INSERT INTO `tests` (`visit_id`, `test_type_id`, `specimen_id`, `test_status_id`, `not_done_reasons`, `person_talked_to_for_not_done`, `created_by`, `requested_by`)
   VALUES ('#{visit_id}', '#{test_type_id}', '#{specimen_id}', '2', '0', '0', 1, '#{requested_by}')")
end

.create_unsync_orders(date, specimen_id) ⇒ Object



118
119
120
121
# File 'app/services/emr_ohsp_interface/emr_lims_interface_service.rb', line 118

def create_unsync_orders(date, specimen_id)
  query(" INSERT INTO `unsync_orders` (`specimen_id`, `data_not_synced`, `data_level`, `sync_status`, `updated_by_name`, `updated_by_id`, `created_at`, `updated_at`)
  VALUES ('#{specimen_id}', 'new order', 'specimen', 'not-synced', 'kBLIS Administrator', '1', '#{date}', '#{date}')")
end

.create_visit(patient_number, date) ⇒ Object



104
105
106
107
# File 'app/services/emr_ohsp_interface/emr_lims_interface_service.rb', line 104

def create_visit(patient_number, date)
  query "INSERT INTO `visits` (`patient_id`, `visit_type`, `ward_or_location`, `created_at`, `updated_at`)
          VALUES ('#{patient_number}', 'Out Patient', 'EM OPD', '#{date}', '#{date}')"
end

.filterpatient_number(data) ⇒ Object



39
40
41
42
43
44
45
# File 'app/services/emr_ohsp_interface/emr_lims_interface_service.rb', line 39

def filterpatient_number(data)
  patient_number = 0
  data.each do |x|
    patient_number = x['patient_identify']
  end
  patient_number
end

.get_day(day) ⇒ Object



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
# File 'app/services/emr_ohsp_interface/emr_lims_interface_service.rb', line 269

def get_day(day)
  case day

  when '01'
    '1'
  when '02'
    '2'
  when '03'
    '3'
  when '04'
    '4'
  when '05'
    '5'
  when '06'
    '6'
  when '07'
    '7'
  when '08'
    '8'
  when '09'
    '9'
  when '10'
    'A'
  when '11'
    'B'
  when '12'
    'C'
  when '13'
    'E'
  when '14'
    'F'
  when '15'
    'G'
  when '16'
    'H'
  when '17'
    'Y'
  when '18'
    'J'
  when '19'
    'K'
  when '20'
    'Z'
  when '21'
    'M'
  when '22'
    'N'
  when '23'
    'O'
  when '24'
    'P'
  when '25'
    'Q'
  when '26'
    'R'
  when '27'
    'S'
  when '28'
    'T'
  when '29'
    'V'
  when '30'
    'W'
  when '31'
    'X'
    end
end

.get_last_accession_numberObject



161
162
163
164
165
166
# File 'app/services/emr_ohsp_interface/emr_lims_interface_service.rb', line 161

def get_last_accession_number
  data = query('SELECT * FROM specimens WHERE accession_number IS NOT NULL ORDER BY id DESC LIMIT 1')
  data.each do |x|
    return last_accession_number = x['accession_number']
  end
end

.get_lims_test_results(tracking_number, patient_id) ⇒ Object

get results from lims ###############



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
# File 'app/services/emr_ohsp_interface/emr_lims_interface_service.rb', line 340

def get_lims_test_results(tracking_number,patient_id)
  external_patient_number = get_patient_dentifier(patient_id)
  data = query("
    SELECT
    visits.ward_or_location, specimens.accession_number,
    tests.created_by, tests.verified_by,
    tests.time_completed,tests.requested_by,tests.interpretation,
    tests.time_created         as         tests_time_created,       
    test_types.name            as    test_types_name,          
    test_categories.name       as   test_categories_name,     
    tests.time_verified        as    tests_time_verified,      
    users.name                 as  users_name,               
    specimen_types.name        as        specimen_types_name,      
    specimen_statuses.name     as              specimen_statuses_name,   
    test_results.result        as          test_results_result,      
    test_results.device_name   as              test_results_device_name, 
    measures.name              as  measures_name,            
    measures.unit             as  measures_unit,            
    measure_ranges.range_lower as measure_ranges_range_lower,
    measure_ranges.range_upper   as  measure_ranges_range_upper                                      
    FROM iblis.patients 
    inner join iblis.visits on `visits`.`patient_id` = `patients`.`patient_number` 
    inner join iblis.tests on `tests`.`visit_id` = `visits`.`id` 
    inner join iblis.test_types on `test_types`.`id` = `tests`.`test_type_id`
    inner join iblis.test_categories on `test_categories`.`id` = `test_types`.`test_category_id`
    inner join iblis.users on `users`.`id` = `tests`.`tested_by`
    inner join iblis.specimens on `specimens`.`id` = `tests`.`specimen_id`
    inner join iblis.specimen_types on `specimen_types`.`id` = `specimens`.`specimen_type_id`
    inner join iblis.specimen_statuses on `specimen_statuses`.`id` = `specimens`.`specimen_status_id`
    right join iblis.test_results on `test_results`.`test_id` = `tests`.`id`
    right join iblis.measures on `measures`.`id` = `test_results`.`measure_id`
    right join iblis.measure_ranges on `measure_ranges`.`measure_id` = `measures`.`id`
    where `patients`.`deleted_at` is null and `specimens`.`tracking_number` = '#{tracking_number}' and  `patients`.`external_patient_number` = '#{external_patient_number}' and `measure_ranges`.`deleted_at` 
    is null group by(`measure_ranges`.`measure_id`) order by time_completed asc ;
    ")
    data
end

.get_month(month) ⇒ Object



239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
# File 'app/services/emr_ohsp_interface/emr_lims_interface_service.rb', line 239

def get_month(month)
  case month

  when '01'
    '1'
  when '02'
    '2'
  when '03'
    '3'
  when '04'
    '4'
  when '05'
    '5'
  when '06'
    '6'
  when '07'
    '7'
  when '08'
    '8'
  when '09'
    '9'
  when '10'
    'A'
  when '11'
    'B'
  when '12'
    'C'
    end
end

.get_patient_dentifier(patient_id) ⇒ Object



47
48
49
50
# File 'app/services/emr_ohsp_interface/emr_lims_interface_service.rb', line 47

def get_patient_dentifier(patient_id)
  patient_dentifier =PatientIdentifier.where('patient_id= ? AND identifier_type = ?', patient_id,3)[0]
  patient_dentifier[:identifier]
end

.get_patient_numberObject



19
20
21
22
23
24
25
26
27
28
# File 'app/services/emr_ohsp_interface/emr_lims_interface_service.rb', line 19

def get_patient_number
  get_patient_id = query "SELECT id FROM iblis.patients order by id desc limit 1;"

  get_patient_id.each do |x|
   return patient_number = x['id']
  end
  


end

.get_specimen_idObject



168
169
170
171
172
173
# File 'app/services/emr_ohsp_interface/emr_lims_interface_service.rb', line 168

def get_specimen_id
  data = query('SELECT * FROM specimens WHERE accession_number IS NOT NULL ORDER BY id DESC LIMIT 1')
  data.each do |x|
    return specimen_id = x['id']
  end
end

.get_user_details(user_id) ⇒ Object



377
378
379
380
# File 'app/services/emr_ohsp_interface/emr_lims_interface_service.rb', line 377

def get_user_details(user_id)
  data = query("SELECT * FROM iblis.users where id = #{user_id};")
  data
end

.get_visit_idObject



175
176
177
178
179
180
# File 'app/services/emr_ohsp_interface/emr_lims_interface_service.rb', line 175

def get_visit_id
  data = query('SELECT * FROM visits WHERE id IS NOT NULL ORDER BY id DESC LIMIT 1')
  data.each do |x|
    return specimen_id = x['id']
  end
end

.initializeObject



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

def initialize
  @mysql_connection_pool = {}
end

.mysqlObject



387
388
389
390
391
392
393
394
395
396
397
398
# File 'app/services/emr_ohsp_interface/emr_lims_interface_service.rb', line 387

def mysql
  # self.initialize
  return mysql_connection if mysql_connection

  connection = Mysql2::Client.new(host: settings['headers']['host'],
                                  username: settings['headers']['username'],
                                  password: settings['headers']['password'],
                                  port: settings['headers']['port'],
                                  database: settings['headers']['database'],
                                  reconnect: true)
  self.mysql_connection = connection
end

.mysql_connectionObject



404
405
406
# File 'app/services/emr_ohsp_interface/emr_lims_interface_service.rb', line 404

def mysql_connection
  @mysql_connection_pool
end

.mysql_connection=(connection) ⇒ Object



400
401
402
# File 'app/services/emr_ohsp_interface/emr_lims_interface_service.rb', line 400

def mysql_connection=(connection)
  @mysql_connection_pool = connection
end

.new_accession_numberObject



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
# File 'app/services/emr_ohsp_interface/emr_lims_interface_service.rb', line 130

def new_accession_number
  # Generate the next accession number for specimen registration
  @mutex = Mutex.new if @mutex.blank?
  @mutex.lock
  max_acc_num = 0
  return_value = nil
  sentinel = 99_999_999

  settings = YAML.load_file("#{Rails.root}/config/application.yml")[Rails.env]
  code = settings['facility_code']
  year = Date.today.year.to_s[2..3]

  record = get_last_accession_number

  unless record.blank?
    max_acc_num = record[5..20].match(/\d+/)[0].to_i # first 5 chars are for facility code and 2 digit year
  end

  if max_acc_num < sentinel
    max_acc_num += 1
  else
    max_acc_num = 1
  end

  max_acc_num = max_acc_num.to_s.rjust(8, '0')
  return_value = "#{code}#{year}#{max_acc_num}"
  @mutex.unlock

  return_value
end

.prepare_next_tracking_numberObject



182
183
184
185
186
187
188
189
190
191
192
193
# File 'app/services/emr_ohsp_interface/emr_lims_interface_service.rb', line 182

def prepare_next_tracking_number
  file = JSON.parse(File.read("#{Rails.root}/public/tracker.json"))
  todate = Time.now.strftime('%Y%m%d')

  counter = file[todate]
  counter = counter.to_i + 1
  fi = {}
  fi[todate] = counter
  File.open("#{Rails.root}/public/tracker.json", 'w') do |f|
    f.write(fi.to_json)
  end
end

.query(sql) ⇒ Object

start connection to lims database ######################################################



382
383
384
385
# File 'app/services/emr_ohsp_interface/emr_lims_interface_service.rb', line 382

def query(sql)
   Rails.logger.debug(sql.to_s)
   result = mysql.query(sql)
end

.settingsObject

start creation of lab test in lims #####################################################



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

def settings
  file = File.read(Rails.root.join('db', 'lims_metadata', 'lims_map.json'))
  JSON.parse(file)
end