Guest User

Untitled

a guest
Feb 20th, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.48 KB | None | 0 0
  1. #!/usr/bin/ruby
  2. require 'fastercsv'
  3. require File.expand_path(File.dirname(__FILE__) + "/../config/environment")
  4.  
  5. def set_patient_esitmated_age(patient, age, date_of_registration)
  6. age = age.to_i
  7. patient_estimated_birthyear = date_of_registration.year - age
  8. patient_estimated_birthmonth = 7
  9. patient_estimated_birthday = 1
  10. patient.birthdate = Date.new(patient_estimated_birthyear, patient_estimated_birthmonth, patient_estimated_birthday)
  11. patient.birthdate_estimated = true
  12. end
  13.  
  14. def set_patient_reason_for_starting(patient, date, reason, adult_or_peds)
  15. encounter = patient.encounters.create(:encounter_datetime => date, :encounter_type => EncounterType.find_by_name("HIV Staging").encounter_type_id)
  16. yes = Concept.find_by_name("Yes").id
  17. stage = reason.to_i
  18. obs = stage < 3 ? Concept.find_by_name("CD4 Count < 250").id : Concept.find_by_name("WHO stage #{stage_number} #{adult_or_peds}").id
  19. encounter.observations.create(:concept_id => obs, :value_coded => yes, :obs_datetime => date, :patient_id => patient.id)
  20. end
  21.  
  22. def create_patient(options)
  23. p = Patient.create(:gender => options[:gender])
  24. set_patient_estimated_age(p, options[:age], options[:date_of_registration])
  25. p.patient_names.create(:given_name => options[:given_name], :family_name => [:family_name])
  26. # The address is blank do we need a TA/village?
  27. p.patient_addresses.create
  28. # Do we need to give a national id?
  29. # Do we need to worry about preceding/padding 0s ("%04d" % arv_number)?
  30. p.patient_identifiers.create(:identifier => "SAL#{options[:arv_number]}", :identifier_type => PatientIdentifierType.find_by_name("Arv national id").id)
  31. p.patient_identifiers.create(:identifier => options[:occupation], :identifier_type => PatientIdentifierType.find_by_name("Occupation").id)
  32. p.save!
  33. p
  34. end
  35.  
  36. def prescribe_drug(patient, drug, dose, frequency, encounter, date = nil)
  37. encounter ||= patient.encounters.create(:encounter_datetime => date, :encounter_type => EncounterType.find_by_name("ART Visit").encounter_type_id)
  38. encounter.observations.create(:value_drug => drug.drug_id, :value_text => frequency, :value_numeric => dose, :concept_id => Concept.find_by_name("Prescribed Dose").concept_id, :obs_datetime => encounter.encounter_datetime)
  39. encounter
  40. end
  41.  
  42. def dispense_drugs(patient, date, drugs)
  43. encounter = patient.encounters.create(:encounter_datetime => date, :encounter_type => EncounterType.find_by_name("Give Drugs").encounter_type_id)
  44. drugs.each{|hash|
  45. order = encounter.orders.create(:order_type_id => 1)
  46. drug_order = order.drug_orders.create(:drug_inventory_id => hash[:drug].drug_id, :quantity => hash[:quantity])
  47. }
  48. end
  49.  
  50. def read(filename)
  51. starter_drug = Drug.find_by_name("Stavudine 30 Lamivudine 150")
  52. continuation_drug = Drug.find_by_name("Stavudine 30 Lamivudine 150 Nevirapine 200")
  53. FasterCSV.foreach(File.dirname(__FILE__) + "/../" + file_name) do |row|
  54. # Need to tune the dates
  55. # Need to tune occupation (so it uses the standard set)
  56. # Need to tune outcome so it uses the concept names
  57. # Need to add age (even if you force it yourself)
  58. options = {
  59. :given_name => row[0],
  60. :family_name => row[1],
  61. :gender => row[2],
  62. :date_of_registration => row[3].to_date,
  63. :outcome => Concept.find_like_name(row[4]).first,
  64. :occupation => row[5].capitalize,
  65. :reason_for_starting => row[6],
  66. :arv_number => row[7],
  67. :continuation => row[8] == 'continuation',
  68. :age => row[9]
  69. }
  70. p = create_patient(row)
  71. if (options[:continuation])
  72. dispense_drugs(p, options[:date_of_registration], [{:drug => starter_drug, :quantity => 30}, {:drug => continuation_drug, :quantity => 15}]
  73. elsif
  74. dispense_drugs(p, options[:date_of_registration], [{:drug => continuation_drug, :quantity => 60}]
  75. end
  76. set_patient_reason_for_starting(p, options[:reason_for_starting], options[:date_of_registration], options[:age] <= 14 ? 'peds' : 'adult')
  77.  
  78. # Is this the correct outcome date? Death date and transfer date will likely be different.
  79. # Will the outcome calculation assume that since they got drugs that they are On ART instead of what we put here?
  80. # In any event keep it at the end of this function!
  81. p.set_outcome(options[:outcome], options[:date_of_registration])
  82. p.save!
  83. end
  84. end
  85.  
  86. User.current_user = User.find(:first)
  87. Location.current_location = Location.find(:first)
  88. puts "Creating old Patients"
  89. read("b0nds_csv.csv")
Add Comment
Please, Sign In to add comment