-
Notifications
You must be signed in to change notification settings - Fork 2
/
auto12epl.rb
executable file
·199 lines (160 loc) · 7.96 KB
/
auto12epl.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
#!/usr/bin/ruby
# Jeremy Espino MD MS
# 28-JAN-2016
class Float
# function to round down a float to an integer value
def round_down n=0
n < 1 ? self.to_i.to_f : (self - 0.5 / 10**n).round(n)
end
end
# Generates EPL code that conforms to the Auto12-A standard for specimen labeling
class Auto12Epl
attr_accessor :element_font
attr_accessor :barcode_human_font
DPI = 203
LABEL_WIDTH_IN = 2.0
LABEL_HEIGHT_IN = 1.0
# font constants
FONT_X_DOTS = [8, 10, 12, 14, 32]
FONT_Y_DOTS = [12, 16, 20, 24, 24]
FONT_PAD_DOTS = 2
# element heights
HEIGHT_MARGIN = 0.031
HEIGHT_ELEMENT = 0.1
HEIGHT_ELEMENT_SPACE = 0.01
HEIGHT_PID = 0.1
HEIGHT_BARCODE = 0.394
HEIGHT_BARCODE_HUMAN = 0.083
# element widths
WIDTH_ELEMENT = 1.94
WIDTH_BARCODE = 1.395
WIDTH_BARCODE_HUMAN = 1.688
# margins
L_MARGIN = 0.031
L_MARGIN_BARCODE = 0.25
# stat locations
L_MARGIN_BARCODE_W_STAT = 0.281
L_MARGIN_W_STAT = 0.202
STAT_WIDTH_ELEMENT = 1.78
STAT_WIDTH_BARCODE = 1.307
STAT_WIDTH_BARCODE_HUMAN = 1.548
# constants for generated EPL code
BARCODE_TYPE = '1A'
BARCODE_NARROW_WIDTH = '2'
BARCODE_WIDE_WIDTH = '2'
BARCODE_ROTATION = '0'
BARCODE_IS_HUMAN_READABLE = 'N'
ASCII_HORZ_MULT = 1
ASCII_VERT_MULT = 1
def initialize(element_font = 2, barcode_human_font = 2)
@element_font = element_font
@barcode_human_font = barcode_human_font
end
# Calculate the number of characters that will fit in a given length
def max_characters(font, length)
dots_per_char = FONT_X_DOTS.at(font-1) + FONT_PAD_DOTS
num_char = ( (length * DPI) / dots_per_char).round_down
num_char.to_int
end
# Use basic truncation rule to truncate the name element i.e., if > maxCharacters cutoff and trail with +
def truncate_name(last_name, first_name, middle_initial, is_stat)
if is_stat
name_max_characters = max_characters(@element_font, STAT_WIDTH_ELEMENT)
else
name_max_characters = max_characters(@element_font, WIDTH_ELEMENT)
end
if concatName(last_name, first_name, middle_initial).length > name_max_characters
# truncate last?
if last_name.length > 12
last_name = last_name[0..11] + '+'
end
# truncate first?
if concatName(last_name, first_name, middle_initial).length > name_max_characters && first_name.length > 7
first_name = first_name[0..7] + '+'
end
end
concatName(last_name, first_name, middle_initial)
end
def concatName(last_name, first_name, middle_initial)
last_name + ', ' + first_name + (middle_initial == nil ? '' : ' ' + middle_initial)
end
# The main function to generate the EPL
def generate_epl(last_name, first_name, middle_initial, pid, dob, age, gender, col_date_time, col_name, tests, stat, acc_num, schema_track)
# format text and set margin
if stat == nil
name_text = truncate_name(last_name, first_name, middle_initial, false)
pid_dob_age_gender_text = full_justify(pid, dob + ' ' + age + ' ' + gender, @element_font, WIDTH_ELEMENT)
l_margin = L_MARGIN
l_margin_barcode = L_MARGIN_BARCODE
else
name_text = truncate_name(last_name, first_name, middle_initial, true)
pid_dob_age_gender_text = full_justify(pid, dob + ' ' + age + ' ' + gender, @element_font, STAT_WIDTH_ELEMENT)
stat_element_text = pad_stat_w_space(stat)
l_margin = L_MARGIN_W_STAT
l_margin_barcode = L_MARGIN_BARCODE_W_STAT
end
barcode_human_text = "#{acc_num} * #{schema_track}"
collector_element_text = "Col: #{col_date_time} #{col_name}"
tests_element_text = tests
# generate EPL statements
name_element = generate_ascii_element(to_dots(l_margin), to_dots(HEIGHT_MARGIN), 0, @element_font, false, name_text)
pid_dob_age_gender_element = generate_ascii_element(to_dots(l_margin), to_dots(HEIGHT_MARGIN + HEIGHT_ELEMENT + HEIGHT_ELEMENT_SPACE), 0, @element_font, false, pid_dob_age_gender_text)
barcode_human_element = generate_ascii_element(to_dots(l_margin_barcode), to_dots(HEIGHT_MARGIN + HEIGHT_ELEMENT + HEIGHT_ELEMENT_SPACE + HEIGHT_ELEMENT + HEIGHT_ELEMENT_SPACE + HEIGHT_BARCODE), 0, @barcode_human_font, false, barcode_human_text)
collector_element = generate_ascii_element(to_dots(l_margin), to_dots(HEIGHT_MARGIN + HEIGHT_ELEMENT + HEIGHT_ELEMENT_SPACE + HEIGHT_ELEMENT + HEIGHT_ELEMENT_SPACE + HEIGHT_BARCODE + HEIGHT_BARCODE_HUMAN + HEIGHT_ELEMENT_SPACE), 0, @element_font, false, collector_element_text)
tests_element = generate_ascii_element(to_dots(l_margin), to_dots(HEIGHT_MARGIN + HEIGHT_ELEMENT + HEIGHT_ELEMENT_SPACE + HEIGHT_ELEMENT + HEIGHT_ELEMENT_SPACE + HEIGHT_BARCODE + HEIGHT_BARCODE_HUMAN + HEIGHT_ELEMENT_SPACE + HEIGHT_ELEMENT + HEIGHT_ELEMENT_SPACE), 0, @element_font, false, tests_element_text)
barcode_element = generate_barcode_element(to_dots(l_margin_barcode), to_dots(HEIGHT_MARGIN + HEIGHT_ELEMENT + HEIGHT_ELEMENT_SPACE + HEIGHT_ELEMENT + HEIGHT_ELEMENT_SPACE), to_dots(HEIGHT_BARCODE)-4, schema_track)
stat_element = generate_ascii_element(to_dots(L_MARGIN)+FONT_Y_DOTS.at(@element_font - 1)+FONT_PAD_DOTS, to_dots(HEIGHT_MARGIN), 1, @element_font, true, stat_element_text)
# combine EPL statements
if stat == nil
"N\n#{name_element}\n#{pid_dob_age_gender_element}\n#{barcode_element}\n#{barcode_human_element}\n#{collector_element}\n#{tests_element}\nP1"
else
"N\n#{name_element}\n#{pid_dob_age_gender_element}\n#{barcode_element}\n#{barcode_human_element}\n#{collector_element}\n#{tests_element}\n#{stat_element}\nP1"
end
end
# Add spaces before and after the stat text so that black bars appear across the left edge of label
def pad_stat_w_space(stat)
num_char = max_characters(@element_font, LABEL_HEIGHT_IN)
spaces_needed = (num_char - stat.length) / 2
space = ''
spaces_needed.times do
space = space + ' '
end
space + stat + space
end
# Add spaces between the NPID and the dob/age/gender so that line is fully justified
def full_justify(pid, dag, font, length)
max_char = max_characters(font, length)
spaces_needed = max_char - pid.length - dag.length
space = ''
spaces_needed.times do
space = space + ' '
end
pid + space + dag
end
# convert inches to number of dots using DPI
def to_dots(inches)
(inches * DPI).round
end
# generate ascii EPL
def generate_ascii_element(x, y, rotation, font, is_reverse, text)
"A#{x.to_s},#{y.to_s},#{rotation.to_s},#{font.to_s},#{ASCII_HORZ_MULT},#{ASCII_VERT_MULT},#{is_reverse ? 'R' : 'N'},\"#{text}\""
end
# generate barcode EPL
def generate_barcode_element(x, y, height, schema_track)
"B#{x.to_s},#{y.to_s},#{BARCODE_ROTATION},#{BARCODE_TYPE},#{BARCODE_NARROW_WIDTH},#{BARCODE_WIDE_WIDTH},#{height.to_s},#{BARCODE_IS_HUMAN_READABLE},\"#{schema_track}\""
end
end
if __FILE__ == $0
auto = Auto12Epl.new
puts auto.generate_epl("Banda", "Mary", "U", "Q23-HGF", "12-SEP-1997", "19y", "F", "01-JAN-2016 14:21", "byGD", "CHEM7,Ca,Mg", nil, "KCH-16-00001234", "1600001234")
puts "\n"
puts auto.generate_epl("Banda", "Mary", "U", "Q23-HGF", "12-SEP-1997", "19y", "F", "01-JAN-2016 14:21", "byGD", "CHEM7,Ca,Mg", "STAT CHEM", "KCH-16-00001234", "1600001234")
puts "\n"
puts auto.generate_epl("Bandajustrightlas", "Mary", "U", "Q23-HGF", "12-SEP-1997", "19y", "F", "01-JAN-2016 14:21", "byGD", "CHEM7,Ca,Mg", "STAT CHEM", "KCH-16-00001234", "1600001234")
puts "\n"
puts auto.generate_epl("Bandasuperlonglastnamethatwonfit", "Marysuperlonglastnamethatwonfit", "U", "Q23-HGF", "12-SEP-1997", "19y", "F", "01-JAN-2016 14:21", "byGD", "CHEM7,Ca,Mg", "STAT CHEM", "KCH-16-00001234", "1600001234")
puts "\n"
puts auto.generate_epl("Bandasuperlonglastnamethatwonfit", "Mary", "U", "Q23-HGF", "12-SEP-1997", "19y", "F", "01-JAN-2016 14:21", "byGD", "CHEM7,Ca,Mg", "STAT CHEM", "KCH-16-00001234", "1600001234")
puts "\n"
puts auto.generate_epl("Banda", "Marysuperlonglastnamethatwonfit", "U", "Q23-HGF", "12-SEP-1997", "19y", "F", "01-JAN-2016 14:21", "byGD", "CHEM7,Ca,Mg", "STAT CHEM", "KCH-16-00001234", "1600001234")
end