<cftry>

  <!--- ── Paths ─────────────────────────────────────────────────────── --->
  <cfset blankPdf  = expandPath("/patientadmin_new/patient/admission_pdfs/agency_#SESSION.agencyid#.pdf")>
  <cfset filepath  = expandPath("/datafiles/patientAdmissionPdf/")>
  <cfif NOT directoryExists(filepath)>
    <cfset directoryCreate(filepath)>
  </cfif>

  <cfset timestamp  = dateFormat(now(),"yyyymmdd") & timeFormat(now(),"HHmmss")>
  <cfset filename   = "patient_#patientId#_admit_#admitId#_new_pdf_admission.pdf">
  <cfset outputPdf  = filepath & filename>

  <!--- ── Helper: safe field read ──────────────────────────────────── --->
  <cffunction name="fv" returntype="string" access="private">
    <cfargument name="newKey" type="string">
    <cfargument name="oldKey" type="string" default="">
    <cfif structKeyExists(requestBody, arguments.newKey) AND len(trim(toString(requestBody[arguments.newKey])))>
      <cfreturn trim(toString(requestBody[arguments.newKey]))>
    </cfif>
    <cfif arguments.oldKey NEQ "" AND structKeyExists(requestBody, arguments.oldKey)>
      <cfreturn trim(toString(requestBody[arguments.oldKey]))>
    </cfif>
    <cfreturn "">
  </cffunction>

  <!--- ── Fill PDF using cfpdfform + cfpdfformparam ────────────────────── --->
  <cfpdfform action="populate"
        source="#blankPdf#"
        destination="#outputPdf#"
        overwrite="yes">

    <!--- ── Admission Service Agreement ─────────────────────────── --->
    <cfpdfformparam name="nametitle if applicable they"             value="#fv('admission_legal_rep_name_title','nametitle if applicable they')#">
    <cfpdfformparam name="i_have_legal_representative"              value="#fv('admission_has_legal_rep','i_have_legal_representative') EQ '1' ? 'Yes' : 'No'#">
    <cfpdfformparam name="namerelationship to patient I"            value="#fv('admission_selected_rep_name_relationship','namerelationship to patient I')#">
    <cfpdfformparam name="my_selected_representative"               value="#fv('admission_has_selected_rep','my_selected_representative') EQ '1' ? 'Yes' : 'No'#">
    <cfpdfformparam name="the_patients_rights"                      value="#fv('admission_send_patient_rights','the_patients_rights') EQ '1' ? 'Yes' : 'No'#">
    <cfpdfformparam name="the_agencys_transfer"                     value="#fv('admission_send_transfer_discharge_policy','the_agencys_transfer') EQ '1' ? 'Yes' : 'No'#">
    <cfpdfformparam name="hmo_participant"                          value="#fv('admission_hmo_member','hmo_participant')#">
    <cfpdfformparam name="billing_method"                           value="#fv('admission_billing_type','billing_method')#">
    <cfpdfformparam name="of charges to"                            value="#fv('admission_insurance_coverage_pct','of charges to')#">
    <cfpdfformparam name="insurance_coverage_varies"                value="#fv('admission_insurance_specify_amounts','insurance_coverage_varies')#">
    <cfpdfformparam name="ackno_sig_agency_representative_signature"          value="#fv('admission_agency_rep_signature','ackno_sig_agency_representative_signature')#">
    <cfpdfformparam name="ackno_sig_shared_agency_rep_date"         value="#fv('admission_agency_rep_sign_date','ackno_sig_shared_agency_rep_date')#">
    <cfpdfformparam name="Patient Signature 2"                      value="#fv('admission_patient_signature','Patient Signature 2')#">
    <cfpdfformparam name="ackno_sig_shared_patient_date"            value="#fv('admission_patient_sign_date','ackno_sig_shared_patient_date')#">
    <cfpdfformparam name="Financial Guarantor Signature (if applicable)" value="#fv('admission_financial_guarantor_signature','Financial Guarantor Signature (if applicable)')#">
    <cfpdfformparam name="shared_financial_guarantor_date"          value="#fv('admission_financial_guarantor_sign_date','shared_financial_guarantor_date')#">
    <cfpdfformparam name="Patient Signature 3"                      value="#fv('privacy_patient_signature','Patient Signature 3')#">
    <cfpdfformparam name="ackno_receipt_shared_patient_date"        value="#fv('ackno_receipt_shared_patient_date')#">
    <cfpdfformparam name="Patient Legal Rep Signature (if applicable)" value="#fv('privacy_legal_rep_signature','Patient Legal Rep Signature (if applicable)')#">
    <cfpdfformparam name="Date26_af_date"                           value="#fv('privacy_legal_rep_sign_date','Date26_af_date')#">
    <cfpdfformparam name="ackno_receipt_agency_representative_signature"         value="#fv('ackno_receipt_agency_representative_signature')#">
    <cfpdfformparam name="ackno_receipt_shared_agency_rep_date"                   value="#fv('ackno_receipt_shared_agency_rep_date')#">
    <cfpdfformparam name="patient_unable_to_sign_because:"          value="#fv('privacy_patient_unable_to_sign_reason','patient_unable_to_sign_because:')#">

    <!--- ── Visit Plan ───────────────────────────────────────────── --->
    <cfpdfformparam name="visit_plan_patient_name"                  value="#fv('visit_plan_patient_name')#">
    <cfpdfformparam name="visit_plan_patient_id"                    value="#fv('visit_plan_patient_id')#">
    <cfpdfformparam name="skilled_nursing_sn"                       value="#fv('visit_plan_svc_sn','skilled_nursing_sn') EQ '1' ? 'Yes' : 'No'#">
    <cfpdfformparam name="freq_skilled_nursing_sn"                  value="#fv('visit_plan_freq_sn','freq_skilled_nursing_sn')#">
    <cfpdfformparam name="home_health_aide_hha"                     value="#fv('visit_plan_svc_hha','home_health_aide_hha') EQ '1' ? 'Yes' : 'No'#">
    <cfpdfformparam name="freq_home_health_aide_hha"                value="#fv('visit_plan_freq_hha','freq_home_health_aide_hha')#">
    <cfpdfformparam name="physical_therapy_pt"                      value="#fv('visit_plan_svc_pt','physical_therapy_pt') EQ '1' ? 'Yes' : 'No'#">
    <cfpdfformparam name="freq_physical_therapy_pt"                 value="#fv('visit_plan_freq_pt','freq_physical_therapy_pt')#">
    <cfpdfformparam name="occupational_therapy_ot"                  value="#fv('visit_plan_svc_ot','occupational_therapy_ot') EQ '1' ? 'Yes' : 'No'#">
    <cfpdfformparam name="freq_occupational_therapy_ot"             value="#fv('visit_plan_freq_ot','freq_occupational_therapy_ot')#">
    <cfpdfformparam name="medical_social_work_msw"                  value="#fv('visit_plan_svc_msw','medical_social_work_msw') EQ '1' ? 'Yes' : 'No'#">
    <cfpdfformparam name="freq_medical_social_work_msw"             value="#fv('visit_plan_freq_msw','freq_medical_social_work_msw')#">
    <cfpdfformparam name="speech_pathology_slp"                     value="#fv('visit_plan_svc_slp','speech_pathology_slp') EQ '1' ? 'Yes' : 'No'#">
    <cfpdfformparam name="freq_speech_pathology_slp"                value="#fv('visit_plan_freq_slp','freq_speech_pathology_slp')#">
    <cfpdfformparam name="visit_plan_patient_signature"             value="#fv('visit_plan_patient_signature','Signature30')#">
    <cfpdfformparam name="visit_plan_patient_sig_date"              value="#fv('visit_plan_patient_sign_date','visit_plan_patient_sig_date')#">
    <cfpdfformparam name="visit_plan_agency_representative"         value="#fv('visit_plan_agency_rep_signature','visit_plan_agency_representative')#">
    <cfpdfformparam name="visit_plan_agency_rep_date"               value="#fv('visit_plan_agency_rep_sign_date','visit_plan_agency_rep_date')#">

    <!--- ── Assignment of Benefits ──────────────────────────────── --->
    <cfpdfformparam name="aob_authorizing_payor_name"               value="#fv('aob_authorizing_payor_name')#">
    <cfpdfformparam name="Program Name"                             value="#fv('aob_primary_program_name','Program Name')#">
    <cfpdfformparam name="Program Name_2"                           value="#fv('aob_secondary_program_name','Program Name_2')#">
    <cfpdfformparam name="Policy Number"                            value="#fv('aob_primary_policy_number','Policy Number')#">
    <cfpdfformparam name="Policy Number_2"                          value="#fv('aob_secondary_policy_number','Policy Number_2')#">
    <cfpdfformparam name="Claim Number"                             value="#fv('aob_primary_claim_number','Claim Number')#">
    <cfpdfformparam name="Claim Number_2"                           value="#fv('aob_secondary_claim_number','Claim Number_2')#">
    <cfpdfformparam name="Group Number"                             value="#fv('aob_primary_group_number','Group Number')#">
    <cfpdfformparam name="Group Number_2"                           value="#fv('aob_secondary_group_number','Group Number_2')#">
    <cfpdfformparam name="Primary Insured"                          value="#fv('aob_primary_insured_name','Primary Insured')#">
    <cfpdfformparam name="Primary Insured_2"                        value="#fv('aob_secondary_insured_name','Primary Insured_2')#">
    <cfpdfformparam name="shared_patient_dob"                       value="#fv('aob_primary_insured_dob','shared_patient_dob')#">
    <cfpdfformparam name="aob_agency_representative"                value="#fv('aob_agency_representative')#">
    <cfpdfformparam name="Patient Signature 4"                      value="#fv('aob_patient_signature','Patient Signature 4')#">
    <cfpdfformparam name="aob_shared_patient_date"                  value="#fv('aob_shared_patient_date')#">
    <cfpdfformparam name="Financial Guarantor Signature 2 (if applicable)" value="#fv('aob_financial_guarantor_signature','Financial Guarantor Signature 2 (if applicable)')#">
    <cfpdfformparam name="aob_shared_financial_guarantor_date"      value="#fv('aob_shared_financial_guarantor_date')#">
    <cfpdfformparam name="aob_shared_agency_rep_date"               value="#fv('aob_shared_agency_rep_date')#">
    <cfpdfformparam name="aob_primary_patient_dob"                  value="#fv('aob_primary_patient_dob')#">
    <cfpdfformparam name="aob_secondary_patient_dob"                value="#fv('aob_secondary_patient_dob')#">

    <!--- ── ACH Authorization ────────────────────────────────────── --->
    <cfpdfformparam name="ach_shared_patient_name"                  value="#fv('ach_shared_patient_name')#">
    <cfpdfformparam name="ach_shared_patient_id"                    value="#fv('ach_shared_patient_id')#">
    <cfpdfformparam name="ach_account_type"                         value="#fv('ach_account_type','ach_account_type')#">
    <cfpdfformparam name="Depository Name"                          value="#fv('ach_depository_name','Depository Name')#">
    <cfpdfformparam name="Account Holder Name"                      value="#fv('ach_account_holder_name','Account Holder Name')#">
    <cfpdfformparam name="Routing Number"                           value="#fv('ach_routing_number','Routing Number')#">
    <cfpdfformparam name="Account Number"                           value="#fv('ach_account_number','Account Number')#">
    <cfpdfformparam name="Account Holder Names"                     value="#fv('ach_account_holder_print_name','Account Holder Names')#">
    <cfpdfformparam name="Account Holder Signature"                 value="#fv('ach_account_holder_signature','Account Holder Signature')#">
    <cfpdfformparam name="Date33_af_date"                           value="#fv('ach_authorization_date','Date33_af_date')#">

    <!--- ── Credit Card ──────────────────────────────────────────── --->
    <cfpdfformparam name="cc_shared_patient_name"                   value="#fv('cc_shared_patient_name')#">
    <cfpdfformparam name="cc_shared_patient_id"                     value="#fv('cc_shared_patient_id')#">
    <cfpdfformparam name="MasterCard"                               value="#fv('cc_card_type_mastercard','MasterCard')#">
    <cfpdfformparam name="VISA"                                     value="#fv('cc_card_type_visa','VISA')#">
    <cfpdfformparam name="Discover"                                 value="#fv('cc_card_type_discover','Discover')#">
    <cfpdfformparam name="AMEX"                                     value="#fv('cc_card_type_amex','AMEX')#">
    <cfpdfformparam name="cc_card_type_other"                       value="#fv('cc_card_type_other','Other')#">
    <cfpdfformparam name="cc_card_type_other_description"           value="#fv('cc_card_type_other_description')#">
    <cfpdfformparam name="Cardholder Name as shown on card"         value="#fv('cc_cardholder_name','Cardholder Name as shown on card')#">
    <cfpdfformparam name="Card Number"                              value="#fv('cc_card_number','Card Number')#">
    <cfpdfformparam name="Expiration Date mmyy"                     value="#fv('cc_expiration_date','Expiration Date mmyy')#">
    <cfpdfformparam name="Cardholder ZIP Code from credit card billing address" value="#fv('cc_billing_zip','Cardholder ZIP Code from credit card billing address')#">
    <cfpdfformparam name="cc_initial_deposit_authorization"         value="#fv('cc_initial_deposit_auth','cc_initial_deposit_authorization')#">
    <cfpdfformparam name="NA  I authorize Agency to charge my credit card"   value="#fv('cc_initial_deposit_amount','NA  I authorize Agency to charge my credit card')#">
    <cfpdfformparam name="cc_medical_supplies_authorization"        value="#fv('cc_supplies_auth','cc_medical_supplies_authorization')#">
    <cfpdfformparam name="NA  I authorize Agency to charge my credit card_2" value="#fv('cc_supplies_amount','NA  I authorize Agency to charge my credit card_2')#">
    <cfpdfformparam name="cc_medical_supplies_frequency_type"       value="#fv('cc_supplies_recurring','cc_medical_supplies_frequency_type')#">
    <cfpdfformparam name="every"                                    value="#fv('cc_recurring_frequency_detail','every')#">
    <cfpdfformparam name="cc_payment_for_services_authorization"    value="#fv('cc_services_payment_auth','cc_payment_for_services_authorization')#">
    <cfpdfformparam name="Cardholder Signature"                     value="#fv('cc_cardholder_signature','Cardholder Signature')#">
    <cfpdfformparam name="Date32_af_date"                           value="#fv('cc_authorization_date','Date32_af_date')#">

    <!--- ── LEP / Sensory ────────────────────────────────────────── --->
    <cfpdfformparam name="i_have_difficulty"                        value="#fv('lep_difficulty_speaking_hearing','i_have_difficulty') EQ '1' ? 'Yes' : 'No'#">
    <cfpdfformparam name="i_require_assistance"                     value="#fv('lep_needs_written_assistance','i_require_assistance') EQ '1' ? 'Yes' : 'No'#">
    <cfpdfformparam name="i_need_translation"                       value="#fv('lep_needs_translation','i_need_translation') EQ '1' ? 'Yes' : 'No'#">
    <cfpdfformparam name="lep_can_speak"                            value="#fv('lep_q1_able_to_speak','lep_can_speak')#">
    <cfpdfformparam name="lep_can_lip_read"                         value="#fv('lep_q2_lip_reading','lep_can_lip_read')#">
    <cfpdfformparam name="lep_can_communicate_writing"              value="#fv('lep_q3_written_communication','lep_can_communicate_writing')#">
    <cfpdfformparam name="lep_picture_word_cards_help"              value="#fv('lep_q4_picture_cards','lep_picture_word_cards_help')#">
    <cfpdfformparam name="lep_telephone_amplifier_benefit"          value="#fv('lep_q5_phone_amplifier','lep_telephone_amplifier_benefit')#">
    <cfpdfformparam name="lep_requires_braille"                     value="#fv('lep_q6_braille','lep_requires_braille')#">
    <cfpdfformparam name="lep_wants_translator_interpreter"         value="#fv('lep_q7_translator_interpreter','lep_wants_translator_interpreter')#">
    <cfpdfformparam name="lep_can_use_tty_tdd"                      value="#fv('lep_q8_tty_tdd','lep_can_use_tty_tdd')#">
    <cfpdfformparam name="lep_shared_patient_name"                  value="#fv('lep_shared_patient_name')#">
    <cfpdfformparam name="lep_shared_patient_date"                  value="#fv('lep_shared_patient_date')#">
    <cfpdfformparam name="Patient Signature 5"                      value="#fv('lep_ack_patient_signature','Patient Signature 5')#">

    <!--- ── Advance Directives ───────────────────────────────────── --->
    <cfpdfformparam name="ad_shared_patient_name"                   value="#fv('ad_shared_patient_name')#">
    <cfpdfformparam name="ad_shared_patient_id"                     value="#fv('ad_shared_patient_id')#">
    <cfpdfformparam name="do_not_have_an_advance_directive"         value="#fv('advdir_status_no_directive','do_not_have_an_advance_directive') EQ '1' ? 'Yes' : 'No'#">
    <cfpdfformparam name="have_a_living_will"                       value="#fv('advdir_status_living_will','have_a_living_will') EQ '1' ? 'Yes' : 'No'#">
    <cfpdfformparam name="have_a_durable_power"                     value="#fv('advdir_status_poa','have_a_durable_power') EQ '1' ? 'Yes' : 'No'#">
    <cfpdfformparam name="Named POA"                                value="#fv('advdir_poa_name','Named POA')#">
    <cfpdfformparam name="Phone"                                    value="#fv('advdir_poa_phone','Phone')#">
    <cfpdfformparam name="have_a_do_not_resuscitate"                value="#fv('advdir_status_dnr','have_a_do_not_resuscitate') EQ '1' ? 'Yes' : 'No'#">
    <cfpdfformparam name="have_another_type_of_advance_directive"   value="#fv('advdir_status_other','have_another_type_of_advance_directive') EQ '1' ? 'Yes' : 'No'#">
    <cfpdfformparam name="I have another type of Advance Directive"  value="#fv('advdir_status_other_specify','I have another type of Advance Directive')#">
    <cfpdfformparam name="Patient Signature 6"                      value="#fv('advdir_patient_signature','Patient Signature 6')#">
    <cfpdfformparam name="ad_shared_patient_date"                   value="#fv('ad_shared_patient_date')#">
    <cfpdfformparam name="ad_agency_representative"                 value="#fv('ad_agency_representative')#">
    <cfpdfformparam name="ad_shared_agency_rep_date"                value="#fv('ad_shared_agency_rep_date')#">

    <!--- ── Patient Rights (page 19) ────────────────────────────── --->
    <cfpdfformparam name="rights_agency_representative"             value="#fv('rights_agency_representative')#">
    <cfpdfformparam name="rights_shared_agency_rep_date"            value="#fv('rights_shared_agency_rep_date')#">
    <cfpdfformparam name="Patient Signature 7"                      value="#fv('page19_patient_signature','Patient Signature 7')#">
    <cfpdfformparam name="rights_shared_patient_date"               value="#fv('rights_shared_patient_date')#">

    <!--- ── HIPAA ────────────────────────────────────────────────── --->
    <cfpdfformparam name="hipaa_patient_name"                       value="#fv('hipaa_patient_name','shared_patient_name')#">
    <cfpdfformparam name="shared_patient_dob"                       value="#fv('hipaa_patient_dob','shared_patient_dob')#">
    <cfpdfformparam name="hipaa_patient_signature_name"             value="#fv('hipaa_patient_signature_name')#">
    <cfpdfformparam name="name_and_relationship"                    value="#fv('hipaa_rep_name_relationship','name_and_relationship')#">
    <cfpdfformparam name="authority_to_act:_poa"                    value="#fv('hipaa_authority_poa','authority_to_act:_poa') EQ '1' ? 'Yes' : 'No'#">
    <cfpdfformparam name="legal_guardian"                           value="#fv('hipaa_authority_legal_guardian','legal_guardian') EQ '1' ? 'Yes' : 'No'#">
    <cfpdfformparam name="Patient Signature 8"                      value="#fv('hipaa_patient_signature','Patient Signature 8')#">
    <cfpdfformparam name="hipaa_patient_signature_date"             value="#fv('hipaa_patient_signature_date')#">
    <cfpdfformparam name="Patient Legal Rep Signature 2 (if applicable)" value="#fv('hipaa_legal_rep_signature','Patient Legal Rep Signature 2 (if applicable)')#">
    <cfpdfformparam name="Date40_af_date"                           value="#fv('hipaa_legal_rep_sign_date','Date40_af_date')#">

    <!--- ── Emergency Preparedness ──────────────────────────────── --->
    <cfpdfformparam name="ep_shared_patient_name"                   value="#fv('ep_shared_patient_name')#">
    <cfpdfformparam name="shared_patient_id"                        value="#fv('emerg_patient_id','shared_patient_id')#">
    <cfpdfformparam name="ep_emergency_contact_name"                value="#fv('emerg_contact_name','ep_emergency_contact_name')#">
    <cfpdfformparam name="ep_emergency_contact_phone"               value="#fv('emerg_contact_phone','ep_emergency_contact_phone')#">
    <cfpdfformparam name="ep_patient_address"                       value="#fv('ep_patient_address')#">
    <cfpdfformparam name="ep_transport_assistance"                  value="#fv('emerg_transport_type','ep_transport_assistance')#">
    <cfpdfformparam name="ep_critical_medication_dme"               value="#fv('ep_critical_medication_dme')#">
    <cfpdfformparam name="ep_additional_life_sustaining_treatments" value="#fv('ep_additional_life_sustaining_treatments')#">
    <cfpdfformparam name="ep_classification_level"                  value="#fv('emerg_patient_classification','ep_classification_level')#">
    <cfpdfformparam name="ep_other_emergency_plan"                  value="#fv('ep_other_emergency_plan')#">
    <cfpdfformparam name="Patient Signature 9"                      value="#fv('emerg_patient_signature','Patient Signature 9')#">
    <cfpdfformparam name="ep_patient_signature_date"                value="#fv('ep_patient_signature_date')#">
    <cfpdfformparam name="agency_representative_signature"          value="#fv('agency_representative_signature')#">
    <cfpdfformparam name="shared_agency_rep_date"                   value="#fv('emerg_agency_rep_sign_date','shared_agency_rep_date')#">

    <!--- ── When to Contact rows ─────────────────────────────────── --->
    <cfpdfformparam name="contact_other1_situation"                 value="#fv('contactguide_row1_situation','contact_other1_situation')#">
    <cfpdfformparam name="contact_other1_call_agency"               value="#fv('contactguide_row1_call_agency','contact_other1_call_agency')#">
    <cfpdfformparam name="contact_other1_call_doctor"               value="#fv('contactguide_row1_call_doctor','contact_other1_call_doctor')#">
    <cfpdfformparam name="contact_other1_call_911"                  value="#fv('contactguide_row1_call_911','contact_other1_call_911')#">
    <cfpdfformparam name="contact_other2_situation"                 value="#fv('contactguide_row2_situation','contact_other2_situation')#">
    <cfpdfformparam name="contact_other2_call_agency"               value="#fv('contactguide_row2_call_agency','contact_other2_call_agency')#">
    <cfpdfformparam name="contact_other2_call_doctor"               value="#fv('contactguide_row2_call_doctor','contact_other2_call_doctor')#">
    <cfpdfformparam name="contact_other2_call_911"                  value="#fv('contactguide_row2_call_911','contact_other2_call_911')#">
    <cfpdfformparam name="contact_other3_situation"                 value="#fv('contactguide_row3_situation','contact_other3_situation')#">
    <cfpdfformparam name="contact_other3_call_agency"               value="#fv('contactguide_row3_call_agency','contact_other3_call_agency')#">
    <cfpdfformparam name="contact_other3_call_doctor"               value="#fv('contactguide_row3_call_doctor','contact_other3_call_doctor')#">
    <cfpdfformparam name="contact_other3_call_911"                  value="#fv('contactguide_row3_call_911','contact_other3_call_911')#">
    <cfpdfformparam name="contact_other4_situation"                 value="#fv('contactguide_row4_situation','contact_other4_situation')#">
    <cfpdfformparam name="contact_other4_call_agency"               value="#fv('contactguide_row4_call_agency','contact_other4_call_agency')#">
    <cfpdfformparam name="contact_other4_call_doctor"               value="#fv('contactguide_row4_call_doctor','contact_other4_call_doctor')#">
    <cfpdfformparam name="contact_other4_call_911"                  value="#fv('contactguide_row4_call_911','contact_other4_call_911')#">

    <!--- ── Discharge & eConsent ─────────────────────────────────── --->
    <cfpdfformparam name="Patient Signature 10"                     value="#fv('discharge_ack_patient_signature','Patient Signature 10')#">
    <cfpdfformparam name="discharge_transfer_patient_date"          value="#fv('discharge_transfer_patient_date')#">
    <cfpdfformparam name="econsent_patient_name"                    value="#fv('econsent_patient_name','shared_patient_name')#">
    <cfpdfformparam name="Patient Signature 11"                     value="#fv('econsent_patient_signature','Patient Signature 11')#">
    <cfpdfformparam name="econsent_sign_date"                       value="#fv('econsent_sign_date','shared_patient_date')#">
    <cfpdfformparam name="Authorized Representative"                value="#fv('econsent_rep_name','Authorized Representative')#">
    <cfpdfformparam name="Relationship"                             value="#fv('econsent_rep_relationship','Relationship')#">

    <!--- ── Shared / repeating fields ────────────────────────────── --->
    <cfpdfformparam name="shared_patient_name"                      value="#fv('shared_patient_name')#">
    <cfpdfformparam name="shared_patient_id"                        value="#fv('shared_patient_id')#">
    <cfpdfformparam name="shared_patient_dob"                       value="#fv('shared_patient_dob','hipaa_patient_dob')#">
    <cfpdfformparam name="shared_patient_date"                      value="#fv('shared_patient_date','admission_patient_sign_date')#">
    <cfpdfformparam name="shared_agency_rep_date"                   value="#fv('shared_agency_rep_date','admission_agency_rep_sign_date')#">
    <cfpdfformparam name="shared_financial_guarantor_date"          value="#fv('shared_financial_guarantor_date','aob_fg_sign_date')#">

  </cfpdfform>

  <!--- Flatten the PDF --->
  <!--- <cfpdf  action="write" source="#outputPdf#" destination="#outputPdf#" flatten="yes"> --->

  <cfset path = 'Patient_files/agency_'&#session.AgencyId# />
  <!--- Upload to Amazon S3--->
  <cfset s3PrefixPath = "s3://#Request.bucket_accesskey#:#Request.bucket_secretkey#@#Request.bucket_name#/#path#" />
  <cfif not directoryExists(s3PrefixPath)>
    <cftry>
      <cfdirectory action="create" directory="#s3PrefixPath#" />
      <cfcatch type="any">
        <!--- S3 bucket/prefix may already exist; directoryExists can be false while create returns 409 BucketAlreadyOwnedByYou --->
        <cfset s3DirErr = cfcatch.message & " " & cfcatch.detail />
        <cfif structKeyExists(cfcatch, "rootCause") AND isStruct(cfcatch.rootCause) AND structKeyExists(cfcatch.rootCause, "message")>
          <cfset s3DirErr = s3DirErr & " " & cfcatch.rootCause.message />
        </cfif>
        <cfif findNoCase("BucketAlreadyOwnedByYou", s3DirErr) EQ 0
          AND findNoCase("BucketAlreadyExists", s3DirErr) EQ 0
          AND findNoCase("ResponseCode: 409", s3DirErr) EQ 0>
          <cfrethrow />
        </cfif>
      </cfcatch>
    </cftry>
  </cfif>
  <cfset pdfBinaryContent = fileReadBinary("#filepath##filename#")>
  <cffile action = "move" destination="s3://#Request.bucket_accesskey#:#Request.bucket_secretkey#@#Request.bucket_name#/#path#/#filename#"
          source = "#filepath##filename#" >

  <!--- ── Register the final submitted PDF in `files` so it can be reused by the
        existing Send Fax / S3 viewer flows (Print, Download, Fax buttons).
        Draft saves (isSubmitted EQ 0) are intentionally skipped. ──────────── --->
  <cfset admissionPdfFileId = 0>
  <cfif isSubmitted EQ 1>
    <cfquery name="InsertAdmissionPdfFile" datasource="#Application.DataSrc#">
      INSERT INTO #Request.prefix_db_agency#.files (
        AgencyId, PatientId, Title, FileName, FileType, Dup_file_name, File, CreatedBy
      ) VALUES (
        <cfqueryparam value="#session.AgencyId#" cfsqltype="cf_sql_integer">,
        <cfqueryparam value="#patientId#" cfsqltype="cf_sql_integer">,
        <cfqueryparam value="New Admission PDF" cfsqltype="cf_sql_varchar">,
        <cfqueryparam value="#filename#" cfsqltype="cf_sql_varchar">,
        <cfqueryparam value="application/pdf" cfsqltype="cf_sql_varchar">,
        <cfqueryparam value="#filename#" cfsqltype="cf_sql_varchar">,
        <cfqueryparam value="#pdfBinaryContent#" cfsqltype="cf_sql_blob">,
        <cfqueryparam value="#val(session.employeeid)#" cfsqltype="cf_sql_integer">
      )
    </cfquery>
    <cfquery name="GetAdmissionPdfFileId" datasource="#Application.DataSrc#">
      SELECT LAST_INSERT_ID() AS fileId
    </cfquery>
    <cfset admissionPdfFileId = val(GetAdmissionPdfFileId.fileId)>
  </cfif>

<cfcatch type="any">
</cfcatch>
</cftry>