
<cfparam name="form.df" default="">
<cfparam name="form.dt" default="">

<div style='margin:60px 0px 0px 20px;'>

<cfquery  name="gets" datasource="#Application.DataSrc#">
	 SELECT
(s.Payment_ID) as Payment_ID,
(select sum(amount) from #Request.prefix_db_agency#.Payments where invoice_id = p.invoice_id) as paid,
(s.WriteOffAmount) as WriteOffAmount, 
s.PatientPayer_ID ,
(select sum(writeoffamount) from #Request.prefix_db_agency#.Payments where invoice_id = p.invoice_id) as wOff,
p.PayerBalance, 
pa.Patient_ID, pa.Last_Name_M0040,
a.Certification_Start,a.Certification_End, pa.First_Name_M0040, pa.Status, 
DATE_FORMAT(pa.DOB_M0066,'%m/%d/%Y') AS DateOfBirth,
p.Invoice_Id,
DATE_FORMAT(p.DateSent,'%Y-%m-%d') AS DateSent,
MAX(DATE_FORMAT(s.PaymentDate,'%Y-%m-%d')) AS PaymentDate,
DATE_FORMAT(p.DateSent,'%m/%d') AS ds, 
(p.InvoiceTotal) as InvoiceTotal,
si.Total_EClaim,si.EClaimed_Final, si.Output_Type, p.PayerBalance,
pr.Payer_Id, pr.PayName, pr.Payer_Number,
(SELECT Invoice_Id FROM #Request.prefix_db_agency#.Invoices WHERE Assessment_Tracker_Id = p.Assessment_Tracker_Id 
AND InvoiceTotal = '0.00' and Deleted <> 1 LIMIT 1) AS FinalBilling
from  
	#Request.prefix_db_agency#.Payments s left outer join #Request.prefix_db_agency#.ScheduleItems si on s.Invoice_ID = si.Invoice_ID
	left outer join #Request.prefix_db_agency#.Invoices p on p.Invoice_ID = si.Invoice_ID 
	left outer join #Request.prefix_db_agency#.patient pa on pa.patient_id = p.patient_id
	left outer join #Request.prefix_db_agency#.Payer pr on pr.payer_id = p.patientpayer_id
	left outer join #Request.prefix_db_agency#.Assessment_New a on si.Assessment_ID = a.Assessment_ID 
	 WHERE
	  p.Agency_Id = '#session.agencyid#'
	 AND  p.Deleted IS NULL OR p.Deleted = 0
	 <cfif isdefined('url.df') and url.df neq ''>
	   AND   p.DateSent >= #CreateODBCDate(url.df)#
	   AND   p.DateSent <= #CreateODBCDate(url.dt)#
	 </cfif>
	 AND ((select sum(amount) from #Request.prefix_db_agency#.Payments where invoice_id = p.invoice_id)-(s.WriteOffAmount))>0
	 AND si.Agency_ID = '#session.AgencyID#'
	 Group BY p.Invoice_Id
	 ORDER BY pr.PayName, Last_Name_M0040, First_Name_M0040 ASC
</cfquery>

 <cfif gets.recordcount gt 0>
 
   <cfset str = """" & "Last Name" & """"
             & "," & """" & "First Name" & """"
             & "," & """" & "Status" & """"
			 & "," & """" & "Pay Name" & """"
			 & "," & """" & "InvoiceID" & """"
             & "," & """" & "Payment Date" & """"
             & "," & """" & "Certification Start " & """"
			 & "," & """" & "Certification End" & """"
             & "," & """" & "Due" & """"
             & "," & """" & "Paid" & """"
             & "," & """" & "Write-off" & """"
             & "," & """" & "Balance" & """"
			 & chr(13)
						 & chr(10)
  />

  <cfoutput query='gets'>

   <!---<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# & """"
                & "," & """" & #Invoice_ID# & """"
                & "," & """" & #DateFormat(PaymentDate,'mm/dd/yyyy')# & """"
				& "," & """" & #DateFormat(Certification_Start,'mm/dd/yyyy')# & """"
                & "," & """" & #DateFormat(Certification_End,'mm/dd/yyyy')# & """"
                & "," & """" & #DollarFormat(InvoiceTotal)# & """"
                & "," & """" & #DollarFormat(paid)# & """"
				& "," & """" & #DollarFormat(wOff)# & """"
				& "," & """" & #bal# & """"
                & chr(13)
						    & chr(10)
   />

  </cfoutput>
  
  <cfset dt = DateFormat(Now(), "yyyy-mm-dd") />
  <cffile action="write" file="#application.datapath##session.AgencyId#_#session.EmployeeId#_#dt#.csv" output="#str#">
  <cfoutput> &raquo; 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/>
	   &laquo; <a href='index.cfm?page=agency&cat=BillingReports&rpt=Paymentsummary_report'>Click Here</a> to go back to Payment Summary Report.
 <cfelse>
  No data could be found based on your search criteria.
   <br/><br/>
  Please try again.
 </cfif>
</div>
