require_relative 'resource_config' module Medivo class Order < LabResource set_element_name 'customer' set_collection_name 'customers' validates_presence_of :first_name, :last_name, :email, :address, :state, :zip, :dob, :home_phone, :psc, :test_types, :gender, :account_number validates_inclusion_of :gender, :in => %w(Male Female), :message => "gender field must be Male or Female" validates_with Medivo::DateValidator, :fields=>[:dob] validates_with Medivo::TestTypesValidator, :fields=>[:test_types] extend ActiveModel::Callbacks # need to set up this dummy call .. for subclass to be able to use it at all define_model_callbacks :create, :only=>[] def initialize(attributes = {}, persisted = false) super(attributes, persisted) self.draw_location = "PSC" self.take_tests_same_day = true end def read_attribute_for_validation(key) @attributes[key] end # overriding save to return false if it does not save without errors def save new? ? create : update valid? end # overriding create to capture 404 status and put that in error # messages instead of throwing exception def create run_callbacks(:create) super @remote_errors = nil rescue ActiveResource::ConnectionError => e @remote_errors = e load_remote_errors(e, true) end def to_param requisition_id end def requisition_id requisition_number end def self.get_order(requsition_id) find(requsition_id) end # loads the requisition with order def self.find_with_requisition(requsition_id) find(requsition_id, :params=>{:include=>:requisition}) end # gets the lab requisition pdf def pdf_requisition Base64::decode64(try(:sameday_requisition)) end # get a lab requisition pdf from requisition id def self.pdf_requisition(requsition_id) find_with_requisition(requsition_id).pdf_requisition end ## # find order and the results as well def self.find_with_results(requsition_id) find(requsition_id, :params=>{:include=>:reconciled_results}) end ## # NOTE: must use #find_with_results to get the order before you call this method def results Array.wrap(try(:reconciled_results).try(:result)) end ## # get the result for a particular test result code # # @param result_lab_code # # NOTE: must use #find_with_results to get the order before you call this method def result_for_test(result_lab_code) results.find{|r| r.result_lab_code == result_lab_code} end # # NOTE: must use #find_with_results to get the order before you call this method def results_summary try(:reconciled_results).try(:results_summary) end # # NOTE: must use #find_with_results to get the order before you call this method def result_date results_summary.try(:collection_date) end # # NOTE: must use #find_with_results to get the order before you call this method def pdf_result Base64::decode64(try(:reconciled_results).try(:results_pdf)) end ## # gets the order, and returns the pdf def self.pdf_result(requsition_id) find_with_results(requsition_id).pdf_result end ## # @param lab_prefix [ qd, lc ] # @param test_name name of test like 'hga1c' or 'hepc' # @param type [ normal, false_positive, positive, abnormal ] # def simulate_result(lab_prefix, test_name, type='normal') unless %w(normal false_positive positive abnormal).include?(type) and %w(qd lc).include?(lab_prefix) raise "Incorrect Result Type" end hl7_file = Rails.root.join("lib/hl7/#{lab_prefix}_#{test_name}_#{type}.hl7").to_s hl7 = Base64.encode64(File.read(hl7_file)) body = "result#{hl7}" post(:mock_order, {}, body) end end end