<cfparam name="URL.cat" default="patient_form" />
<cfparam name="url.new" default="0" />


<cfif session.cs.patientid NEQ 0>
<cfset Request.patient_status =  '0,1,4' >
<cfquery name="GetP" datasource="#Application.DataSrc#">
SELECT 
 0 AS Primary_ReferringPhys_ID_M0072,'' AS Medicare_Number_M0063, pPatients.*
  FROM #Request.prefix_db_agency#.pPatients
	WHERE Patient_ID =  <cfqueryparam value = "#session.cs.patientid#" CFSQLType ="CF_SQL_INTEGER">
		AND Status   NOT IN (2,3)

</cfquery>
</cfif>
 <cfset session.agencyVisit_Lock     = #getTimeZone.Visit_Lock# />
 <cfset Visit_Lock            = #getTimeZone.Visit_Lock# /> 
 <cfset runvisitlock = 0 >
 <cfparam name="url.cat" default="">
 <cfif url.cat EQ 'Patient' OR  url.cat EQ 'patientedit_visit'>
 	<cfset runvisitlock = 1 >
</cfif>

<cfset request.pendingvisitcount = 0 />
<cfset request.pendingoasis =  0 />
<cfset request.blockoasis =  0 />
<cfset request.incompleassesment = 0 />
<cfset request.missingcareplan = 0 />

<cfif runvisitlock EQ 1>
<cfquery  name="getpendingallvisits"  datasource="#Application.DataSrc#">
	SELECT pPatients.Patient_ID, pPatients.Pt_Last AS PatientLast, pPatients.Pt_First AS PatientFirst, 
	pSchedules.Visit_Type AS F26, pSchedules.Assmt_ID, pSchedules.Schedule_ID,pSchedules.Visit_Date,pSchedules.Visit_Type,pSchedules.Skill,
	pEmployee.Emp_Last,pEmployee.Emp_First,pSchedules.pgnotesdraft,
		(SELECT pAss.F25 FROM #Request.prefix_db_agency#.pAssessments pAss
				  WHERE pAss.Patient_ID =  pSchedules.Patient_ID
				  AND pAss.F26 =  ( CASE 
									WHEN (pSchedules.Visit_Type = "1 - SOC - SN" )  THEN '1 - Start of care - further visits planned'
									WHEN (pSchedules.Visit_Type = "1 - SOC - PT" ) THEN '1 - Start of care - further visits planned - Physical Therapy'
									WHEN (pSchedules.Visit_Type = "1 - SOC - OT" ) THEN '1 - Start of care - further visits planned - Occupational Therapy'
									WHEN (pSchedules.Visit_Type = "3 - ROC - SN"  || pSchedules.Visit_Type = "3 - ROC - PT") THEN '3 - Resumption of care (after inpatient stay)'
									WHEN (pSchedules.Visit_Type = "PT Eval" ) THEN 'PT Adult Evaluation'
									WHEN (pSchedules.Visit_Type = "OT Eval" ) THEN 'OT Adult Evaluation'
									WHEN (pSchedules.Visit_Type = "ST Eval" ) THEN 'ST Adult Evaluation'
									WHEN (pSchedules.Visit_Type = "SN Eval" ) THEN 'SN Adult Evaluation'
									WHEN (pSchedules.Visit_Type = "MSW Eval" ) THEN 'MSW Evaluation'
									WHEN (pSchedules.Visit_Type = "PT FU Eval" ) THEN 'PT Followup'
							    WHEN (pSchedules.Visit_Type = "OT FU Eval" ) THEN 'OT Followup'
							    WHEN (pSchedules.Visit_Type = "ST FU Eval" ) THEN 'ST Followup'
							    WHEN (pSchedules.Visit_Type = "MSW FU Eval" ) THEN 'MSW Followup'
							    WHEN (pSchedules.Visit_Type = "SN FU Eval" ) THEN 'SN Followup'
							    WHEN (pSchedules.Visit_Type = "Hospice Comprehensive" ) THEN 'Hospice Comprehensive'
							    WHEN (pSchedules.Visit_Type = "SN Hospice" ) THEN 'SN Hospice'
							    WHEN (pSchedules.Visit_Type = "Chaplain Comprehensive Evaluation" ) THEN 'Chaplain Comprehensive Evaluation'
							    WHEN (pSchedules.Visit_Type = "Hospice Discharge" ) THEN 'Hospice Discharge'
									WHEN (pSchedules.Visit_Type = "4 - Recertification" ) THEN '4 - Recertification (follow-up) reassessment'
									WHEN (pSchedules.Visit_Type = "4 - Recertification PT" ) THEN '4 - Recertification (follow-up) reassessment - PT'
									WHEN (pSchedules.Visit_Type = "4 - Recertification OT" ) THEN '4 - Recertification (follow-up) reassessment - OT'
									WHEN (pSchedules.Visit_Type = "5 - Other follow-up" ) THEN '5 - Other follow-up'
									WHEN (pSchedules.Visit_Type = "6 - Transfer no discharge" ) THEN '6 - Transferred to an inpatient facility - patient not discharged from agency'
									WHEN (pSchedules.Visit_Type = "7 - Transfer discharged" ) THEN '7 - Transferred to an inpatient facility - patient discharged from agency'
									WHEN (pSchedules.Visit_Type = "8 - Death" ) THEN '8 - Death at home'
									WHEN (pSchedules.Visit_Type = "9 - Discharge from agency" ) THEN '9 - Discharge from agency'
							ELSE 0
							END )
				  AND pAss.Status = 0  AND pSchedules.Visit_Date = pAss.F176
				  ORDER BY F25 ASC LIMIT 0,1) AS assessments_completed 

	FROM #Request.prefix_db_agency#.pSchedules
	JOIN #Request.prefix_db_agency#.pPatients ON  pPatients.Patient_ID = pSchedules.Patient_ID
	LEFT OUTER JOIN  #Request.prefix_db_lookup#.pEmployee ON  pEmployee.Emp_ID = pSchedules.Emp_ID
	WHERE pPatients.Status  = 0
  AND  pSchedules.status = 0
  AND pSchedules.Missed = 0 
  AND pSchedules.pgnotesdraft != 2 
  AND pSchedules.Visit_Date < '#DateFormat(now(),"yyyy-mm-dd")#'
   <!---<cfif CGI.REMOTE_ADDR EQ '121.200.49.65'>
  	AND pSchedules.Patient_ID = 885
  </cfif>
  AND pSchedules.pgnotesdraft != 2 
  AND (LEFT(Visit_Type,1) IN (1,3,4,5,6,7,8,9) OR Visit_Type like '%Eval') --->
  <cfif  session.eaRole does not contain 'Project Manager' >
  		 AND pSchedules.Emp_ID = #session.EmployeeID#
  </cfif>
  ORDER BY pPatients.Pt_Last ASC, pPatients.Pt_First ASC
