<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>


<!---<cfftp action = "open"
	username = "XP_LG14"
	connection = "objConnection"
	password = "p5gNM7Kx"
	port="22" timeout="60"
	server = "sftp.quadax.com"
	secure = "yes">--->
	
	
	<cfftp action = "open"
	username = "124641"
	connection = "objConnection"
	password = "demriz41"
	port="22" timeout="80"
	server = "sshftp.zirmed.com"
	secure = "yes">
	

<cfftp connection="objConnection" timeout="60" action="GetCurrentDir" stoponerror="Yes">

<cfoutput>
	<h3>The current directory is:#cfftp.returnvalue# </h3>
</cfoutput>

<cfftp connection="objConnection" action="listdir" directory="/Download/" name="dirlist" stoponerror="Yes">

<!---<div class="portlet box blue">
	<div class="portlet-title">
		<div class="caption">
			<i class="fa fa-gear"></i> FTP Directory Listing
		</div>
		<div class="tools">
			<a class="collapse" href="javascript:;" data-original-title="" title="">
			</a>
		</div>
	</div>
	<div class="portlet-body flip-scroll">
		<table class="table table-bordered table-striped table-condensed flip-content">
			<thead class="flip-content">
				<tr>
					<th width="20%">
						Name
					</th>
					<th class="numeric">
						Path
					</th>
					<th class="numeric">
						URL
					</th>
					<th class="numeric">
						Length
					</th>
					<th class="numeric">
						LastModified
					</th>
					<th class="numeric">
						IsDirectory
					</th>
				</tr>
			</thead>
			<tbody>
				<cfoutput query="dirlist">
					<tr>
						<td>
							#name#
						</td>
						<td class="numeric">
							#path#
						</td>
						<td class="numeric">
							#url#
						</td>
						<td class="numeric">
							#length#
						</td>
						<td class="numeric">
							#DateFormat(LastModified, "yyyy-mm-dd")# #TimeFormat(LastModified, "hh:mm:ss")#
						</td>
						<td class="numeric">
							#IsDirectory#
						</td>
					</tr>
				</cfoutput>
			</tbody>
		</table>
	</div>
</div>--->

