
<cfset myArr1 = ArrayNew(1) />

 <cfquery  name="myQuery" datasource="#Application.DataSrc#" >
		select Agency_Login from #Request.prefix_db_lookup#.Agency where status='active'
		 
	</cfquery>
   <cfoutput query='myQuery'>
   <cfscript>
    ArrayAppend(myArr1,#Agency_Login#);
   </cfscript>
</cfoutput>
<cfsetting requesttimeout="66000">
<cfloop from="1" to="#arraylen(myArr1)#" index="REF">
<cftry>
<cfquery  name="SN_pain"  datasource="#Application.DataSrc#">								
								SELECT MAX(p.Progress_Details_ID),
								s.Visit_Type,pa.Last_Name_M0040,pa.First_Name_M0040,p.Patient_ID,MAX(p.severity) as severity,a.Freq_of_Pain_M0420,a.Start_of_Care_M0030,
								(select count(Visit_Date) from #Request.prefix_db_agency#.ScheduleItems where  
								Patient_Id = p.Patient_ID and (Visit_Type like '%Skilled Nursing%' or Visit_Type like '%SN%') )
								as SN_Visits, a.Start_of_Care_M0030, MAX(s.Visit_Date) as Visit_Date,
								DATEDIFF(MAX(s.Visit_Date),a.Start_of_Care_M0030) as Diff_visit,1
								FROM #Request.prefix_db_agency#.PatientVisitDetails p, #Request.prefix_db_agency#.ScheduleItems s, #Request.prefix_db_agency#.Assessment_New a, #Request.prefix_db_agency#.patient pa
								where p.Agency_ID = #myArr1[REF]#
								and p.ScheduleItems_ID = s.ScheduleItems_ID
								and pa.Patient_ID = p.Patient_ID
								and pa.status = 'active'
								and s.Assessment_ID = a.Assessment_ID
								and (s.Visit_Type like '%SN%' or s.Visit_Type like '%Skilled Nursing%')
								and s.Progress_Note = 'Yes'
								and p.Type = 'symptom'
								and p.Description like '%pain%'
								and s.Deleted = 0
								group by p.Patient_ID
								order by p.Patient_ID								
							</cfquery>
							<cfquery  name="SN_dyspnea"  datasource="#Application.DataSrc#">
								SELECT MAX(p.Progress_Details_ID),
								s.Visit_Type,pa.Last_Name_M0040,pa.First_Name_M0040,p.Patient_ID,MAX(p.severity) as severity, a.Breathing_M0490 ,a.Start_of_Care_M0030,
								(select count(Visit_Date) from #Request.prefix_db_agency#.ScheduleItems where  
								Patient_Id = p.Patient_ID and (Visit_Type like '%Skilled Nursing%' or Visit_Type like '%SN%') )
								as SN_Visits, a.Start_of_Care_M0030, MAX(s.Visit_Date) as Visit_Date,
								DATEDIFF(MAX(s.Visit_Date),a.Start_of_Care_M0030) as Diff_visit,1
								FROM #Request.prefix_db_agency#.PatientVisitDetails p, #Request.prefix_db_agency#.ScheduleItems s, #Request.prefix_db_agency#.Assessment_New a, #Request.prefix_db_agency#.patient pa
								where p.Agency_ID = #myArr1[REF]#
								and p.ScheduleItems_ID = s.ScheduleItems_ID
								and pa.Patient_ID = p.Patient_ID
								and pa.status = 'active'
								and s.Assessment_ID = a.Assessment_ID
								and (s.Visit_Type like '%SN%' or s.Visit_Type like '%Skilled Nursing%')
								and s.Progress_Note = 'Yes'
								and p.Type = 'symptom'
								and p.Description like '%dyspnea%'
								and s.Deleted = 0
								group by p.Patient_ID
								order by p.Patient_ID
							</cfquery>
							<cfquery  name="SN_surgical"  datasource="#Application.DataSrc#">
								SELECT MAX(p.Progress_Details_ID),
								s.Visit_Type,pa.Last_Name_M0040,pa.First_Name_M0040,p.Patient_ID,MAX(p.severity) as severity,a.Surgical_Wound_Status_M0488,a.Start_of_Care_M0030,
								(select count(Visit_Date) from #Request.prefix_db_agency#.ScheduleItems where  
								Patient_Id = p.Patient_ID and (Visit_Type like '%Skilled Nursing%' or Visit_Type like '%SN%') )
								as SN_Visits, a.Start_of_Care_M0030, MAX(s.Visit_Date) as Visit_Date,
								DATEDIFF(MAX(s.Visit_Date),a.Start_of_Care_M0030) as Diff_visit,1
								FROM #Request.prefix_db_agency#.PatientVisitDetails p, #Request.prefix_db_agency#.ScheduleItems s, #Request.prefix_db_agency#.Assessment_New a, #Request.prefix_db_agency#.patient pa
								where p.Agency_ID = #myArr1[REF]#
								and p.ScheduleItems_ID = s.ScheduleItems_ID
								and pa.Patient_ID = p.Patient_ID
								and pa.status = 'active'
								and s.Assessment_ID = a.Assessment_ID
								and (s.Visit_Type like '%SN%' or s.Visit_Type like '%Skilled Nursing%')
								and s.Progress_Note = 'Yes'
								and p.Type = 'symptom'
								and p.Description like '%surgical%'
								and s.Deleted = 0
								group by p.Patient_ID
								order by p.Patient_ID
							</cfquery>
							<cfquery  name="OT_bathe"  datasource="#Application.DataSrc#">
								SELECT MAX(p.Progress_Details_ID),
								s.Visit_Type,pa.Last_Name_M0040,pa.First_Name_M0040,p.Patient_ID,MAX(p.severity) as severity,Bathing_M0670,a.Start_of_Care_M0030,
								(select count(Visit_Date) from #Request.prefix_db_agency#.ScheduleItems where  
								Patient_Id = p.Patient_ID and (Visit_Type like '%OT%' or Visit_Type like '%occupational%') )
								as OT_Visits, a.Start_of_Care_M0030, MAX(s.Visit_Date) as Visit_Date,
								DATEDIFF(MAX(s.Visit_Date),a.Start_of_Care_M0030) as Diff_visit,1
								FROM #Request.prefix_db_agency#.PatientVisitDetails p, #Request.prefix_db_agency#.ScheduleItems s, #Request.prefix_db_agency#.Assessment_New a, #Request.prefix_db_agency#.patient pa
								where p.Agency_ID = #myArr1[REF]#
								and p.ScheduleItems_ID = s.ScheduleItems_ID
								and pa.Patient_ID = p.Patient_ID
								and pa.status = 'active'
								and s.Assessment_ID = a.Assessment_ID
								and (s.Visit_Type like '%OT%' or s.Visit_Type like '%occupational%')
								and s.Progress_Note = 'Yes'
								and p.Type = 'symptom'
								and p.Description like '%bath%'
								and s.Deleted = 0
								group by p.Patient_ID
								order by p.Patient_ID
							</cfquery>
							<cfquery  name="PT_transfer"  datasource="#Application.DataSrc#">
								SELECT MAX(p.Progress_Details_ID),
								s.Visit_Type,pa.Last_Name_M0040,pa.First_Name_M0040,p.Patient_ID,MAX(p.severity) as severity,a.Transferring_M0690,a.Start_of_Care_M0030,
								(select count(Visit_Date) from #Request.prefix_db_agency#.ScheduleItems where  
								Patient_Id = p.Patient_ID and (Visit_Type like '%PT%' or Visit_Type like '%physical%') )
								as PT_Visits, a.Start_of_Care_M0030, MAX(s.Visit_Date) as Visit_Date,
								DATEDIFF(MAX(s.Visit_Date),a.Start_of_Care_M0030) as Diff_visit,1
								FROM #Request.prefix_db_agency#.PatientVisitDetails p, #Request.prefix_db_agency#.ScheduleItems s, #Request.prefix_db_agency#.Assessment_New a, #Request.prefix_db_agency#.patient pa
								where p.Agency_ID = #myArr1[REF]#
								and p.ScheduleItems_ID = s.ScheduleItems_ID
								and pa.Patient_ID = p.Patient_ID
								and pa.status = 'active'
								and s.Assessment_ID = a.Assessment_ID
								and (s.Visit_Type like '%PT%' or s.Visit_Type like '%physical%')
								and s.Progress_Note = 'Yes'
								and p.Type = 'symptom'
								and p.Description like '%transfer%'
								and s.Deleted = 0
								group by p.Patient_ID
								order by p.Patient_ID
							</cfquery>
							
							<cfquery  name="PT_ambulat"  datasource="#Application.DataSrc#">
								SELECT MAX(p.Progress_Details_ID),
								s.Visit_Type,pa.Last_Name_M0040,pa.First_Name_M0040,p.Patient_ID,MAX(p.severity) as severity,a.Ambulation_M0700,a.Start_of_Care_M0030,
								(select count(Visit_Date) from #Request.prefix_db_agency#.ScheduleItems where  
								Patient_Id = p.Patient_ID and (Visit_Type like '%PT%' or Visit_Type like '%physical%') )
								as PT_Visits, a.Start_of_Care_M0030, MAX(s.Visit_Date) as Visit_Date,
								DATEDIFF(MAX(s.Visit_Date),a.Start_of_Care_M0030) as Diff_visit,1
								FROM #Request.prefix_db_agency#.PatientVisitDetails p, #Request.prefix_db_agency#.ScheduleItems s, #Request.prefix_db_agency#.Assessment_New a, #Request.prefix_db_agency#.patient pa
								where p.Agency_ID = #myArr1[REF]#
								and p.ScheduleItems_ID = s.ScheduleItems_ID
								and pa.Patient_ID = p.Patient_ID
								and pa.status = 'active'
								and s.Assessment_ID = a.Assessment_ID
								and (s.Visit_Type like '%PT%' or s.Visit_Type like '%physical%')
								and s.Progress_Note = 'Yes'
								and p.Type = 'symptom'
								and p.Description like '%ambul%'
								and s.Deleted = 0
								group by p.Patient_ID
								order by p.Patient_ID
							</cfquery>							
							<cfquery  name="SN_medication"  datasource="#Application.DataSrc#">
								SELECT MAX(p.Progress_Details_ID),
								s.Visit_Type,pa.Last_Name_M0040,pa.First_Name_M0040,p.Patient_ID,MAX(p.severity) as severity,a.Oral_Meds_M0780,a.Start_of_Care_M0030,
								(select count(Visit_Date) from #Request.prefix_db_agency#.ScheduleItems where  
								Patient_Id = p.Patient_ID and (Visit_Type like '%Skilled Nursing%' or Visit_Type like '%SN%') )
								as SN_Visits, a.Start_of_Care_M0030, MAX(s.Visit_Date) as Visit_Date,
								DATEDIFF(MAX(s.Visit_Date),a.Start_of_Care_M0030) as Diff_visit,1
								FROM #Request.prefix_db_agency#.PatientVisitDetails p, #Request.prefix_db_agency#.ScheduleItems s, #Request.prefix_db_agency#.Assessment_New a, #Request.prefix_db_agency#.patient pa
								where p.Agency_ID = #myArr1[REF]#
								and p.ScheduleItems_ID = s.ScheduleItems_ID
								and pa.Patient_ID = p.Patient_ID
								and pa.status = 'active'
								and s.Assessment_ID = a.Assessment_ID
								and (s.Visit_Type like '%SN%' or s.Visit_Type like '%Skilled Nursing%')
								and s.Progress_Note = 'Yes'
								and p.Type = 'symptom'
								and p.Description like '%medication%'
								and s.Deleted = 0
								group by p.Patient_ID
								order by p.Patient_ID
							</cfquery>
							
							<cfset col = ''>
							<cfset result = ''>
							<cfset pain_same=0 />
							<cfset pain_worse=0 />
							<cfset pain_Better=0 />
							<cfset result = 0 />
							<cfset surgical_same=0 />
							<cfset surgical_worse=0 />
							<cfset surgical_Better=0 />
							
							<cfoutput query="SN_pain">
							<cfif isnumeric(Left(Freq_of_Pain_M0420,1))>
							<cfif severity eq #numberformat(Left(Freq_of_Pain_M0420,1))#>
	                         <cfset pain_same = pain_same+1>
                              <cfelseif severity gt #numberformat(Left(Freq_of_Pain_M0420,1))#>
	                            <cfset pain_worse =pain_worse+1>
                               <cfelseif severity lt #numberformat(Left(Freq_of_Pain_M0420,1))#>
	                            <cfset pain_Better = pain_Better+1>
							 </cfif>	
                                <cfelse>
                           		 	<cfset result = ''>
                           			</cfif>
								</cfoutput>		
								
									<cfoutput query="SN_surgical">
								      <cfif isnumeric(Left(Surgical_Wound_Status_M0488,1))>
									      <cfif severity eq #numberformat(Left(Surgical_Wound_Status_M0488,1))#>
		                                   <cfset surgical_same = surgical_same+1>
							<cfelseif severity gt #numberformat(Left(Surgical_Wound_Status_M0488,1))#>
		                              <cfset surgical_worse = surgical_worse+1>
									  	<cfelseif severity lt #numberformat(Left(Surgical_Wound_Status_M0488,1))#>
		                               			<cfset surgical_Better = surgical_Better+1>
	                                		</cfif>
                            	     <cfelse>
                           		 		<cfset result = ''>
                           				 </cfif>
											</cfoutput>		
											
											<cfset dyspnea_same=0 />
							                <cfset dyspnea_worse=0 />
							               <cfset dyspnea_Better=0 />
									<cfoutput query="SN_dyspnea">
								                        	<cfif isnumeric(Left(Breathing_M0490,1))>
									                       		<cfif severity eq #numberformat(Left(Breathing_M0490,1))#>
		                                                        	<cfset dyspnea_same = dyspnea_same+1>
																<cfelseif severity gt #numberformat(Left(Breathing_M0490,1))#>
		                               						   		<cfset dyspnea_worse = dyspnea_worse+1>
	                                							<cfelseif severity lt #numberformat(Left(Breathing_M0490,1))#>
	                               								<cfset dyspnea_Better = dyspnea_Better+1>
	                                							</cfif>
                            								<cfelse>
                           		 								<cfset result = ''>
                           			</cfif>
											</cfoutput>
											<cfset OT_bathe_same=0 />
							                <cfset OT_bathe_worse=0 />
							               <cfset OT_bathe_Better=0 />
											
											<cfoutput query="OT_bathe">
								                        	<cfif isnumeric(Left(Bathing_M0670,1))>
									                       		<cfif severity eq #numberformat(Left(Bathing_M0670,1))#>
		                                                        	<cfset OT_bathe_same = OT_bathe_same+1>
	                                							<cfelseif severity gt #numberformat(Left(Bathing_M0670,1))#>
			                               						   		<cfset OT_bathe_worse = OT_bathe_worse+1>
	                                							<cfelseif severity lt #numberformat(Left(Bathing_M0670,1))#>
		                               								<cfset OT_bathe_Better = OT_bathe_Better+1>
	                                							</cfif>
                            								<cfelse>
                           		 								<cfset result = ''>
                           									</cfif>
											
											</cfoutput>
											<cfset PT_transfer_same=0 />
							                <cfset PT_transfer_worse=0 />
							               <cfset PT_transfer_Better=0 />
											
											<cfoutput query="PT_transfer">    
						                        	<cfif isnumeric(Left(Transferring_M0690,1))>
							                       		<cfif severity eq #numberformat(Left(Transferring_M0690,1))#>
		                                                	<cfset PT_transfer_same = PT_transfer_same+1>
	                        							<cfelseif severity gt #numberformat(Left(Transferring_M0690,1))#>
			                       						   		<cfset PT_transfer_worse = PT_transfer_worse+1>
	                        							<cfelseif severity lt #numberformat(Left(Transferring_M0690,1))#>
	                       									<cfset PT_transfer_Better = PT_transfer_Better+1>
	                        							</cfif>
                    								<cfelse>
                   		 								<cfset result = ''>
                   									</cfif>    
												</cfoutput>
												
											<cfset PT_ambulat_same=0 />
							                <cfset PT_ambulat_worse=0 />
							               <cfset PT_ambulat_Better=0 />
												<cfoutput query="PT_ambulat">														
								                        	<cfif isnumeric(Left(Ambulation_M0700,1))>
									                       		<cfif severity eq #numberformat(Left(Ambulation_M0700,1))#>
	                                                        	<cfset PT_ambulat_same = PT_ambulat_same+1>
	                                							<cfelseif severity gt #numberformat(Left(Ambulation_M0700,1))#>
	                               						   		<cfset PT_ambulat_worse = PT_ambulat_worse+1>
	                                							<cfelseif severity lt #numberformat(Left(Ambulation_M0700,1))#>
	                               								<cfset PT_ambulat_Better = PT_ambulat_Better+1>
																</cfif>
                            								<cfelse>
                           		 								<cfset result = ''>
                           									</cfif>	
															</cfoutput>
															
															
																
											<cfset SN_medication_same=0 />
							                <cfset SN_medication_worse=0 />
							               <cfset SN_medication_Better=0 />
																<cfoutput query="SN_medication">
								                        	<cfif isnumeric(Left(Oral_Meds_M0780,1))>
									                       		<cfif severity eq #numberformat(Left(Oral_Meds_M0780,1))#>
		                                                        	<cfset SN_medication_same = SN_medication_same+1>
	                                							<cfelseif severity gt #numberformat(Left(Oral_Meds_M0780,1))#>
		                               						   		<cfset SN_medication_worse = SN_medication_worse+1>
	                                							<cfelseif severity lt #numberformat(Left(Oral_Meds_M0780,1))#>
		                               								<cfset SN_medication_Better = SN_medication_Better+1>
                                								</cfif>
                            								<cfelse>
                           		 								<cfset result = ''>
                           									</cfif>
															</cfoutput>
															<cfset total_same1= 0/>
															<cfset total_worse1 =0/>
															<cfset total_Better1 =0/>
											<cfset total_same1=pain_same+surgical_same+dyspnea_same+OT_bathe_same+ PT_transfer_same+PT_ambulat_same+SN_medication_same />
											<cfset total_worse1=pain_worse+surgical_worse+dyspnea_worse+OT_bathe_worse+ PT_transfer_worse+PT_ambulat_worse+SN_medication_worse />
											<cfset total_Better1=pain_Better+surgical_Better+dyspnea_Better+OT_bathe_Better+ PT_transfer_Better+PT_ambulat_Better+SN_medication_Better />
											<!---<cfset total_pain =pain_same+pain_worse+pain_Better/>
											<cfset total_surgical =surgical_same+surgical_worse+surgical_Better/>
											<cfset total_dyspnea =dyspnea_same+dyspnea_worse+dyspnea_Better/>
											<cfset total_OT_bathe =OT_bathe_same+OT_bathe_worse+OT_bathe_Better/>
											<cfset total_PT_transfer =PT_transfer_same+PT_transfer_worse+PT_transfer_Better/>
											<cfset total_PT_ambulat =PT_ambulat_same+PT_ambulat_worse+PT_ambulat_Better/>
									<cfset total_SN_medication =SN_medication_same+SN_medication_worse+SN_medication_Better/>
											--->
								
	<cfquery  name="Graph2" datasource="#Application.DataSrc#">
	Update #Request.prefix_db_lookup#.Agency a
	set a.OMsame=#total_same1#,
	a.OMworse=#total_worse1#,
	a.OMbetter=#total_Better1#
	where a.Agency_Login =#myArr1[REF]# 
</cfquery>
<cfcatch type="any">
	<!---<cfmail to="velmurugan@myhomecarebiz.com" from="error@myhomecarebiz.com" type="html" subject="graph2 Error">
		<p><b>An error occurred on the website.</b></p>
		<cfoutput>
		Agency Login :#session.agencylogin#	 
		Agency Name:#session.agencyname#	 
		Agency ID:#session.agencyid#	 
		Display Name:#session.display# 
		User ID:#Session.EmployeeID#
		User Name:#session.username# 
		</cfoutput>
	</cfmail>--->
	</cfcatch>
</cftry>		 
						
	</cfloop>										
													