</cfquery>
<cfset today_dat  = #dateTimeFormat(Now(),"yyyy-mm-dd")# />
<cfset visittypes = "1,3,4,5,6,7,8,9,Eval">

<cfoutput query="getpendingallvisits">
	<!--- <cfquery name="Getpending_Ass"   datasource="#Application.DataSrc#">
		SELECT pAss.F25 FROM #Request.prefix_db_agency#.pAssessments pAss
	    WHERE pAss.Patient_ID =  '#Patient_ID#' 
	    AND pAss.F26 = '#assesments#'
	    AND F176 =  '#Visit_Date#'
	    AND pAss.Status = 0
	    ORDER BY F25 ASC 
	</cfquery>
	<cfset assessments_completed = Getpending_Ass.F25 />
	<cfif (assessments_completed EQ '1' 
	 AND FindNoCase(Visit_Type,visittypes) EQ 1 AND  pgnotesdraft LT 2) OR assessments_completed NEQ 1 AND Visit_Date LT today_dat>
		<cfset request.pendingvisitcount = request.pendingvisitcount + 1 />
		<cfif  (assessments_completed NEQ 1) AND FindNoCase(Visit_Type,visittypes) EQ 0>
			<cfset request.pendingvisitcount = request.pendingvisitcount - 1 />
		</cfif>
	</cfif> --->
	<cfset visittypes = "3,4,5,6,7,8,9,FU Eval">
	<cfif ((#listContains(visittypes ,Right(Visit_Type,7))# GT 0 OR  #listContains(visittypes ,LEFT(Visit_Type,1))# GT 0) AND assessments_completed NEQ 1)
		OR  
		((#listContains(visittypes ,Right(Visit_Type,7))# GT 0  OR  #listContains(visittypes ,LEFT(Visit_Type,1))# GT 0) AND pgnotesdraft LT 2 )  
		OR 
		((#listContains(visittypes ,Right(Visit_Type,7))# EQ 0  OR  #listContains(visittypes ,LEFT(Visit_Type,1))# EQ 0) AND len(Visit_Type) GT 0 
			AND assessments_completed NEQ 1)
		OR (len(Visit_Type) EQ 0 AND   pgnotesdraft LT 2)>
		<cfset request.pendingvisitcount = request.pendingvisitcount + 1 />
		<cfif ((#listContains(visittypes ,Right(Visit_Type,7))# GT 0
		 OR  #listContains(visittypes ,LEFT(Visit_Type,1))# GT 0) AND pgnotesdraft LT 2 )  >
		<cfelseif  (assessments_completed NEQ 1) AND FindNoCase(Visit_Type,visittypes) EQ 0>
			<cfset request.pendingvisitcount = request.pendingvisitcount - 1 />			
		</cfif>
	</cfif>

</cfoutput>
	<cfif  request.pendingvisitcount gt 0 >
		<cfset request.blockoasis = 1 />
	</cfif>
	<cfquery  name="getsincompleteassessment"  datasource="#Application.DataSrc#">
			SELECT pPatients.Patient_ID, pPatients.Pt_Last AS PatientLast, pPatients.Pt_First AS PatientFirst, 
		pEmployee.Emp_ID, pEmployee.Emp_Last, pEmployee.Emp_First, 
		pAssessments.Assmt_ID,pAssessments.* 
		FROM   #Request.prefix_db_agency#.pAssessments
		JOIN #Request.prefix_db_agency#.pAdmit ON  pAdmit.Admit_ID = pAssessments.Admit_ID AND pAdmit.status = 0
		JOIN #Request.prefix_db_agency#.pPatients ON  pPatients.Patient_ID = pAssessments.Patient_ID
		AND pAdmit.Patient_ID = pPatients.Patient_ID
		LEFT OUTER JOIN  #Request.prefix_db_agency#.assessment_assign ON assessment_assign.Assmt_ID =  pAssessments.Assmt_ID
		LEFT OUTER JOIN hhapowerpath.pEmployee ON assessment_assign.Emp_ID = pEmployee.Emp_ID
		WHERE pAssessments.status = 0
		AND pPatients.Status = 0
		AND (pAssessments.F25 IS  NULL OR pAssessments.F25  = '0')
	   AND pAssessments.F176 <  '#DateFormat(now(),"yyyy-mm-dd")#'
		<cfif session.earole contains 'Field' AND session.eaRole does not contain 'Project Manager'>
	        AND  pPatients.Patient_ID IN  (SELECT  pSchedules.Patient_ID FROM #Request.prefix_db_agency#.pSchedules 
	        JOIN #Request.prefix_db_agency#.pAssessments  ON  pAssessments.Assmt_ID = pSchedules.Assmt_ID
	        WHERE pSchedules.Emp_ID = <cfqueryparam value = "#session.employeeid#" CFSQLType ="CF_SQL_INTEGER">
	         AND pSchedules.Status = 0 AND pAssessments.Status =0 )
	        AND assessment_assign.Emp_ID = <cfqueryparam value = "#session.employeeid#" CFSQLType ="CF_SQL_INTEGER">
	   </cfif> 
	   
		ORDER BY pPatients.Pt_Last ASC, pPatients.Pt_Last ASC, pPatients.Pt_First ASC, pEmployee.Emp_Last ASC, pEmployee.Emp_First ASC
	</cfquery>
	<cfset  request.incompleassesment = getsincompleteassessment.recordcount />
</cfif>

<cfloop list="SN,PT,OT,ST,MSW,HHA" index="skillitem">
    <cfset Request[skillitem & "_missingcareplan"] =  0>
</cfloop>
<cfif (session.eavisitlock Eq 0 AND session.agencyvisit_lock EQ 1 AND session.eaRole does not contain 'Project Manager'  AND session.eaRole  does not contain  'Quality') OR CGI.REMOTE_ADDR EQ '121.200.49.65'>
	<cfquery  name="Getmissingcareplan" datasource="#Application.DataSrc#">
			SELECT parent_oasis.F26 AS parent_assessment, parent_oasis.F176 AS parent_Assessment_Completed_M0090,
			 pAssessments.Assmt_ID,DATEDIFF (now(),pAssessments.F176) AS daydiff,
			 pAssessments.Record_creation_Date, pAssessments.Assmt_ID, pAssessments.F27,pAssessments.Admit_ID,
			pAssessments.Patient_ID, pAssessments.F25 AS admission_status, pAssessments.F26 AS Assessment_Reason_M0100, pAssessments.F177 AS Certification_Start, 
			pAssessments.F178 AS Certification_End, pAssessments.F176 AS Assessment_Completed_M0090,
			 pAssessments.F197 AS Transmission_status, 
			pPatients.Pt_First, pPatients.Pt_Last, Pt_Middle,

			(SELECT count(*) FROM #Request.prefix_db_agency#.pCareplan WHERE pCareplan.Assmt_ID = pAssessments.Assmt_ID
			AND pCareplan.Status = 0 AND pCareplan.Skill = pAssessments.F27)  AS careplancount,

			(SELECT count(*) FROM #Request.prefix_db_agency#.pCareplan WHERE pCareplan.Assmt_ID = assessment_assign.Parent_Assmt_ID
			AND pCareplan.Status = 0 AND pCareplan.Skill = pAssessments.F27)  AS notcreatedcareplan,


			(SELECT count(*) FROM #Request.prefix_db_agency#.pCareplan WHERE pCareplan.Assmt_ID =  assessment_assign.Parent_Assmt_ID
			AND pCareplan.Status = 0   AND pCareplan.Skill = pAssessments.F27  
			AND  pCareplan.Type IN ('discharge_plan','patient_goal')  
			AND  pCareplan.Description  IN ( 'add patient-stated goal','add discharge plan'))  AS ptgoalsnotadded,

			(SELECT  count(*) FROM  #Request.prefix_db_agency#.pWoundsVitals 
			    WHERE Assmt_ID = pAssessments.Assmt_ID
			    AND Status = 0
			     AND (Description != 'weight' OR Description != 'Weight')
			    AND VitalOrWound = 'vital' AND (Param_Lo IS NULL OR Param_Hi IS NULL OR Param_Lo= '' OR Param_Hi = '')) AS required_vital,

			(SELECT count(*) FROM #Request.prefix_db_agency#.pCareplan 
			WHERE pCareplan.Assmt_ID =  assessment_assign.Parent_Assmt_ID
			AND pCareplan.Status = 0   AND pCareplan.Skill = pAssessments.F27  
			AND  pCareplan.Type IN ('standing_order'))  AS standingorders_count,


			(SELECT count(*) FROM #Request.prefix_db_agency#.pCareplan WHERE pCareplan.Assmt_ID = pAssessments.Assmt_ID
			AND pCareplan.Status = 0 AND pCareplan.Skill = pAssessments.F27 
			AND  pCareplan.Type IN ('discharge_plan','patient_goal') )  AS patientgoalscount,

		
			(SELECT count(*) FROM #Request.prefix_db_agency#.pCareplan WHERE pCareplan.Assmt_ID 
				IN ( SELECT Assmt_ID FROM #Request.prefix_db_agency#.pAssessments ass 
					WHERE ass.Admit_ID = pAdmit.Admit_ID AND ass.Status =0)
			AND pCareplan.Status = 0 AND pCareplan.Skill = pAssessments.F27) AS adultcareplancount ,


			(SELECT count(*) FROM #Request.prefix_db_agency#.pPtmeds 
			 WHERE pPtmeds.In_Home  = 2 AND pPtmeds.Status = 0 AND pPatients.Patient_ID = pPtmeds.Patient_ID) 
			AS incomplete_meds, 

			  (SELECT  count(Communication.Comm_ID) FROM #Request.prefix_db_agency#.Communication
		            JOIN  #Request.prefix_db_agency#.CommunicationSubjects  
		             ON CommunicationSubjects.CommSub_ID = Communication.CommSub_ID  
		            WHERE Communication.Status = 0 
		            AND Communication.Patient_ID = pPatients.Patient_ID AND pAdmit.M0030_START_CARE_DT < Communication.Comm_Date
		            AND CommunicationSubjects.Subject like "Discharge Summary - PT%") AS PTdischargecount,
			  (SELECT  count(Communication.Comm_ID) FROM #Request.prefix_db_agency#.Communication
		            JOIN  #Request.prefix_db_agency#.CommunicationSubjects  
		             ON CommunicationSubjects.CommSub_ID = Communication.CommSub_ID  
		            WHERE Communication.Status = 0 
		            AND Communication.Patient_ID = pPatients.Patient_ID AND pAdmit.M0030_START_CARE_DT < Communication.Comm_Date
		            AND CommunicationSubjects.Subject like "Discharge Summary - OT%") AS OTdischargecount,
			  (SELECT  count(Communication.Comm_ID) FROM #Request.prefix_db_agency#.Communication
		            JOIN  #Request.prefix_db_agency#.CommunicationSubjects  
		             ON CommunicationSubjects.CommSub_ID = Communication.CommSub_ID  
		            WHERE Communication.Status = 0 
		            AND Communication.Patient_ID = pPatients.Patient_ID AND pAdmit.M0030_START_CARE_DT < Communication.Comm_Date
		            AND CommunicationSubjects.Subject like "Discharge Summary - ST%") AS STdischargecount,
			  (SELECT  count(Communication.Comm_ID) FROM #Request.prefix_db_agency#.Communication
		            JOIN  #Request.prefix_db_agency#.CommunicationSubjects  
		             ON CommunicationSubjects.CommSub_ID = Communication.CommSub_ID  
		            WHERE Communication.Status = 0 
		            AND Communication.Patient_ID = pPatients.Patient_ID AND pAdmit.M0030_START_CARE_DT < Communication.Comm_Date
		            AND CommunicationSubjects.Subject like "Discharge Summary - SN%") AS SNdischargecount,
			pEmployee.Emp_First,pEmployee.Emp_Last, pEmployee.Emp_ID AS Employee_ID
			FROM #Request.prefix_db_agency#.pAssessments
			JOIN #Request.prefix_db_agency#.pPatients  ON pAssessments.Patient_ID = pPatients.Patient_ID 
			JOIN  #Request.prefix_db_agency#.pAdmit  ON pAssessments.Admit_ID = pAdmit.Admit_ID  AND pAdmit.Status = 0
			 LEFT OUTER JOIN  #Request.prefix_db_agency#.assessment_assign ON assessment_assign.Assmt_ID =  pAssessments.Assmt_ID
		     AND assessment_assign.F26 = pAssessments.F26
			JOIN  #Request.prefix_db_lookup#.pEmployee ON pEmployee.Emp_ID =  assessment_assign.Emp_ID
			LEFT OUTER JOIN #Request.prefix_db_agency#.pAssessments parent_oasis ON parent_oasis.Assmt_ID =  assessment_assign.Parent_Assmt_ID

				Where pPatients.Loc_ID = '#session.agencyid#'
		 	AND pPatients.Status = 0
			AND pAssessments.Status = 0 
			AND pAssessments.F25 = 1 
			AND (pAssessments.F26 LIKE '1%' OR pAssessments.F26 LIKE '4%'
				OR pAssessments.F26 LIKE 'PT Adult%' OR pAssessments.F26 LIKE 'OT Adult%' OR pAssessments.F26 LIKE 'ST Adult%' 
				OR pAssessments.F26 LIKE 'SN Adult%' ) 
			  AND (pAssessments.F178 >= '#DateFormat(now(),"yyyy-mm-dd")#'  
			  AND parent_oasis.F176 <= '#DateFormat(now(),"yyyy-mm-dd")#')
			<cfif session.earole contains 'Field' AND session.eaRole does not contain 'Project Manager' 
			AND  session.eaRole does not contain 'Quality'>
		        AND  pPatients.Patient_ID IN  (SELECT  pSchedules.Patient_ID FROM #Request.prefix_db_agency#.pSchedules 
		        JOIN #Request.prefix_db_agency#.pAssessments  ON  pAssessments.Assmt_ID = pSchedules.Assmt_ID
		        WHERE pSchedules.Emp_ID = <cfqueryparam value = "#session.employeeid#" CFSQLType ="CF_SQL_INTEGER">
		         AND pSchedules.Status = 0 AND pAssessments.Status =0 )
		        <!--- AND assessment_assign.Emp_ID = #session.EmployeeID# --->
		    </cfif> 
		    AND  pAssessments.Admit_ID NOT IN (SELECT  pAssessments.Admit_ID FROM agency_1150.pAssessments  
			        WHERE  (pAssessments.F26 like '9%' OR pAssessments.F26 like '7%' OR pAssessments.F26 like '8%')
			         AND  pAssessments.Status =0 AND pAssessments.F25 = 1) 
				GROUP BY pAssessments.Assmt_ID 
			<!--- ORDER BY pAssessments.F178 ASC --->
		</cfquery>


	<cfoutput query="Getmissingcareplan">
		
	  	  	<cfif Employee_ID EQ session.employeeid AND daydiff GT 0>
		  	  	 <cfif  ((LEFT(Assessment_Reason_M0100,1) EQ 1 OR LEFT(Assessment_Reason_M0100,1) EQ 4)
	                    	 	  AND (ptgoalsnotadded GT 0 OR notcreatedcareplan EQ 0 OR required_vital GT 0 OR standingorders_count EQ 0))  	
	                    	 	<!--- OR incomplete_meds gt 0 --->
	                    	    OR (LEFT(Assessment_Reason_M0100,2) EQ  'PT' AND PTdischargecount EQ 0 
	                    	     AND  (ptgoalsnotadded GT 0 OR notcreatedcareplan EQ 0))
	                    	    OR (LEFT(Assessment_Reason_M0100,2) EQ  'OT' AND OTdischargecount EQ 0  
	                    	    	AND  (ptgoalsnotadded GT 0 OR notcreatedcareplan EQ 0))
	                    	    OR (LEFT(Assessment_Reason_M0100,2) EQ  'ST' AND STdischargecount EQ 0  
	                    	    	AND  (ptgoalsnotadded GT 0 OR notcreatedcareplan EQ 0))
	                    	    OR (LEFT(Assessment_Reason_M0100,2) EQ  'SN' AND SNdischargecount EQ 0  
	                    	    	AND  (ptgoalsnotadded GT 0 OR notcreatedcareplan EQ 0)) >
	                  <cfset request.missingcareplan = request.missingcareplan + 1 />  	    
	            </cfif>
            </cfif>
	</cfoutput>
	<cfif  request.missingcareplan gt 0 >
		<cfset request.blockoasis = 1 />
	</cfif> 
</cfif> 


<!--- <cfset request.pendingvisitcount = request.missingcareplan + request.pendingvisitcount /> --->


<cfset session.meds_reconilation = 0 >
<cfif isdefined('GetP.Med_Recon_Field') AND GetP.Med_Recon_Field NEQ 1 > 
	<cfset session.meds_reconilation = 1 >
</cfif>


<!--- <cfquery  datasource="#Application.DataSrc#" name="GtDX2">
	 SELECT p.*, pt.*,a.AutoAssmt_ID,a.Admit_ID,a.Assmt_ID,a.F26,a.F176,
	 a.F320 AS QA_View  FROM #Request.prefix_db_agency#.pProblem p
     JOIN #Request.prefix_db_agency#.pAssessments a ON a.Assmt_ID = p.Assmt_ID
	 JOIN #Request.prefix_db_agency#.pPatients pt  ON pt.Patient_ID = a.Patient_ID
	 JOIN #Request.prefix_db_agency#.pAdmit ad  ON ad.Admit_ID = a.Admit_ID
	 WHERE ad.status = 0
	 AND pt.status = 0
	 AND a.status = 0
	 AND p.status = 0
	 AND pt.Patient_ID = '#session.cs.patientid#'
	  AND ad.M0030_START_CARE_DT >= '2024-10-01'
	     AND (a.F26 like '1%' OR a.F26 like '4%')
	     <!---   AND a.F25 = 1  --->   
	 <!---AND p.Skill ='SN'
	 AND ad.Admit_ID NOT IN (SELECT  pAssessments.Admit_ID FROM #Request.prefix_db_agency#.pAssessments  
	        WHERE  (pAssessments.F26 like '9%' OR pAssessments.F26 like '7%' OR pAssessments.F26 like '8%')
	                AND  pAssessments.Status =0 AND pAssessments.F25 = 1)--->
     AND p.M0_No like 'M102%'
     GROUP BY p.Problem_ID,p.Assmt_ID
	 ORDER BY a.F177 DESC, p.dx_order ASC
</cfquery>
<cfset allassmt_ids = #ValueList(GtDX2.Assmt_ID)# /> 
 <cfquery  datasource="#Application.DataSrc#" name="getallmeds">
	 SELECT pMed_Recon.*,Meds_New.Classification FROM #Request.prefix_db_agency#.pMed_Recon 
	 JOIN  #Request.prefix_db_agency#.pPtmeds ON pPtmeds.PtMeds_ID = pMed_Recon.PtMeds_ID AND pPtmeds.status = 0 
	 LEFT OUTER JOIN #Request.prefix_db_lookup#.Meds_New ON pPtmeds.Drug_ID = Meds_New.Trade_Num
	 WHERE pMed_Recon.status = 0 <!--- AND Assmt_ID = '#Assmt_ID#' --->
	  <cfif len(allassmt_ids) gt 0 >
	 	AND pMed_Recon.Assmt_ID IN (#allassmt_ids#)
	 </cfif>
	 AND (pPtmeds.DateDCed > '#DateFormat(now(),"yyyy-mm-dd")#' OR pPtmeds.DateDCed IS  NULL)  
	 GROUP BY PtMeds_ID
</cfquery>
<cfquery  name="GetComms" datasource="#Application.DataSrc#">
	 SELECT c.* ,
	 	REPLACE(c.Memo, '&amp;', '') AS comm_memo 
	 FROM   #Request.prefix_db_agency#.Communication c
	 JOIN  #Request.prefix_db_agency#.CommunicationSubjects cs on   cs.CommSub_Id = c.CommSub_Id
	 WHERE   c.Patient_ID = #session.cs.patientid# <!--- AND c.Status = 0 --->
	 AND cs.Subject like 'Med Reconciliation'
	 ORDER BY Subject
</cfquery>

<cfif  getallmeds.recordcount EQ 0 and GtDX2.recordcount gt 0>
	<cfset session.meds_reconilation = 1 >
</cfif>
<cfif GtDX2.recordcount gt 0>
	<cfloop query ="getallmeds" group="PtMeds_ID">
		<cfquery  datasource="#Application.DataSrc#" name="getall_unpaired">
			SELECT pMed_Recon.* from #Request.prefix_db_agency#.pMed_Recon
			 where pMed_Recon.status = 0
			 <cfif len(allassmt_ids) gt 0 >
			 	AND pMed_Recon.Assmt_ID IN (#allassmt_ids#)
			 </cfif>
			AND pMed_Recon.PtMeds_ID = '#PtMeds_ID#'
			AND Paired ='Y' 
		</cfquery>

		<cfquery  dbtype="query" name="getsunpaired">
			SELECT * FROM getallmeds
			WHERE PtMeds_ID = '#PtMeds_ID#'
			AND Drug_Name = '#Drug_Name#'
			 <cfif len(allassmt_ids) gt 0 >
			 	AND  Assmt_ID IN (#allassmt_ids#)
			 </cfif>
			AND Paired ='N' 
		</cfquery>
		<cfset unpaired_count = '#getsunpaired.recordcount#' />

		<cfset tempdrugname = #Replace(Drug_Name, "&", "", "all")#  />
		<cfquery  dbtype="query" name="getcom">
			SELECT * FROM GetComms
			WHERE  comm_memo like '%#tempdrugname#%'
		</cfquery>
		

		<cfif  getall_unpaired.recordcount EQ 0 AND getcom.recordcount EQ 0>
				
				<cfset session.meds_reconilation = 1 >
					 <cfif CGI.REMOTE_ADDR EQ '121.200.49.65'>
				  	 <cfdump var="#getall_unpaired#">
				  	 <cfdump var="#getcom#">
				  	  <cfdump var="#tempdrugname#">
				  	   <cfdump var="#GetComms.comm_memo#">
				  </cfif>
				  <cfbreak>
		</cfif>
	</cfloop>
</cfif> --->
<!--- <cfset session.meds_reconilation = 0 >--->

<cfparam name="url.message" default="">
<cfif url.message NEQ ''>
	<script>
		$(window).on('load', function() {
		 		alertmessages = '<cfoutput>#url.message#</cfoutput>'
				bootbox.alert(alertmessages);
		});

	</script>
</cfif>
<script>
	$(window).on('load', function() {
            document.body.style.zoom = "75%" ;
        
    });
	function OASIS_Error_Check(assid, assessment_reason, wizard, answers) {
    return new Promise(function(resolve, reject) {
        jQuery.ajax({
            url: "/components/general/oasischeck.cfc",
            type: "POST",
            data: {
                method: "OASIS_Error_Check",
                assid: assid,
                wizard: wizard,
                answers: answers,
                assessment_reason: assessment_reason
            },
            success: function(return_data) {
                var obj = JSON.parse(return_data);
                if (obj.data != '') {
                    resolve(obj.data);  // resolve the Promise with the error data
                } else {
                    resolve('success');  // resolve with 'success' if no error
                }
            },
            error: function(xhr, textStatus, errorThrown) {
                reject(errorThrown);  // reject the Promise if there's an error
            }
        });
    });
}

</script>
<cfset request.assestimeout = '500' >
<cfparam name="url.rpt" default="" />

<cfoutput>
	<cfif url.page eq "patient" and url.cat neq "chart">
		<cfif url.cat neq "">
			<!--- #10058/#10373/#10374: Route generic cat=SOC|ROC to SN/PT/OT wizard (stagesecure only); do not override SOC_PT/SOC_OT --->
			<cfif CGI.HTTP_HOST EQ 'stagesecure.myhomecarebiz.com'
				AND ListFindNoCase("SOC_new,SOC_PT,SOC_OT", url.cat) EQ 0
				AND (CompareNoCase(Trim(url.cat), "SOC") EQ 0 OR CompareNoCase(Trim(url.cat), "ROC") EQ 0)>
				<cfparam name="url.ID" default="0" />
				<cfparam name="url.assid" default="0" />
				<cfset _socRouteAssId = Val(url.assid) />
				<cfif _socRouteAssId EQ 0 AND IsDefined("session.CS.AssessmentId")>
					<cfset _socRouteAssId = Val(session.CS.AssessmentId) />
				</cfif>
				<cfset _socRouteF26 = "" />
				<cfif _socRouteAssId GT 0>
					<cfquery name="getSocRouteF26" datasource="#Application.DataSrc#">
						SELECT F26 FROM #Request.prefix_db_agency#.pAssessments
						WHERE Assmt_ID = <cfqueryparam cfsqltype="cf_sql_integer" value="#_socRouteAssId#">
						AND Status = <cfqueryparam cfsqltype="cf_sql_integer" value="0">
						LIMIT 1
					</cfquery>
					<cfif getSocRouteF26.recordcount GT 0>
						<cfset _socRouteF26 = Trim(getSocRouteF26.F26) />
					</cfif>
				</cfif>
				<cfif _socRouteF26 EQ "1 - Start of care - further visits planned - Physical Therapy">
					<cfset url.cat = "SOC_PT" />
				<cfelseif _socRouteF26 EQ "1 - Start of care - further visits planned - Occupational Therapy">
					<cfset url.cat = "SOC_OT" />
				<cfelseif _socRouteF26 EQ "1 - Start of care - further visits planned" OR _socRouteF26 EQ "3 - Resumption of care (after inpatient stay)">
					<cfset url.cat = "SOC_new" />
				<cfelse>
					<cfset url.cat = "SOC" />
				</cfif>
			</cfif>
			<cfinclude template="#url.cat#.cfm">
		<cfelse>
			Invalid Patient Option #url.cat#
		</cfif>
	<cfelseif url.page eq "patient" and url.cat eq "chart">
		<cfset url.cat="patient_chart" />
		<cfset url.page="chart" />

		<cfinclude template="/patientchart/index.cfm">
	<cfelseif url.page eq "quickbooks">
		<cflocation url="../../quickbooks/index.cfm?#CGI.QUERY_STRING#" addtoken="no">
	<cfelse>
		Invalid Page Type #url.page#
	</cfif>
</cfoutput>