<!---
This file is Copyright � 1998-2009 by Home Care Business Services. This work may not be reproduced, in whole or in part, 
using any medium, including, but not limited to, electronic transmission, CD-ROM or published in print, without the express 
permission of Home Care Business Services.

$Revision: 1.26 $
$Id: assessment_action.cfm,v 1.26 2010/06/26 20:55:54 melissa Exp $
$Date: 2010/06/26 20:55:54 $
--->
<!--- These lines are used to create the Claim_Key field --->


<cfif isDefined("form.New_Payer_ID")>
	<cfset Form.Payer_ID = #Form.New_Payer_ID#>
</cfif>

<cfset Session.Payer_ID = #Form.Payer_ID#>

<!--- If adding new assessment --->
<cfif Form.AID EQ -1>
	<!--- Calculate the Certification Start & End dates --->
	<cfswitch expression="#Left(Form.M0100,1)#">
		<cfcase value="1,3,5">
			<cfset Form.Certification_Start = Form.M0030>
			<cfset Form.Certification_Period = 60>
			<cfset Form.Certification_End = DateAdd('D', Form.Certification_Period, Form.M0030)>
		</cfcase>
		<cfcase value="4">
			<!--- This code was written to calculate the start date of the next 60 day assessment cycle --->
			<!--- Get the original Start of Care date --->
			<cfset OldStart = Form.M0030>
			<!--- Determine the number of days between original start date and current date --->
			<cfset Differ = DateDiff('D',OldStart,Now())>
			<!--- Calculate the number of days to add --->
			<!--- <cfset DaysToAdd = (Ceiling(Differ/60) * 60) + 1> --->
			<cfset DaysToAdd = (Ceiling(Differ/60) * 60)+1>
			<cfset Form.Certification_Start = DateAdd('D', DaysToAdd, OldStart)>
			<cfset Form.Certification_End = DateAdd('D', 60, Form.Certification_Start)>
		</cfcase>
		<cfdefaultcase>
			<cfset Form.Certification_Period = 60>
			<cfset Form.Certification_Start = "">
			<cfset Form.Certification_End = "">
		</cfdefaultcase>
	</cfswitch>
</cfif>

<cfif isDefined("form.M0032NA")>
	<cfset var.M0032NA = #form.M0032NA#>
<cfelse>
	<cfset var.M0032NA = ''>
</cfif>
<!---<cfif isDefined("form.M0063NA")>
	<cfset var.M0063NA = #form.M0063NA#>
<cfelse>
	<cfset var.M0063NA= 0>
</cfif>
<cfif isDefined("form.M0065NA")>
	<cfset var.M0065NA = #form.M0065NA#>
<cfelse>
	<cfset var.M0065NA= 0>
</cfif>--->
<cfif isDefined("form.Information_Release")>
	<cfset var.Information_Release = #form.Information_Release#>
<cfelse>
	<cfset var.Information_Release = 0>
</cfif>
<cfif isDefined("form.Deleted")>
	<cfset var.Deleted = #form.Deleted#>
<cfelse>
	<cfset var.Deleted = 0>
</cfif>

<cfif isDefined("form.NotAdmitted")>
	<cfset var.NotAdmitted = 1>
<cfelse>
	<cfset var.NotAdmitted = 0>
</cfif>

<cfif isDefined("form.Archive")>
	<cfset var.Archive = #form.Archive#>
<cfelse>
	<cfset var.Archive = 0>
</cfif>

<cfif NOT isDefined("form.M0906")>
		<cfset form.M0906="">
</cfif>
<cfif NOT isDefined("form.Previous_Assessment_Number")>
		<cfset form.Previous_Assessment_Number="">
</cfif>

