<cfset myArr = ArrayNew(1) />

 <cfquery  name="myQuery" datasource="#Application.DataSrc#" >
		select Agency_ID from #Request.prefix_db_lookup#.Agency where status='active'
		 
	</cfquery>
   <cfoutput query='myQuery'>
   <cfscript>
    ArrayAppend(myArr,#Agency_ID#);
   </cfscript>
</cfoutput>

<cfloop from="1" to="#arraylen(myArr)#" index="REF">
<cftry>
<cfquery  name="M0102_Physician_SOC_" datasource="#Application.DataSrc#">
		select
		p.Last_Name_M0040,p.First_Name_M0040,a.Start_of_Care_M0030,e.Last_Name,e.First_Name,
		DATEDIFF(a.M0102_Physician_SOC ,a.Start_of_Care_M0030) as M0102,
		DATEDIFF(a.M0104_Date_of_Referral  ,a.Start_of_Care_M0030) as M0104,
		M0102_Physician_SOC,
		M0104_Date_of_Referral
		from
		#Request.prefix_db_agency#.Assessment_New a left join
		#Request.prefix_db_agency#.patient p on a.patient_Id = p.patient_Id left join #Request.prefix_db_lookup#.Employee e on e.Employee_ID = a.Record_Created_By 
		where
		Assessment_Reason_M0100 like
		'1 - Start of care - further visits planned'		
		and p.status != 'deleted' and a.Admission_Status = 'Complete' and a.Deleted = 0
		and a.Agency_ID = #myArr[REF]#
	</cfquery>
<cfset Final_Yes = 0>
<cfset Final_No = 0>
<cfset total =0>
<cfquery  name="M0102_Physician_SOC_" datasource="#Application.DataSrc#">
		select
		p.Last_Name_M0040,p.First_Name_M0040,a.Start_of_Care_M0030,e.Last_Name,e.First_Name,
		DATEDIFF(a.M0102_Physician_SOC ,a.Start_of_Care_M0030) as M0102,
		DATEDIFF(a.M0104_Date_of_Referral  ,a.Start_of_Care_M0030) as M0104,
		M0102_Physician_SOC,
		M0104_Date_of_Referral
		from
		#Request.prefix_db_agency#.Assessment_New a left join
		#Request.prefix_db_agency#.patient p on a.patient_Id = p.patient_Id left join #Request.prefix_db_lookup#.Employee e on e.Employee_ID = a.Record_Created_By 
		where
		Assessment_Reason_M0100 like
		'1 - Start of care - further visits planned'		
		and p.status != 'deleted' and a.Admission_Status = 'Complete' and a.Deleted = 0
		and a.Agency_ID = #myArr[REF]#
	</cfquery>

                     <cfquery  name="M2010_Med_Edu_" datasource="#Application.DataSrc#">
							select
							count(a.M2010_Med_Edu) as M2010_Med_Edu
							from
							#Request.prefix_db_agency#.Assessment_New a left join
							#Request.prefix_db_agency#.patient p on a.patient_Id = p.patient_Id 
							where
							Assessment_Reason_M0100 like
							'1 - Start of care - further visits planned'
							and p.status != 'deleted' and a.Admission_Status = 'Complete' and a.Deleted = 0
							 and M2010_Med_Edu like '%Yes'
							and a.Agency_ID=#myArr[REF]#
						</cfquery>




                        <cfquery  name="M2010_Med_Edu_No" datasource="#Application.DataSrc#">
							select
							count(a.M2010_Med_Edu) as M2010_Med_Edu
							from							
							#Request.prefix_db_agency#.Assessment_New a left join
							#Request.prefix_db_agency#.patient p on a.patient_Id = p.patient_Id
							where
							Assessment_Reason_M0100 like '1 - Start of care - further visits planned'
							and M2010_Med_Edu like '%No'
							and p.status != 'deleted' and a.Admission_Status = 'Complete' and a.Deleted = 0
							and a.Agency_ID=#myArr[REF]#
						</cfquery>
					
						<cfquery  name="M1910_Fall_Risk_" datasource="#Application.DataSrc#">
							select
							count(a.M1910_Fall_Risk) as M1910_Fall_Risk
							from #Request.prefix_db_agency#.Assessment_New a left join
							#Request.prefix_db_agency#.patient p on a.patient_Id = p.patient_Id
							where Assessment_Reason_M0100 like '1 - Start of care - further visits planned'
							 and M1910_Fall_Risk like '%Yes%'
							and p.status != 'deleted' and a.Admission_Status = 'Complete' and a.Deleted = 0
							and a.Agency_ID=#myArr[REF]#
						</cfquery>
						<cfquery  name="M1910_Fall_Risk_No" datasource="#Application.DataSrc#">
							select
							count(a.M1910_Fall_Risk) as M1910_Fall_Risk
							from
							#Request.prefix_db_agency#.Assessment_New a left join
							#Request.prefix_db_agency#.patient p on a.patient_Id = p.patient_Id
							where Assessment_Reason_M0100 like '1 - Start of care - further visits planned'
							 and M1910_Fall_Risk like '%No'
							and p.status != 'deleted' and a.Admission_Status = 'Complete' and a.Deleted = 0
							and a.Agency_ID=#myArr[REF]#
						</cfquery>
					
						<cfquery  name="M1730_Depression_" datasource="#Application.DataSrc#">
							select
							count(a.M1730_Depression) as M1730_Depression
							from #Request.prefix_db_agency#.Assessment_New a left join
							#Request.prefix_db_agency#.patient p on a.patient_Id = p.patient_Id
							where Assessment_Reason_M0100 like '1 - Start of care - further visits planned'
							and M1730_Depression like '%Yes%'
							and p.status != 'deleted' and a.Admission_Status = 'Complete' and a.Deleted = 0
							and a.Agency_ID=#myArr[REF]#
						</cfquery>
						<cfquery  name="M1730_Depression_No" datasource="#Application.DataSrc#">
							select
							count(a.M1730_Depression) as M1730_Depression
							from
							#Request.prefix_db_agency#.Assessment_New a left join
							#Request.prefix_db_agency#.patient p on a.patient_Id = p.patient_Id
							where Assessment_Reason_M0100 like '1 - Start of care - further visits planned'
							 and M1730_Depression like '%No'
							and p.status != 'deleted' and a.Admission_Status = 'Complete' and a.Deleted = 0
							and a.Agency_ID=#myArr[REF]#
						</cfquery>
					
						<cfquery  name="M2250b_Diabetic_" datasource="#Application.DataSrc#">
							select
							count(a.M2250b_Diabetic) as M2250b_Diabetic
							from
							#Request.prefix_db_agency#.Assessment_New a left join
							#Request.prefix_db_agency#.patient p on a.patient_Id = p.patient_Id
							where Assessment_Reason_M0100 like '1 - Start of care - further visits planned'
						 and M2250b_Diabetic like 	'%Yes%'
							and p.status != 'deleted' and a.Admission_Status = 'Complete' and a.Deleted = 0
							and a.Agency_ID=#myArr[REF]#
						</cfquery>
						<cfquery  name="M2250b_Diabetic_No" datasource="#Application.DataSrc#">
							select
							count(a.M2250b_Diabetic) as M2250b_Diabetic
							from
							#Request.prefix_db_agency#.Assessment_New a left join
							#Request.prefix_db_agency#.patient p on a.patient_Id = p.patient_Id
							where Assessment_Reason_M0100 like '1 - Start of care - further visits planned'
							 and M2250b_Diabetic like '%No'
							and p.status != 'deleted' and a.Admission_Status = 'Complete' and a.Deleted = 0
							and a.Agency_ID=#myArr[REF]#
						</cfquery>
					
						<cfquery  name="M1240_Pain_Assess_" datasource="#Application.DataSrc#">
							select
							count(a.M1240_Pain_Assess) as M1240_Pain_Assess
							from
							#Request.prefix_db_agency#.Assessment_New a left join
							#Request.prefix_db_agency#.patient p on a.patient_Id = p.patient_Id
							where Assessment_Reason_M0100 like '1 - Start of care - further visits planned'
							 and M1240_Pain_Assess like '%Yes%'
							and p.status != 'deleted' and a.Admission_Status = 'Complete' and a.Deleted = 0
							and a.Agency_ID=#myArr[REF]#
						</cfquery>
						<cfquery  name="M1240_Pain_Assess_No" datasource="#Application.DataSrc#">
							select
							count(a.M1240_Pain_Assess) as M1240_Pain_Assess
							from #Request.prefix_db_agency#.Assessment_New a left join
							#Request.prefix_db_agency#.patient p on a.patient_Id = p.patient_Id
							where Assessment_Reason_M0100 like '1 - Start of care - further visits planned'
						    and M1240_Pain_Assess like '%No'
							and p.status != 'deleted' and a.Admission_Status = 'Complete' and a.Deleted = 0
							and a.Agency_ID=#myArr[REF]#
						</cfquery>
					
						<cfquery  name="M2250e_Pain_" datasource="#Application.DataSrc#">
							select
							count(a.M2250e_Pain) as M2250e_Pain
							from #Request.prefix_db_agency#.Assessment_New a left join
							#Request.prefix_db_agency#.patient p on a.patient_Id = p.patient_Id
							where Assessment_Reason_M0100 like '1 - Start of care - further visits planned'
						    and M2250e_Pain like '%Yes%'
							and p.status != 'deleted' and a.Admission_Status = 'Complete' and a.Deleted = 0
							and a.Agency_ID=#myArr[REF]#
						</cfquery>
						<cfquery  name="M2250e_Pain_No" datasource="#Application.DataSrc#">
							select
							count(a.M2250e_Pain) as M2250e_Pain
							from #Request.prefix_db_agency#.Assessment_New a left join
							#Request.prefix_db_agency#.patient p on a.patient_Id = p.patient_Id
							where Assessment_Reason_M0100 like '1 - Start of care - further visits planned'
							 and M2250e_Pain like '%No'
							and p.status != 'deleted' and a.Admission_Status = 'Complete' and a.Deleted = 0
							and a.Agency_ID=#myArr[REF]#
						</cfquery>
					
						<cfquery  name="M2250g_PressUlcMoist_" datasource="#Application.DataSrc#">
							select
							count(a.M2250g_PressUlcMoist) as M2250g_PressUlcMoist
							from #Request.prefix_db_agency#.Assessment_New a left join
							#Request.prefix_db_agency#.patient p on a.patient_Id = p.patient_Id
							where Assessment_Reason_M0100 like '1 - Start of care - further visits planned'
							and M2250g_PressUlcMoist like '%Yes%'
							and p.status != 'deleted' and a.Admission_Status = 'Complete' and a.Deleted = 0
							and a.Agency_ID=#myArr[REF]#
						</cfquery>
						<cfquery  name="M2250g_PressUlcMoist_No" datasource="#Application.DataSrc#">
							select
							count(a.M2250g_PressUlcMoist) as M2250g_PressUlcMoist
							from #Request.prefix_db_agency#.Assessment_New a left join
							#Request.prefix_db_agency#.patient p on a.patient_Id = p.patient_Id
							where Assessment_Reason_M0100 like '1 - Start of care - further visits planned'
						  and M2250g_PressUlcMoist like '%No'
							and p.status != 'deleted' and a.Admission_Status = 'Complete' and a.Deleted = 0
							and a.Agency_ID=#myArr[REF]#
						</cfquery>
					
						<cfquery  name="M2250f_PressUlc_" datasource="#Application.DataSrc#">
							select
							count(a.M2250f_PressUlc) as M2250f_PressUlc
							from #Request.prefix_db_agency#.Assessment_New a left join
							#Request.prefix_db_agency#.patient p on a.patient_Id = p.patient_Id
							where Assessment_Reason_M0100 like '1 - Start of care - further visits planned'
							 and M2250f_PressUlc like '%Yes%'
							and p.status != 'deleted' and a.Admission_Status = 'Complete' and a.Deleted = 0
							and a.Agency_ID=#myArr[REF]#
						</cfquery>
						<cfquery  name="M2250f_PressUlc_No" datasource="#Application.DataSrc#">
							select
							count(a.M2250f_PressUlc) as M2250f_PressUlc
							from #Request.prefix_db_agency#.Assessment_New a left join
							#Request.prefix_db_agency#.patient p on a.patient_Id = p.patient_Id 
							where Assessment_Reason_M0100 like '1 - Start of care - further visits planned'
						 and M2250f_PressUlc like '%No'
							and p.status != 'deleted' and a.Admission_Status = 'Complete' and a.Deleted = 0
							and a.Agency_ID=#myArr[REF]#
						</cfquery>
					
						<cfquery  name="M1300_Pressure_" datasource="#Application.DataSrc#">
							select
							count(a.M1300_Pressure) as M1300_Pressure
							from #Request.prefix_db_agency#.Assessment_New a left join
							#Request.prefix_db_agency#.patient p on a.patient_Id = p.patient_Id
							where Assessment_Reason_M0100 like '1 - Start of care - further visits planned'
							and M1300_Pressure like '%Yes%'
							and p.status != 'deleted' and a.Admission_Status = 'Complete' and a.Deleted = 0
							and a.Agency_ID=#myArr[REF]#
						</cfquery>
						<cfquery  name="M1300_Pressure_No" datasource="#Application.DataSrc#">
							select
							count(a.M1300_Pressure) as M1300_Pressure
							from #Request.prefix_db_agency#.Assessment_New a left join
							#Request.prefix_db_agency#.patient p on a.patient_Id = p.patient_Id
							where Assessment_Reason_M0100 like '1 - Start of care - further visits planned'
							and M1300_Pressure like '%No'
							and p.status != 'deleted' and a.Admission_Status = 'Complete' and a.Deleted = 0
							and a.Agency_ID=#myArr[REF]#
						</cfquery>
						
						<cfquery  name="M1040_Influ_" datasource="#Application.DataSrc#">
							select
							count(a.M1040_Influ) as M1040_Influ
							from #Request.prefix_db_agency#.Assessment_New a left join
							#Request.prefix_db_agency#.patient p on a.patient_Id = p.patient_Id
							where (Assessment_Reason_M0100 like '9 -%' or Assessment_Reason_M0100 like '6 -%' or Assessment_Reason_M0100 like '7 -%')
							 and (M1040_Influ like '%Yes%' or M1040_Influ like 'NA%')
							and p.status != 'deleted' and a.Admission_Status = 'Complete' and a.Deleted = 0
							and a.Agency_ID=#myArr[REF]#
						</cfquery>
						<cfquery  name="M1040_Influ_No" datasource="#Application.DataSrc#">
							select
							count(a.M1040_Influ) as M1040_Influ
							from #Request.prefix_db_agency#.Assessment_New a left join
							#Request.prefix_db_agency#.patient p on a.patient_Id = p.patient_Id
							where (Assessment_Reason_M0100 like '9 -%' or Assessment_Reason_M0100 like '6 -%' or Assessment_Reason_M0100 like '7 -%')
							and (M1040_Influ like '%No' and M1045_Influ not like '7%')
							and p.status != 'deleted' and a.Admission_Status = 'Complete' and a.Deleted = 0
							and a.Agency_ID=#myArr[REF]#
						</cfquery>
						
						
						
						<cfquery  name="M1050_Pneu_" datasource="#Application.DataSrc#">
							select
							count(a.M1050_Pneu) as M1050_Pneu
							from #Request.prefix_db_agency#.Assessment_New a left join
							#Request.prefix_db_agency#.patient p on a.patient_Id = p.patient_Id
							where (Assessment_Reason_M0100 like '9 -%' or Assessment_Reason_M0100 like '6 -%' or Assessment_Reason_M0100 like '7 -%')
							 and (M1050_Pneu like '%Yes%' or M1050_Pneu like 'NA%')
							and p.status != 'deleted' and a.Admission_Status = 'Complete' and a.Deleted = 0
							and a.Agency_ID=#myArr[REF]#
						</cfquery>
						<cfquery  name="M1050_Pneu_No" datasource="#Application.DataSrc#">
							select
							count(a.M1050_Pneu) as M1050_Pneu
							from #Request.prefix_db_agency#.Assessment_New a left join
							#Request.prefix_db_agency#.patient p on a.patient_Id = p.patient_Id
							where (Assessment_Reason_M0100 like '9 -%' or Assessment_Reason_M0100 like '6 -%' or Assessment_Reason_M0100 like '7 -%')
						   and (M1050_Pneu like '%No' and M1055_Pneu not like '5%')
							and p.status != 'deleted' and a.Admission_Status = 'Complete' and a.Deleted = 0
							and a.Agency_ID=#myArr[REF]#
						</cfquery>
						
						<cfquery  name="M1500_Symptoms_" datasource="#Application.DataSrc#">
							select
							count(a.M1500_Symptoms) as M1500_Symptoms
							from #Request.prefix_db_agency#.Assessment_New a left join
							#Request.prefix_db_agency#.patient p on a.patient_Id = p.patient_Id
							where (Assessment_Reason_M0100 like '9 -%' or Assessment_Reason_M0100 like '6 -%' or Assessment_Reason_M0100 like '7 -%')
							 and M1500_Symptoms like '%Yes%'
							and p.status != 'deleted' and a.Admission_Status = 'Complete' and a.Deleted = 0
							and a.Agency_ID=#myArr[REF]#
						</cfquery>
						<cfquery  name="M1500_Symptoms_No" datasource="#Application.DataSrc#">
							select
							count(a.M1500_Symptoms) as M1500_Symptoms
							from #Request.prefix_db_agency#.Assessment_New a left join
							#Request.prefix_db_agency#.patient p on a.patient_Id = p.patient_Id
							where (Assessment_Reason_M0100 like '9 -%' or Assessment_Reason_M0100 like '6 -%' or Assessment_Reason_M0100 like '7 -%')
							 and M1500_Symptoms like '%No'
							and p.status != 'deleted' and a.Admission_Status = 'Complete' and a.Deleted = 0
							and a.Agency_ID=#myArr[REF]#
						</cfquery>
						
						<cfset M0102_Yes = 0>
							<cfset M0102_No = 0>
							
							<cfoutput  query="M0102_Physician_SOC_">
								<cfif M0102 neq ''>
									<cfif M0102 gt 2>
										<cfset M0102_No = M0102_No+1>
									<cfelseif M0102 lt 2>
											<cfset M0102_Yes = M0102_Yes+1>
									</cfif>
								<cfelse>
									<cfif M0104 neq ''>
										<cfif M0104 gt 2>
											<cfset M0102_No = M0102_No+1>
										<cfelseif M0104 lt 2>
												<cfset M0102_Yes = M0102_Yes+1>
										</cfif>
									</cfif>
								</cfif>
							</cfoutput>
						
				

                         <cfset Final_Yes = M0102_Yes + M2010_Med_Edu_.M2010_Med_Edu + M1910_Fall_Risk_.M1910_Fall_Risk + M1730_Depression_.M1730_Depression + M2250b_Diabetic_.M2250b_Diabetic + M1240_Pain_Assess_.M1240_Pain_Assess + M2250e_Pain_.M2250e_Pain + M2250g_PressUlcMoist_.M2250g_PressUlcMoist + M2250f_PressUlc_.M2250f_PressUlc + M1300_Pressure_.M1300_Pressure + M1040_Influ_.M1040_Influ + M1050_Pneu_.M1050_Pneu +  M1500_Symptoms_.M1500_Symptoms />
						 
			         <cfset Final_No = M0102_No + M2010_Med_Edu_No.M2010_Med_Edu + M1910_Fall_Risk_No.M1910_Fall_Risk + M1730_Depression_No.M1730_Depression + M2250b_Diabetic_No.M2250b_Diabetic + M1240_Pain_Assess_No.M1240_Pain_Assess + M2250e_Pain_No.M2250e_Pain + M2250g_PressUlcMoist_No.M2250g_PressUlcMoist + M2250f_PressUlc_No.M2250f_PressUlc + M1300_Pressure_No.M1300_Pressure + M1040_Influ_No.M1040_Influ + M1050_Pneu_No.M1050_Pneu +  M1500_Symptoms_No.M1500_Symptoms />
						 
						 <cfset total = Final_Yes + Final_No />
	
	
	<cfquery  name="Graph" datasource="#Application.DataSrc#">
	Update #Request.prefix_db_lookup#.Agency a
	set a.final_yes=#Final_Yes#,
	a.final_no=#Final_No#
	where a.Agency_ID =#myArr[REF]# 
</cfquery>

<cfcatch type="any">
	<cfmail to="velmurugan@myhomecarebiz.com" from="error@myhomecarebiz.com" type="html" subject="graph 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>

						 
			