/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
- class Task < ActiveRecord::Base
- set_table_name :task
- set_primary_key :task_id
- include Openmrs
- # Try to find the next task for the patient at the given location
- def self.next_task(location, patient, session_date = Date.today, current_encounter_types = nil)
- tasks = self.all(:conditions => ["location = ? OR location = ?", "MNCH Hotline Station", "*"], :order => 'sort_weight ASC')
- current_encounter_types = patient.encounters.find_by_date(session_date).map{|e| e.type.name rescue ''}.uniq rescue [] if current_encounter_types.nil?
- tasks.each do |task|
- # By default, we don't want to skip this task
- skip = false
- # Skip this task if we already run it
- next if task.encounter_type.present? && current_encounter_types.include?(task.encounter_type)
- if task.gender.present?
- #skip this task if patient is not a female adult
- skip = true if (task.gender == "F" && !patient.female_adult?)
- # skip this task if the patient is female_adult
- # because the question is for children of either sex
- skip = true if task.gender == "*" && patient.female_adult?
- end
- # skip if there is neither PREGNANCY STATUS nor CHILD HEALTH SYMPTOMS encounter
- skip = true if (!(current_encounter_types.include?("PREGNANCY STATUS") || current_encounter_types.include?("CHILD HEALTH SYMPTOMS")))
- # return to patient dashboard
- skip = !skip if task.location == "*"
- # We need to skip this task for some reason
- next if skip
- # Nothing failed, this is the next task, lets replace any macros
- task.url = task.url.gsub(/\{patient\}/, "#{patient.patient_id}")
- task.url = task.url.gsub(/\{person\}/, "#{patient.person.person_id rescue nil}")
- task.url = task.url.gsub(/\{location\}/, "#{location.location_id}")
- logger.debug "next_task: #{task.id} - #{task.description}"
- return task
- end
- # if all fails, return to the patient dashboard
- task = tasks.last
- task.url = task.url.gsub(/\{patient\}/, "#{patient.patient_id}")
- return task
- end
-
- def self.validate_task(patient, task, location, session_date = Date.today)
- #return task unless task.has_program_id == 1
- return task if task.encounter_type == 'REGISTRATION'
- #check if the latest HIV program is closed - if yes, the app should redirect the user to update state screen
- if patient.encounters.find_by_encounter_type(EncounterType.find_by_name('ART_INITIAL').id)
- latest_hiv_program = [] ; patient.patient_programs.collect{ | p |next unless p.program_id == 1 ; latest_hiv_program << p }
- if latest_hiv_program.last.closed?
- task.url = '/patients/programs/{patient}' ; task.encounter_type = 'Program enrolment'
- return task
- end rescue nil
- end
- return task if task.url == "/patients/show/{patient}"
- art_encounters = ['ART_INITIAL','HIV RECEPTION','VITALS','HIV STAGING','ART VISIT','ART ADHERENCE','TREATMENT','DISPENSING']
- #if the following happens - that means the patient is a transfer in and the reception are trying to stage from the transfer in sheet
- if task.encounter_type == 'HIV STAGING' and location.name.match(/RECEPTION/i)
- return task
- end
- #if the following happens - that means the patient was refered to see a clinician
- if task.description.match(/REFER/i) and location.name.match(/Clinician/i)
- return task
- end
- if patient.encounters.find_by_encounter_type(EncounterType.find_by_name(art_encounters[0]).id).blank? and task.encounter_type != art_encounters[0]
- task.url = "/patients/summary?patient_id={patient}&skipped={encounter_type}"
- task.url = task.url.gsub(/\{encounter_type\}/, "#{art_encounters[0].gsub(' ','_')}")
- return task
- elsif patient.encounters.find_by_encounter_type(EncounterType.find_by_name(art_encounters[0]).id).blank? and task.encounter_type == art_encounters[0]
- return task
- end
-
- hiv_reception = Encounter.find(:first,
- :conditions =>["patient_id = ? AND encounter_type = ? AND DATE(encounter_datetime) = ?",
- patient.id,EncounterType.find_by_name(art_encounters[1]).id,session_date],
- :order =>'encounter_datetime DESC')
- if hiv_reception.blank? and task.encounter_type != art_encounters[1]
- task.url = "/patients/summary?patient_id={patient}&skipped={encounter_type}"
- task.url = task.url.gsub(/\{encounter_type\}/, "#{art_encounters[1].gsub(' ','_')}")
- return task
- elsif hiv_reception.blank? and task.encounter_type == art_encounters[1]
- return task
- end
- reception = Encounter.find(:first,:conditions =>["patient_id = ? AND DATE(encounter_datetime) = ? AND encounter_type = ?",
- patient.id,session_date,EncounterType.find_by_name(art_encounters[1]).id]).observations.to_s rescue ''
-
- if reception.match(/PATIENT PRESENT FOR CONSULTATION: YES/)
- vitals = Encounter.find(:first,
- :conditions =>["patient_id = ? AND encounter_type = ? AND DATE(encounter_datetime) = ?",
- patient.id,EncounterType.find_by_name(art_encounters[2]).id,session_date],
- :order =>'encounter_datetime DESC')
- if vitals.blank? and task.encounter_type != art_encounters[2]
- task.url = "/patients/summary?patient_id={patient}&skipped={encounter_type}"
- task.url = task.url.gsub(/\{encounter_type\}/, "#{art_encounters[2].gsub(' ','_')}")
- return task
- elsif vitals.blank? and task.encounter_type == art_encounters[2]
- return task
- end
- end
- if patient.encounters.find_by_encounter_type(EncounterType.find_by_name(art_encounters[3]).id).blank? and task.encounter_type != art_encounters[3]
- task.url = "/patients/summary?patient_id={patient}&skipped={encounter_type}"
- task.url = task.url.gsub(/\{encounter_type\}/, "#{art_encounters[3].gsub(' ','_')}")
- return task
- elsif patient.encounters.find_by_encounter_type(EncounterType.find_by_name(art_encounters[3]).id).blank? and task.encounter_type == art_encounters[3]
- return task
- end
- art_visit = Encounter.find(:first,
- :conditions =>["patient_id = ? AND encounter_type = ? AND DATE(encounter_datetime) = ?",
- patient.id,EncounterType.find_by_name(art_encounters[4]).id,session_date],
- :order =>'encounter_datetime DESC',:limit => 1)
- if art_visit.blank? and task.encounter_type != art_encounters[4]
- task.url = "/patients/summary?patient_id={patient}&skipped={encounter_type}"
- task.url = task.url.gsub(/\{encounter_type\}/, "#{art_encounters[4].gsub(' ','_')}")
- return task
- elsif art_visit.blank? and task.encounter_type == art_encounters[4]
- return task
- end
- unless patient.drug_given_before(session_date).blank?
- art_adherance = Encounter.find(:first,
- :conditions =>["patient_id = ? AND encounter_type = ? AND DATE(encounter_datetime) = ?",
- patient.id,EncounterType.find_by_name(art_encounters[5]).id,session_date],
- :order =>'encounter_datetime DESC',:limit => 1)
-
- if art_adherance.blank? and task.encounter_type != art_encounters[5]
- task.url = "/patients/summary?patient_id={patient}&skipped={encounter_type}"
- task.url = task.url.gsub(/\{encounter_type\}/, "#{art_encounters[5].gsub(' ','_')}")
- return task
- elsif art_adherance.blank? and task.encounter_type == art_encounters[5]
- return task
- end
- end
- if patient.prescribe_arv_this_visit(session_date)
- art_treatment = Encounter.find(:first,
- :conditions =>["patient_id = ? AND encounter_type = ? AND DATE(encounter_datetime) = ?",
- patient.id,EncounterType.find_by_name(art_encounters[6]).id,session_date],
- :order =>'encounter_datetime DESC',:limit => 1)
- if art_treatment.blank? and task.encounter_type != art_encounters[6]
- task.url = "/patients/summary?patient_id={patient}&skipped={encounter_type}"
- task.url = task.url.gsub(/\{encounter_type\}/, "#{art_encounters[6].gsub(' ','_')}")
- return task
- elsif art_treatment.blank? and task.encounter_type == art_encounters[6]
- return task
- end
- end
- task
- end
- end