Last active
January 16, 2020 23:29
-
-
Save sirramongabriel/4118bbdd1091e79b001f6d9c4c61b23d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module PdfManager::MedicalRecordForm | |
class CreatePdf < ApplicationService | |
def initialize(args) | |
@pdfs = [] | |
@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 | |
# sort_meds | |
set_pages_to_print | |
set_scheduled_meds | |
# set_static_pdf_fields | |
# set_prn_meds | |
# Save all pdfs seperately | |
# Combine pdfs | |
# combine_then_save_pdfs | |
end | |
def set_pages_to_print | |
count = 0 | |
pdf_page_count = 0 | |
scheduled_meds = resident.medications.where(usage_type: 'Scheduled') | |
prn_meds = resident.medications.where(usage_type: 'PRN') | |
scheduled_pages = scheduled_meds.count.to_f.divmod(5) | |
prn_pages = scheduled_meds.count.to_f.divmod(3) | |
# | |
# IF scheduled_pages > prn_pages | |
# set pdf_page_count to scheduled_pages[0] | |
# create one additional page for remainders in either scheduled_meds or prn_meds | |
# add pdf to list | |
# | |
if scheduled_pages[0] > prn_pages[0] | |
scheduled_pages[0].times do | |
# @pdfs << instance_variable_set("@pdf_#{count+=1}", FillablePDF.new('app/pdfs/medication_record_form.pdf')) | |
instance_variable_set("@pdf_#{count+=1}", FillablePDF.new('app/pdfs/medication_record_form.pdf')) | |
end | |
if scheduled_pages[1] > 0.0 || prn_pages[1] > 0.0 | |
# @pdfs << instance_variable_set("@pdf_#{count+=1}", FillablePDF.new('app/pdfs/medication_record_form.pdf')) | |
instance_variable_set("@pdf_#{count+=1}", FillablePDF.new('app/pdfs/medication_record_form.pdf')) | |
end | |
elsif scheduled_pages[0] < prn_pages[0] | |
prn_pages[0].times do | |
# @pdfs << instance_variable_set("@pdf_#{count+=1}", FillablePDF.new('app/pdfs/medication_record_form.pdf')) | |
instance_variable_set("@pdf_#{count+=1}", FillablePDF.new('app/pdfs/medication_record_form.pdf')) | |
end | |
if scheduled_pages[1] > 0.0 || prn_pages[1] > 0.0 | |
# @pdfs << instance_variable_set("@pdf_#{count+=1}", FillablePDF.new('app/pdfs/medication_record_form.pdf')) | |
instance_variable_set("@pdf_#{count+=1}", FillablePDF.new('app/pdfs/medication_record_form.pdf')) | |
end | |
end | |
end | |
def set_scheduled_meds | |
# For every 5 meds create new pdf | |
# Fill out scheduled med_names & mark histogram with employee initials or 'X' | |
# Scheduled med vars | |
count = 0 | |
med_name_count = 1 | |
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 | |
# Meds | |
scheduled_meds = resident.medications.where(usage_type: 'Scheduled') | |
prn_meds = resident.medications.where(usage_type: 'PRN') | |
# For every 3 PRN meds create new pdf | |
# Fill out PRN med_names if any & mark histogram with employee initials or 'X' | |
# PRN med vars | |
prn_count = 0 | |
prn_med_name_count = 1 | |
prn_given_at_count = 1 | |
scheduled_pages = scheduled_meds.count.to_f.divmod(5) | |
prn_pages = scheduled_meds.count.to_f.divmod(3) | |
pdf_page_count = 0 | |
if scheduled_pages[0] > prn_pages[0] | |
scheduled_pages[0].times do | |
@pdfs << instance_variable_set("@pdf_#{count+=1}", FillablePDF.new('app/pdfs/medication_record_form.pdf')) | |
# instance_variable_set("@pdf_#{count+=1}", FillablePDF.new('app/pdfs/medication_record_form.pdf')) | |
end | |
if scheduled_pages[1] > 0.0 || prn_pages[1] > 0.0 | |
@pdfs << instance_variable_set("@pdf_#{count+=1}", FillablePDF.new('app/pdfs/medication_record_form.pdf')) | |
# instance_variable_set("@pdf_#{count+=1}", FillablePDF.new('app/pdfs/medication_record_form.pdf')) | |
end | |
elsif scheduled_pages[0] < prn_pages[0] | |
prn_pages[0].times do | |
@pdfs << instance_variable_set("@pdf_#{count+=1}", FillablePDF.new('app/pdfs/medication_record_form.pdf')) | |
# instance_variable_set("@pdf_#{count+=1}", FillablePDF.new('app/pdfs/medication_record_form.pdf')) | |
end | |
if scheduled_pages[1] > 0.0 || prn_pages[1] > 0.0 | |
@pdfs << instance_variable_set("@pdf_#{count+=1}", FillablePDF.new('app/pdfs/medication_record_form.pdf')) | |
# instance_variable_set("@pdf_#{count+=1}", FillablePDF.new('app/pdfs/medication_record_form.pdf')) | |
end | |
end | |
# Seperate loops for Scheduled & PRN medicine | |
scheduled_meds.map do |med| | |
if med_name_count >= 5 | |
med_name_count = 1 | |
count +=1 | |
# 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(: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')) | |
instance_variable_get("@pdf_#{count}").set_field("med#{med_name_count}_name", med.name) | |
# Print scheduled medicine name to page | |
instance_variable_get("@pdf_#{count}").set_field("med#{med_name_count+=1}_name", med.name) | |
# Print the times medicine was given | |
med.administers.each 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 | |
# Illustrate using 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 | |
# completed_pdfs << instance_variable_get("@pdf_#{count}") | |
end | |
# END Scheduled meds loop | |
# START PRN meds loop | |
prn_meds.map do |med| | |
# instance_variable_set("@pdf_#{count+=1}", FillablePDF.new('app/pdfs/medication_record_form.pdf')) | |
# Create a new fillable pdf | |
if prn_med_name_count >= 3 | |
prn_med_name_count = 1 | |
count +=1 | |
# instance_variable_set("@pdf_#{count+=1}", FillablePDF.new('app/pdfs/medication_record_form.pdf')) | |
# # 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')); | |
end | |
# Start print to PRN & its histogram | |
# Print medicine name to page | |
instance_variable_get("@pdf_#{count}").set_field("prn_med#{prn_med_name_count}_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 = 1 | |
end | |
instance_variable_get("@pdf_#{count}").set_field("prn_med#{prn_med_name_count}_given_day_#{prn_given_at_count}", med.time_formatted(x.time_taken)) | |
instance_variable_get("@pdf_#{count}").set_field("prn_med#{prn_med_name_count}_given", med.time_formatted(x.time_taken)) | |
# 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( | |
"prn_med#{prn_med_name_count}_given_day_#{today.strftime('%d')}", | |
employee.initials | |
) | |
elsif today.strftime('%d') <= Date.current.strftime('%d') | |
instance_variable_get("@pdf_#{count}").set_field( | |
"prn_med#{prn_med_name_count}_given_day_#{today.strftime('%-d')}", | |
'X' | |
) | |
end | |
end | |
end | |
end | |
end | |
# Generate name, filepath & upload to s3 each pdf in list | |
@pdfs.map do |pdf| | |
# Set name and file-path | |
stamp_time = FormatHelper.pdf_date_formatted(@medical_record_form.date_of_record) | |
# stamp_time = @medical_record_form.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