<!--- Sets printCompletedDate and completed_by ("Completed & Digitally Signed by ... on ...") for an assessment print.
     Expects AssForm with Assmt_ID, Patient_ID, F26, F176 and assesments (the schedule Visit_Type of F26).
     pSchedules.Assmt_ID holds the episode's SOC / recert for every visit of the episode, so the assessment's own visit
     is the visit of its type on its M0090 date (F176), not any visit linked to the Assmt_ID. --->
<cfset printCompletedDate = AssForm.F176 />
<cfset completed_by = "" />
<cfset _printSignerEmpID = 0 />

<cfif IsDate(AssForm.F176) AND Len(Trim(AssForm.assesments)) AND AssForm.assesments NEQ "0">
	<cfquery name="getPrintSignerVisit" datasource="#Application.DataSrc#">
		SELECT pSchedules.Emp_ID
		FROM #Request.prefix_db_agency#.pSchedules
		WHERE pSchedules.Status = 0
		AND pSchedules.Patient_ID = <cfqueryparam cfsqltype="cf_sql_integer" value="#AssForm.Patient_ID#">
		AND pSchedules.Visit_Type = <cfqueryparam cfsqltype="cf_sql_varchar" value="#AssForm.assesments#">
		AND pSchedules.Visit_Date = <cfqueryparam cfsqltype="cf_sql_date" value="#AssForm.F176#">
		AND IFNULL(pSchedules.Missed, 0) = 0
		AND IFNULL(pSchedules.Emp_ID, 0) > 0
		ORDER BY pSchedules.Schedule_ID DESC
		LIMIT 1
	</cfquery>
	<cfif getPrintSignerVisit.recordcount GT 0>
		<cfset _printSignerEmpID = Val(getPrintSignerVisit.Emp_ID) />
	</cfif>
</cfif>

<cfif _printSignerEmpID EQ 0>
	<cfquery name="getPrintSignerAssign" datasource="#Application.DataSrc#">
		SELECT Emp_ID
		FROM #Request.prefix_db_agency#.assessment_assign
		WHERE Assmt_ID = <cfqueryparam cfsqltype="cf_sql_integer" value="#AssForm.Assmt_ID#">
		AND F26 = <cfqueryparam cfsqltype="cf_sql_varchar" value="#AssForm.F26#">
		AND IFNULL(Emp_ID, 0) > 0
		LIMIT 1
	</cfquery>
	<cfif getPrintSignerAssign.recordcount GT 0>
		<cfset _printSignerEmpID = Val(getPrintSignerAssign.Emp_ID) />
	</cfif>
</cfif>

<cfif _printSignerEmpID EQ 0>
	<cfquery name="getPrintSignerCreator" datasource="#Application.DataSrc#">
		SELECT Record_Created_By
		FROM #Request.prefix_db_agency#.pAssessments
		WHERE Assmt_ID = <cfqueryparam cfsqltype="cf_sql_integer" value="#AssForm.Assmt_ID#">
		AND Status = 0
		LIMIT 1
	</cfquery>
	<cfset _printSignerEmpID = Val(getPrintSignerCreator.Record_Created_By) />
</cfif>

<cfif _printSignerEmpID GT 0>
	<cfquery name="getPrintSigner" datasource="#Application.DataSrc#">
		SELECT Emp_First, Emp_Last, Skill_1, Skill_2, Skill_3
		FROM #Request.prefix_db_lookup#.pEmployee
		WHERE Emp_ID = <cfqueryparam cfsqltype="cf_sql_integer" value="#_printSignerEmpID#">
	</cfquery>
	<cfif getPrintSigner.recordcount GT 0>
		<cfset completed_by = Trim(getPrintSigner.Emp_First & " " & getPrintSigner.Emp_Last) />
		<cfif Len(Trim(getPrintSigner.Skill_1))><cfset completed_by = completed_by & ", " & Trim(getPrintSigner.Skill_1) /></cfif>
		<cfif Len(Trim(getPrintSigner.Skill_2))><cfset completed_by = completed_by & " " & Trim(getPrintSigner.Skill_2) /></cfif>
		<cfif Len(Trim(getPrintSigner.Skill_3))><cfset completed_by = completed_by & " " & Trim(getPrintSigner.Skill_3) /></cfif>
	</cfif>
</cfif>
