Class: Lab::LabellingService::OrderLabel

Inherits:
Object
  • Object
show all
Defined in:
app/services/lab/labelling_service/order_label.rb

Overview

Prints an order label for order with given accession number.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(order_id) ⇒ OrderLabel

Returns a new instance of OrderLabel.



12
13
14
# File 'app/services/lab/labelling_service/order_label.rb', line 12

def initialize(order_id)
  @order = Lab::LabOrder.find(order_id)
end

Instance Attribute Details

#orderObject (readonly)

Returns the value of attribute order.



10
11
12
# File 'app/services/lab/labelling_service/order_label.rb', line 10

def order
  @order
end

Instance Method Details

#auto12eplObject



101
102
103
# File 'app/services/lab/labelling_service/order_label.rb', line 101

def auto12epl
  Auto12Epl.new
end

#drawerObject



60
61
62
63
64
65
66
67
68
69
70
71
# File 'app/services/lab/labelling_service/order_label.rb', line 60

def drawer
  return 'N/A' if order.concept_id == unknown_concept.concept_id

  drawer_id = User.find(order.discontinued_by || order.creator).person_id
  draw_date = (order.discontinued_date || order.start_date).strftime('%d/%^b/%Y %H:%M:%S')

  name = PersonName.find_by_person_id(drawer_id)
  return "#{name.given_name} #{name.family_name} #{draw_date}" if name

  user = User.find_by_user_id(drawer_id)
  user ? "#{user.username} #{draw_date}" : 'N/A'
end

#patientObject



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'app/services/lab/labelling_service/order_label.rb', line 42

def patient
  return @patient if @patient

  person = Person.find(order.patient_id)
  person_name = PersonName.find_by_person_id(order.patient_id)
  patient_identifier = PatientIdentifier.where(type: PatientIdentifierType.where(name: 'National id'),
                                               patient_id: order.patient_id)
                                        .first

  @patient = OpenStruct.new(
    given_name: person_name.given_name,
    family_name: person_name.family_name,
    birthdate: person.birthdate,
    gender: person.gender,
    nhid: patient_identifier&.identifier || 'Unknown'
  )
end


16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'app/services/lab/labelling_service/order_label.rb', line 16

def print
  # NOTE: The arguments are passed into the method below not in the order
  #       the method expects (eg patient_id is passed to middle_name field)
  #       to retain compatibility with labels generated by the `lab test controller`
  #       application of the NLIMS suite.
  auto12epl.generate_epl(patient.given_name,
                         patient.family_name,
                         patient.nhid,
                         patient.birthdate.strftime('%d/%^b/%Y'),
                         '',
                         patient.gender,
                         '',
                         drawer,
                         '',
                         tests,
                         reason_for_test,
                         order.accession_number,
                         order.accession_number)
end

#reason_for_testObject



36
37
38
39
40
# File 'app/services/lab/labelling_service/order_label.rb', line 36

def reason_for_test
  return 'Unknown' unless order.reason_for_test

  short_concept_name(order.reason_for_test.value_coded) || 'Unknown'
end

#short_concept_name(concept_id) ⇒ Object



91
92
93
94
95
# File 'app/services/lab/labelling_service/order_label.rb', line 91

def short_concept_name(concept_id)
  ConceptName.where(concept_id: concept_id)
             .min_by { |concept| concept.name.size }
             &.name
end

#specimenObject



73
74
75
76
77
# File 'app/services/lab/labelling_service/order_label.rb', line 73

def specimen
  return 'N/A' if order.concept_id == unknown_concept.concept_id

  ConceptName.find_by_concept_id(order.concept_id)&.name || 'Unknown'
end

#testsObject



79
80
81
82
83
84
85
86
87
88
89
# File 'app/services/lab/labelling_service/order_label.rb', line 79

def tests
  tests = order.tests.map do |test|
    name = short_concept_name(test.value_coded) || 'Unknown'

    next 'VL' if name.match?(/Viral load/i)

    name.size > 7 ? name[0..6] : name
  end

  tests.join(', ')
end

#unknown_conceptObject



97
98
99
# File 'app/services/lab/labelling_service/order_label.rb', line 97

def unknown_concept
  ConceptName.find_by_name('Unknown')
end