Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save sirramongabriel/846b68d04826ac292bb46c4d6c0a96b6 to your computer and use it in GitHub Desktop.
Save sirramongabriel/846b68d04826ac292bb46c4d6c0a96b6 to your computer and use it in GitHub Desktop.
First try
module PdfManager::MedicalRecordForm
class CreatePdf < ApplicationService
# attrs aren't needed to test
# attr_reader :provider, :account, :resident, :medical_record_form, :medical_record_forms
def initialize(args)
# @pdf = FillablePDF.new('app/pdfs/medication_record_form.pdf')
@pdfs = []
# Not needed in test
# @provider = args[:provider]
# @account = args[:account]
# @resident = args[:resident]
# @medical_record_form = args[:medical_record_form]
# @medical_record_forms = args[:medical_record_forms]
end
def call
convert_to_pdf
end
private
def convert_to_pdf
set_scheduled_meds
end
def set_scheduled_meds
# For every 5 meds create new pdf
# Fill out scheduled meds & histogram
# Scheduled med vars
count = 0
med_name_count = 0
given_at_count = 0
# Vars universal to page
current_month = (Date.current.beginning_of_month..Date.current.end_of_month).to_a
stamp_time = @medical_record_form.date_of_record
# stamp_time = FormatHelper.pdf_date_formatted(@medical_record_form.date_of_record)
pdf_uuid_count = 0
completed_pdfs = []
combined_pdf = CombinePDF.new
# For every 3 PRN meds create new pdf
# Fill out PRN meds if any & histogram
# PRN med vars
prn_count = 0
prn_med_name_count = 0
prn_given_at_count = 0
resident.medications.where(usage_type: 'Scheduled').each_slice(5) do |page|
if med_name_count == 0
# Create a new fillable pdf
instance_variable_set("@pdf_#{count+=1}", FillablePDF.new('app/pdfs/medication_record_form.pdf'))
end
# Set page's static fields
instance_variable_get("@pdf_#{count}").set_field(:resident_full_name, resident.full_name)
instance_variable_get("@pdf_#{count}").set_field(:month, medical_record_form.date_of_record.strftime('%B'))
instance_variable_get("@pdf_#{count}").set_field(:year, medical_record_form.date_of_record.strftime('%Y'));
page.map do |med|
# Print to scheduled medicine & histogram
if med_name_count >= 5
med_name_count = 0
# instance_variable_set("@pdf_#{count+=1}", FillablePDF.new('app/pdfs/medication_record_form.pdf'))
end
# Print medicine name to page
instance_variable_get("@pdf_#{count}").set_field("med#{med_name_count+=1}_name", med.name)
# Print times medicine was given
med.administers.each_slice(4) do |page|
page.map do |x|
if given_at_count >= 4
given_at_count = 0
end
instance_variable_get("@pdf_#{count}").set_field("med#{med_name_count}_given#{given_at_count+=1}", med.time_formatted(x.time_taken))
end
end
# Illustrate with Employee initials if med was given for specific day in histogram
# Place X for days that have passed but no medicine administration given
current_month.map do |today|
if medical_record_forms.any? { |record| record.date_of_record == today && record.medications.include?(med) && today.strftime('%d') <= Date.current.strftime('%d')}
employee_id = medical_record_forms.where({date_of_record: today}).first.employee_id
employee = Employee.find_by!(id: employee_id)
instance_variable_get("@pdf_#{count}").set_field(
"med#{med_name_count}_given#{given_at_count}_day_#{today.strftime('%d')}",
employee.initials
)
elsif today.strftime('%d') <= Date.current.strftime('%d')
instance_variable_get("@pdf_#{count}").set_field(
"med#{med_name_count}_given#{given_at_count}_day_#{today.strftime('%d')}",
'X'
)
end
end
end
completed_pdfs << instance_variable_get("@pdf_#{count}")
end
# END Scheduled meds loop
# START PRN meds loop
resident.medications.where(usage_type: 'PRN').each_slice(3) do |page|
# Create a new fillable pdf
if prn_med_name_count >= 3
prn_med_name_count = 0
instance_variable_set("@pdf_#{count+=1}", FillablePDF.new('app/pdfs/medication_record_form.pdf'))
end
page.map do |med|
# Start print to PRN & its histogram
# Print medicine name to page
instance_variable_get("@pdf_#{count}".to_sym).set_field("prn_med#{prn_med_name_count+=1}_name", med.name)
# Print times medicine was given
med.administers.each_slice(1) do |page|
page.map do |x|
if prn_given_at_count >= 1
prn_given_at_count = 0
end
instance_variable_get("@pdf_#{count}").set_field("prn_med#{prn_med_name_count}_given_day_#{prn_given_at_count+=1}", med.time_formatted(x.time_taken))
end
end
# Illustrate with Employee initials if med was given for specific day in histogram
# Place X for days that have passed but no medicine administration given
current_month.map do |today|
if medical_record_forms.any? { |record| record.date_of_record == today && record.medications.include?(med) && today.strftime('%d') <= Date.current.strftime('%d')}
# given_at_count+=1
employee_id = medical_record_forms.where({date_of_record: today}).first.employee_id
employee = Employee.find_by!(id: employee_id)
instance_variable_get("@pdf_#{count}").set_field(
"prn_med#{prn_med_name_count}_given",
employee.initials
)
elsif today.strftime('%d') <= Date.current.strftime('%d')
instance_variable_get("@pdf_#{count}").set_field(
"prn_med#{prn_med_name_count}_given",
'X'
)
end
end
end
completed_pdfs << instance_variable_get("@pdf_#{count}")
end
completed_pdfs.each do |pdf|
# Set name and file-path
stamp_time = FormatHelper.pdf_date_formatted(@medical_record_form.date_of_record)
pdf_uuid = "#{resident.first_name.downcase}_#{resident.last_name.downcase}_medication_record_for_#{medical_record_form.pdf_date_formatted(stamp_time)}_#{pdf_uuid_count+=1}.pdf"
pdf_uuid_filepath = Rails.root.join('app/pdfs/store/medical_record_forms/', pdf_uuid).to_s
# Save locally
# pdf = combined_pdf.save(pdf_uuid_filepath)
# instance_variable_get("@pdf_#{count}").save_as(pdf_uuid_filepath)
pdf.save_as(pdf_uuid_filepath)
# Upload to s3
s3 = Aws::S3::Resource.new(region:'us-east-1')
obj = s3.bucket('whizcharts').object("pdfs/medical_record_forms/#{pdf_uuid}")
# obj = s3.bucket('whizcharts').object("/pdfs/medical_record_forms/#{pdf_uuid}")
obj.upload_file("#{pdf_uuid_filepath}")
# File.delete(Rails.root + '/foo.jpg')
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment