<!---60day_createSummary.cfm--->

<cfset name_item = '' />
<cfset Physician = '' />
<cfset vitals =  '' />
<cfparam name="form.hdnGoals" default="" />
<cfparam name="form.hdnTreatment" default="" />
<cfparam name="form.AssessmentID" default="" />

<cfset fd = DateFormat(dateadd('d', -60, Now()), 'mm/dd/yyyy') />
<cfset td = DateFormat(Now(), 'mm/dd/yyyy') />
<cfif isdefined('url.AID')  and #url.AID# neq ''>
<cfset url.AID   = decrypt(#url.AID#,"54kczhzgHTj4a8n+SRZPyg==","AES","Hex") />
<cfset form.AssessmentID = #url.AID# />
</cfif>
 
<cfif isdefined('form.AssessmentID')>
<cfquery name="GetAss" datasource="#Application.DataSrc#">
Select * from #Request.prefix_db_agency#.Assessment_New where Assessment_ID='#form.AssessmentID#'
</cfquery>
</cfif>

<cfquery name="GetP" datasource="#Application.DataSrc#">
	SELECT Patient_ID, Last_Name_M0040 AS Last_Name, First_Name_M0040 AS First_Name, Middle_Initial_M0040 AS Middle
	      ,Payer_ID ,DOB_M0066, Gender_M0069 
	FROM  #Request.prefix_db_agency#.patient 
	WHERE Patient_Id = '#session.cs.patientid#'
	AND   Agency_Id = '#session.AgencyId#'
</cfquery>

<cfset name_item = '60 Day Summary for<br />'&#GetP.Last_Name#&#GetP.First_Name#&#GetP.Middle# />

<cfquery name="GetE" datasource="#Application.DataSrc#">
select Last_Name, First_Name,Address1,City,Zip,Home_phone from #Request.prefix_db_lookup#.Employee where Employee_ID = '#GetAss.Record_Created_By#'
</cfquery>

<cfset Physician = #GetE.Last_Name#&#GetE.First_Name#&'<br />'&#GetE.Address1#&'&nbsp;'&#GetE.City#&'&nbsp;'&#GetE.Zip# />

<cfquery name="vital" datasource="#Application.DataSrc#">
SELECT distinct trim(pd.Description) as Description,pd.Skill,pd.Type,pvd.Severity,pvd.Reading1,pvd.Reading2
from #Request.prefix_db_agency#.Pathway_Details pd
LEFT OUTER JOIN #Request.prefix_db_agency#.PatientVisitDetails pvd on pd.Pathway_Details_ID = pvd.Pathway_Details_ID
where pd.Assessment_ID='#form.AssessmentID#'
AND  pd.Record_Creation_Date BETWEEN #CreateODBCDate(fd)# AND #CreateODBCDate(td)#
AND  pd.Type IN ('Vital Sign', 'Laboratory')
and (pd.Deleted is null and pd.Resolved_Date is null)
</cfquery>


<cfoutput query="vital">
 <cfset vitals = #vitals#&#Description#&'&nbsp;:&nbsp;'&#Reading1#&' - '&#Reading2#&'<br/>' />
</cfoutput>

<cfif isdefined('form.AssessmentID')>
<cfset M0230='#form.AssessmentID#'&' M0230' />
<cfquery name="M0230other" datasource="#Application.DataSrc#">
select distinct * from #Request.prefix_db_agency#.Diagnosis  WHERE Assessment_ID = '#form.AssessmentID#'
AND (M0190_Link is not null or M0210_Link is not null or M0230_Link is not null or M1012_Link is not null)
</cfquery>
</cfif>

<!---set Diagnosis--->
<cfset otherICD='' />
<cfif isdefined('M0230other.recordcount') and M0230other.recordcount gt 0>
<cfloop query="M0230other">
<cfset ICDTemp=ICD />
<cfset otherICD=otherICD&'<br/>'&ICDTemp>
</cfloop>
<cfelse>
<cfset otherICD='' />
</cfif>

<cfset otherdiagnosis='' />
<cfif isdefined('M0230other.recordcount') and M0230other.recordcount gt 0>
<cfloop query="M0230other">
<cfif Short_Diagnosis neq ''>
<cfset otherTemp= #Left(Short_Diagnosis, 25)#>
<cfelse>
<cfset otherTemp= #Left(Diagnosis, 25)#>
</cfif>
<cfset otherTemp= #Replace(otherTemp, ",", "", "ALL")#>
<cfset otherdiagnosis=otherdiagnosis&'<br/>'&otherTemp>
</cfloop>
<cfelse>
<cfset otherdiagnosis='' />
</cfif>


<cfset visit_block = 'SN Visit Schedule '& '#GetAss.SN_Visit_orders#'&'<br/>'&'Therapy Visit Schedule ' & '#GetAss.Therapy_Visit_Orders#'&'<br/>'&'HHA Visit Schedule '&'#GetAss.HHA_Visit_Orders#'&'<br/>'>

<cfif isdefined('form.hdnGoals') and trim(form.hdnGoals) eq '' and isdefined('form.AssessmentID')>
<cfquery name="Goalinterventions" datasource="#Application.DataSrc#">
select distinct trim(Description) as Description,Skill,Type
from #Request.prefix_db_agency#.Pathway_Details pd
where Assessment_ID='#form.AssessmentID#'
and Type IN ('Goal')
and (Deleted is null and Resolved_Date is null)
</cfquery>

<cfset GoalValue='' />
<cfif isdefined('Goalinterventions.recordcount') and Goalinterventions.recordcount gt 0>   
    <cfloop query="Goalinterventions">
	<cfif GoalValue eq ''>
	<cfset GoalValue='Goals:'&Goalinterventions.Description />
	<cfelse>
	<cfset GoalValue=GoalValue&','&Goalinterventions.Description />
	</cfif>	
    </cfloop>
	<cfset form.hdnGoals=GoalValue />
</cfif>
</cfif>


<cfif isdefined('form.hdnTreatment') and trim(form.hdnTreatment) eq '' and isdefined('form.AssessmentID')>
<cfquery name="Treatmentinterventions" datasource="#Application.DataSrc#">
select distinct trim(Description) as Description,Skill,Type
from #Request.prefix_db_agency#.Pathway_Details pd
where Assessment_ID='#form.AssessmentID#'
and Type IN ('Intervention')
and (Deleted is null and Resolved_Date is null)
</cfquery>
<cfset SNValue='' />
<cfset PTValue='' />
<cfset OTValue='' />
<cfset STValue='' />
<cfset MSWValue='' />
<cfset BRVValue='' />
<cfset SPRValue='' />
<cfset HHAValue='' />
<cfset HSNValue='' />
<cfset HMSValue='' />

<cfif Treatmentinterventions.recordcount gt 0>   
    <cfloop query="Treatmentinterventions">
	<cfif Treatmentinterventions.Skill eq 'SN'>
	<cfif SNValue eq ''>
	<cfset SNValue='SN<br/>'&Treatmentinterventions.Description>
	<cfelse>
	<cfset SNValue=SNValue&','&Treatmentinterventions.Description />
	</cfif>	
	</cfif>	
    </cfloop>
	
	<cfloop query="Treatmentinterventions">
	<cfif Treatmentinterventions.Skill eq 'PT'>
	<cfif PTValue eq ''>
	<cfset PTValue='PT<br/>'&Treatmentinterventions.Description>
	<cfelse>
	<cfset PTValue=PTValue&','&Treatmentinterventions.Description />
	</cfif>	
	</cfif>	
    </cfloop>
	
	<cfloop query="Treatmentinterventions">
	<cfif Treatmentinterventions.Skill eq 'OT'>
	<cfif OTValue eq ''>
	<cfset OTValue='OT<br/>'&Treatmentinterventions.Description>
	<cfelse>
	<cfset OTValue=OTValue&','&Treatmentinterventions.Description />
	</cfif>	
	</cfif>	
    </cfloop>
	
	<cfloop query="Treatmentinterventions">
	<cfif Treatmentinterventions.Skill eq 'ST'>
	<cfif STValue eq ''>
	<cfset STValue='ST<br/>'&Treatmentinterventions.Description>
	<cfelse>
	<cfset STValue=STValue&','&Treatmentinterventions.Description />
	</cfif>	
	</cfif>	
    </cfloop>
	
	
	<cfloop query="Treatmentinterventions">
	<cfif Treatmentinterventions.Skill eq 'MSW'>
	<cfif MSWValue eq ''>
	<cfset MSWValue='MSW<br/>'&Treatmentinterventions.Description>
	<cfelse>
	<cfset MSWValue=MSWValue&','&Treatmentinterventions.Description />
	</cfif>	
	</cfif>	
    </cfloop>
	
	<cfloop query="Treatmentinterventions">
	<cfif Treatmentinterventions.Skill eq 'BRV'>
	<cfif BRVValue eq ''>
	<cfset BRVValue='BRV<br/>'&Treatmentinterventions.Description>
	<cfelse>
	<cfset BRVValue=BRVValue&','&Treatmentinterventions.Description />
	</cfif>	
	</cfif>	
    </cfloop>
	
	<cfloop query="Treatmentinterventions">
	<cfif Treatmentinterventions.Skill eq 'SPR'>
	<cfif SPRValue eq ''>
	<cfset SPRValue='SPR<br/>'&Treatmentinterventions.Description>
	<cfelse>
	<cfset SPRValue=SPRValue&','&Treatmentinterventions.Description />
	</cfif>	
	</cfif>	
    </cfloop>
	
	<cfloop query="Treatmentinterventions">
	<cfif Treatmentinterventions.Skill eq 'HHA'>
	<cfif HHAValue eq ''>
	<cfset HHAValue='HHA<br/>'&Treatmentinterventions.Description>
	<cfelse>
	<cfset HHAValue=HHAValue&','&Treatmentinterventions.Description />
	</cfif>	
	</cfif>	
    </cfloop>
	
	
	<cfloop query="Treatmentinterventions">
	<cfif Treatmentinterventions.Skill eq 'HSN'>
	<cfif HSNValue eq ''>
	<cfset HSNValue='HSN<br/>'&Treatmentinterventions.Description>
	<cfelse>
	<cfset HSNValue=HSNValue&','&Treatmentinterventions.Description />
	</cfif>	
	</cfif>	
    </cfloop>
	
	<cfloop query="Treatmentinterventions">
	<cfif Treatmentinterventions.Skill eq 'HMS'>
	<cfif HMSValue eq ''>
	<cfset HMSValue='HMS<br/>'&Treatmentinterventions.Description>
	<cfelse>
	<cfset HMSValue=HMSValue&','&Treatmentinterventions.Description />
	</cfif>
	</cfif>	
    </cfloop>
	
	    <cfif SNValue neq ''>
	    <cfset SNValue = SNValue&'<br/><br/>'/>
		</cfif>
		<cfif PTValue neq ''>
		<cfset PTValue = PTValue&'<br/><br/>' />
		</cfif>
		<cfif OTValue neq ''>
		<cfset OTValue = OTValue&'<br/><br/>' />
		</cfif>
		<cfif STValue neq ''>
		<cfset STValue = STValue&'<br/><br/>' />
		</cfif>
		<cfif MSWValue neq ''>
		<cfset MSWValue = MSWValue&'<br/><br/>' />
		</cfif>
		<cfif HMSValue neq ''>
		<cfset BRVValue = BRVValue&'<br/><br/>' />
		</cfif>
		<cfif SPRValue neq ''>
		<cfset SPRValue = SPRValue&'<br/><br/>' />
		</cfif>
		<cfif HHAValue neq ''>
		<cfset HHAValue = HHAValue&'<br/><br/>' />
		</cfif>
		<cfif HSNValue neq ''>
		<cfset HSNValue = HSNValue&'<br/><br/>' />
		</cfif>
		<cfif HMSValue neq ''>
		<cfset HMSValue = HMSValue&'<br/><br/>' />
		</cfif>
		
	<cfset form.hdnTreatment=#visit_block#&'<br/><br/>'&SNValue&PTValue&OTValue&STValue&MSWValue&BRVValue&SPRValue&HHAValue&HSNValue&HMSValue>
</cfif>
</cfif>
<!---<cfoutput>
                 '#session.cs.patientid#'<br />
				<cfif #otherICD# neq ''>,'#otherICD#'</cfif><br />
				<cfif #otherdiagnosis# neq ''>,'#otherdiagnosis#'</cfif><br />
				  ,'#Session.AgencyLogin#'<br />
				 ,'60-day'<br />
				 ,'Not Signed'<br />
				 <cfif #name_item# neq ''>,#name_item#</cfif><br />
				 , #now()#<br />
				 , #now()#<br />
				 <cfif isdefined('GetAss.Certification_Start') and GetAss.Certification_Start neq ''>,#CreateODBCDate(GetAss.Certification_Start)#</cfif><br />
				 <cfif isdefined('GetAss.Certification_End') and GetAss.Certification_End neq ''>,#CreateODBCDate(GetAss.Certification_End)#</cfif><br />
				 <cfif isdefined('GetAss.Assessment_Tracker_ID')>,'#GetAss.Assessment_Tracker_ID#'</cfif><br />
				 <cfif isdefined('form.AssessmentID')>,'#form.AssessmentID#'</cfif><br />
				 ,'#Session.EmployeeId#'<br />
				 <cfif GetP.DOB_M0066 neq ''> ,#GetP.DOB_M0066#</cfif><br />
				  <cfif GetP.Gender_M0069 neq ''>,#GetP.Gender_M0069#</cfif><br />
				  <cfif #Physician# neq ''>,#Physician#</cfif><br />
				 ,#Session.EmployeeId#<br />
				<cfif isdefined('form.hdnTreatment') and form.hdnTreatment NEQ "">,'#form.hdnTreatment#' </cfif><br />
				 <cfif isdefined('form.hdnGoals') and form.hdnGoals NEQ "">,'#form.hdnGoals#' </cfif><br />
                <cfif #vitals# neq ''> ,'#vitals#'</cfif><br />
				 ,'#GetAss.Assessment_Note#'<br />
				 </cfoutput>
				 <cfabort>--->

<cfset dataspace=" " />
<cfset today=#DateFormat(now(),'yyyy-mm-dd')# />
			<cfquery  datasource="#Application.DataSrc#" name="AddForm">
				INSERT INTO #Request.prefix_db_agency#.Assessment_Forms (
				   Patient_Id
				 <cfif #otherICD# neq ''>,M0230_ICD_Item_11</cfif>
				 <cfif #otherdiagnosis# neq ''>,M0230_Diagnosis_Item_11</cfif>	
				 , Agency_Id
				 , Form_Type
				 , Form_Status
				 <cfif #name_item# neq ''>, Name_Item_6</cfif>
				 , Record_Creation_Date
				 , Record_Mod_Date
				 <cfif isdefined('GetAss.Certification_Start') and GetAss.Certification_Start neq ''>, Cert_Start_Item_3a</cfif>
				 <cfif isdefined('GetAss.Certification_End') and GetAss.Certification_End neq ''>, Cert_End_Item_3b</cfif>
				 <cfif isdefined('GetAss.Assessment_Tracker_ID')> , Assessment_Tracker_Id</cfif>
				 <cfif isdefined('form.AssessmentID')>, Assessment_Number</cfif>
				, Record_Modified_By
				<cfif GetP.DOB_M0066 neq ''>,DOB_Item_8</cfif>
				<cfif GetP.Gender_M0069 neq ''>,Gender_Item_9</cfif>
				 <cfif #Physician# neq ''>,Physician_Item_24</cfif>
				 , Record_Created_By
				<cfif isdefined('form.hdnTreatment') and form.hdnTreatment neq ''>,Order_item_21</cfif>
				<cfif isdefined('form.hdnGoals') and form.hdnGoals neq ''>,Goals_Item_22</cfif>
                  <cfif #vitals# neq ''>,Addendum_Details_10</cfif>
                <cfif #GetAss.Assessment_Note# neq ''>,Addendum_Details_22</cfif>
                 ) 
				VALUES 
				(
				  '#session.cs.patientid#'
				<cfif #otherICD# neq ''>,'#otherICD#'</cfif>
				<cfif #otherdiagnosis# neq ''>,'#otherdiagnosis#'</cfif>
				  ,'#Session.AgencyLogin#'
				 ,'60-day'
				 ,'Not Signed'
				 <cfif #name_item# neq ''>,'#name_item#'</cfif>
				 , #now()#
				 , #now()#
				 <cfif isdefined('GetAss.Certification_Start') and GetAss.Certification_Start neq ''>,#CreateODBCDate(GetAss.Certification_Start)#</cfif>
				 <cfif isdefined('GetAss.Certification_End') and GetAss.Certification_End neq ''>,#CreateODBCDate(GetAss.Certification_End)#</cfif>
				 <cfif isdefined('GetAss.Assessment_Tracker_ID')>,'#GetAss.Assessment_Tracker_ID#'</cfif>
				 <cfif isdefined('form.AssessmentID')>,'#form.AssessmentID#'</cfif>
				 ,'#Session.EmployeeId#'
				 <cfif GetP.DOB_M0066 neq ''> ,#GetP.DOB_M0066#</cfif>
				  <cfif GetP.Gender_M0069 neq ''>,'#GetP.Gender_M0069#'</cfif>
				  <cfif #Physician# neq ''>,'#Physician#'</cfif>
				 ,#Session.EmployeeId#
				<cfif isdefined('form.hdnTreatment') and form.hdnTreatment NEQ "">,'#form.hdnTreatment#' </cfif>
				 <cfif isdefined('form.hdnGoals') and form.hdnGoals NEQ "">,'#form.hdnGoals#' </cfif>
                <cfif #vitals# neq ''> ,'#vitals#'</cfif>
				<cfif #GetAss.Assessment_Note# neq ''> ,'#GetAss.Assessment_Note#'</cfif>
                    )
			</cfquery>	
			
		<cfquery name="LastID" datasource="#Application.DataSrc#">
		select LAST_INSERT_ID() as keyvalue
		</cfquery>
<cfoutput>
     <cfset  enc_afid = encrypt(#LastID.keyvalue#,"54kczhzgHTj4a8n+SRZPyg==","AES","Hex") />
<cflocation url="index.cfm?a=1&page=patient&px=y&cat=60day_summary_update&afid=#enc_afid#" addtoken="no">
</cfoutput>