<cfif form.AID EQ 0 and form.ATID eq 0>
	<cfif NOT isDefined("form.Certification_Period")>
		<cfset form.Certification_Period="">
		<cfset form.Certification_Start="">
	</cfif>
	

	<cfif form.Certification_Period IS NOT "" AND form.Certification_Start IS NOT "" >
		<cfset newdate = #dateadd('D',form.Certification_Period,form.Certification_Start)#>
		<cfset newdate = #dateadd('D',-1,newdate)#>
	</cfif>
	
	<cftransaction>

	<cfquery  name="ins_tracker" datasource="#Application.DataSrc#">
		insert into #Request.prefix_db_agency#.assessment_Tracker (Agency_ID, Employee_ID, Patient_ID, Assessment_Type,Assessment_ID,Status)
		values (#session.AgencyID#, #Session.EmployeeID#, #form.PID#, '#form.M0100#',0,'active');
	</cfquery>
	<cfquery  name="tracker" datasource="#Application.DataSrc#">
		SELECT MAX(Assessment_Tracker_ID) as maxtrackerid
		FROM #Request.prefix_db_agency#.assessment_Tracker
		WHERE Patient_ID = #form.PID# and Agency_ID = #session.AgencyID# and Employee_ID = #Session.EmployeeID# and Status='active' and Assessment_ID = 0
	</cfquery>

	<cfset form.ATID = tracker.maxtrackerid>

	<cfquery  name="AC" datasource="#Application.DataSrc#">
	INSERT INTO #Request.prefix_db_agency#.Assessment_New (
		Assessment_Tracker_ID,
		Patient_ID,
		Agency_Number,
		Agency_ID,
		HHAC_Agency_ID,
		Assessment_Number,
		<cfif form.M0030 IS NOT "">
		Start_of_Care_M0030,
		</cfif>
		<cfif form.M0032 IS NOT "">
		Resump_of_Care_M0032,
		</cfif>
		Resump_of_Care_NA_M0032,
		<!---Medicare_Number_M0063,
		Medicare_Number_NA_M0063,
		Medicaid_Number_M0065,
		Medicaid_Number_NA_M0065,--->
		<cfif isDefined("form.M0090") and form.M0090 IS NOT "">
		Assessment_Completed_M0090,
		</cfif>
		<cfif form.M0906 IS NOT "">
		Date_Discharge_M0906,
		</cfif>
		<cfif form.Previous_Assessment_Number IS NOT "">
		Previous_Assessment_Number,
		</cfif>

<!---    M0104_Date_of_Referral, --->
    Physician_Last_Seen_Date,
		Assessment_Reason_M0100,
		Source_of_Admission,
		<cfif form.Certification_Start IS NOT "">
		Certification_Start,
		</cfif>
		<cfif form.Certification_Period IS NOT "">
		Certification_End,
		</cfif>
		Admission_Status,
		Transmission_Status,
		SN_Visit_Orders,
		Therapy_Visit_Orders,
		HHA_Visit_Orders,
		Assessment_note,
		Information_Release,		
		Claim_Key,
		UniqueAuth,
		NotAdmitted,
		Payer_ID,
		Record_Creation_Date

		) VALUES (

		#tracker.maxtrackerid#,
		#form.PID#,
		#session.AgencyID#,
		#session.AgencyID#,
		#session.AgencyLogin#,
		'#form.tracker#',
		<cfif form.M0030 IS NOT "">
		#CreateODBCDate(form.M0030)#,
		</cfif>
		<cfif form.M0032 IS NOT "">
		#CreateODBCDate(form.M0032)#,
		</cfif>
		'#var.M0032NA#',
		<!---'#form.M0063#',
		'#var.M0063NA#',
		'#form.M0065#',
		'#var.M0065NA#',--->
		<cfif isDefined("form.M0090") and form.M0090 IS NOT "">
		#CreateODBCDate(form.M0090)#,
		</cfif>
		<cfif form.M0906 IS NOT "">
		#CreateODBCDate(form.M0906)#,
		</cfif>
		<cfif form.Previous_Assessment_Number IS NOT "">
		'#form.Previous_Assessment_Number#',
		</cfif>

<!---		#CreateODBCDate(form.M0104)#, --->
    <cfif form.Physician_Last_Seen_Date neq ''>#CreateODBCDate(form.Physician_Last_Seen_Date)#<cfelse>null</cfif>,
		'#form.M0100#',
		'#form.Source_of_Admission#',
		<cfif form.Certification_Start IS NOT "">
			#CreateODBCDate(form.Certification_Start)#,
		</cfif>
		<cfif form.Certification_Period IS NOT "">
			#CreateODBCDate(newdate)#,
		</cfif>
		
		<cfif form.M0100 IS "NO ASSESSMENT">
			'not applicable',
		<cfelse>
		'In Progress',			
		</cfif>
				
		<cfif form.M0100 IS "NO ASSESSMENT" or form.M0100 Contains "Evaluation" or form.M0100 Contains "Hospice">
			'not applicable',
		<cfelse>
		'OASIS In Progress',		
		</cfif>
		'#SN_Visit_Orders#',
		'#Therapy_Visit_Orders#',
		'#HHA_Visit_Orders#',
		'#Assessment_note#',
		'#var.Information_Release#',
		'#Claim_Key#',
		'#UniqueAuth#',
		#var.NotAdmitted#,
		#form.Payer_ID#,
		#CreateODBCDate(Now())#
		)
	</cfquery>
	
	<cfquery  name="assessment" datasource="#Application.DataSrc#">
		SELECT Assessment_ID from #Request.prefix_db_agency#.Assessment_New where Assessment_Tracker_ID = #tracker.maxtrackerid#
	</cfquery>

	<cfquery  name="upd_tracker" datasource="#Application.DataSrc#">
		UPDATE #Request.prefix_db_agency#.assessment_Tracker set Assessment_ID = #assessment.Assessment_ID# where Assessment_Tracker_ID= #tracker.maxtrackerid#
	</cfquery>

	<cfset form.Aid=#assessment.Assessment_ID#>
	<cfset Session.CS.AssessmentID=#assessment.Assessment_ID#>
	<cfset form.Atid=#tracker.maxtrackerid#>
	<cfset Session.CS.AssessmentTrackerID=#tracker.maxtrackerid#>
	<cfset Session.CS.PatientID=#form.PID#>
	<cfset session.rollover=1>

	</cftransaction>


	<cfif isDefined("form.timeauthorization_count")>
		<cfset thecount=#form.timeauthorization_count#-1>
		<cfloop index=j from=0 to=#thecount#>
			<cfset theid=#evaluate('form.assessment_timeauthorizations_#j#')#>
			<cfset therevid=#evaluate('form.assessment_revcodeid_#j#')#>
			<cfset thevalue=#evaluate('form.approvedhours_#j#')#>
			<cfif theid eq 0>			
				<cfquery  name="upd" datasource="#Application.DataSrc#">
				INSERT INTO #Request.prefix_db_lookup#.Assessment_TimeAuthorizations 
				(Agency_ID,Assessment_ID,RevCode_ID,Payer_ID,Assessment_Tracker_ID,ApprovedHours,Patient_ID)
				VALUES('#session.agencyid#','#form.AID#','#therevid#','#Session.Payer_ID#','#form.ATID#','#thevalue#','#Session.CS.PatientID#')
				</cfquery>
			<cfelse>
				<cfquery  name="upd" datasource="#Application.DataSrc#">
				UPDATE #Request.prefix_db_lookup#.Assessment_TimeAuthorizations set approvedhours=#thevalue# 
				WHERE Assessment_TimeAuthorizations_ID = #theid#
				</cfquery>
			</cfif>	
		</cfloop>
	</cfif>


  <cfset visitdate=#CreateODBCDate(Now())#>


	<cfoutput>
	 <script>
    window.location = '/schedules/index.cfm?page=schedules&cat=&rpt=schedule_edit&restrict=1&SID=0&Date=#DateFormat(visitdate,'mm/dd/yyyy')#';
	 </script>
	</cfoutput>

	

