
<cfquery  name="getAgency" datasource="#Application.DataSrc#">
	SELECT * FROM #Request.prefix_db_lookup#.Agency  where status = 'active'; 
</cfquery>
<cfloop query="getAgency">
	<cfset emailNotification = #Email_Notification#>
	<cfoutput>
		<cfquery  name="getExpiredEmployee" datasource="#Application.DataSrc#">
			SELECT distinct e.employee_id,e.email,el.License_Type,el.Expiration_Date,e.First_Name,e.Last_Name,e.Daily_Mail_Date,e.Weekly_Mail_Date,e.2Week_Mail_Date,e.Monthly_Mail_Date FROM #Request.prefix_db_lookup#.Agency a inner join #Request.prefix_db_lookup#.Employee e on a.agency_id = e.agency_id inner join #Request.prefix_db_agency#.EmployeeLicenses el on el.employee_id = e.employee_id where a.agency_id = #agency_Id# and e.status='active' and e.email is not null and e.email <> '' and (curdate()> el.expiration_date) and e.email LIKE '%@%';
		</cfquery>
		
		<cfquery  name="getExpiringEmployee" datasource="#Application.DataSrc#">
			SELECT distinct e.employee_id,e.email,el.License_Type,el.Expiration_Date,e.First_Name,e.Last_Name,e.License_Mail_Date FROM #Request.prefix_db_lookup#.Agency a inner join #Request.prefix_db_lookup#.Employee e on a.agency_id = e.agency_id inner join #Request.prefix_db_agency#.EmployeeLicenses el on el.employee_id = e.employee_id where a.agency_id = #agency_Id# and e.status='active' and e.email is not null and e.email <> '' and (curdate()< el.expiration_date) and e.email LIKE '%@%';
		</cfquery>		
	</cfoutput>		
		<cfloop query="getExpiringEmployee">
			<cfif #Expiration_Date# NEQ ''>
				<cfset todayDate = #DateFormat(#now()#, 'mmm-dd-yyyy')#>
				<cfset checkDate = #DateFormat(ParseDateTime(Expiration_Date),"dd-mm-yyyy")#>
				<cfset exp = DateDiff("d",#todayDate#,#checkDate#) >
				<cfset days = #DaysInMonth(now())#>
				<cfif #License_Mail_Date# NEQ ''>
					<cfset license = #DateFormat(ParseDateTime(License_Mail_Date),"dd-mm-yyyy")#>
				<cfelse>
					<cfset license = '0'>
				</cfif>
			<cfif ((#exp# EQ 30 and #days# EQ 30) OR (#exp# EQ 31 and #days# EQ 31 ))  and (#license# EQ '0' OR #license# gt #checkDate#)>
				<cfquery  name="getPatientScheduleDetails" datasource="#Application.DataSrc#">
					Select s.Visit_Date,s.StartTime,s.EndTime,p.First_Name_M0040,p.Last_Name_M0040 from #Request.prefix_db_agency#.ScheduleItems s inner join #Request.prefix_db_agency#.patient p on s.patient_id = p.patient_id where s.employee_id = #employee_id# and s.Visit_Date > #Expiration_Date#
				</cfquery>
				<cfquery  name="UpdateDaily" datasource="#Application.DataSrc#">
						Update #Request.prefix_db_lookup#.Employee SET 
							License_Mail_Date = #Expiration_Date# WHERE employee_id = #employee_id#
					</cfquery>
				<cfmail from="noreply@myhomecarebiz.com" to="#email#" server="mail.msitl.com" port="25" username="secure@msitl.com" password="pa$$word" subject="License Expiration Remainder"  type="html">
							 <table cellpadding="0" cellspacing="0" border="0">
								<tr>
									<td>
										Hi #Last_Name# #First_Name#,<br /><br />
									</td>
								</tr>
								<tr>
									<td>
										Your #License_Type# License is due to expired on #DateFormat(ParseDateTime(Expiration_Date),"dd-mm-yyyy")# <br /><br />
										Your visits for the coming week are <br /><br />
									</td>
								</tr>
								<tr>
									<td>	
									<cfset isRecordAvail = 'false'>		
									<cfloop query="getPatientScheduleDetails">																	
										<cfif #getPatientScheduleDetails.recordcount#  GT 0>
											Visit Date &nbsp;&nbsp; Start Time &nbsp; &nbsp; End Time &nbsp; &nbsp; Patient Name &nbsp;&nbsp;
											<cfset isRecordAvail = 'true'>									
										</cfif>
									</cfloop>
									<cfif isRecordAvail EQ 'false'>
										Paitent(s) Not Available.
									</cfif>
									</td>
								</tr>
								<cfloop query="getPatientScheduleDetails">
								<tr>
									<td>
									<cfif #getPatientScheduleDetails.recordcount#  GT 0>
										#DateFormat(ParseDateTime(Visit_Date),"dd-mm-yyyy")#&nbsp;&nbsp; #StartTime# &nbsp; &nbsp; #EndTime# &nbsp; &nbsp; #Last_Name_M0040# #First_Name_M0040# &nbsp;&nbsp;<br />
									</cfif>
									</td>
								</tr>
								</cfloop>
								<tr>
									<td>
										<br /><br />Thanks & Regards,<br /> HomeCare Team.										
									</td>
								</tr>
							</table>
					</cfmail>
			</cfif>
			</cfif>
		</cfloop>
		<cfset weeklyTwice = #getExpiredEmployee.2Week_Mail_Date#>
		<cfloop query="getExpiredEmployee">
			<cfif #Expiration_Date# NEQ ''>
			<cfquery  name="getScheduleDetails" datasource="#Application.DataSrc#">
				Select s.Visit_Date,s.StartTime,s.EndTime,p.First_Name_M0040,p.Last_Name_M0040 from #Request.prefix_db_agency#.ScheduleItems s inner join #Request.prefix_db_agency#.patient p on s.patient_id = p.patient_id where s.employee_id = #employee_id# and s.Visit_Date > #Expiration_Date#
			</cfquery>
			<cfset isMailSend = 'false'>
			<cfif #emailNotification# EQ 'Daily'>				
				<cfif #Daily_Mail_Date# EQ '' OR #Daily_Mail_Date# EQ 'NULL'>			
					<cfquery  name="UpdateDaily" datasource="#Application.DataSrc#">
						Update #Request.prefix_db_lookup#.Employee SET 
							Daily_Mail_Date = date_add(curdate(), INTERVAL 1 DAY) WHERE employee_id = #employee_id#
					</cfquery>
					<cfset isMailSend = 'true'>
				<cfelseif #DateFormat(ParseDateTime(Daily_Mail_Date),"dd-mm-yyyy")# EQ #DateFormat(ParseDateTime(Now()),"dd-mm-yyyy")#>
					<cfquery  name="UpdateDaily" datasource="#Application.DataSrc#">
						Update #Request.prefix_db_lookup#.Employee SET 
							Daily_Mail_Date = date_add(curdate(), INTERVAL 1 DAY) WHERE employee_id = #employee_id#
					</cfquery>
					<cfset isMailSend = 'true'>
				</cfif> 
			<cfelseif #emailNotification# EQ 'Weekly'>
				<cfif #Weekly_Mail_Date# EQ '' OR #Weekly_Mail_Date# EQ 'NULL'>			
					<cfquery  name="UpdateDaily" datasource="#Application.DataSrc#">
						Update #Request.prefix_db_lookup#.Employee SET 
							Weekly_Mail_Date = date_add(curdate(), INTERVAL 7 DAY) WHERE employee_id = #employee_id#
					</cfquery>
					<cfset isMailSend = 'true'>
				<cfelseif #DateFormat(ParseDateTime(Weekly_Mail_Date),"dd-mm-yyyy")# EQ #DateFormat(ParseDateTime(Now()),"dd-mm-yyyy")#>
					<cfquery  name="UpdateDaily" datasource="#Application.DataSrc#">
						Update #Request.prefix_db_lookup#.Employee SET 
							Weekly_Mail_Date = date_add(curdate(), INTERVAL 7 DAY) WHERE employee_id = #employee_id#
					</cfquery>
					<cfset isMailSend = 'true'>
				</cfif> 
			<cfelseif #emailNotification# EQ 'Every 2 weeks'>
				<cfif #weeklyTwice# EQ '' OR #weeklyTwice# EQ 'NULL'>			
					<cfquery  name="UpdateDaily" datasource="#Application.DataSrc#">
						Update #Request.prefix_db_lookup#.Employee SET 
							2Week_Mail_Date = date_add(curdate(), INTERVAL 14 DAY) WHERE employee_id = #employee_id#
					</cfquery>
					<cfset isMailSend = 'true'>
				<cfelseif #DateFormat(ParseDateTime(weeklyTwice),"dd-mm-yyyy")# EQ #DateFormat(ParseDateTime(Now()),"dd-mm-yyyy")#>
					<cfquery  name="UpdateDaily" datasource="#Application.DataSrc#">
						Update #Request.prefix_db_lookup#.Employee SET 
							2Week_Mail_Date = date_add(curdate(), INTERVAL 14 DAY) WHERE employee_id = #employee_id#
					</cfquery>
					<cfset isMailSend = 'true'>
				</cfif> 
			<cfelseif #emailNotification# EQ 'Monthly'>
				<cfif #Monthly_Mail_Date# EQ '' OR #Monthly_Mail_Date# EQ 'NULL'>			
					<cfquery  name="UpdateDaily" datasource="#Application.DataSrc#">
						Update #Request.prefix_db_lookup#.Employee SET 
							Monthly_Mail_Date = date_add(curdate(), INTERVAL 30 DAY) WHERE employee_id = #employee_id#
					</cfquery>
					<cfset isMailSend = 'true'>
				<cfelseif #DateFormat(ParseDateTime(Monthly_Mail_Date),"dd-mm-yyyy")# EQ #DateFormat(ParseDateTime(Now()),"dd-mm-yyyy")#>
					<cfquery  name="UpdateDaily" datasource="#Application.DataSrc#">
						Update #Request.prefix_db_lookup#.Employee SET 
							Monthly_Mail_Date = date_add(curdate(), INTERVAL 30 DAY) WHERE employee_id = #employee_id#
					</cfquery>
					<cfset isMailSend = 'true'>
				</cfif> 
			</cfif>
			
			<cfif #isMailSend# EQ 'true'>
				<cfset isMailSend = 'false'>
				<cfmail from="noreply@myhomecarebiz.com" to="#email#" server="mail.msitl.com" port="25" username="secure@msitl.com" password="pa$$word" subject="License Expiration Remainder"  type="html">
							 <table cellpadding="0" cellspacing="0" border="0">
								<tr>
									<td>
										Hi #Last_Name# #First_Name#,<br /><br />
									</td>
								</tr>
								<tr>
									<td>
										Your #License_Type# License is expired on #DateFormat(ParseDateTime(Expiration_Date),"dd-mm-yyyy")# <br /><br />
										Your visits for the coming week are <br /><br />
									</td>
								</tr>
								<tr>
									<td>
										Visit Date &nbsp;&nbsp; Start Time &nbsp; &nbsp; End Time &nbsp; &nbsp; Patient Name &nbsp;&nbsp;
									</td>
								</tr>
								<cfloop query="getScheduleDetails">
								<tr>
									<td>
										#DateFormat(ParseDateTime(Visit_Date),"dd-mm-yyyy")#										&nbsp;&nbsp; #StartTime# &nbsp; &nbsp; #EndTime# &nbsp; &nbsp; #Last_Name_M0040# #First_Name_M0040# &nbsp;&nbsp;<br />
									</td>
								</tr>
								</cfloop>
								<tr>
									<td>
										<br /><br />Thanks & Regards,<br /> HomeCare Team.										
									</td>
								</tr>
							</table>
					</cfmail>
			</cfif>
			</cfif>	
		</cfloop>
</cfloop>
