<!---Receivables_Totals_Details_export.cfm--->
<cfparam name="url.Pay_ID" default="">
<cfparam name="url.patient_filter" default="">
<cfparam name="url.df" default="">
<cfparam name="url.dt" default="">
<cfparam name="url.age" default="">
<cfparam name="url.ID" default="">

<cfquery  name="getpayer" datasource="#Application.DataSrc#">
	select * from #Request.prefix_db_agency#.pPayer WHERE Loc_ID = '#session.AgencyID#' and status =0
</cfquery>

<cfquery  name="getpatient" datasource="#Application.DataSrc#">
SELECT p.Patient_ID, p.Pt_Last, p.Pt_First, p.Status
			  FROM #Request.prefix_db_agency#.pPatients p
			  WHERE  p.Loc_ID = '#session.agencyid#'
			  ORDER BY Pt_Last
</cfquery>

<cfset tInvTotal = 0 />
<cfset tPaid = 0 />
<cfset tWriteOff = 0 />
<cfset tDue = 0 />
<cfquery  name="GetI" datasource="#Application.DataSrc#">
		SELECT 
		i.Invoice_Id,i.errors, i.DateSent, i.MethodSent, i.Comment, i.InvoiceTotal, i.PayerBalance, i.Assmt_ID ,DATEDIFF(CURDATE(), i.DateSent) AS age ,
		 i.User_defined_Status,
		p.Patient_Id, p.Status, p.Pt_Last, p.Pt_First, p.Pt_Middle ,p.Pt_Agy_ID,
		a.F177 AS START, a.F178 AS END , a.F26 AS TYPE, i.claim_status,i.Error_description,

		pr.Pay_Name, pr.Pay_ID, pr.Pay_Contact, pr.Pay_Phone,pr.Pay_Phone AS Contact_Phone_Ext,
		pr.Output AS PayerOutputType ,
		 (SELECT SUM(Amount) FROM #Request.prefix_db_agency#.Payments WHERE Invoice_Id = i.Invoice_Id and deleted <> 1) AS Paid ,
		(SELECT SUM(WriteOffAmount) FROM #Request.prefix_db_agency#.Payments WHERE Invoice_Id = i.Invoice_Id and deleted <> 1) AS wOff 

	  FROM #Request.prefix_db_agency#.Invoices i 
	     join #Request.prefix_db_agency#.pSchedules s    on  s.patient_id = i.patient_id AND s.Status = 0 
	     join #Request.prefix_db_agency#.pPatients p          on  p.patient_id = s.patient_id AND p.Status  IN (#Request.patient_status#)
	     join #Request.prefix_db_agency#.pAssessments a   on   i.Assmt_ID = a.Assmt_ID AND a.Status = 0 
	     join #Request.prefix_db_agency#.pPayer pr           on  i.PatientPayer_ID = pr.Pay_ID AND pr.Status = 0 
	WHERE
		i.Agency_ID = #session.AgencyId#
		AND i.Deleted <> 1
		<cfif url.df NEQ "" and url.dt NEQ "">
		 AND i.DateSent BETWEEN  '#DateFormat(url.df,"yyyy-mm-dd")#'  and '#DateFormat(url.dt,"yyyy-mm-dd")#' 
		<cfelse>
		 AND  i.DateSent = '#DateFormat(Now(),"yyyy-mm-dd")#'
		</cfif>
		AND p.patient_id = i.patient_id
		AND i.Assmt_ID = a.Assmt_ID
		AND a.Assmt_ID = s.Assmt_ID
		and i.PatientPayer_ID = pr.Pay_ID
		<cfif isdefined('url.ID') and url.ID neq 'all' and url.ID neq '' AND url.ID NEQ 0>
		AND pr.Pay_ID = #url.ID#
		</cfif>
		<!--- <cfif isdefined('url.Output') and url.Output neq 'all' and url.Output neq ''>
		AND  i.Output = '#url.Output#'
		</cfif> --->
		AND i.InvoiceTotal > 0
		AND i.Output is not null
		<!--- 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 )  --->
		Group by i.Invoice_Id
		ORDER BY Pt_Last, Pt_First

</cfquery>
<div class="portlet box blue">
		<div class="portlet-title">
			<div class="caption">
				<i class="fa fa-gift"></i>Receivables Totals Export File
			</div>
			<div class="tools">
				<a class="collapse" href="" data-original-title="" title="">
				</a>
			</div>
			<div class="actions">
			     <a href="javascript:;" class="btn btn-default btn-sm" onclick="history.back();"> Back </a>
		    </div>
		</div>
<div class="portlet-body form">

<cfif GetI.recordcount gt 0>
  <cfset str = """" & "Payer" & """"
             & "," & """" & "Patient Last Name" & """"
             & "," & """" & "Patient First Name" & """"
             & "," & """" & "MRN " & """"
			 & "," & """" & "First Visit" & """"
			 & "," & """" & "Last Visit" & """"
        & "," & """" & "Date Billed" & """"
        & "," & """" & "Invoice ID" & """"
         & "," & """" & "Age" & """"
			 & "," & """" & "Total" & """"
			 & chr(13)
						 & chr(10)
  />
  <cfset Total_InvAmt = 0 />
   <cfset _Cstatus = 0>
  <cfoutput query='GetI'>
   					<cfset _Cstatus = 0 />
					    <cfset InvAmt = GetI.InvoiceTotal />
						<cfif InvAmt eq ''>
							<cfset InvAmt = 0 />
						</cfif>

						<cfset amtPaid = GetI.paid />
						<cfif amtPaid eq ''>
							<cfset amtPaid = 0 />
						</cfif>

						<cfset writeOff = GetI.wOff />
						<cfif writeOff eq ''>
							<cfset writeOff = 0 />
						</cfif>

						<cfset bal = DollarFormat(InvAmt - amtPaid - writeOff) />
						<cfif bal neq '$0.00' or bal eq '$0.00'>
						<cfset _Cstatus = _Cstatus + 1 />
						<cfset tInvTotal = tInvTotal +(InvAmt) />
                            <cfif amtPaid eq 0 or writeOff gt 0>
								<cfif writeOff gt 0>
									<cfset tWriteOff = tWriteOff + writeOff />
									<cfset tDue = (tDue - writeOff) />
								</cfif>
							</cfif>
             </cfif>
              <cfquery  name="Get" datasource="#Application.DataSrc#">
								(SELECT Min(Visit_Date) as MIN,Max(Visit_Date) as MAX FROM #Request.prefix_db_agency#.pSchedules 
								where Assmt_ID = #Assmt_ID# 
								AND Invoice_ID  = #Invoice_ID#
								and Status = 0 ORDER BY Visit_Date) 
								</cfquery>
								<cfif GetI.PayerOutputType eq 'Medicare'>
									<cfset cerstart = 	#DateFormat(START,"mm/dd/yy")#  /> 
									<cfset cerend =	-#DateFormat(END,"mm/dd/yy")# />
								<cfelse>
								<cfloop query="Get">
									<cfset cerstart = 	#DateFormat(MIN,"mm/dd/yy")#  /> 
									<cfset cerend =	-#DateFormat(MAX,"mm/dd/yy")# />
								</cfloop>
							</cfif>



   <cfset str = str
        & """" & #Pay_Name# & """"
        & "," & """" & #trim(Pt_Last)# & """"
				& "," & """" & #trim(Pt_First)# & """"
				& "," & """" & #trim(Pt_Agy_ID)# & """"
        & "," & """" & #DateFormat(cerstart,"mm/dd/yy")# & """"
				 & "," & """" & #DateFormat(cerend,"mm/dd/yy")# & """"
        & "," & """" & #DateFormat(DateSent,"mm/dd/yy")# & """"
        & "," & """" & #Invoice_ID# & """"
				& "," & """" & #DollarFormat(InvAmt)# & """"
				& "," & """" & #age# & """"
				& chr(13) & chr(10)
   />
    	<cfset Total_InvAmt = Total_InvAmt + InvAmt />
		 <cfset tDue = tDue + (InvAmt - amtPaid - writeOff) />
</cfoutput>

 <cfset str = str
       &"""" & "" & """"
        & "," & """" & "" & """"
         & "," & """" & "" & """"
        & "," & """" & "" & """"
        & "," & """" & "" & """"
  		 & "," & """" & "" & """"
  		 & "," & """" & "" & """"
			 & "," & """" & "Total" & """"
			 & "," & """" & #DollarFormat(Total_InvAmt)# & """"			
			 & "," & """" & "" & """"
			 & chr(13)
						 & chr(10)
  />
  
 	<cfset dt = DateFormat(Now(), "yyyy-mm-dd") /><br /><br/>
  <cffile action="write" file="#application.datapath##session.AgencyId#_#session.EmployeeId#_#dt#.csv" output="#str#">
  <cfoutput> &raquo; 1.Please <a href="#application.webdatapath##session.AgencyId#_#session.EmployeeId#_#dt#.csv"> Click Here</a> and choose "Save Target As..." to download the Exported data file and save it to your computer.
  </cfoutput> <br /><br />
     &laquo;2. <a href='index.cfm?page=agency&cat=BillingReports&rpt=Daily_Billing_Report'>Click Here</a> to start Daily Billing Report Details Report search.
	  <br/><br/><br/>
 <cfelse>
 			 No data could be found based on your search criteria.
   		<br/><br/>
  		Please try again.
 </cfif>
</div>
</div>