<cfelse>
	<cfif isDefined("form.Certification_Period") and form.Certification_Period neq "" and form.Certification_Start IS NOT "">
		<cfset newdate = #dateadd('D',form.Certification_Period,form.Certification_Start)#>
		<cfset newdate = #dateadd('D',-1,newdate)#>
	</cfif>

	<cfquery  name="Aupd" datasource="#Application.DataSrc#">
		UPDATE #Request.prefix_db_agency#.Assessment_New set
			<cfif form.M0030 IS NOT "">
			Start_of_Care_M0030 = #CreateODBCDate(form.M0030)#,
			</cfif>
			<cfif form.M0032 IS NOT "">
			Resump_of_Care_M0032 = #CreateODBCDate(form.M0032)#,
			</cfif>
			Resump_of_Care_NA_M0032 = '#var.M0032NA#',

      Physician_Last_Seen_Date = <cfif form.Physician_Last_Seen_Date neq ''>#CreateODBCDate(form.Physician_Last_Seen_Date)#<cfelse>null</cfif>,
			<cfif isDefined("form.M0090") and form.M0090 IS NOT "">
			Assessment_Completed_M0090 = #CreateODBCDate(form.M0090)#,
			</cfif>
			<cfif form.M0906 IS NOT "">
			Date_Discharge_M0906 = #CreateODBCDate(form.M0906)#,
			</cfif>
			<cfif form.Previous_Assessment_Number IS NOT "">
			Previous_Assessment_Number = '#form.Previous_Assessment_Number#',
			</cfif>
			Assessment_Reason_M0100 = '#form.M0100#',
			Source_of_Admission = '#form.Source_of_Admission#',
			<cfif isDefined("form.Certification_Period") and form.Certification_Start IS NOT "">
				Certification_Start = #CreateODBCDate(form.Certification_Start)#,
			</cfif>

			<cfif isDefined("form.Certification_Period") and form.Certification_Start IS NOT "">
				Certification_End = #CreateODBCDate(newdate)#,
			</cfif>
			SN_Visit_Orders = '#form.SN_Visit_Orders#',
			Therapy_Visit_Orders = '#form.Therapy_Visit_Orders#',
			HHA_Visit_Orders = '#form.HHA_Visit_Orders#',
			Assessment_note = '#form.Assessment_note#',
			Information_Release = '#var.Information_Release#',
			Deleted = '#var.Deleted#',
			NotAdmitted = '#var.NotAdmitted#',
			Archive = '#var.Archive#',
			Payer_ID = #form.Payer_ID#,
			Claim_Key = '#Claim_Key#',
			UniqueAuth = '#UniqueAuth#',
			Record_Mod_Date = #CreateODBCDate(Now())#
		WHERE Assessment_ID = #form.AID#
	</cfquery>


	<script>
	<cfoutput>
  <cfif isDefined('form.rtn') and form.rtn eq 'dash-face-to-face'>
	 <cflocation url='/dashboard/index.cfm?page=dashboard&cat=chartcompliance&rpt=dash_face_to_face.cfm' addtoken='no' />
	<cfelseif isdefined("form.rtn") and gettoken(form.rtn,2,':') neq ''>
		<cfset page=gettoken(form.rtn,1,':')>
		<cfset cat=gettoken(form.rtn,2,':')>
		<cfset rpt=gettoken(form.rtn,3,':')>
		secureHREF('index.cfm?page=#page#&cat=#cat#&rpt=#rpt#&ID=#form.id#');
	<cfelse>
		secureHREF('index.cfm?page=#form.page#&cat=#form.cat#&ID=#form.id#');
	</cfif>
	</cfoutput>
	</script>
</cfif>