<cfparam name="url.redirect_from" default="">
<cfparam name="url.download" default="">
<cfparam name="url.report" default="visit_utlization">
<cfset Redirect_to = #url.redirect_from# />
<cfset session.cs.patientid = #url.ID# />
<cfset Patient_ID = #url.ID# />
 <cfif isNumeric(url.AID)>
  <cfset Assessment_ID = #url.AID# />  
<cfelse>
 <cfset Assessment_ID   = decrypt(#url.AID#,"54kczhzgHTj4a8n+SRZPyg==","AES","Hex") />
  <cfset url.AID   = decrypt(#url.AID#,"54kczhzgHTj4a8n+SRZPyg==","AES","Hex") />
</cfif>
<cfset Request.prefix_db_lookup = 'hhapowerpath' />
<cfset user_SN = '0' />
<cfset user_Th = '0' />
<cfset user_HHA = '0' />
<cfset user_All = '0' />
<cfset user_Profit = '0' />
<cfset SN_2019 = '0' />
<cfset Th_2019 = '0' />
<cfset HHA_2019 = '0' />
<cfset All_2019 = '0' />
<cfset All_2019_Profit = '0' />
<cfset SN_2020 = '0' />
<cfset Th_2020 = '0' />
<cfset HHA_2020 = '0' />
<cfset All_2020 = '0' />
<cfset All_2020_Profit = '0' />
<cfset All_2020 = '0' />
<cfset All_2020_Profit = '0' />
<cfset H1_new = ''>
<cfset H4_new = ''>
<cfset rank_2019 = '0'>
<cfset rank_2020 = '0'>
<cfset Summary = ''>
<cfset Get_User_Subject = ''>
<cfset Get_User_Skill = ''>
<cfset Get_User_Dx = ''>
<cfset Get_User_Goal = ''>
<cfset Get_CW_Subject = ''>
<cfset Get_CW_Skill = ''>
<cfset Get_CW_Dx = ''>
<cfset Get_CW_Goal = ''>
<cfset OT_percent = '0'>
<cfset PT_percent = '0'>
<cfset OT_visits_2019 = '0'>
<cfset PT_visits_2019 = '0'>
<cfset OT_visits_2020 = '0'>
<cfset PT_visits_2020 = '0'>
<cfquery name="Get_Session" datasource="#Application.DataSrc#">
    SELECT * from #Request.prefix_db_agency#.Coding_Results
    where Assessment_ID = #url.AID#
    and Patient_ID = #url.ID#
    and Message_ID = 78
    </cfquery>
<cfset Session.CS.PDGM_4 = Get_Session.M0_Item_Medication />
<cfset Session.CS.Count_Thpy = Get_Session.Order_ID />
<cfset Session.CS.Count_PT = Get_Session.Diagnosis />
<cfset Session.CS.Count_OT = Get_Session.Textfield_10 />
<cfset Session.CS.Caregiver = Get_Session.Textfield_11 />
<cfset Session.CS.Confusion = Get_Session.Textfield_14 />
<cfset Session.CS.Therapy_Analysis = Get_Session.Textfield_15 />
<cfquery  name="update" datasource="#Application.DataSrc#">
    Update #Request.prefix_db_agency#.Coding_Results c, #Request.prefix_db_lookup#.CW_Coding_Class_PDGM u
    set c.Textfield_8 = u.Goal
    where c.Assessment_ID = #url.AID#
    and c.Patient_ID = #url.ID#
    and c.Order_ID = u.Priority
     </cfquery>
<cfquery name="Get_CW_Therapy" datasource="#Application.DataSrc#">
    SELECT OASIS
    FROM #Request.prefix_db_agency#.Coding_Results
    where Assessment_ID = #url.AID#
    and Patient_ID = #url.ID#
    and Message_ID = 10
    order by Results_ID
    </cfquery>
<cfloop query="Get_CW_Therapy" >
  <cfset Get_CW_Subject = "Get_CW_Therapy "&#OASIS#&"<br><br>">
</cfloop>
<cfquery  name="Get_H" datasource="#Application.DataSrc#">
    Select Left(a.HIPPS_Code,4) AS HIPPS, a.HIPPS_Code ,a.SN_Visit_Count, a.Therapy_Visit_Count, a.M0826_Ther_Need, a.HHA_Visit_Count, u.Total_Reimburse
    from #Request.prefix_db_agency#.Assessment_New a, #Request.prefix_db_lookup#.CW_Utilization_2019 u
    where a.Assessment_ID = #url.AID#
    and a.Patient_ID = #url.ID#
    and Left(a.HIPPS_Code,4) = u.HIPPS_Code
    and u.rank = '1'
     </cfquery>
<cfif Session.CS.Count_Thpy neq 0>
  <cfset OT_percent = #Session.CS.Count_OT#/#Session.CS.Count_Thpy#*100>
  <cfset PT_percent = #Session.CS.Count_PT#/#Session.CS.Count_Thpy#*100>
  <cfelse>
  <cfset OT_percent = 0>
  <cfset PT_percent = 0>
</cfif>
<cfif Session.CS.Caregiver eq 'A'>
  <cfset rank_2019 = '1'>
  <cfset Summary = 'PATIENT HAS A TEACHABLE CAREGIVER AVAILABLE DURING THE DAY. CAREGIVER WILL BE TAUGHT SYMPTOM MANAGEMENT AND DISEASE PREVENTION.'>
  <cfelseif Session.CS.Caregiver eq 'B' and Session.CS.Confusion eq 'A' >
  <cfset rank_2019 = '2'>
  <cfset Summary = 'PATIENT HAS NO TEACHABLE CAREGIVER AVAILABLE DURING THE DAY. PATIENT IS TEACHABLE AND WILL BE TAUGHT SYMPTOM MANAGEMENT AND DISEASE PREVENTION.'>
  <cfelseif Session.CS.Caregiver eq 'B' and Session.CS.Confusion eq 'B' >
  <cfset rank_2019 = '3'>
  <cfset Summary = 'PATIENT HAS NO TEACHABLE CAREGIVER AND IS CONFUSED. LONG-TERM SYMPTOM MONITORING AND DISEASE PREVENTION POSSIBLE.'>
</cfif>
<cfif Session.CS.Count_Thpy gt 0>
  <cfif rank_2019 eq '1'>
    <cfset Summary = '#Summary#' & '<br><br>' & 'Patient has ' & #Session.CS.Count_Thpy# & ' ADL/mobility deficits requiring therapy. Since there is a caregiver available during the day, home health aide is not indicated.'>
    <cfelseif rank_2019 eq '2' >
    <cfset Summary = '#Summary#' & '<br><br>' & 'Patient has ' & #Session.CS.Count_Thpy# & ' ADL/mobility deficits requiring therapy. Since there is no caregiver available during the day and the patient has ADL deficits home health aide IS INDICATED.'>
    <cfelseif rank_2019 eq '3' >
    <cfset Summary = '#Summary#' & '<br><br>' & 'Patient has ' & #Session.CS.Count_Thpy# & ' ADL/mobility deficits requiring therapy. Since patient is confused and has no caregiver, home health aide IS INDICATED. Since patient is not teachable, intermittent long-term rehabilitation and home health aide services is possible.'>
  </cfif>
  <cfelse>
  <cfset Summary = '#Summary#' & '<br><br>' & ' PATIENT HAS NO ADL DEFICITS REQUIRING THERAPY. VIEW THERAPY ANALYSIS BELOW.'>
</cfif>
<cfquery  name="redo_coding" datasource="#Application.DataSrc#">
    Delete from #Request.prefix_db_agency#.Coding_Scrub where Patient_ID = #session.cs.patientid#  <cfif isdefined('url.AID')> AND Assessment_ID = #url.AID#   </cfif>
</cfquery>
<cfquery  name="Insert_Coding_results" datasource="#Application.DataSrc#">
       INSERT IGNORE INTO #Request.prefix_db_agency#.Coding_Scrub
		  (Assessment_ID,Patient_ID,Message_ID,M0_Item,Risk_Type,Risk,Message,Created_by,Created_Date)
		 VALUES (#url.AID#,#url.ID#,76,'#Get_H.HIPPS_Code#','#Get_H.SN_Visit_Count#','#Get_H.Therapy_Visit_Count#','#Get_H.HHA_Visit_Count#','#Get_H.Total_Reimburse#','#DateFormat(Now(),"yyyy-mm-dd")#')
	  </cfquery>
<cfif Get_H.SN_Visit_Count neq '' and Get_H.SN_Visit_Count gt 0>
  <cfset user_SN = Get_H.SN_Visit_Count * 150>
  <cfelse>
  <cfset user_SN = '0'>
</cfif>
<cfset user_Th_visits = 0>
<cfif Get_H.Therapy_Visit_Count neq '' and Get_H.Therapy_Visit_Count gt 0>
  <cfset user_Th = Get_H.Therapy_Visit_Count * 164>
  <cfset user_Th_visits = Get_H.Therapy_Visit_Count>
  <cfelse>
  <cfset user_Th = ISNULL(Get_H.M0826_Ther_Need * 164)>
  <cfset user_Th_visits = Get_H.M0826_Ther_Need>
</cfif>
<cfif Get_H.HHA_Visit_Count neq '' and Get_H.HHA_Visit_Count gt 0>
  <cfset user_HHA = Get_H.HHA_Visit_Count * 68>
  <cfelse>
  <cfset user_HHA = '0'>
</cfif>
<cfif (Get_H.SN_Visit_Count neq '' and Get_H.SN_Visit_Count gt 0) or (Get_H.HHA_Visit_Count neq '' and Get_H.HHA_Visit_Count gt 0)>
  <cfset user_All =  user_SN + user_Th + user_HHA>
  <cfset user_Profit = Get_H.Total_Reimburse - user_All>
  <cfquery  name="update" datasource="#Application.DataSrc#">
    Update #Request.prefix_db_agency#.Coding_Scrub c, #Request.prefix_db_lookup#.CW_Utilization_Therapy_2019 u
    set c.Answer_1 = 
     concat('#Summary#','<br><br>','HIPPS code > ','#Get_H.HIPPS_Code#','<br><br>60-Day Episode Reimbursement > ','#LSEuroCurrencyFormat(Get_H.Total_Reimburse, "local")#','    Total Cost > ','#LSEuroCurrencyFormat(user_All, "local")#','    Net Profit > ','#LSEuroCurrencyFormat(user_Profit, "local")#',
    '<br><br>SN ','#Get_H.SN_Visit_Count#',' visits | Cost > ','#LSEuroCurrencyFormat(user_SN, "local")#',
    '<br><br>Therapy ', '#user_Th_visits#',' visits | Cost > ','#LSEuroCurrencyFormat(Th_2019, "local")#',
    '<br><br>HHA ','#Get_H.HHA_Visit_Count#',' visits | Cost > ','#LSEuroCurrencyFormat(user_HHA, "local")#', '<br><br>','#Session.CS.Therapy_Analysis#')
    where c.Assessment_ID = #url.AID#
    and c.Patient_ID = #url.ID#
     </cfquery>
  <cfelse>
  <cfquery  name="update" datasource="#Application.DataSrc#">
		Update #Request.prefix_db_agency#.Coding_Scrub c
		set c.Answer_1 = 
		concat('#Summary#','<br><br>','HIPPS code > ','#Get_H.HIPPS_Code#','<br><br>60-Day Episode Reimbursement > ','#LSEuroCurrencyFormat(Get_H.Total_Reimburse, "local")#',
        '<br><br>Therapy ', '#user_Th_visits#',' visits | Cost > ','#LSEuroCurrencyFormat(user_Th, "local")#','<br><br>','#Session.CS.Therapy_Analysis#')		
		where c.Assessment_ID = #url.AID#
		and c.Patient_ID = #url.ID#
	</cfquery>
</cfif>
<cfset Therapy_Util_Code = Session.CS.Caregiver&Session.CS.Confusion&Session.CS.Count_Thpy>
<cfquery  name="update" datasource="#Application.DataSrc#">
    Update #Request.prefix_db_agency#.Coding_Scrub c, #Request.prefix_db_lookup#.CW_Utilization_Therapy_2019 u
    set c.Answer_2 = u.Visits_Therapy_2019, c.GG_1 = '#Therapy_Util_Code#'
    where c.Assessment_ID = #url.AID#
    and c.Patient_ID = #url.ID#
    and u.Code = '#Therapy_Util_Code#'
    </cfquery>
<cfquery  name="Get_S" datasource="#Application.DataSrc#">
    Select Answer_2
    from #Request.prefix_db_agency#.Coding_Scrub
    where Assessment_ID = #url.AID#
    and Patient_ID = #url.ID#
    and Message_ID = 76
    </cfquery>
<cfif (Get_S.Answer_2 gte 0 and Get_S.Answer_2 lte 5) and (Left(Get_H.HIPPS,1) eq 1 or Left(Get_H.HIPPS,1) eq 2 or Left(Get_H.HIPPS,1) eq 5)>
  <cfset H1_new = '1'>
  <cfset H4_new = 'K'>
  <cfelseif (Get_S.Answer_2 gte 14 and Get_S.Answer_2 lte 15) and (Left(Get_H.HIPPS,1) eq 1 or Left(Get_H.HIPPS,1) eq 2 or Left(Get_H.HIPPS,1) eq 5)>
  <cfset H1_new = '2'>
  <cfset H4_new = 'K'>
  <cfelseif (Get_S.Answer_2 gte 0 and Get_S.Answer_2 lte 5) and (Left(Get_H.HIPPS,1) eq 1 or Left(Get_H.HIPPS,1) eq 2 or Left(Get_H.HIPPS,1) eq 5)>
  <cfset H1_new = '3'>
  <cfset H4_new = 'K'>
  <cfelseif (Get_S.Answer_2 gte 14 and Get_S.Answer_2 lte 15) and (Left(Get_H.HIPPS,1) eq 3 or  Left(Get_H.HIPPS,1) eq 4)>
  <cfset H1_new = '4'>
  <cfset H4_new = 'K'>
  <cfelseif (Get_S.Answer_2 gte 20) >
  <cfset H1_new = '5'>
  <cfset H4_new = 'K'>
  <cfelseif (Get_S.Answer_2 gte 0 and Get_S.Answer_2 lte 5) and Left(Get_H.HIPPS,1) eq 5 >
  <cfset H1_new = '1'>
  <cfset H4_new = 'K'>
  <cfelseif (Get_S.Answer_2 gte 14 and Get_S.Answer_2 lte 15) and Left(Get_H.HIPPS,1) eq 5 >
  <cfset H1_new = '2'>
  <cfset H4_new = 'K'>
  <cfelseif (Get_S.Answer_2 eq 6) and (Left(Get_H.HIPPS,1) eq 1 or Left(Get_H.HIPPS,1) eq 2 or Left(Get_H.HIPPS,1) eq 5)>
  <cfset H1_new = '1'>
  <cfset H4_new = 'L'>
  <cfelseif (Get_S.Answer_2 gte 16 and Get_S.Answer_2 lte 17) and (Left(Get_H.HIPPS,1) eq 1 or Left(Get_H.HIPPS,1) eq 2 or Left(Get_H.HIPPS,1) eq 5)>
  <cfset H1_new = '2'>
  <cfset H4_new = 'L'>
  <cfelseif (Get_S.Answer_2 eq 6) and (Left(Get_H.HIPPS,1) eq 3 or  Left(Get_H.HIPPS,1) eq 4)>
  <cfset H1_new = '3'>
  <cfset H4_new = 'L'>
  <cfelseif (Get_S.Answer_2 gte 16 and Get_S.Answer_2 lte 17) and (Left(Get_H.HIPPS,1) eq 3 or  Left(Get_H.HIPPS,1) eq 4)>
  <cfset H1_new = '4'>
  <cfset H4_new = 'L'>
  <cfelseif (Get_S.Answer_2 gte 7 and Get_S.Answer_2 lte 9) and (Left(Get_H.HIPPS,1) eq 1 or Left(Get_H.HIPPS,1) eq 2 or Left(Get_H.HIPPS,1) eq 5 or Left(Get_H.HIPPS,1) eq '')>
  <cfset H1_new = '1'>
  <cfset H4_new = 'M'>
  <cfelseif (Get_S.Answer_2 gte 18 and Get_S.Answer_2 lte 19) and (Left(Get_H.HIPPS,1) eq 1 or Left(Get_H.HIPPS,1) eq 2 or Left(Get_H.HIPPS,1) eq 5)>
  <cfset H1_new = '2'>
  <cfset H4_new = 'M'>
  <cfelseif (Get_S.Answer_2 gte 7 and Get_S.Answer_2 lte 9) and (Left(Get_H.HIPPS,1) eq 3 or  Left(Get_H.HIPPS,1) eq 4)>
  <cfset H1_new = '3'>
  <cfset H4_new = 'M'>
  <cfelseif (Get_S.Answer_2 gte 18 and Get_S.Answer_2 lte 19) and (Left(Get_H.HIPPS,1) eq 3 or  Left(Get_H.HIPPS,1) eq 4)>
  <cfset H1_new = '4'>
  <cfset H4_new = 'M'>
  <cfelseif (Get_S.Answer_2 eq '10') and (Left(Get_H.HIPPS,1) eq 1 or Left(Get_H.HIPPS,1) eq 2 or Left(Get_H.HIPPS,1) eq 5)>
  <cfset H1_new = '1'>
  <cfset H4_new = 'N'>
  <cfelseif (Get_S.Answer_2 eq '10') and (Left(Get_H.HIPPS,1) eq 3 or  Left(Get_H.HIPPS,1) eq 4)>
  <cfset H1_new = '3'>
  <cfset H4_new = 'N'>
  <cfelseif (Get_S.Answer_2 gte '11' and Get_S.Answer_2 lte 13) and (Left(Get_H.HIPPS,1) eq 1 or Left(Get_H.HIPPS,1) eq 2 or Left(Get_H.HIPPS,1) eq 5)>
  <cfset H1_new = '1'>
  <cfset H4_new = 'P'>
  <cfelseif (Get_S.Answer_2 gte '11' and Get_S.Answer_2 lte 13) and (Left(Get_H.HIPPS,1) eq 3 or  Left(Get_H.HIPPS,1) eq 4)>
  <cfset H1_new = '3'>
  <cfset H4_new = 'P'>
  <cfelse>
  <cfset H1_new = '1'>
  <cfset H4_new = 'K'>
</cfif>
<cfset New_HIPPS = H1_new&Mid(Get_H.HIPPS,2,1)&Mid(Get_H.HIPPS,3,1)&H4_new>
<cfset New_HIPPS_5 = H1_new&Mid(Get_H.HIPPS,2,1)&Mid(Get_H.HIPPS,3,1)&H4_new&Mid(Get_H.HIPPS_Code,5,1)>
<cfquery  name="update" datasource="#Application.DataSrc#">
    Update #Request.prefix_db_agency#.Coding_Scrub c, #Request.prefix_db_lookup#.CW_Utilization_2019 u
    set c.GG_2 = '#New_HIPPS#', c.Subject = u.Visits_SN, c.Desired = u.Visits_Therapy, c.Short = u.Visits_HHA, c.Field_1 = u.Total_Reimburse
    where c.Assessment_ID = #url.AID#
    and c.Patient_ID = #url.ID#
    and u.HIPPS_Code = '#New_HIPPS#'
    and u.rank = #rank_2019#
    and u.Visits_Therapy = '#Get_S.Answer_2#'
    </cfquery>
<cfquery  name="Get_OD" datasource="#Application.DataSrc#">
    Select IFNULL(Subject,0) as Subject, IFNULL(Desired,0) as Desired, IFNULL(Short,0) as Short, IFNULL(Field_1,0) as Field_1
    from #Request.prefix_db_agency#.Coding_Scrub
    where Assessment_ID = #url.AID#
    and Patient_ID = #url.ID#
    and Message_ID = 76
    </cfquery>
<cfif OT_Percent neq 0>
  <cfset OT_Visits_2019 = Get_OD.Desired * #OT_Percent#/100>
  <cfelse>
  <cfset OT_Visits_2019 = '0'>
</cfif>
<cfif PT_Percent neq 0>
  <cfset PT_Visits_2019 = Get_OD.Desired * #PT_Percent#/100>
  <cfelse>
  <cfset PT_Visits_2019 = '0'>
</cfif>
<cfif Get_OD.Subject neq ''>
  <cfset SN_2019 = Get_OD.Subject * 150>
  <cfelse>
  <cfset SN_2019 = '0'>
</cfif>
<cfif Get_OD.Desired neq ''>
  <cfset Th_2019 = Get_OD.Desired * 164>
  <cfelse>
  <cfset Th_2019 = '0'>
</cfif>
<cfif Get_OD.Desired neq ''>
  <cfset HHA_2019 = Get_OD.Short * 68>
  <cfelse>
  <cfset HHA_2019 = '0'>
</cfif>
<cfset All_2019 =  SN_2019 + Th_2019 + HHA_2019>
<cfset All_2019_Profit = Get_OD.Field_1 - All_2019>
<cfquery  name="update" datasource="#Application.DataSrc#">
    Update #Request.prefix_db_agency#.Coding_Scrub 
    set Field_2 = 
    concat('#Summary#','<br><br>','Recommended 2019 OASIS-D HIPPS code > ','#New_HIPPS_5#','<br><br>60-Day Episode Reimbursement > ','#LSEuroCurrencyFormat(Get_OD.Field_1, "local")#','<br><br>Total Cost > ','#LSEuroCurrencyFormat(All_2019, "local")#','<br><br>Net Profit > ','#LSEuroCurrencyFormat(All_2019_Profit, "local")#',
    '<br><br>SN ','#Get_OD.Subject#',' visits | Cost > ','#LSEuroCurrencyFormat(SN_2019, "local")#',' *Allows for multiple careplan changes. If no changes occur less visits may be sufficient.',
    '<br><br>PT ', '#NumberFormat(PT_Visits_2019, "_")#',' visits | OT  ','#NumberFormat(OT_Visits_2019, "_")#',' visits | Cost > ','#LSEuroCurrencyFormat(Th_2019, "local")#',
    '<br><br>HHA ','#Get_OD.Short#',' visits | Cost > ','#LSEuroCurrencyFormat(HHA_2019, "local")#', '<br><br>','#Session.CS.Therapy_Analysis#')
    where Assessment_ID = #url.AID#
    and Patient_ID = #url.ID#
    and Message_ID = 76
    </cfquery>
<!---<cfquery  name="Get_First" datasource="#Application.DataSrc#">
SELECT *
From #Request.prefix_db_agency#.Coding_Results p, #Request.prefix_db_lookup#.CW_PDGM_Comorbidities d
where p.Patient_ID = #url.ID#
and p.Safety_Hazards_M0320 = d.Descrip_1
</cfquery>--->
<cfquery  name="Get_First" datasource="#Application.DataSrc#">
SELECT Textfield_1, Textfield_3
From #Request.prefix_db_agency#.Coding_Results
where Patient_ID = #url.ID#
and Assessment_ID = #url.AID#
and Dx_Order = 1
</cfquery>
<cfif Get_First.Textfield_1 eq 'WOUND'>
  <cfset PDGM_2 = "C">
  <cfelseif Get_First.Textfield_1 eq 'NEURO_REHAB' or Get_First.Textfield_1 eq 'Neuro Rehab'>
  <cfset PDGM_2 = "B">
  <cfelseif Get_First.Textfield_1 eq 'MMTA_ENDO' or Get_First.Textfield_1 eq 'MMTA-Endocrine'>
  <cfset PDGM_2 = "I">
  <cfelseif Get_First.Textfield_1 eq 'MS_REHAB' or Get_First.Textfield_1 eq 'MS Rehab'>
  <cfset PDGM_2 = "E">
  <cfelseif Get_First.Textfield_1 eq 'MMTA_OTHER' or Get_First.Textfield_1 eq 'MMTA-Other'>
  <cfset PDGM_2 = "A">
  <cfelseif Get_First.Textfield_1 eq 'MMTA_CARDIAC' or Get_First.Textfield_1 eq 'MMTA-Cardiac'>
  <cfset PDGM_2 = "H">
  <cfelseif Get_First.Textfield_1 eq 'MMTA_AFTER' or Get_First.Textfield_1 eq 'MMTA-Surgical Aftercare'>
  <cfset PDGM_2 = "G">
  <cfelseif Get_First.Textfield_1 eq 'MMTA_RESP' or Get_First.Textfield_1 eq 'MMTA-Respiratory'>
  <cfset PDGM_2 = "L">
  <cfelseif Get_First.Textfield_1 eq 'MMTA_INFECT' or Get_First.Textfield_1 eq 'MMTA-Infection'>
  <cfset PDGM_2 = "K">
  <cfelseif Get_First.Textfield_1 eq 'COMPLEX' or Get_First.Textfield_1 eq 'Complex Nursing'>
  <cfset PDGM_2 = "D">
  <cfelseif Get_First.Textfield_1 eq 'MMTA_GI_GU' or Get_First.Textfield_1 eq 'MMTA-GI/GU'>
  <cfset PDGM_2 = "J">
  <cfelseif Get_First.Textfield_1 eq  'BEHAVE_HEALTH' or Get_First.Textfield_1 eq 'MMTA-Behavioral'>
  <cfset PDGM_2 = "F">
  <cfelse>
  <cfset PDGM_2 = "A">
</cfif>
<cfset PDGM_3 = "">
<cfif Mid(Get_H.HIPPS,3,1) eq "F">
  <cfset PDGM_3 = "A">
  <cfelseif Mid(Get_H.HIPPS,3,1) eq "G">
  <cfset PDGM_3 = "B">
  <cfelseif Mid(Get_H.HIPPS,3,1) eq "H">
  <cfset PDGM_3 = "C">
</cfif>
<cfset HIPPS2020 = '2'&PDGM_2&PDGM_3&Session.CS.PDGM_4&'1'>
<cfquery  name="update" datasource="#Application.DataSrc#">
    Update  #Request.prefix_db_agency#.Coding_Scrub
    set Field_9 = '#HIPPS2020#', Field_10 = '#HIPPS2020#'
    where Assessment_ID = #url.AID#
    and Patient_ID = #url.ID#
</cfquery>
<cfset Summary = ''>
<cfif Session.CS.Count_Thpy eq 0>
  <cfset rank_2020 = '0'>
  <cfif Session.CS.Caregiver eq 'A'>
    <cfset Summary = 'PATIENT HAS A TEACHABLE CAREGIVER AVAILABLE DURING THE DAY. CAREGIVER WILL BE TAUGHT SYMPTOM MANAGEMENT AND DISEASE PREVENTION.'>
    <cfelseif Session.CS.Caregiver eq 'B' and Session.CS.Confusion eq 'A' >
    <cfset Summary = 'PATIENT HAS NO TEACHABLE CAREGIVER AVAILABLE DURING THE DAY. PATIENT IS TEACHABLE AND WILL BE TAUGHT SYMPTOM MANAGEMENT AND DISEASE PREVENTION.'>
    <cfelseif Session.CS.Caregiver eq 'B' and Session.CS.Confusion eq 'B' >
    <cfset Summary = 'PATIENT HAS NO TEACHABLE CAREGIVER AND IS CONFUSED. LONG-TERM SYMPTOM MONITORING AND DISEASE PREVENTION POSSIBLE.'>
  </cfif>
</cfif>
<cfif Session.CS.Count_Thpy gt 0>
  <cfif Session.CS.Caregiver eq 'A'>
    <cfset rank_2020 = '1'>
    <cfset Summary = 'PATIENT HAS A TEACHABLE CAREGIVER AVAILABLE DURING THE DAY. CAREGIVER WILL BE TAUGHT SYMPTOM MANAGEMENT AND DISEASE PREVENTION.' & '<br><br>' & 'Patient has ' & #Session.CS.Count_Thpy# & ' ADL/mobility deficits requiring therapy. Since there is a caregiver available during the day, home health aide is not indicated.'>
    <cfelseif Session.CS.Caregiver eq 'B' and Session.CS.Confusion eq 'A'>
    <cfset rank_2020 = '2'>
    <cfset Summary = 'PATIENT HAS NO TEACHABLE CAREGIVER AVAILABLE DURING THE DAY. PATIENT IS TEACHABLE AND WILL BE TAUGHT SYMPTOM MANAGEMENT AND DISEASE PREVENTION.' & '<br><br>' & 'Patient has ' & #Session.CS.Count_Thpy# & ' ADL/mobility deficits requiring therapy. Since there is no caregiver available during the day and the patient has ADL deficits home health aide IS INDICATED.'>
    <cfelseif Session.CS.Caregiver eq 'B' and Session.CS.Confusion eq 'B' >
    <cfset rank_2020 = '3'>
    <cfset Summary = 'PATIENT HAS NO TEACHABLE CAREGIVER AND IS CONFUSED. LONG-TERM SYMPTOM MONITORING AND DISEASE PREVENTION POSSIBLE.' & '<br><br>' & 'Patient has ' & #Session.CS.Count_Thpy# & ' ADL/mobility deficits requiring therapy. Since patient is confused and has no caregiver, home health aide IS INDICATED. Since patient is not teachable, intermittent long-term rehabilitation and home health aide services is possible.'>
  </cfif>
  <cfelse>
  <cfset Summary = '#Summary#' & '<br><br>' & ' PATIENT HAS NO ADL DEFICITS REQUIRING THERAPY. VIEW THERAPY ANALYSIS BELOW.'>
</cfif>
<cfquery  name="update" datasource="#Application.DataSrc#">
    Update #Request.prefix_db_agency#.Coding_Scrub c, #Request.prefix_db_lookup#.CW_Utilization_2020 u
    set c.Field_3 = u.Visits_SN, c.Field_4 = u.Visits_Therapy, c.Field_5 = u.Visits_HHA, c.Field_6 = u.Total_Reimburse
    where c.Assessment_ID = #url.AID#
    and c.Patient_ID = #url.ID#
    and u.HIPPS_Code = '#HIPPS2020#'
    and u.rank = '#rank_2020#'
</cfquery>
<cfquery  name="Get_PD" datasource="#Application.DataSrc#">
    Select IFNULL(Field_3,0) as Field_3,IFNULL(Field_4,0) as Field_4,IFNULL(Field_5,0) as Field_5 , IFNULL(Field_6,0) as Field_6
    from #Request.prefix_db_agency#.Coding_Scrub
    where Assessment_ID = #url.AID#
    and Patient_ID = #url.ID#
    and Message_ID = 76
    </cfquery>
<cfif OT_Percent neq 0>
  <cfset OT_Visits_2020 = Get_PD.Field_4 * #OT_Percent#/100>
  <cfelse>
  <cfset OT_Visits_2020 = '0'>
</cfif>
<cfif PT_Percent neq 0>
  <cfset PT_Visits_2020 = Get_PD.Field_4 * #PT_Percent#/100>
  <cfelse>
  <cfset PT_Visits_2020 = '0'>
</cfif>
<cfif Get_PD.Field_3 neq ''>
  <cfset SN_2020 = Get_PD.Field_3 * 150>
  <cfelse>
  <cfset SN_2020 = '0'>
</cfif>
<cfif Get_PD.Field_4 neq ''>
  <cfset Th_2020 = Get_PD.Field_4 * 164>
  <cfelse>
  <cfset Th_2020 = '0'>
</cfif>
<cfif Get_PD.Field_5 neq ''>
  <cfset HHA_2020 = Get_PD.Field_5 * 68>
  <cfelse>
  <cfset HHA_2020 = '0'>
</cfif>
<cfset All_2020 =  SN_2020 + Th_2020 + HHA_2020>
<cfset All_2020_Profit = Get_PD.Field_6 - All_2020>
<cfquery  name="update" datasource="#Application.DataSrc#">
    Update #Request.prefix_db_agency#.Coding_Scrub 
    set Field_7 = 
    concat('#Summary#','<br><br>','2020 PDGM HIPPS code > ','#HIPPS2020#','<br><br>30-Day Episode Reimbursement > ','#LSEuroCurrencyFormat(Get_PD.Field_6, "local")#','<br><br>Total Cost > ','#LSEuroCurrencyFormat(All_2020, "local")#','<br><br>Net Profit > ','#LSEuroCurrencyFormat(All_2020_Profit, "local")#',
    '<br><br>SN ','#Get_PD.Field_3#',' visits | Cost > ','#LSEuroCurrencyFormat(SN_2020, "local")#',
    '<br><br>PT ', '#NumberFormat(PT_Visits_2020, "_")#',' visits | OT ','#NumberFormat(OT_Visits_2020, "_")#',' visits | Cost > ','#LSEuroCurrencyFormat(Th_2020, "local")#',
    '<br><br>HHA ','#Get_PD.Field_5#',' visits | Cost > ','#LSEuroCurrencyFormat(HHA_2020, "local")#','<br><br>#Session.CS.Therapy_Analysis#')
    where Assessment_ID = #url.AID#
    and Patient_ID = #url.ID#
    and Message_ID = 76
    </cfquery>
<cfquery  name="Get_Diagnosis" datasource="#Application.DataSrc#">
    Select M0_Item_Medication, Diagnosis
    from #Request.prefix_db_agency#.Coding_Results
    where Assessment_ID = #url.AID#
    and Patient_ID = #url.ID#
    and M0_Item_Medication like '%M102%'
    Order by Dx_Order ASC
    </cfquery>
<cfset list = ''>
<cfloop query="Get_Diagnosis">
  <cfset list = list & '#Diagnosis#' &'<br>' & 'from Face-to-Face | from H&P | from H&P - PDGM co-morbidity that adds casemix points | from OASIS - PDGM co-morbidity that adds casemix points | from clinician OASIS narrative'&'<br>' >
</cfloop>
<cfset CG = ''>
<cfset CF = ''>
<cfif Session.CS.Caregiver eq 'A'>
  <cfset CG = 'According to the OASIS assessment the patient has a teachable daytime caregiver - see M1100.'>
  <cfelse>
  <cfset CG = 'According to the OASIS assessment the patient does not have a teachable daytime caregiver - see M1100.'>
</cfif>
<cfif Session.CS.Confusion eq 'A'>
  <cfset CF = ' However the patient is teachable - see M1710.'>
  <cfelse>
  <cfset CF = ' Additionally, the patient is confused and not teachable - see M1710 or M1740-2.'>
</cfif>
<cfset Team_Message = ''>
<cfset Team_Message = 'PROCESS 2 - message1' & '<br><br>' & 'Please authorize/change recommended Coding & Utilization' & '<br><br>' & 
'Hi All,'    & '<br><br>' &
'We have completed our document review.' & '<br><br>' & 
'We recommend the following diagnosis codes.  Please see the attached report for rationale.'& '<br><br>' &
#list#    & '<br><br>' &  #CG# & #CF#    & '<br><br>' &
'The patient has ' & #Session.CS.Count_Thpy# &' deficits requiring therapy.'  & '<br><br>' & 
'Based on caregiver availability and number of ADL/IADL deficits we recommend the following utilization. Refer to the Utilization Report for complete therapy rationale.'  & '<br><br>' &  
'SN - ' & #Get_OD.Subject# & ' visits'   & '<br>' &
'OT - ' & '#NumberFormat(OT_Visits_2019, "_")#' & ' visits'  & '<br>' &
'PT - ' & '#NumberFormat(PT_Visits_2019, "_")#' & ' visits'  & '<br>' &
'HHA - ' & '#Get_OD.Short#' & ' visits'    & '<br><br>' &
'So that we can create and upload the final Visit and Teaching Plan, please reply with any changes to the above recommendations. Once your changes and/or approval is received, we’ll update the OASIS and CMS485 and upload all updated reports, including Teaching and Visit Plan.' & '<br><br>' &
'Coding Wizard Team' & '<br><br><br><br>' & 
'message2' & '<br><br>' & 
'OASIS & CMS485 Updated, Final Reports Attached' & '<br><br>' & 
'Hi All,'    & '<br><br>' &
'We have updated the OASIS with approved diagnosis codes and updated the CMS485 with goals and interventions. Attached are:' & '<br><br>' & 
'(1) list of diagnosis-specific goals and interventions that were used to update the CMS485'& '<br>' &
'(2) Teaching Plan'& '<br>' &
'(3) Patient Recall Sheet'& '<br>' &
'(4) Visits Plans based on approved Utilization'& '<br><br>' &
'Please advise if additional changes are needed.'& '<br><br>' & 'Coding Wizard Team' & '<br><br><br><br>' &
'PROCESS 3 - message1' & '<br><br>' & 'Please authorize/change recommended Goals and Interventions, Utilization' & '<br><br>' & 
'Hi All,'    & '<br><br>' &
'We have completed our document review and coded the OASIS with the following diagnosis codes.  Please see the attached report for rationale.'& '<br><br>' &
#list#    & '<br><br>' &  #CG# & #CF#    & '<br><br>' &
'The patient has ' & #Session.CS.Count_Thpy# &' deficits requiring therapy.'  & '<br><br>' & 
'Based on caregiver availability and number of ADL/IADL deficits we recommend the following utilization. Refer to the Utilization Report for complete therapy rationale.'  & '<br><br>' &  
'SN - ' & #Get_OD.Subject# & ' visits'   & '<br>' &
'OT - ' & '#NumberFormat(OT_Visits_2019, "_")#' & ' visits'  & '<br>' &
'PT - ' & '#NumberFormat(PT_Visits_2019, "_")#' & ' visits'  & '<br>' &
'HHA - ' & '#Get_OD.Short#' & ' visits'    & '<br><br>' &
'So that we can update the CMS485 with goals and interventions and upload the final Visit and Teaching Plan, please reply with any changes to the above recommendations. Once your changes and/or approval is received, we’ll upload all updated reports, including Teaching, Patient Recall and Visit Plan.' & '<br><br>' &
'Coding Wizard Team' & '<br><br><br><br>' & 
'message2' & '<br><br>' & 
'Visit Plan and Patient Recall Sheet Attached' & '<br><br>' & 
'Hi All,'    & '<br><br>' &
'Attached are:' & '<br><br>' & 
'(1) Patient Recall Sheet'& '<br>' &
'(2) Visits Plans based on approved Utilization'& '<br><br>' &
'Please advise if additional changes are needed.'& '<br><br>' & 'Coding Wizard Team' & '<br><br><br><br>' &
 'PROCESS 4 - message1' & '<br><br>' & 'Please authorize/change recommended Utilization' & '<br><br>' &      
 'Hi All,'    & '<br><br>' &
'We have completed our document review, coded OASIS with the following diagnosis codes and updated the CMS485 with related goals and interventions. Please see the attached reports for rationale.'& '<br><br>' &
#list#    & '<br><br>' &  #CG# & #CF#    & '<br><br>' &
'The patient has ' & #Session.CS.Count_Thpy# &' deficits requiring therapy.'  & '<br><br>' & 
'Based on caregiver availability and number of ADL/IADL deficits we recommend the following utilization. Refer to the Utilization Report for complete therapy rationale.'  & '<br><br>' &  
'SN - ' & #Get_OD.Subject# & ' visits'   & '<br>' &
'OT - ' & '#NumberFormat(OT_Visits_2019, "_")#' & ' visits'  & '<br>' &
'PT - ' & '#NumberFormat(PT_Visits_2019, "_")#' & ' visits'  & '<br>' &
'HHA - ' & '#Get_OD.Short#' & ' visits'    & '<br><br>' &
'So that we can upload the final Visit Plan and Patient Recall Sheet, please reply with any changes to the above recommendations. Once your changes and/or approval is received, we’ll upload all updated reports.' & '<br><br>' &
'Coding Wizard Team' & '<br><br><br><br>' & 
'message2' & '<br><br>' & 
'Visit Plan and Patient Recall Sheet Attached' & '<br><br>' & 
'Hi All,'    & '<br><br>' &
'Attached are:' & '<br><br>' & 
'(1) Patient Recall Sheet'& '<br>' &
'(2) Visits Plans based on approved Utilization'& '<br><br>' &
'Please advise if additional changes are needed.'& '<br><br>' & 'Coding Wizard Team'>
<cfquery  name="update" datasource="#Application.DataSrc#">
    Update #Request.prefix_db_agency#.Coding_Scrub 
    set Field_11 = '#Team_Message#'
    where Assessment_ID = #url.AID#
    and Patient_ID = #url.ID#
    and Message_ID = 76
    </cfquery>
<cfset Session.CS.PT_Visits_2019 = PT_Visits_2019 />
<cfset Session.CS.PT_Visits_2020 = PT_Visits_2020 />
<cfset Session.CS.OT_Visits_2019 = OT_Visits_2019 />
<cfset Session.CS.OT_Visits_2020 = OT_Visits_2020 />
<!---<cflocation url="index.cfm?page=patient&ID=#session.cs.patientid#&cat=cron_coding_CREATEVISIT_SN&AID=#Assessment_ID#" addtoken="no">--->
<cfinclude template="cron_coding_CREATEVISIT_SN.cfm" />