<cfset name2=ArrayNew(1) />
<cfset  c=0 />
<cfset counts = #dirlist.recordCount# - 1 />
<cfdump var="#dirlist#" />
<cfloop from="1" to="#dirlist.recordCount#" index="k">
		        <cfset filepath = "/Download/" />
				<cfset name2 = #dirlist.name[k]# />
				<cfset filepath2 = filepath&name2 >
				<cfif filepath2 does not Contain '/Download/Archive' and  (#Right(name2,7)#  eq  '277.edi'  OR #Right(name2,7)#  eq  '277.txt')>
					<cfftp action="getfile"
					remotefile="#filepath##name2#"
					 localfile="/var/www/Zirmed_Download_files_Backup/#name2#"
					 failIfExists="no" timeout="60"
					 connection="objConnection" />
		
					<cffile action="read" source="/var/www/Zirmed_Download_files_Backup/#name2#"   variable="fileContent" file="/var/www/Zirmed_Download_files_Backup/#name2#" >
					<cfset strWords = #fileContent# />
					<cfset errors = ArrayNew(1)>
					<cfset errorarray= ArrayNew(1) />

			         <cfset strWords = Replace(#strWords#, "TRN", "<", "all") />
					  <cfset var1 = ListToArray(#strWords#,'<') />
					  <cfset  date  =ListToArray(#var1[1]#,'|')/>
					 
						
			<table class="table table-bordered table-striped table-condensed flip-content">
				<thead class="flip-content">
					<tr>
						<th>
							Internal Control Number
						</th>
						<th class="numeric">
							Error
						</th>
						 <th class="numeric">
                             Date
						</th>
						<th class="numeric">
                           Status
						</th>
						
                       </tr>
					</thead>
                    <tbody>   
                        
                      <cfoutput>  
					   <cfloop from="4" to="#arraylen(var1)#" index="i">
					   
                       <cfset arrGirls = ListToArray(#var1[i]#,'|') />
					   <cfset Internal_Control_number_dup =#arrGirls[2]# />
					    <cfset a = Internal_Control_number_dup.Split("~") >
						<cfset Internal_Control_number = a[1] />
                        <cfset Error =#arrGirls[3]# />
                        <cfset Rejected =#arrGirls[4]# />
						<cfset status =#arrGirls[5]# />
						<cfif arrGirls[5] eq 'WQ'>
						<cfset date1 = createObject("java","java.text.SimpleDateFormat").init("yyyyMMdd").parse(#Rejected#,createObject("java","java.text.ParsePosition").init(0*0)) />
						<cfset Rejected_date=#dateformat(date1,"yyyy-mm-dd")# />
						</cfif>
						<cfset Errors =  ''>
						<cfset Errordescription =  ''>
                        <!--- <cfset  c=c+1 />
						 <cfset Errors = '' />
						 <cfset Errors_array = ListToArray(#arrGirls[2]#,'STC') />
						 <cfloop from="2" to="#arraylen(Errors_array)#" index="j">
						 <cfoutput>#Mid(Errors_array[j],2,6)#<br /></cfoutput>
						 <cfset Errors = Errors&#Mid(Errors_array[j],2,6)#&','/>
						 </cfloop>
						 <cfoutput>#Errors#</cfoutput>--->
                       <tr>
									<td>#Internal_Control_number#</td>
									<cfif arrGirls[5] neq 'WQ' and arrGirls[5] eq 'U'  >
									<cfset errors = #arrGirls[7]# />
									<cfset i = 7>
									<cfloop condition="#Right(arrGirls[i],3)#  contains 'STC'">
									<cfset i = i + 5 />
									<cfset errors = errors&#arrGirls[i]# />
									</cfloop>
									<cfset MyString = Replace(errors, "~STC", "<br />", "ALL")>
									<cfset Errordescription = Replace(MyString, "~REF", "", "ALL")>
									<td class="numeric">#Errordescription#</td>
									<!---<cfloop from="1" to="#arraylen(errors)#" index="j">
									 <cfset errorarray= ListToArray(#errors[j]#,'|') />
									<cfset Errors = Errors&errorarray[12]&'<br />' />
									</cfloop>
									<cfset errorarray = ListToArray(#var1[i]#,'|U|') />
									<cfloop from="1" to="#arraylen(errorarray)#" index="j">
									<td class="numeric"> #errorarray[9]#</td>
									<cfset errors = ListToArray(#errorarray[j]#,'|') />
									<cfloop from="1" to="#arraylen(errors)#" index="k">
											<td class="numeric"> #errors[k]#</td>
											 </cfloop>
										 </cfloop>--->
									<cfelse>
									<td class="numeric"></td>
									</cfif>
									
									<cfif arrGirls[5] eq 'WQ'>
									<td class="numeric"> #Rejected_date#</td>
									<td class="numeric"> #status# &nbsp;Accepted</td>
									<cfelse>
									<td class="numeric">#DateFormat(Now(),"YYYY-MM-DD")#</td>
									<td class="numeric"> #status# &nbsp; Rejected</td>
									</cfif>
									
                     </tr>
					 
					 
			<cfif Internal_control_number contains '-'>
			<cfset b = Internal_Control_number.Split("-") >
			<cfset Request.prefix_db_agency = 'agency_'&#b[1]# />
			</cfif>
								
  	<cfquery  name="getInvoiceId" dataSource="#Application.DataSrc#">
			SELECT * FROM  #Request.prefix_db_agency#.Invoices
			Where Internal_control_number='#Internal_control_number#'
		</cfquery>
			<cfset getsucess= generalCFC.AddInvoiceHistory(valueList(getInvoiceId.Invoice_ID)) />
		<cfif Request.prefix_db_agency NEQ 'prefix_db_agency_invalid'>
			            <cfif arrGirls[5] eq 'WQ'>
						     <cfquery name="updateInvoices" dataSource="#Application.DataSrc#">
                                        UPDATE  IGNORE  #Request.prefix_db_agency#.Invoices
										SET  claim_status ='Accepted',
										<!---Error_description = '#Errordescription#',--->
										User_defined_Status ='Processing' ,
										claim_received_date = CURDATE()
            				,Record_Modified_By = '#Session.EmployeeId#'
            				,Record_Modified_Date = #now()#
										WHERE Internal_control_number ='#Internal_control_number#'
										AND Internal_control_number !=0
									</cfquery>
						<cfelse>
							
							    <cfquery name="updateInvoices" dataSource="#Application.DataSrc#">
                                        UPDATE IGNORE  #Request.prefix_db_agency#.Invoices
										SET  claim_status ='Rejected',
										Error_description = '#Errordescription#',
										User_defined_Status ='Needs fix' ,
										claim_rejected_date =  CURDATE()
            				,Record_Modified_By = '#Session.EmployeeId#'
            				,Record_Modified_Date = #now()#
										WHERE Internal_control_number ='#Internal_control_number#'
										AND Internal_control_number !=0
									</cfquery>
							
							
						</cfif>
									
			<cfelse>
			        <cfif arrGirls[5] eq 'WQ'>
					 
									 <cfloop from=1 to=#ArrayLen(myarray)# index=ind>
												<cfif ArrayLen(myarray) gt 1>
													<cfset Request.prefix_db_agency = 'agency_'&myarray[ind] />
												</cfif>
									            <cfquery name="updateInvoices" dataSource="#Application.DataSrc#">
													UPDATE IGNORE #Request.prefix_db_agency#.Invoices
													SET  claim_status ='Accepted',
													<!---Error_description = '#Errordescription#',--->
													User_defined_Status ='Processing' ,
													claim_received_date = CURDATE()
            							,Record_Modified_By = '#Session.EmployeeId#'
            							,Record_Modified_Date = #now()#
													WHERE Internal_control_number ='#Internal_control_number#'
													AND Internal_control_number !=0
											     </cfquery>
									 </cfloop>
						<cfelse>
						
						           <cfloop from=1 to=#ArrayLen(myarray)# index=ind>
												<cfif ArrayLen(myarray) gt 1>
													<cfset Request.prefix_db_agency = 'agency_'&myarray[ind] />
												</cfif>
		                     <cfquery name="updateInvoices" dataSource="#Application.DataSrc#">
													UPDATE  IGNORE #Request.prefix_db_agency#.Invoices
													SET  claim_status ='Rejected',
													Error_description = '#Errordescription#',
													User_defined_Status ='Needs fix' ,
													claim_rejected_date =  CURDATE()
            							,Record_Modified_By = '#Session.EmployeeId#'
            							,Record_Modified_Date = #now()#
													WHERE Internal_control_number ='#Internal_control_number#'
													AND Internal_control_number !=0
												</cfquery>
									</cfloop>
						</cfif>
									
		</cfif>	
			 <cfset Request.prefix_db_agency ='prefix_db_agency_invalid'>	 
					 </cfloop>
					 </cfoutput></tbody></table>
					 </cfif>
					 </cfloop>
	
<cfloop from="1" to="#dirlist.recordCount#" index="k">
		<cfset filepath = "/Download/" />

		<cfset name2 = #dirlist.name[k]# />
		<cfset filepath2 = filepath&name2 >
		<cfif filepath2 does not Contain '/Download/Archive' and  (#Right(name2,7)#  eq  '277.edi'  OR #Right(name2,7)#  eq  '277.txt')>
			<cfftp action="remove"  item="#filepath##name2#"  connection="objConnection"  stoponerror="yes" />
		</cfif>	
	</cfloop>
	

<cfftp action="close" connection="objConnection"/>