<cfcomponent>
	<cffunction name="myFunction" access="remote" returnformat="plain" returntype="string" output="false">
		<cfargument name="PID">
		<cfargument name="M0102">
		<cfargument name="M0104">
		<cfargument name="AgencyID">
		<cfargument name="EmployeeID">
		<cfargument name="M0100">
        <cfargument name="M0080">
        <cfargument name="Admission_Description">
		<!---<cfargument name="New_Payer_ID">--->
		<cfargument name="Payer_ID">
		<cfargument name="Certification_Start">
		<cfargument name="Certification_Period">
		<cfargument name="Certification_End">
		<cfargument name="M0030">		
		<cfargument name="M0032NA">
        <cfargument name="M0102NA">
		<cfargument name="Information_Release">
		<!---<cfargument name="Deleted">
		<cfargument name="NotAdmitted">
		<cfargument name="archive">--->
		<cfargument name="M0906">		
		<cfargument name="Previous_Assessment_Number">
		<cfargument name="ATID">
		<cfargument name="AID">
		<cfargument name="tracker">
		<cfargument name="M0032">
		<cfargument name="M0090"> 
		<cfargument name="Physician_Last_Seen_Date">
		<cfargument name="patient_rights_signed">
		<cfargument name="Source_of_Admission"> 
		<!---<cfargument name="SN_Visit_Orders">
		<cfargument name="Therapy_Visit_Orders">
		<cfargument name="HHA_Visit_Orders">
		<cfargument name="Assessment_note">
		<cfargument name="Claim_Key">
		<cfargument name="UniqueAuth">--->
		
		<!---<cfif New_Payer_ID NEQ ''>
			<cfset Payer_ID = #New_Payer_ID# />
		</cfif>--->		
		<cfset Session.Payer_ID = #Payer_ID# />
		
		<!--- If adding new assessment --->
		<cfif AID EQ -1>
			<!--- Get Admit_Type to determine certification period --->
			<cfset Admit_Type = "" />
			<cfif M0030 IS NOT "">
				<cfquery name="getAdmitType" datasource="#Application.DataSrc#">
					SELECT Admit_Type FROM #Request.prefix_db_agency#.pAdmit
					WHERE Patient_ID = <cfqueryparam cfsqltype="cf_sql_integer" value="#PID#">
					AND M0030_START_CARE_DT = <cfqueryparam cfsqltype="cf_sql_date" value="#M0030#">
					AND Status = 0
					ORDER BY Admit_ID DESC
					LIMIT 1
				</cfquery>
				<cfif getAdmitType.recordcount GT 0>
					<cfset Admit_Type = getAdmitType.Admit_Type />
				</cfif>
			</cfif>
			<!--- Determine certification period: 90 days for Hospice, 60 days otherwise --->
			<cfif Admit_Type EQ 'Hospice'>
				<cfset Certification_Period_Days = 90 />
			<cfelse>
				<cfset Certification_Period_Days = 60 />
			</cfif>
			<!--- Calculate the Certification Start & End dates --->
			<cfswitch expression="#Left(M0100,1)#">
				<cfcase value="1,3,5">
					<cfset Certification_Start = M0030 />
					<cfset Certification_Period = Certification_Period_Days />
					<cfset Certification_End = DateAdd('D', Certification_Period, M0030) />
				</cfcase>
				<cfcase value="4">
					<!--- This code was written to calculate the start date of the next assessment cycle --->
					<!--- Get the original Start of Care date --->
					<cfset OldStart = 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/Certification_Period_Days) * Certification_Period_Days)+1 />
					<cfset Certification_Start = DateAdd('D', DaysToAdd, OldStart) />
					<cfset Certification_End = DateAdd('D', Certification_Period_Days, Certification_Start) />
				</cfcase>
				<cfdefaultcase>
					<cfset Certification_Period = Certification_Period_Days />
					<cfset Certification_Start = "" />
					<cfset Certification_End = "" />
				</cfdefaultcase>
			</cfswitch>
		</cfif>
		
		<cfif AID EQ 0 and ATID eq 0>
		<cfif Certification_Period EQ ''>
			<cfset Certification_Period="" />
			<cfset Certification_Start="" />
		</cfif>
		
	
		<cfif Certification_Period IS NOT "" AND Certification_Start IS NOT "" >
			<cfset newdate = #dateadd('D',Certification_Period,Certification_Start)# />
			<cfset newdate = #dateadd('D',-1,newdate)# />
		</cfif>
		
		<cfquery name="InsertAssTracker" datasource="#Application.DataSrc#">
			insert into #Request.prefix_db_agency#.assessment_Tracker (Agency_ID, Employee_ID, Patient_ID, Assessment_Type,Assessment_ID,Status)
			values ('#AgencyID#', '#EmployeeID#', '#PID#', '#M0100#',0,'active')
		</cfquery>
		
		<cfquery name="LastInsertAssTracker" datasource="#Application.DataSrc#">
		 select LAST_INSERT_ID() as keyvalue
	   </cfquery>
		
		<cfquery name="AC" datasource="#Application.DataSrc#">
			INSERT INTO #Request.prefix_db_agency#.Assessment_New (
				Assessment_Tracker_ID,
				Patient_ID,
				Agency_Number,
				Agency_ID,
				<cfif len(M0102) gt 0>M0102_Physician_SOC,</cfif>
				<cfif len(M0104) gt 0>M0104_Date_of_Referral,</cfif>
				<cfif isdefined('session.AgencyLogin') and session.AgencyLogin neq ''>
				HHAC_Agency_ID,
				</cfif>
				Assessment_Number,
				<cfif M0030 IS NOT "">
				Start_of_Care_M0030,
				</cfif>
				<cfif M0032 IS NOT "">
				Resump_of_Care_M0032,
				</cfif>
				Resump_of_Care_NA_M0032,
                
				<cfif M0090 IS NOT "">
				Assessment_Completed_M0090,
				</cfif>
				<cfif M0906 IS NOT "">
				Date_Discharge_M0906,
				</cfif>
				<cfif Previous_Assessment_Number IS NOT "">
				Previous_Assessment_Number,
				</cfif>
				Physician_Last_Seen_Date,
				patient_rights_signed,
				Assessment_Reason_M0100,
                Person_Complete_M0080,
                Admission_Description,
				Source_of_Admission,
				<cfif Certification_Start IS NOT "">
				Certification_Start,
				</cfif>
				<cfif 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 (		
				'#LastInsertAssTracker.keyvalue#',
				'#PID#',
				'#AgencyID#',
				'#AgencyID#',
				<cfif len(M0102) gt 0>#CreateODBCDate(M0102)#,</cfif>
				<cfif len(M0104) gt 0>#CreateODBCDate(M0104)#,</cfif>
				<cfif isdefined('session.AgencyLogin') and session.AgencyLogin neq ''>
				'#session.AgencyLogin#',
				</cfif>
				'#tracker#',
				<cfif M0030 IS NOT "">
				#CreateODBCDate(M0030)#,
				</cfif>
				<cfif M0032 IS NOT "">
				#CreateODBCDate(M0032)#,
				</cfif>
				'#M0032NA#',
                
				<cfif M0090 IS NOT "">
				#CreateODBCDate(M0090)#,
				</cfif>
				<cfif M0906 IS NOT "">
				#CreateODBCDate(M0906)#,
				</cfif>
				<cfif Previous_Assessment_Number IS NOT "">
				'#Previous_Assessment_Number#',
				</cfif>
				<cfif Physician_Last_Seen_Date neq ''>#CreateODBCDate(Physician_Last_Seen_Date)#<cfelse>null</cfif>,
				<cfif patient_rights_signed neq ''>#CreateODBCDate(patient_rights_signed)#<cfelse>null</cfif>,
				'#M0100#',
                '#M0080#',
                '#Admission_Description#',
				'#Source_of_Admission#',
				<cfif Certification_Start IS NOT "">
					#CreateODBCDate(Certification_Start)#,
				</cfif>
				<cfif Certification_Period IS NOT "">
					#CreateODBCDate(newdate)#,
				</cfif>
				
				<cfif M0100 IS "NO ASSESSMENT">
					'not applicable',
				<cfelse>
				'In Progress',			
				</cfif>
						
				<!---<cfif M0100 IS "NO ASSESSMENT" or M0100 IS "SN Adult Evaluation" or M0100 IS "PT Adult Evaluation"
				 or M0100 IS "OT Adult Evaluation" or M0100 IS "Ped - Pediatric Evaluation"or M0100 IS "Hospice Comprehensive">
					'not applicable',
				<cfelse>
				'OASIS In Progress',		
				</cfif>--->
				<cfif M0100 contains 'Evaluation'>
					null,
				<cfelse>
				'OASIS unlocked',		
				</cfif>
				<!---'#SN_Visit_Orders#',
				'#Therapy_Visit_Orders#',
				'#HHA_Visit_Orders#',
				'#Assessment_note#',--->
				'#Information_Release#',
				<!---'#Claim_Key#',
				'#UniqueAuth#',
				'#NotAdmitted#',--->
				'#Payer_ID#',
				#CreateODBCDate(Now())#
				)
			</cfquery>
			
			<cfquery name="LastInsertAss" datasource="#Application.DataSrc#">
			 select LAST_INSERT_ID() as AssValue
		   </cfquery>
			
			<cfquery name="assessment" datasource="#Application.DataSrc#">
				SELECT Assessment_ID from #Request.prefix_db_agency#.Assessment_New where Assessment_Tracker_ID = '#LastInsertAssTracker.keyvalue#'
			</cfquery>
		
			<cfquery name="upd_tracker" datasource="#Application.DataSrc#">
				UPDATE #Request.prefix_db_agency#.assessment_Tracker set Assessment_ID = '#LastInsertAss.AssValue#' where Assessment_Tracker_ID= '#LastInsertAssTracker.keyvalue#'
			</cfquery>
		
			<cfset Aid=#LastInsertAss.AssValue# />
			<cfset Session.CS.AssessmentID=#LastInsertAss.AssValue# />
			<cfset Atid=#LastInsertAssTracker.keyvalue# />
			<cfset Session.CS.AssessmentTrackerID=#LastInsertAssTracker.keyvalue# />
			<cfset Session.CS.PatientID=#PID# />
			<cfset cookie.Assessment_ID=#LastInsertAss.AssValue# />
		</cfif>
		<cfset myresult=#LastInsertAss.AssValue# />
		<cfreturn myresult/>		
	</cffunction>
	<cffunction name="OrderFunctionPatientContact" access="remote" returntype="query">		
		<cfquery name="GetPatientContacts" datasource="#Application.DataSrc#">
				Select * 
				from   #Request.prefix_db_agency#.PatientContact 
				where  patient_id = '#cookie.patientId#' 
				and    contactstatus = 0
				order by contactname
			</cfquery>		
		
		<cfreturn GetPatientContacts/>
	</cffunction>
</cfcomponent>
