/application/app/models/task.rb

https://github.com/dhedlund/mnch-hotline · Ruby · 173 lines · 124 code · 34 blank · 15 comment · 48 complexity · a4cb76b9a60708dadd4f1a9967a82174 MD5 · raw file

  1. class Task < ActiveRecord::Base
  2. set_table_name :task
  3. set_primary_key :task_id
  4. include Openmrs
  5. # Try to find the next task for the patient at the given location
  6. def self.next_task(location, patient, session_date = Date.today, current_encounter_types = nil)
  7. tasks = self.all(:conditions => ["location = ? OR location = ?", "MNCH Hotline Station", "*"], :order => 'sort_weight ASC')
  8. current_encounter_types = patient.encounters.find_by_date(session_date).map{|e| e.type.name rescue ''}.uniq rescue [] if current_encounter_types.nil?
  9. tasks.each do |task|
  10. # By default, we don't want to skip this task
  11. skip = false
  12. # Skip this task if we already run it
  13. next if task.encounter_type.present? && current_encounter_types.include?(task.encounter_type)
  14. if task.gender.present?
  15. #skip this task if patient is not a female adult
  16. skip = true if (task.gender == "F" && !patient.female_adult?)
  17. # skip this task if the patient is female_adult
  18. # because the question is for children of either sex
  19. skip = true if task.gender == "*" && patient.female_adult?
  20. end
  21. # skip if there is neither PREGNANCY STATUS nor CHILD HEALTH SYMPTOMS encounter
  22. skip = true if (!(current_encounter_types.include?("PREGNANCY STATUS") || current_encounter_types.include?("CHILD HEALTH SYMPTOMS")))
  23. # return to patient dashboard
  24. skip = !skip if task.location == "*"
  25. # We need to skip this task for some reason
  26. next if skip
  27. # Nothing failed, this is the next task, lets replace any macros
  28. task.url = task.url.gsub(/\{patient\}/, "#{patient.patient_id}")
  29. task.url = task.url.gsub(/\{person\}/, "#{patient.person.person_id rescue nil}")
  30. task.url = task.url.gsub(/\{location\}/, "#{location.location_id}")
  31. logger.debug "next_task: #{task.id} - #{task.description}"
  32. return task
  33. end
  34. # if all fails, return to the patient dashboard
  35. task = tasks.last
  36. task.url = task.url.gsub(/\{patient\}/, "#{patient.patient_id}")
  37. return task
  38. end
  39. def self.validate_task(patient, task, location, session_date = Date.today)
  40. #return task unless task.has_program_id == 1
  41. return task if task.encounter_type == 'REGISTRATION'
  42. #check if the latest HIV program is closed - if yes, the app should redirect the user to update state screen
  43. if patient.encounters.find_by_encounter_type(EncounterType.find_by_name('ART_INITIAL').id)
  44. latest_hiv_program = [] ; patient.patient_programs.collect{ | p |next unless p.program_id == 1 ; latest_hiv_program << p }
  45. if latest_hiv_program.last.closed?
  46. task.url = '/patients/programs/{patient}' ; task.encounter_type = 'Program enrolment'
  47. return task
  48. end rescue nil
  49. end
  50. return task if task.url == "/patients/show/{patient}"
  51. art_encounters = ['ART_INITIAL','HIV RECEPTION','VITALS','HIV STAGING','ART VISIT','ART ADHERENCE','TREATMENT','DISPENSING']
  52. #if the following happens - that means the patient is a transfer in and the reception are trying to stage from the transfer in sheet
  53. if task.encounter_type == 'HIV STAGING' and location.name.match(/RECEPTION/i)
  54. return task
  55. end
  56. #if the following happens - that means the patient was refered to see a clinician
  57. if task.description.match(/REFER/i) and location.name.match(/Clinician/i)
  58. return task
  59. end
  60. if patient.encounters.find_by_encounter_type(EncounterType.find_by_name(art_encounters[0]).id).blank? and task.encounter_type != art_encounters[0]
  61. task.url = "/patients/summary?patient_id={patient}&skipped={encounter_type}"
  62. task.url = task.url.gsub(/\{encounter_type\}/, "#{art_encounters[0].gsub(' ','_')}")
  63. return task
  64. elsif patient.encounters.find_by_encounter_type(EncounterType.find_by_name(art_encounters[0]).id).blank? and task.encounter_type == art_encounters[0]
  65. return task
  66. end
  67. hiv_reception = Encounter.find(:first,
  68. :conditions =>["patient_id = ? AND encounter_type = ? AND DATE(encounter_datetime) = ?",
  69. patient.id,EncounterType.find_by_name(art_encounters[1]).id,session_date],
  70. :order =>'encounter_datetime DESC')
  71. if hiv_reception.blank? and task.encounter_type != art_encounters[1]
  72. task.url = "/patients/summary?patient_id={patient}&skipped={encounter_type}"
  73. task.url = task.url.gsub(/\{encounter_type\}/, "#{art_encounters[1].gsub(' ','_')}")
  74. return task
  75. elsif hiv_reception.blank? and task.encounter_type == art_encounters[1]
  76. return task
  77. end
  78. reception = Encounter.find(:first,:conditions =>["patient_id = ? AND DATE(encounter_datetime) = ? AND encounter_type = ?",
  79. patient.id,session_date,EncounterType.find_by_name(art_encounters[1]).id]).observations.to_s rescue ''
  80. if reception.match(/PATIENT PRESENT FOR CONSULTATION: YES/)
  81. vitals = Encounter.find(:first,
  82. :conditions =>["patient_id = ? AND encounter_type = ? AND DATE(encounter_datetime) = ?",
  83. patient.id,EncounterType.find_by_name(art_encounters[2]).id,session_date],
  84. :order =>'encounter_datetime DESC')
  85. if vitals.blank? and task.encounter_type != art_encounters[2]
  86. task.url = "/patients/summary?patient_id={patient}&skipped={encounter_type}"
  87. task.url = task.url.gsub(/\{encounter_type\}/, "#{art_encounters[2].gsub(' ','_')}")
  88. return task
  89. elsif vitals.blank? and task.encounter_type == art_encounters[2]
  90. return task
  91. end
  92. end
  93. if patient.encounters.find_by_encounter_type(EncounterType.find_by_name(art_encounters[3]).id).blank? and task.encounter_type != art_encounters[3]
  94. task.url = "/patients/summary?patient_id={patient}&skipped={encounter_type}"
  95. task.url = task.url.gsub(/\{encounter_type\}/, "#{art_encounters[3].gsub(' ','_')}")
  96. return task
  97. elsif patient.encounters.find_by_encounter_type(EncounterType.find_by_name(art_encounters[3]).id).blank? and task.encounter_type == art_encounters[3]
  98. return task
  99. end
  100. art_visit = Encounter.find(:first,
  101. :conditions =>["patient_id = ? AND encounter_type = ? AND DATE(encounter_datetime) = ?",
  102. patient.id,EncounterType.find_by_name(art_encounters[4]).id,session_date],
  103. :order =>'encounter_datetime DESC',:limit => 1)
  104. if art_visit.blank? and task.encounter_type != art_encounters[4]
  105. task.url = "/patients/summary?patient_id={patient}&skipped={encounter_type}"
  106. task.url = task.url.gsub(/\{encounter_type\}/, "#{art_encounters[4].gsub(' ','_')}")
  107. return task
  108. elsif art_visit.blank? and task.encounter_type == art_encounters[4]
  109. return task
  110. end
  111. unless patient.drug_given_before(session_date).blank?
  112. art_adherance = Encounter.find(:first,
  113. :conditions =>["patient_id = ? AND encounter_type = ? AND DATE(encounter_datetime) = ?",
  114. patient.id,EncounterType.find_by_name(art_encounters[5]).id,session_date],
  115. :order =>'encounter_datetime DESC',:limit => 1)
  116. if art_adherance.blank? and task.encounter_type != art_encounters[5]
  117. task.url = "/patients/summary?patient_id={patient}&skipped={encounter_type}"
  118. task.url = task.url.gsub(/\{encounter_type\}/, "#{art_encounters[5].gsub(' ','_')}")
  119. return task
  120. elsif art_adherance.blank? and task.encounter_type == art_encounters[5]
  121. return task
  122. end
  123. end
  124. if patient.prescribe_arv_this_visit(session_date)
  125. art_treatment = Encounter.find(:first,
  126. :conditions =>["patient_id = ? AND encounter_type = ? AND DATE(encounter_datetime) = ?",
  127. patient.id,EncounterType.find_by_name(art_encounters[6]).id,session_date],
  128. :order =>'encounter_datetime DESC',:limit => 1)
  129. if art_treatment.blank? and task.encounter_type != art_encounters[6]
  130. task.url = "/patients/summary?patient_id={patient}&skipped={encounter_type}"
  131. task.url = task.url.gsub(/\{encounter_type\}/, "#{art_encounters[6].gsub(' ','_')}")
  132. return task
  133. elsif art_treatment.blank? and task.encounter_type == art_encounters[6]
  134. return task
  135. end
  136. end
  137. task
  138. end
  139. end