<!--- #10668: Build Orders for Discipline and Treatment from active pAdmit_Svcs + pAdmit_Svcs_Procs.
     Expects: ordersAdmitID, ordersPatientID
     Sets:    ordersDisciplineCalculated --->
<cfset ordersDisciplineCalculated = "" />
<cfparam name="ordersAdmitID" default="0" />
<cfparam name="ordersPatientID" default="0" />

<cfif Val(ordersAdmitID) GT 0 AND Val(ordersPatientID) GT 0>
	<cfquery name="GetAdmitOrdersCalc" datasource="#Application.DataSrc#">
		SELECT pAdmit_Svcs.AutoAdmitSvcs_ID,
			pAdmit_Svcs.Skill,
			IFNULL(TRIM(pAdmit_Svcs.Notes), '') AS Notes,
			pAdmit_Svcs.Start_Date
		FROM #Request.prefix_db_agency#.pAdmit_Svcs
		WHERE pAdmit_Svcs.Admit_ID = <cfqueryparam value="#Val(ordersAdmitID)#" cfsqltype="cf_sql_integer">
		AND pAdmit_Svcs.Patient_ID = <cfqueryparam value="#Val(ordersPatientID)#" cfsqltype="cf_sql_integer">
		AND pAdmit_Svcs.Status = 0
		AND pAdmit_Svcs.Skill NOT IN ('PT FU Eval', 'OT FU Eval', '0', '9 - Discharge from agency')
		ORDER BY pAdmit_Svcs.Start_Date, pAdmit_Svcs.AutoAdmitSvcs_ID
	</cfquery>

	<cfquery name="GetAdmitOrderProcsCalc" datasource="#Application.DataSrc#">
		SELECT pAdmit_Svcs_Procs.Admit_Svcs_ID,
			IFNULL(TRIM(MAX(pPathway.Description)), '') AS Proc_Description,
			pAdmit_Svcs_Procs.Item_ID
		FROM #Request.prefix_db_agency#.pAdmit_Svcs_Procs
		JOIN #Request.prefix_db_lookup#.pPathway
			ON pPathway.Item_ID = pAdmit_Svcs_Procs.Item_ID
		WHERE pAdmit_Svcs_Procs.Status = 0
		AND pAdmit_Svcs_Procs.Admit_ID = <cfqueryparam value="#Val(ordersAdmitID)#" cfsqltype="cf_sql_integer">
		AND TRIM(IFNULL(pPathway.Description, '')) <> ''
		GROUP BY pAdmit_Svcs_Procs.Admit_Svcs_ID, pAdmit_Svcs_Procs.Item_ID
		ORDER BY pAdmit_Svcs_Procs.Admit_Svcs_ID, Proc_Description
	</cfquery>

	<cfloop query="GetAdmitOrdersCalc">
		<cfset orderLine = Trim(Skill) />
		<cfset orderNotes = Trim(Notes) />
		<!--- Do not print AI Intake wording on physician 485 --->
		<cfset orderNotes = REReplaceNoCase(orderNotes, "Created from AI Intake(\s*-\s*)?", "", "all") />
		<cfset orderNotes = REReplaceNoCase(orderNotes, "\bAI Intake\b", "", "all") />
		<cfset orderNotes = Trim(REReplace(orderNotes, "\s{2,}", " ", "all")) />
		<cfset orderNotes = Trim(REReplace(orderNotes, "^[\-\s]+|[\-\s]+$", "", "all")) />
		<cfif ListFindNoCase("SOC,Hospice Admission,Palliative", orderNotes)>
			<cfset orderNotes = "" />
		</cfif>

		<cfset procList = "" />
		<cfloop query="GetAdmitOrderProcsCalc">
			<cfif Val(GetAdmitOrderProcsCalc.Admit_Svcs_ID) EQ Val(GetAdmitOrdersCalc.AutoAdmitSvcs_ID)>
				<cfset procDesc = Trim(Proc_Description) />
				<cfif Len(procDesc) GT 0 AND NOT ListFindNoCase(procList, procDesc)>
					<cfset procList = ListAppend(procList, procDesc) />
				</cfif>
			</cfif>
		</cfloop>
		<!--- Same display pattern as Add Services grid: Notes - Proc1, Proc2 --->
		<cfset detailParts = "" />
		<cfif Len(orderNotes) GT 0>
			<cfset detailParts = orderNotes />
		</cfif>
		<cfif Len(procList) GT 0>
			<cfset procDisp = Replace(procList, ",", ", ", "all") />
			<cfif Len(detailParts) GT 0>
				<cfset detailParts = detailParts & " - " & procDisp />
			<cfelse>
				<cfset detailParts = procDisp />
			</cfif>
		</cfif>
		<cfif Len(detailParts) GT 0>
			<cfset orderLine = orderLine & ": " & detailParts />
		</cfif>

		<cfset ordersDisciplineCalculated = ordersDisciplineCalculated
			& (ordersDisciplineCalculated NEQ "" ? Chr(13) & Chr(10) : "")
			& orderLine />
	</cfloop>
</cfif>
