Skip to content

Instantly share code, notes, and snippets.

@gmgent
Created March 31, 2011 00:54
Show Gist options
  • Save gmgent/895622 to your computer and use it in GitHub Desktop.
Save gmgent/895622 to your computer and use it in GitHub Desktop.
require "prawn/core"
require "prawn/layout"
include ActionView::Helpers::NumberHelper
class PrintableDrugRecord < Prawn::Document
attr_accessor :drug_record
PAGE_WIDTH = 560
LEFT_COL_WIDTH = 330
RIGHT_COL_WIDTH = 210
BLACK = "000000"
WHITE = "FFFFFF"
LIGHT_GREY = "666666"
LIGHT_YELLOW = "FFFFCC"
LIGHT_GREEN = "D8ECAE"
LIGHT_BLUE = "EBF1F5"
HARD_GREEN = "84C554"
VERTICAL_OFFSET = 47
def initialize(options={}, &block)
@drug_record = options.delete(:drug_record)
raise Exception.new("No drug record provided") unless @drug_record
super(options, &block)
get_units
get_data
fill_color WHITE
table @drug_name_data,
:position => :center,
:width => PAGE_WIDTH,
:font_size => 16,
:row_colors => [HARD_GREEN],
:border_width => 0,
:vertical_padding => 7,
:horizontal_padding => 7
fill_color BLACK
if @alert_data
table @alert_data,
:position => :center,
:border_width => 0,
:font_size => 9,
:width => PAGE_WIDTH,
:row_colors => [LIGHT_YELLOW],
:vertical_padding => 5,
:horizontal_padding => 35
alert_art = "#{RAILS_ROOT}/public/images/icons/warning.png"
image alert_art, :at => [0, 683], :scale => 0.65
end
move_down 5
# header
table @header_data,
:position => :center,
:width => PAGE_WIDTH,
:headers => @report_headers,
:column_widths => @header_column_widths,
:font_size => 10,
:header_text_color => LIGHT_GREY,
:row_colors => [WHITE, LIGHT_BLUE],
:border_width => 0,
:vertical_padding => 3,
:horizontal_padding => 3
move_down 5
#Delivery Method
table @delivery_method_data,
:position => :center,
:width => PAGE_WIDTH,
:align => { 0 => :left, 1 => :right },
:row_colors => [LIGHT_GREEN],
:border_width => 0,
:vertical_padding => 3,
:horizontal_padding => 10
move_down 6
fill_color LIGHT_GREY
text "Approval Status: #{@drug_record.approval_status.name}", :size => 10
move_down 20
# Dosing
fill_color LIGHT_GREY
text "Dosing -", :style => :bold
fill_color BLACK
table @dosing_data,
:position => :left,
:width => LEFT_COL_WIDTH,
:headers => ["hard min", "soft min", "soft max", "hard max", "initial", "cumulative max"],
:align => { 0 => :center, 1 => :center, 2 => :center, 3 => :center, 4 => :center, 5 => :center },
:column_widths => { 0 => 50, 1 => 50, 2 => 50, 3 => 50, 4 => 50, 5 => 76 },
:header_text_color => HARD_GREEN,
:row_colors => [WHITE, LIGHT_BLUE],
:vertical_padding => 3,
:horizontal_padding => 3,
:font_size => 9
move_down 10
fill_color LIGHT_GREY
# Concentration
text "Concentration -", :style => :bold
move_down 2
#concentration options
fill_color BLACK
if @concentration_options
table @concentration_options,
:position => :left,
:width => (LEFT_COL_WIDTH-20),
:row_colors => [WHITE, LIGHT_BLUE],
:vertical_padding => 3,
:horizontal_padding => 3,
:font_size => 9
move_down 10
end
if !@drug_record.section_blank?(:concentration)
table @concentration_data,
:position => :left,
:width => LEFT_COL_WIDTH,
:headers => ["hard min", "soft min", "soft max", "hard max"],
:align => { 0 => :center, 1 => :center, 2 => :center, 3 => :center },
:column_widths => { 0 => 50, 1 => 50, 2 => 50, 3 => 50 },
:header_text_color => HARD_GREEN,
:row_colors => [WHITE, LIGHT_BLUE],
:vertical_padding => 3,
:horizontal_padding => 3,
:font_size => 9
end
move_down 10
# Duration
fill_color LIGHT_GREY
text "Duration -", :style => :bold
if !@drug_record.section_blank?(:duration)
fill_color BLACK
table @duration_data,
:position => :left,
:width => LEFT_COL_WIDTH,
:headers => ["hard min", "soft min", "soft max", "hard max", "initial"],
:align => { 0 => :center, 1 => :center, 2 => :center, 3 => :center, 4 => :center },
:column_widths => { 0 => 50, 1 => 50, 2 => 50, 3 => 50, 4 => 50 },
:header_text_color => HARD_GREEN,
:row_colors => [WHITE, LIGHT_BLUE],
:vertical_padding => 3,
:horizontal_padding => 3,
:font_size => 9
end
fill_color LIGHT_GREY
if !@drug_record.section_blank?(:bolus_administration_rate)
move_down 10
# Bolus
text "Bolus Administration Rate -", :style => :bold
fill_color BLACK
table @bolus_data,
:position => :left,
:width => LEFT_COL_WIDTH,
:headers => ["hard min", "soft min", "soft max", "hard max", "initial"],
:align => { 0 => :center, 1 => :center, 2 => :center, 3 => :center, 4 => :center },
:column_widths => { 0 => 50, 1 => 50, 2 => 50, 3 => 50, 4 => 50 },
:header_text_color => HARD_GREEN,
:row_colors => [WHITE, LIGHT_BLUE],
:vertical_padding => 3,
:horizontal_padding => 3,
:font_size => 9
end
move_down 10
fill_color LIGHT_GREY
# custom fields
text "Custom Fields -", :style => :bold
fill_color BLACK
if @custom_fields_with_custom_section && @custom_fields_with_custom_section.first
move_down 10
fill_color LIGHT_GREY
text "with custom section"
fill_color BLACK
table @custom_fields_with_custom_section,
:position => :left,
:width => LEFT_COL_WIDTH,
:headers => [ "section name", "custom field name", "value" ],
:align => { 0 => :center, 1 => :center, 2 => :center },
:column_widths => { 0 => 80, 1 => 100, 2 => 145 },
:header_text_color => HARD_GREEN,
:row_colors => [WHITE, LIGHT_BLUE],
:vertical_padding => 3,
:horizontal_padding => 3,
:font_size => 9
end
if @custom_fields_no_section && @custom_fields_no_section.first
move_down 10
fill_color LIGHT_GREY
text "no custom section"
fill_color BLACK
table @custom_fields_no_section,
:position => :left,
:width => LEFT_COL_WIDTH,
:headers => [ "custom field name", "value" ],
:align => { 0 => :center, 1 => :center },
:column_widths => { 0 => 110, 1 => 215 },
:header_text_color => HARD_GREEN,
:row_colors => [WHITE, LIGHT_BLUE],
:vertical_padding => 3,
:horizontal_padding => 3,
:font_size => 9
end
fill_color BLACK
#add notes
bounding_box([330, (585-@alert_vertical_offset)], :width => RIGHT_COL_WIDTH) do
if @drug_record.notes
table [[@drug_record.notes]],
:row_colors => [WHITE, LIGHT_BLUE],
:headers => ["notes"],
:header_text_color => HARD_GREEN,
:width => RIGHT_COL_WIDTH,
:font_size => 9,
:border_width => 0.5
move_down 10
end
if @drug_record.clinical_advisory
table [[@drug_record.clinical_advisory]],
:row_colors => [WHITE, LIGHT_BLUE],
:headers => ["clinical advisory"],
:header_text_color => HARD_GREEN,
:width => RIGHT_COL_WIDTH,
:font_size => 9,
:border_width => 0.5
move_down 10
end
if @drug_record.dosing_adjustment_information
table [[@drug_record.dosing_adjustment_information]],
:row_colors => [WHITE, LIGHT_BLUE],
:headers => ["dosing adjustment information"],
:header_text_color => HARD_GREEN,
:width => RIGHT_COL_WIDTH,
:font_size => 9,
:border_width => 0.5
end
end
# add timestamp footer
move_down 20
bounding_box([5,10], :width => 300) do
text "#{Time.now.to_s(:pap_mdy_at_hm)}", :size => 9
end
end
def to_pdf
render
end
def get_units
@concentration_unit = @drug_record.concentration_unit ? @drug_record.concentration_unit.abbreviation : ""
@bolus_unit = @drug_record.bolus_administration_rate_unit ? @drug_record.bolus_administration_rate_unit.abbreviation : ""
@dosing_unit = @drug_record.dosing_unit ? @drug_record.dosing_unit.abbreviation : ""
@dosing_cumulative_max_unit = @drug_record.dosing_cumulative_max_unit ? @drug_record.dosing_cumulative_max_unit.abbreviation : ""
@duration_unit = @drug_record.duration_unit ? @drug_record.duration_unit.abbreviation : ""
end
def get_data
if @drug_record.concentration_options_quantity
concentration_options = @drug_record.concentration_options_quantity + " " + @drug_record.concentration_unit.abbreviation
end
if @drug_record.prime_a_pump?
custom_mixture_options = @drug_record.custom_mixture_options
end
@concentration_options = [["mixture options:" ],
[custom_mixture_options ? custom_mixture_options : "" ],
["final concentration options:" ],
[concentration_options ? concentration_options : "" ]] unless (!concentration_options && !custom_mixture_options)
@concentration_data = [[@drug_record.concentration_hard_min_quantity ? "#{number_with_delimiter(@drug_record.concentration_hard_min_quantity,
:delimiter => ',')}\n#{@concentration_unit}" : "",
@drug_record.concentration_soft_min_quantity ? "#{number_with_delimiter(@drug_record.concentration_soft_min_quantity,
:delimiter => ',')}\n#{@concentration_unit}" : "",
@drug_record.concentration_soft_max_quantity ? "#{number_with_delimiter(@drug_record.concentration_soft_max_quantity,
:delimiter => ',')}\n#{@concentration_unit}" : "",
@drug_record.concentration_hard_max_quantity ? "#{number_with_delimiter(@drug_record.concentration_hard_max_quantity,
:delimiter => ',')}\n#{@concentration_unit}" : ""]]
@dosing_data = [[@drug_record.dosing_hard_min_quantity ? "#{number_with_delimiter(@drug_record.dosing_hard_min_quantity, :delimiter => ',')}\n#{@dosing_unit}" : "",
@drug_record.dosing_soft_min_quantity ? "#{number_with_delimiter(@drug_record.dosing_soft_min_quantity, :delimiter => ',')}\n#{@dosing_unit}" : "",
@drug_record.dosing_soft_max_quantity ? "#{number_with_delimiter(@drug_record.dosing_soft_max_quantity, :delimiter => ',')}\n#{@dosing_unit}" : "",
@drug_record.dosing_hard_max_quantity ? "#{number_with_delimiter(@drug_record.dosing_hard_max_quantity, :delimiter => ',')}\n#{@dosing_unit}" : "",
@drug_record.dosing_initial_quantity ? "#{number_with_delimiter(@drug_record.dosing_initial_quantity, :delimiter => ',')}\n#{@dosing_unit}" : "",
@drug_record.dosing_cumulative_max_quantity ? "#{number_with_delimiter(@drug_record.dosing_cumulative_max_quantity, :delimiter => ',')}\n#{@dosing_cumulative_max_unit}" : ""]]
@bolus_data = [[@drug_record.bolus_administration_rate_hard_min_quantity ? "#{number_with_delimiter(@drug_record.bolus_administration_rate_hard_min_quantity,
:delimiter => ',')}\n#{@bolus_unit}" : "",
@drug_record.bolus_administration_rate_soft_min_quantity ? "#{number_with_delimiter(@drug_record.bolus_administration_rate_soft_min_quantity,
:delimiter => ',')}\n#{@bolus_unit}" : "",
@drug_record.bolus_administration_rate_soft_max_quantity ? "#{number_with_delimiter(@drug_record.bolus_administration_rate_soft_max_quantity,
:delimiter => ',')}\n#{@bolus_unit}" : "",
@drug_record.bolus_administration_rate_hard_max_quantity ? "#{number_with_delimiter(@drug_record.bolus_administration_rate_hard_max_quantity,
:delimiter => ',')}\n#{@bolus_unit}" : "",
@drug_record.bolus_administration_rate_initial_quantity ? "#{number_with_delimiter(@drug_record.bolus_administration_rate_initial_quantity,
:delimiter => ',')}\n#{@bolus_unit}" : ""]]
@duration_data = [[@drug_record.duration_hard_min_quantity ? "#{number_with_delimiter(@drug_record.duration_hard_min_quantity,
:delimiter => ',')}\n#{@duration_unit}" : "",
@drug_record.duration_soft_min_quantity ? "#{number_with_delimiter(@drug_record.duration_soft_min_quantity,
:delimiter => ',')}\n#{@duration_unit}" : "",
@drug_record.duration_soft_max_quantity ? "#{number_with_delimiter(@drug_record.duration_soft_max_quantity,
:delimiter => ',')}\n#{@duration_unit}" : "",
@drug_record.duration_hard_max_quantity ? "#{number_with_delimiter(@drug_record.duration_hard_max_quantity,
:delimiter => ',')}\n#{@duration_unit}" : "",
@drug_record.duration_initial_quantity ? "#{number_with_delimiter(@drug_record.duration_initial_quantity,
:delimiter => ',')}\n#{@duration_unit}" : ""]]
@custom_fields = []
if @drug_record.prime_a_pump?
@drug_name_data = [["#{@drug_record.full_drug_name} : #{@drug_record.care_area.name}"]]
@header_data = [[@drug_record.full_population_name,
@drug_record.drug_class.name,
@drug_record.indication.readable_indication]]
@report_headers = ["Population", "Classification", "Indication"]
@header_column_widths = { 0 => 200, 1 => 150, 2 => 150 }
else
@drug_name_data = [["#{@drug_record.record_label} : #{@drug_record.care_area.name}"]]
@header_data = [[@drug_record.full_drug_name,
@drug_record.full_population_name,
@drug_record.drug_class.name,
@drug_record.indication.readable_indication]]
@report_headers = ["Drug Name", "Population", "Classification", "Indication"]
@header_column_widths = { 0 => 100, 1 => 180, 2 => 140, 3 => 140 }
end
@delivery_method_data = [[@drug_record.full_delivery_method_name,
"Version #{@drug_record.version}, #{@drug_record.created_at.to_date}"]]
@alert_vertical_offset = @drug_record.safety_alerts.any?{|x| x.alert_text.to_s.size>1200} ? 80 : 0
if @drug_record.safety_alerts.last
@drug_record.safety_alerts.each {|each_alert| @alert_vertical_offset += VERTICAL_OFFSET }
@alert_data = [[@drug_record.safety_alerts.map {|alert| "#{alert.kind.name} Alert: #{alert.text_without_newlines}"}.join("\n\n")]]
end
if @drug_record.custom_fields.first
@custom_field_sections = []
@custom_fields_no_section = []
@custom_fields_with_custom_section = []
#collect custom field sections
@drug_record.custom_fields.each do |custom_field|
if custom_field.attribute_format.drug_record_section_id
@custom_field_sections << DrugRecordSection.find(custom_field.attribute_format.drug_record_section_id)
end
end
@custom_field_sections.uniq!
#cycle custom field sections and add field values
@custom_field_sections.each do |section|
@drug_record.custom_fields.each do |custom_field|
if custom_field.attribute_format.drug_record_section_id && custom_field.attribute_format.drug_record_section_id == section.id
@custom_fields_with_custom_section << [section.name, custom_field.attribute_format.name, custom_field.string_value]
end
end
end
#collect non-custom field sections
@drug_record.custom_fields.each do |custom_field|
if !custom_field.attribute_format.drug_record_section_id
@custom_fields_no_section << [ custom_field.attribute_format.name, custom_field.string_value ]
end
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment