
<cfset myarray[1] = "empty value">


<cfquery name="Agency" datasource="#Application.DataSrc#">
	select agency_id from #Request.prefix_db_lookup#.Agency where status='active' ORDER BY agency_id
</cfquery>

<cfset myarray=arraynew(1)>
<cfloop query="Agency">
	<cfset myarray[CurrentRow]=agency_id>
</cfloop>

<cfif isdefined('url.PID') and url.PID neq ''>
		<!---<cfif #Session.TurboClaim# eq 1>--->
		<cfquery  datasource="#Application.DataSrc#" name="GetData">
				SELECT p.Agency_ID, a.Agent_FEIN, a.Agency_Name, a.Natl_Provider_ID as ANPI, p.Patient_ID, p.Last_Name_M0040, p.First_Name_M0040, 
				p.Medicare_Number_M0063, p.SSN_M0064, p.DOB_M0066, p.Gender_M0069, p.Record_Creation_Date, DATE_ADD(p.Record_Creation_Date,INTERVAL 60 DAY)
				FROM #Request.prefix_db_agency#.patient p, #Request.prefix_db_lookup#.Agency a, #Request.prefix_db_agency#.Payer y
				<!---WHERE NOT EXISTS (SELECT Eligibility.Patient_ID
				FROM #Request.prefix_db_agency#.Eligibility
				WHERE p.Patient_ID = '#form.PID#'
				and Eligibility.Expiration_Date > Now())--->
				WHERE p.Patient_ID = '#url.PID#'
				and p.Agency_ID = a.Agency_ID
				and p.Payer_ID = y.Payer_ID
				and y.Output_Type = 'Medicare'
			  <!---  and p.SSN_M0064 != ''--->
				AND p.Status = 'active'
				</cfquery>
		
		<cfset finaldata = "">
		<cfset ctr = 0 />
		<cfset Gender = #Mid(GetData.Gender_M0069,5,1)#>
		<cfset DATEFROM = #NOW()#>
		<cfset DATETO = #DateAdd('d', 60, DATEFROM)#>


		<cfoutput query = "GetData">
				 <cfset finaldata = finaldata & 
				 "ISA*00*          *00*          *ZZ*MyHomecareBiz*ZZ*ZIRMED*#DateFormat(Now(), "YYMMDD")#*#DateFormat(Now(),"HHMM")#*^*00501*000001111*1*P*>~
				 GS*HS*0000000000*CMS*#DateFormat(Now(), "YYYYMMDD")#*#DateFormat(Now(),"HHMMSS")#*1111*X*005010X279A1~
				 ST*270*0001*005010X279A1~
				 BHT*0022*13*1111*#DateFormat(Now(), "YYYYMMDD")#*#DateFormat(Now(), "HHMM")#~
				 HL*1**20*1~
				 NM1*PR*2*CMS*****PI*MEDX~
				 HL*2*1*21*1~
				 NM1*1P*2*#UCase(Agency_Name)#*****XX*#ANPI#~
				 HL*3*2*22*0~
				 TRN*1*#Patient_ID#*000~
				 NM1*IL*1*#UCase(Last_Name_M0040)#*#UCase(First_Name_M0040)#****MI*#UCase(Medicare_Number_M0063)#~
				 DMG*D8*#DateFormat(DOB_M0066,"yyyymmdd")#*#Gender#~
				 DTP*291*RD8*#DateFormat(DATEFROM,"yyyymmdd")#-#DateFormat(DATETO,"yyyymmdd")#~
				 EQ*30^42~
				 III*ZZ*12~
				 SE*14*0001~
				 GE*1*1111~
				 IEA*1*000001111~">
		</cfoutput>

		<cfset invoice= #GetData.First_Name_M0040# >
		<cfset strFilePath = "/var/www/Zirmed_Upload_files_Backup/" >
		<cfset CONTRO = #DateFormat(Now(),"YYMMDDHHMMSS")#>
		<cfset CONTROL = #session.AgencyId#&'-'&#CONTRO# />
		<cfset filename= 'ELG'&#CONTROL#&'.ELG' />
	
		<cffile action="write" file="#strFilePath##filename#" output="#finaldata#" >

		<!---<cfset objFTPProperties = {
                Server = "sftp.quadax.com",
                Port = "22",
                Username = "XP_LG14",
                Password = "p5gNM7Kx",
				fingerprint = "d9:89:e1:5a:9b:c4:ca:ac:8f:93:8f:6d:c4:e0:92:fe",
                Secure = true
                } />--->
				
		<cfset objFTPProperties = {
							Server = "sshftp.zirmed.com",
							Port = "22",
							Username = "124641",
							Password = "demriz41",
							fingerprint = "c3:a3:24:d4:50:75:d2:0f:b9:a4:34:d8:94:11:17:36",
							Secure = true
							} />

		<cfftp action="open" connection="objConnection" attributeCollection="#objFTPProperties#" >
		<cfftp action="putfile" localfile="#strFilePath##filename#" remotefile="/Upload/#filename#" connection="objConnection" >
		<cfftp action="close" connection="objConnection" >



		<cfmail to="mcott@maine.rr.com" bcc="velmurugan@myhomecarebiz.com" 
			from="melissa@myhomecarebiz.com" 
			subject="Verify Eligibility Now"
			type="HTML">
			
			#session.AgencyLogin#, #session.AgencyName# - #Trim(GetData.First_Name_M0040)# #Trim(GetData.Last_Name_M0040)#, Medicare No. #Trim(GetData.Medicare_Number_M0063)#, DOB #DateFormat(GetData.DOB_M0066, "mm/dd/yyyy")#<br /><br />
			
			#finaldata#<br/><br/>
			
			Melissa Cott<br />
		  vox 888-710-6888 x2<br />
		  fax 207-221-9995<br />
		  melissa@myhomecarebiz.com<br />
		 </cfmail>
      <!---</cfif>--->
       <div class="portlet box green">
				<div class="portlet-title">
					<div class="caption">
						<i class="fa fa-gear"></i>Eligibility Report
					</div>
					<div class="tools">
						<a href="javascript:;" class="collapse"></a>
					</div>
					<div class="actions">
						<input type="button" class="btn btn-default" name="Cancel" value="Back" onClick="window.history.back();">
					</div>
				</div>
		
				<div class="portlet-body">
				<cfoutput>
				<p><b>#Trim(GetData.First_Name_M0040)# #Trim(GetData.Last_Name_M0040)# </b>patient Eligibility have uploaded sucessfully</p><br>
				<p>if you need to go back Report..So Please Click <a href="index.cfm?page=dashboard&cat=chartcompliance&rpt=Eligibility_Report">here </a></p>
				</cfoutput>
				</div>
				</div>


<cfelse>

		<cfquery name="Get_Eligibility" datasource="#Application.DataSrc#">
			select * from #Request.prefix_db_agency#.Eligibility_Report 
			JOIN #Request.prefix_db_agency#.patient on   patient.Patient_ID = Eligibility_Report.Patient_ID
			JOIN #Request.prefix_db_agency#.Payer  on Payer.Payer_ID = patient.Payer_ID
		</cfquery>


		<div class="portlet box blue">
				<div class="portlet-title">
					<div class="caption">
						<i class="fa fa-gear"></i>Eligibility Report
					</div>
					<div class="tools">
						<a href="javascript:;" class="collapse"></a>
					</div>
					<div class="actions">
						<input type="button" class="btn btn-default" name="Cancel" value="Back" onClick="window.history.back();">
					</div>
				</div>
		
				<div class="portlet-body">
					<!---<div class="text-center margin-bottom-10">
						<strong>IMPORTANT:</strong> You can download multiple assessments BUT ONLY one (1) assessment per patient.
					</div>--->
					
								<table class="table table-bordered table-striped table-condensed flip-content" id="sample_2">
									  <thead class="flip-content">
										   <tr>
										   <th></th>
												<th>Patient Name</th>
												<th>Period Checked(12 months)</th>
												<th>Status</th>
												<th>Last Checked Date</th>
												<th>HHA Episodes</th>
												<th>PAYERS</th>
												<th>EVENT</th>
												<th>Check?</th>
											</tr>
										</thead>
										<tbody>   
											 <cfoutput query="Get_Eligibility">  
													 <tr>
													 <td></td>
														<td>#Last_Name_M0040#,#First_Name_M0040#</td>
														<td >
														<cfif checked_month_start neq '' and checked_month_End neq ''>#dateformat(checked_month_start,"yyyy-mm-dd")# - #dateformat(checked_month_End,"yyyy-mm-dd")#</cfif> </td>
														<cfif Eligibility_Status EQ 'ACTIVE'>
														<td style="color:green;">#Eligibility_Status#</td>
														<cfelseif Eligibility_Status EQ 'INACTIVE'>
														<td style="color:red;">#Eligibility_Status#</td>
														</cfif>
														<td>#dateformat(Last_Checked_Date,"yyyy-mm-dd")#</td>
														<td><cfif DOLBA neq ''><b>DOLBA:&nbsp;</b>#dateformat(DOLBA,"yyyy-mm-dd")#
														 <cfelseif DOEBA neq ''><b>DOEBA:&nbsp;</b> #dateformat(DOEBA,"yyyy-mm-dd")#
														 <cfelseif HHA_Episodes_Start neq '' and HHA_Episodes_End neq ''>#dateformat(HHA_Episodes_Start,"yyyy-mm-dd")# - #dateformat(HHA_Episodes_End,"yyyy-mm-dd")#
														  </cfif>
														</td>
														<td><cfif  Payer neq ''>#Payer#<cfelse>#PayName#</cfif></td>
														<td>&nbsp;</td>
														<td><input type="button" class="btn yellow small" name="Check" value="Check" onClick="location='index.cfm?page=dashboard&cat=chartcompliance&rpt=Eligibility_Report&PID=#Patient_ID#';">
														</td>
														
													 </tr>
											  </cfoutput>
									   </tbody>
							  </table>
				   </div>
			</div>

</cfif>