<cfparam name="url.payname" default="">
<cfparam name="url.patfilter" default="">
<cfparam name="url.df" default="">
<cfparam name="url.dt" default="">
<cfparam name="url.age" default="">
<cfparam name="form.amount_filter" default="">

<div style='margin:60px 0px 0px 20px;'>

<cfquery  name="gets" datasource="#Application.DataSrc#">
select CONCAT(p.Last_Name_M0040,' ',p.First_Name_M0040,' ','(',pr.PayName,')',' ',Date_Format(a.Certification_End,'%m/%d/%Y')) as Name, CONCAT(p.City,',',p.State_M0050,' ',Zip_M0060) as Addr, i.Invoice_Id, i.DateSent, i.MethodSent, i.Comment, i.InvoiceTotal, i.PayerBalance, i.Assessment_Tracker_Id ,DATEDIFF(CURDATE(), i.DateSent) AS age , p.Patient_Id, p.Status, p.Last_Name_M0040, p.First_Name_M0040, p.Middle_Initial_M0040 ,
a.Certification_Start AS START, a.Certification_End AS END , a.Assessment_Reason_M0100 AS TYPE, pr.PayName, pr.Payer_Id, 
pr.Output_Type AS PayerOutputType , (SELECT SUM(Amount) FROM #Request.prefix_db_agency#.Payments WHERE Invoice_Id = i.Invoice_Id) AS Paid ,
(SELECT SUM(WriteOffAmount) FROM #Request.prefix_db_agency#.Payments WHERE Invoice_Id = i.Invoice_Id) AS wOff ,
(SELECT Invoice_Id FROM #Request.prefix_db_agency#.Invoices WHERE Assessment_Tracker_Id = i.Assessment_Tracker_Id AND InvoiceTotal = '0.00' and Deleted <> 1 LIMIT 1) AS FinalBilling , (SELECT DATEDIFF(CURDATE(), DateSent) FROM #Request.prefix_db_agency#.Invoices WHERE Assessment_Tracker_Id = i.Assessment_Tracker_Id AND InvoiceTotal = '0.00' and Deleted <> 1 LIMIT 1) AS FinalAge,
(select Output_Type from #Request.prefix_db_agency#.ScheduleItems where invoice_id = i.invoice_id AND Total_EClaim > 0 AND Deleted <> 1 limit 1 ) as Output_Type, (select Total_Eclaim from #Request.prefix_db_agency#.ScheduleItems where invoice_id = i.invoice_id 
 AND Total_EClaim > 0 AND Deleted <> 1 limit 1 ) as Total_Eclaim
 FROM #Request.prefix_db_agency#.Invoices i, #Request.prefix_db_agency#.Payer pr, #Request.prefix_db_agency#.patient p, #Request.prefix_db_agency#.Assessment_New a 
WHERE
 i.Agency_ID = '#session.agencyid#' AND i.Deleted <> 1 AND i.InvoiceTotal > 0 
 <cfif form.amount_filter NEQ "">
  AND i.InvoiceTotal = '#form.amount_filter#'
 </cfif>
 <cfif url.df NEQ "">
   AND SUBSTR(i.DateSent,1,10) >= #CreateODBCDate(url.df)#
 </cfif>
 <cfif url.dt NEQ "">
   AND SUBSTR(i.DateSent,1,10) <= #CreateODBCDate(url.dt)#
 </cfif>
 <cfif url.age NEQ "">
  AND DATEDIFF(CURDATE(), i.DateSent) >= '#url.age#'
 </cfif>
  <cfif url.patfilter NEQ "">
   AND (p.Last_Name_M0040 LIKE '%#url.patfilter#%' OR p.First_Name_M0040 LIKE "%#url.patfilter#%")
  </cfif>
  <cfif url.payname NEQ "">
  AND pr.PayName LIKE '%#url.payname#%'
 </cfif>

AND ( (i.InvoiceTotal - (select sum(amount + writeoffamount) from #Request.prefix_db_agency#.Payments 
where invoice_id = i.invoice_id) > 0) OR (select sum(amount + writeoffamount) 
from #Request.prefix_db_agency#.Payments where invoice_id = i.invoice_id) is null ) 
AND p.patient_id = i.patient_id 
AND i.Assessment_Tracker_ID = a.Assessment_Tracker_ID 
AND pr.payer_id = i.patientpayer_id
and i.QBExport = 0
ORDER BY Last_Name_M0040, First_Name_M0040, i.Invoice_Id

</cfquery>

<cfquery  name="gets1" datasource="#Application.DataSrc#">
select CONCAT(p.Last_Name_M0040,' ',p.First_Name_M0040,' ','(',pr.PayName,')',' ',Date_Format(a.Certification_End,'%m/%d/%Y')) as Name, CONCAT(p.City,',',p.State_M0050,' ',Zip_M0060) as Addr, i.Invoice_Id, i.DateSent, i.MethodSent, i.Comment, i.InvoiceTotal, i.PayerBalance, i.Assessment_Tracker_Id ,DATEDIFF(CURDATE(), i.DateSent) AS age , p.Patient_Id, p.Status, p.Last_Name_M0040, p.First_Name_M0040, p.Middle_Initial_M0040 ,
p.Street_Address, p.Mailing_Address,
	p.City, p.State_M0050, p.Zip_M0060, a.Certification_Start AS START, a.Certification_End AS END , a.Assessment_Reason_M0100 AS TYPE, pr.PayName, pr.Payer_Id, 
pr.Output_Type AS PayerOutputType , (SELECT SUM(Amount) FROM #Request.prefix_db_agency#.Payments WHERE Invoice_Id = i.Invoice_Id) AS Paid ,
(SELECT SUM(WriteOffAmount) FROM #Request.prefix_db_agency#.Payments WHERE Invoice_Id = i.Invoice_Id) AS wOff ,
(SELECT Invoice_Id FROM #Request.prefix_db_agency#.Invoices WHERE Assessment_Tracker_Id = i.Assessment_Tracker_Id AND InvoiceTotal = '0.00' and Deleted <> 1 LIMIT 1) AS FinalBilling , (SELECT DATEDIFF(CURDATE(), DateSent) FROM #Request.prefix_db_agency#.Invoices WHERE Assessment_Tracker_Id = i.Assessment_Tracker_Id AND InvoiceTotal = '0.00' and Deleted <> 1 LIMIT 1) AS FinalAge,
(select Output_Type from #Request.prefix_db_agency#.ScheduleItems where invoice_id = i.invoice_id AND Total_EClaim > 0 AND Deleted <> 1 limit 1 ) as Output_Type, (select Total_Eclaim from #Request.prefix_db_agency#.ScheduleItems where invoice_id = i.invoice_id 
 AND Total_EClaim > 0 AND Deleted <> 1 limit 1 ) as Total_Eclaim
 FROM #Request.prefix_db_agency#.Invoices i, #Request.prefix_db_agency#.Payer pr, #Request.prefix_db_agency#.patient p, #Request.prefix_db_agency#.Assessment_New a 
WHERE
 i.Agency_ID = '#session.agencyid#' AND i.Deleted <> 1 AND i.InvoiceTotal > 0 
 <cfif form.amount_filter NEQ "">
  AND i.InvoiceTotal = '#form.amount_filter#'
 </cfif>
 <cfif url.df NEQ "">
   AND SUBSTR(i.DateSent,1,10) >= #CreateODBCDate(url.df)#
 </cfif>
 <cfif url.dt NEQ "">
   AND SUBSTR(i.DateSent,1,10) <= #CreateODBCDate(url.dt)#
 </cfif>
 <cfif url.age NEQ "">
  AND DATEDIFF(CURDATE(), i.DateSent) >= '#url.age#'
 </cfif>
  <cfif url.patfilter NEQ "">
   AND (p.Last_Name_M0040 LIKE '%#url.patfilter#%' OR p.First_Name_M0040 LIKE "%#url.patfilter#%")
  </cfif>
  <cfif url.payname NEQ "">
  AND pr.PayName LIKE '%#url.payname#%'
 </cfif>

AND ( (i.InvoiceTotal - (select sum(amount + writeoffamount) from #Request.prefix_db_agency#.Payments 
where invoice_id = i.invoice_id) > 0) OR (select sum(amount + writeoffamount) 
from #Request.prefix_db_agency#.Payments where invoice_id = i.invoice_id) is null ) 
AND p.patient_id = i.patient_id 
AND i.Assessment_Tracker_ID = a.Assessment_Tracker_ID 
AND pr.payer_id = i.patientpayer_id 
and i.QBExport = 0
Group by Patient_ID
ORDER BY Last_Name_M0040, First_Name_M0040, i.Invoice_Id
</cfquery>

<cfoutput query="gets">
 <cfset getsucess= generalCFC.AddInvoiceHistory(gets.Invoice_Id) />
<cfquery  name="updateqb" datasource="#Application.DataSrc#">
Update #Request.prefix_db_agency#.Invoices SET
QBExport = 1
,Record_Modified_By = '#Session.EmployeeId#'
,Record_Modified_Date = #now()#
Where Invoice_Id = #gets.Invoice_Id#
</cfquery>
</cfoutput>


 <cfif gets.recordcount gt 0>
  <cfset str = """" & "Last Name" & """"
             & "," & """" & "First Name" & """"
             & "," & """" & "Status " & """"
			 & "," & """" & "Payer" & """"
             & "," & """" & "Date Billed" & """"
  			 & "," & """" & "Balance" & """"
             & "," & """" & "Paid" & """"
             & "," & """" & "Write-off" & """"
             & "," & """" & "Total" & """"
             & "," & """" & "Certification Start" & """"
			 & "," & """" & "Certification End" & """"
			 & "," & """" & "Comment" & """"
			 & chr(13)
						 & chr(10)
  />
  
   <cfset _Cstatus = 0>
  <cfoutput query='gets'>
  
  <cfset InvEclaim = gets.Total_EClaim />
    <cfif InvEclaim eq ''>
	 <cfset InvEclaim = 0 />
    </cfif>
   <!---<cfif GetI.Output_Type eq 'RAP' or (InvAmt eq 0)>--->
    <cfset InvAmt = gets.InvoiceTotal />
   <!---</cfif>--->
   <cfif InvAmt eq ''>
	<cfset InvAmt = 0 />
   </cfif>

   <cfset amtPaid = paid />
   <cfif amtPaid eq ''>
	<cfset amtPaid = 0 />
   </cfif>

   <cfset writeOff = wOff />
   <cfif writeOff eq ''>
	<cfset writeOff = 0 />
   </cfif>
  
<cfset bal = DollarFormat(InvAmt - amtPaid - writeOff)>  

   <cfset str = str
                      & """" & #Last_Name_M0040# & """"
                & "," & """" & #First_Name_M0040# & """"
				& "," & """" & #Status# & """"
                & "," & """" & #PayName# & """"
                & "," & """" & #DateFormat(DateSent,"mm/dd/yy")# & """"
                & "," & """" & #bal# & """"
				& "," & """" & #DollarFormat(Paid)# & """"
				& "," & """" & #DollarFormat(writeOff)# & """"
				& "," & """" & #DollarFormat(InvAmt)# & """"
				& "," & """" & #DateFormat(START,"mm/dd/yy")# & """"
				& "," & """" & #DateFormat(END,"mm/dd/yy")# & """"
				& "," & """" & #Comment# & """"
				
                & chr(13)
						    & chr(10)
   />
   
   </cfoutput>
   
   
   <cfset str1 = """" & "" & """"
             & "," & """" & "!TRNS" & """"   
             & "," & """" & "TRNSID" & """"
	         & "," & """" & "TRNSTYPE" & """"
             & "," & """" & "DATE" & """"
             & "," & """" & "ACCNT" & """"
			 & "," & """" & "NAME" & """"
			 & "," & """" & "CLASS" & """"
             & "," & """" & "AMOUNT" & """"
  			 & "," & """" & "DOCNUM" & """"
  			 & "," & """" & "MEMO" & """"
  			 & "," & """" & "CLEAR" & """"
  			 & "," & """" & "TOPRINT" & """"
  			 & "," & """" & "NAMEISTAXABLE" & """"
  			 & "," & """" & "DUEDATE" & """"
  			 & "," & """" & "TERMS" & """"
  			 & "," & """" & "PAYMETH" & """"
  			 & "," & """" & "SHIPVIA" & """"
  			 & "," & """" & "SHIPDATE" & """"
  			 & "," & """" & "REP" & """"
  			 & "," & """" & "FOB" & """"
  			 & "," & """" & "PONUM" & """"
  			 & "," & """" & "INVMEMO" & """"
  			 & "," & """" & "ADDR1" & """"
  			 & "," & """" & "ADDR2" & """"
  			 & "," & """" & "ADDR3" & """"
  			 & "," & """" & "ADDR4" & """"
  			 & "," & """" & "ADDR5" & """"
  			 & "," & """" & "SADDR1" & """"
  			 & "," & """" & "SADDR2" & """"
  			 & "," & """" & "SADDR3" & """"
  			 & "," & """" & "SADDR4" & """"
  			 & "," & """" & "SADDR5" & """"
  			 & "," & """" & "TOSEND" & """"
  			 & "," & """" & "ISAJE" & """"
  			 & "," & """" & "OTHER1" & """"
			 & chr(10)
						 & chr(11)
             & "," & """" & "!SPL" & """"
             & "," & """" & "SPLID" & """"
	         & "," & """" & "TRNSTYPE" & """"
             & "," & """" & "DATE" & """"
             & "," & """" & "ACCNT" & """"
			 & "," & """" & "NAME" & """"
			 & "," & """" & "CLASS" & """"
             & "," & """" & "AMOUNT" & """"
  			 & "," & """" & "DOCNUM" & """"
  			 & "," & """" & "MEMO" & """"
  			 & "," & """" & "CLEAR" & """"
  			 & "," & """" & "QNTY" & """"
  			 & "," & """" & "PRICE" & """"
  			 & "," & """" & "INVITEM" & """"
  			 & "," & """" & "PAYMETH" & """"
  			 & "," & """" & "TAXABLE" & """"
  			 & "," & """" & "EXTRA" & """"
  			 & "," & """" & "VATCODE" & """"
  			 & "," & """" & "VATRATE" & """"
  			 & "," & """" & "VATAMOUNT" & """"
  			 & "," & """" & "VALADJ" & """"
  			 & "," & """" & "SERVICEDATE" & """"
  			 & "," & """" & "TAXCODE" & """"
  			 & "," & """" & "TAXRATE" & """"
  			 & "," & """" & "TAXAMOUNT" & """"
  			 & "," & """" & "TAXITEM" & """"
  			 & "," & """" & "OTHER2" & """"
  			 & "," & """" & "OTHER3" & """"
  			 & "," & """" & "REIMBEXP" & """"
			 & chr(13)
						 & chr(11)
			& "," & """" & "!ENDTRNS" & """"			 
			 & chr(13)
						 & chr(11)
  />
  
   <cfoutput query='gets'>
   
     <cfset InvEclaim = gets.Total_EClaim />
    <cfif InvEclaim eq ''>
	 <cfset InvEclaim = 0 />
    </cfif>
   <!---<cfif GetI.Output_Type eq 'RAP' or (InvAmt eq 0)>--->
    <cfset InvAmt = gets.InvoiceTotal />
   <!---</cfif>--->
   <cfif InvAmt eq ''>
	<cfset InvAmt = 0 />
   </cfif>

   <cfset amtPaid = paid />
   <cfif amtPaid eq ''>
	<cfset amtPaid = 0 />
   </cfif>

   <cfset writeOff = wOff />
   <cfif writeOff eq ''>
	<cfset writeOff = 0 />
   </cfif>
  
<cfset bal = DollarFormat(InvAmt - amtPaid - writeOff)>  
<cfset bal1 = (InvAmt - amtPaid - writeOff)> 
<cfset bal2 = -(InvAmt - amtPaid - writeOff)>
   
         <cfset str1 = str1
                      & """" & '' & """"
				& "," & """" & "TRNS" & """"	  
				& "," & """" & " " & """"	  
				& "," & """" & "INVOICE" & """"	  
				& "," & """" & #DateFormat(DateSent,"mm/dd/yy")# & """"
				& "," & """" & "Accounts Receivable" & """"
				& "," & """" & #Name# & """"
				& "," & """" & " " & """"
				& "," & """" & #bal1# & """"
				& "," & """" & #Invoice_Id# & """"
				& "," & """" & " " & """"
				& "," & """" & " " & """"	  
				& "," & """" & "N" & """"	
				& "," & """" & "N" & """"
				& "," & """" & #DateFormat(DateSent,"mm/dd/yy")# & """"
				& "," & """" & " " & """"	  
				& "," & """" & " " & """"	  
				& "," & """" & " " & """"	  
				& "," & """" & " " & """"	  
				& "," & """" & " " & """"	  
				& "," & """" & " " & """"	  
				& "," & """" & " " & """"	  
				& "," & """" & " " & """"	  
				& "," & """" & #Name# & """"
				& "," & """" & " " & """"	  
				& "," & """" & " " & """"	  
				& "," & """" & " " & """"	  
				& "," & """" & " " & """"	  
				& "," & """" & " " & """"	  
				& "," & """" & " " & """"	  
				& "," & """" & " " & """"	  
				& "," & """" & " " & """"	  
				& "," & """" & " " & """"	  
				& "," & """" & "N" & """"
				& "," & """" & " " & """"	  
				& "," & """" & " " & """"	  
                & chr(13)
						    & chr(10)
				& "," & """" & "SPL" & """"	  
				& "," & """" & " " & """"	  
				& "," & """" & "INVOICE" & """"	  
				& "," & """" & #DateFormat(DateSent,"mm/dd/yy")# & """"
				& "," & """" & "INVOICE" & """"
				& "," & """" & "" & """"
				& "," & """" & " " & """"	
				& "," & """" & #bal2# & """"
				& "," & """" & #Invoice_Id# & """"
				& "," & """" & " " & """"	
				& "," & """" & " " & """"	  
				& "," & """" & "-1" & """"	  
				& "," & """" & #bal1# & """"
				& "," & """" & " " & """"	
				& "," & """" & " " & """"	  
				& "," & """" & " " & """"	
				& "," & """" & " " & """"	  
				& "," & """" & " " & """"	
				& "," & """" & " " & """"	  
				& "," & """" & "0" & """"	  
				& "," & """" & " " & """"	
				& "," & """" & " " & """"	  
				& "," & """" & " " & """"	
				& "," & """" & " " & """"	  
				& "," & """" & "0" & """"	
				& "," & """" & " " & """"	  
				& "," & """" & " " & """"	
				& "," & """" & " " & """"	  
				& "," & """" & " " & """"	
                & chr(13)
						    & chr(10)
				& "," & """" & "ENDTRNS" & """"
                & chr(13)
						    & chr(10)
   />

  </cfoutput>
  
   <cfset gtr = """" & "!CUST" & """"
             & "," & """" & "NAME" & """"
             & "," & """" & "BADDR1" & """"
			 & "," & """" & "BADDR3" & """"
			 & chr(13)
						 & chr(10)
	 />	
	
 <cfoutput query='gets1'>
   <cfset gtr = gtr
                      & """" & "CUST" & """"
				& "," & """" & #Name# & """"
				& "," & """" & #Street_Address# & """"
				& "," & """" & #Addr# & """"
                & chr(13)
						    & chr(10)
   />
  </cfoutput>
   
  <cfset dt = DateFormat(Now(), "yyyy-mm-dd") />
  <cfset dts = DateFormat(Now(), "yyyy-mm-dd") />
  <cffile action="write" file="#application.datapath##session.AgencyId#_#session.EmployeeId#_#dt#.csv" output="#str#">
  <cfoutput> &raquo; 1. <b>Print all fields</b> - Please <a href="#application.webdatapath##session.AgencyId#_#session.EmployeeId#_#dt#.csv">Right Click Here</a> and choose "Save Target As..." to download the Exported data file and save it to your computer.
  </cfoutput> 
   <br/><br/><br/>
  <cffile action="write" file="#application.datapath##session.AgencyId#_#dt#.csv" output="#gtr#">
  <cfoutput> &raquo; 2. <b>Print Patient Information only</b> - Please <a href="#application.webdatapath##session.AgencyId#_#dt#.csv">Right Click Here</a> and choose "Save Target As..." to download the Exported data file and save it to your computer.
  </cfoutput>
   <br/><br/><br/>
  <cffile action="write" file="#application.datapath##session.AgencyId#_#dts#1.csv" output="#str1#">
  <cfoutput> &raquo; 3. <b>Print New Format</b> - Please <a href="#application.webdatapath##session.AgencyId#_#dts#1.csv">Right Click Here</a> and choose "Save Target As..." to download the Exported data file and save it to your computer.
  </cfoutput>
   <br/><br/><br/>
   &laquo; <a href='index.cfm?page=agency&cat=BillingReports&rpt=outstanding_invoices'>Click Here</a> to start Outstanding Invoices Report search.
 <cfelse>
  No data could be found based on your search criteria.
   <br/><br/>
  Please try again.
 </cfif>
</div>



