
<cfparam name="url.df" default="">
<cfparam name="url.dt" default="">
<cfparam name="url.pr" default="">
<cfparam name="url.pid" default="">
<cfparam name="url.categories" default="">


	<cfquery  name="gets" datasource="#Application.DataSrc#">
		SELECT 
		(i.InvoiceTotal) as InvoiceTotal, (i.PayerBalance) as PayerBalance, i.Output,i.Seq_Amount, pay.Amount AS paid_amount,
        i.Seq_code,pay.Pay_Seq_Amount,
		(select sum(amount) from #Request.prefix_db_agency#.Payments where invoice_id = i.invoice_id 
                AND deleted != 1  ) as paid,
		DATEDIFF(CURDATE(), i.DateSent) AS age ,pr.Output AS PayerOutputType , pr.Output AS  Output_Type,
        pr.Pay_frequency,
		i.invoice_id, <!--- (select Output_Type from #Request.prefix_db_agency#.pSchedules where invoice_id = i.invoice_id 
		AND Total_EClaim > 0 AND status <> 1 limit 1 ) as Output_Type, --->
		(select sum(Total_Eclaim) from #Request.prefix_db_agency#.pSchedules where invoice_id = i.invoice_id 
		 AND Total_EClaim > 0 AND Status = 0 limit 1 ) as Total_Eclaim,
		pr.Pay_ID, pr.Pay_Name, pr.Pay_Contact, pr.Pay_Phone, pr.Pay_Phone AS Contact_Phone_Ext, pay.writeoffamount,
        pay.Payment_ID,
        case when pay.WriteOffType ='' then '-'
		else pay.WriteOffType end as WriteOffType,
        pay.PaymentDate,
        case when pay.Comments ='' then '-'
	      else pay.Comments end as Comments,
		      (select sum(writeoffamount) from #Request.prefix_db_agency#.Payments where invoice_id = i.invoice_id AND deleted <> 1) as wOff,
		p.Patient_ID,p.Pt_Last,p.Pt_First, p.Status,
		CASE WHEN p.Status = 0 THEN 'Active'
			ELSE 'Inactive' END AS pat_status, 
		a.F177 AS START, a.F178 AS END , a.F26 AS TYPE, 
		  i.ContractTotal, i.LUPA_contract,
                CASE WHEN LUPA_threshold = 1 THEN 
            LUPA_contract
            ELSE
                    ROUND(IFNULL(( CASE 
                            WHEN  pr.Output = 'Medicare' THEN   
                                    (CASE  WHEN DATEDIFF(s.Visit_Date,a.F177) < 30 THEN  a.F299
                            ELSE  a.F298
                            END) 
                         ELSE 
                            i.ContractTotal
                         END),0),2) 
                END 
                    AS AR_total_old,
                      ROUND(IFNULL(( CASE 
                        WHEN  pr.Output = 'Medicare' THEN   
                                (CASE  WHEN DATEDIFF(s.Visit_Date,a.F177) < 30 THEN  a.F299
                        ELSE  a.F298
                        END) 
                     ELSE 
                        i.ContractTotal
                     END),0),2) AS AR_total,
		DATE_FORMAT(p.Pt_DOB,'%m/%d/%Y') AS DateOfBirth 
		FROM
		#Request.prefix_db_agency#.Invoices i
        JOIN  #Request.prefix_db_agency#.pPayer pr ON pr.Pay_ID = i.patientpayer_id 
        JOIN #Request.prefix_db_agency#.pPatients p ON p.patient_id = i.patient_id
        LEFT OUTER JOIN #Request.prefix_db_agency#.pAssessments a ON i.Assmt_ID = a.Assmt_ID
            AND a.Status = 0
        JOIN #Request.prefix_db_agency#.Payments pay ON  i.invoice_id =pay.invoice_id 
         LEFT OUTER JOIN #Request.prefix_db_agency#.pSchedules s ON  i.invoice_id =s.invoice_id AND s.Status = 0
        AND s.Missed = 0
		WHERE
		i.Agency_ID = '#session.agencyid#'
		AND i.Deleted <> 1
		AND i.InvoiceTotal > 0
		<cfif  URL.df neq ''>
			AND   pay.PaymentDate >= #CreateODBCDate(URL.df)#
			AND   pay.PaymentDate <= #CreateODBCDate(URL.dt)#
			</cfif>
			<cfif URL.pr neq ''>
	 		AND   pr.Pay_ID = '#URL.pr#'
			</cfif>
		<cfif URL.pid neq ''>
	 		AND   i.patient_id = '#URL.pid#'
		</cfif>
        <cfif URL.categories EQ 'Write-offs'>
            AND (pay.WriteOffType like 'PR%'
                OR (pr.Output = 'Medicare' AND pr.Pay_frequency = 'Episodic'
                    AND REPLACE(REPLACE(LOWER(pay.WriteOffType),'-',''),' ','') = 'writeoff')
				OR (pr.Output != 'Medicare'
					AND EXISTS (
						SELECT 1
						FROM #Request.prefix_db_agency#.Visits_payments vp
						WHERE vp.Payment_ID = pay.Payment_ID
						AND vp.Status = 0
						AND IFNULL(vp.adjust_contract_balance,'No') = 'Yes'
					)))
        <cfelseif URL.categories EQ 'Contractual Adjustments'>
            AND (((pay.WriteOffType like '%45%'  OR pay.WriteOffType like '%253%' OR pay.WriteOffType like '%29%') AND pay.WriteOffType like 'CO%')
               OR pr.Output != 'Medicare' )
        <cfelseif URL.categories EQ 'Adjustments to revenue'>
            AND (pay.WriteOffType like '%70%' OR pr.Output != 'Medicare' )
        <cfelse>
             AND ((pay.WriteOffType like 'PR%'
                OR (pr.Output = 'Medicare' AND pr.Pay_frequency = 'Episodic'
                    AND REPLACE(REPLACE(LOWER(pay.WriteOffType),'-',''),' ','') = 'writeoff')
				OR (pr.Output != 'Medicare'
					AND EXISTS (
						SELECT 1
						FROM #Request.prefix_db_agency#.Visits_payments vp
						WHERE vp.Payment_ID = pay.Payment_ID
						AND vp.Status = 0
						AND IFNULL(vp.adjust_contract_balance,'No') = 'Yes'
					)) )

             OR (((pay.WriteOffType like '%45%'  OR pay.WriteOffType like '%253%' OR pay.WriteOffType like '%29%') AND pay.WriteOffType like 'CO%')
               OR pr.Output != 'Medicare' )
             OR 
             (pay.WriteOffType like '%70%' OR pr.Output != 'Medicare' ))
        </cfif>
        AND p.Status  IN (#Request.patient_status#)
        AND (pay.payment_type != 'Reversal Payment' OR pay.payment_type IS NULL)
        AND pr.Status = 0
          AND pay.Deleted <> 1
		AND pr.Pay_ID = i.patientpayer_id
		<cfif len(session.export_payments_id) gt 0 >
			AND pay.Payment_ID IN (#session.export_payments_id#)
		<cfelse>
			AND pay.Payment_ID IN (0)
		</cfif>
        <!--- GROUP BY i.invoice_id --->
        GROUP BY pay.Payment_ID 
		ORDER BY  Patient_ID ASC,i.invoice_id ASC, Pay_Name asc
	</cfquery>



	<cfquery  name="getallpayments" datasource="#Application.DataSrc#">
        SELECT 
        	 <cfif URL.categories EQ 'Write-offs'>
				CASE WHEN Visits_payments.write_offcode like 'PR%'
					OR (pr2.Output != 'Medicare' AND IFNULL(Visits_payments.adjust_contract_balance,'No') = 'Yes')
					THEN Visits_payments.write_offamount
                ELSE 0 END AS first_wamount,
				CASE WHEN Visits_payments.second_write_offcode like 'PR%'
					OR (pr2.Output != 'Medicare' AND IFNULL(Visits_payments.adjust_contract_balance,'No') = 'Yes')
					THEN Visits_payments.second_write_offamount
                ELSE 0 END AS second_wamount,
				CASE WHEN Visits_payments.third_write_offcode like 'PR%'
					OR (pr2.Output != 'Medicare' AND IFNULL(Visits_payments.adjust_contract_balance,'No') = 'Yes')
					THEN  Visits_payments.third_write_offamount
                ELSE 0 END AS third_wamount,
				CASE WHEN Visits_payments.fourth_write_offcode like 'PR%'
					OR (pr2.Output != 'Medicare' AND IFNULL(Visits_payments.adjust_contract_balance,'No') = 'Yes')
					THEN  Visits_payments.fourth_write_offamount
                ELSE 0 END AS fourth_wamount,
            <cfelseif URL.categories EQ 'Contractual Adjustments'>
                CASE WHEN Visits_payments.write_offcode like 'CO%' 
                    AND Visits_payments.write_offcodesecond IN ('45','253','29')  THEN Visits_payments.write_offamount
                ELSE 0 END AS first_wamount,
                CASE WHEN Visits_payments.second_write_offcode like 'CO%' 
                    AND Visits_payments.second_write_offcodesecond  IN ('45','253','29') THEN Visits_payments.second_write_offamount
                ELSE 0 END AS second_wamount, 
                CASE WHEN Visits_payments.third_write_offcode like 'CO%' 
                    AND Visits_payments.third_write_offcodesecond  IN ('45','253','29') THEN Visits_payments.third_write_offamount
                ELSE 0 END AS third_wamount, 
                CASE WHEN Visits_payments.fourth_write_offcode like 'CO%' 
                    AND Visits_payments.fourth_write_offcodesecond  IN ('45','253','29') THEN Visits_payments.fourth_write_offamount
                ELSE 0 END AS fourth_wamount,
            <cfelseif URL.categories EQ 'Adjustments to revenue'>
                     CASE WHEN Visits_payments.write_offcode like 'CO%' 
                    AND Visits_payments.write_offcodesecond IN ('70')  THEN Visits_payments.write_offamount
                ELSE 0 END AS first_wamount,
                CASE WHEN Visits_payments.second_write_offcode like 'CO%' 
                    AND Visits_payments.second_write_offcodesecond  IN ('70') THEN Visits_payments.second_write_offamount
                ELSE 0 END AS second_wamount, 
                CASE WHEN Visits_payments.third_write_offcode like 'CO%' 
                    AND Visits_payments.third_write_offcodesecond  IN ('70') THEN Visits_payments.third_write_offamount
                ELSE 0 END AS third_wamount, 
                CASE WHEN Visits_payments.fourth_write_offcode like 'CO%' 
                    AND Visits_payments.fourth_write_offcodesecond  IN ('70') THEN Visits_payments.fourth_write_offamount
                ELSE 0 END AS fourth_wamount,
            <cfelse>
                <!--- Visits_payments.write_offamount  AS first_wamount,
                Visits_payments.second_write_offamount AS second_wamount,
                Visits_payments.third_write_offamount AS third_wamount,
                Visits_payments.fourth_write_offamount AS fourth_wamount, --->
                    CASE WHEN Visits_payments.write_offcode like 'PR%' OR 
                      Visits_payments.write_offcodesecond IN ('45','253','29','70')  THEN Visits_payments.write_offamount
                ELSE 0 END AS first_wamount,
                CASE WHEN Visits_payments.second_write_offcode like 'PR%' 
                    OR Visits_payments.second_write_offcodesecond  IN ('45','253','29','70') THEN Visits_payments.second_write_offamount
                ELSE 0 END AS second_wamount, 
                CASE WHEN Visits_payments.third_write_offcode like 'PR%' 
                    OR Visits_payments.third_write_offcodesecond  IN ('45','253','29','70') THEN Visits_payments.third_write_offamount
                ELSE 0 END AS third_wamount, 
                CASE WHEN Visits_payments.fourth_write_offcode like 'PR%' 
                    OR Visits_payments.fourth_write_offcodesecond  IN ('45','253','29','70') THEN Visits_payments.fourth_write_offamount
                ELSE 0 END AS fourth_wamount,
            </cfif>
			 Visits_payments.*,Payments.Invoice_ID,pr2.Output AS PayerOutputType  FROM  #Request.prefix_db_agency#.Visits_payments 
        JOIN  #Request.prefix_db_agency#.Payments 
        ON  Payments.Payment_ID = Visits_payments.Payment_ID 
		JOIN #Request.prefix_db_agency#.Invoices i2 ON i2.Invoice_ID = Payments.Invoice_ID
		JOIN #Request.prefix_db_agency#.pPayer pr2 ON pr2.Pay_ID = i2.patientpayer_id
        JOIN  #Request.prefix_db_agency#.pSchedules ON pSchedules.Schedule_ID =  Visits_payments.Schedule_ID
        AND pSchedules.Status = 0
        AND Payments.Deleted <> 1
        WHERE  Visits_payments.Status = 0 
        <cfif len(session.export_visitpayments_id) gt 0 >
        	AND Visits_payments.Visit_Pay_ID IN (#session.export_visitpayments_id#)
        <cfelse>
        	AND Visits_payments.Visit_Pay_ID IN (0)
        </cfif>
         <cfif URL.categories EQ 'Write-offs'>
            AND (Visits_payments.write_offcode like 'PR%' OR Visits_payments.second_write_offcode like 'PR%'
			 OR Visits_payments.third_write_offcode like 'PR%'  OR Visits_payments.fourth_write_offcode like 'PR%'
			 OR (pr2.Output != 'Medicare' AND IFNULL(Visits_payments.adjust_contract_balance,'No') = 'Yes') )
        <cfelseif URL.categories EQ 'Contractual Adjustments'>
            AND  (Visits_payments.write_offcode like 'CO%'  AND (Visits_payments.write_offcodesecond IN ('45','253','29')) 
                    OR ( Visits_payments.second_write_offcode like 'CO%' AND 
                            Visits_payments.second_write_offcodesecond  IN ('45','253','29'))
                    OR (Visits_payments.third_write_offcode like 'CO%' 
                            AND Visits_payments.third_write_offcodesecond  IN ('45','253','29'))
                    OR (Visits_payments.fourth_write_offcode like 'CO%' AND 
                        Visits_payments.fourth_write_offcodesecond  IN ('45','253','29')) )
        <cfelseif URL.categories EQ 'Adjustments to revenue'>
                    AND ( Visits_payments.write_offcodesecond like '70%' 
                        OR Visits_payments.second_write_offcodesecond like '70%' 
                 OR Visits_payments.third_write_offcodesecond like '70%' 
                 OR Visits_payments.fourth_write_offcodesecond like '70%')
        <cfelse>
             AND ((Visits_payments.write_offcode like 'PR%' OR Visits_payments.second_write_offcode like 'PR%'
                 OR Visits_payments.third_write_offcode like 'PR%'  OR Visits_payments.fourth_write_offcode like 'PR%' )

              OR  (Visits_payments.write_offcode like 'CO%'  AND (Visits_payments.write_offcodesecond IN ('45','253','29')) 
                    OR ( Visits_payments.second_write_offcode like 'CO%' AND 
                            Visits_payments.second_write_offcodesecond  IN ('45','253','29'))
                    OR (Visits_payments.third_write_offcode like 'CO%' 
                            AND Visits_payments.third_write_offcodesecond  IN ('45','253','29'))
                    OR (Visits_payments.fourth_write_offcode like 'CO%' AND 
                        Visits_payments.fourth_write_offcodesecond  IN ('45','253','29')) )
                OR ( Visits_payments.write_offcodesecond like '70%' 
                        OR Visits_payments.second_write_offcodesecond like '70%' 
                 OR Visits_payments.third_write_offcodesecond like '70%' 
                 OR Visits_payments.fourth_write_offcodesecond like '70%'))

        </cfif>
        AND  (Visits_payments.write_offamount > 0 OR  
       Visits_payments.second_write_offamount > 0 OR  
       Visits_payments.third_write_offamount > 0 OR  
       Visits_payments.fourth_write_offamount > 0 )
       AND (Payments.payment_type != 'Reversal Payment' OR Payments.payment_type IS NULL)
    </cfquery>

<div class="portlet box blue">
		<div class="portlet-title">
			<div class="caption">
				<i class="fa fa-gift"></i>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 gets.recordcount gt 0>
 
   <cfset str = """" & "Last Name" & """"
             & "," & """" & "First Name" & """"
             & "," & """" & "Status" & """"
			 & "," & """" & "Pay Name" & """"
			 & "," & """" & "InvoiceID" & """"
             & "," & """" & "Adjustment Date" & """"
             & "," & """" & "Certification Start" & """"
			 & "," & """" & "Certification End" & """"
			 & "," & """" & "Adjustment Amount" & """"
			  & "," & """" & "Adjustment Type" & """"
			 & "," & """" & "Write-off" & """"
			 & "," & """" & "Contract Adjustment Amount" & """"            
             & "," & """" & "Comment" & """"
			 & chr(13)
						 & chr(10)
  />
  	<cfset tot_woff  =  0 >
  	<cfset PR_total =  0 >
	 <cfset Total_PR_total =  0 >
	 <cfset Total_Contract_Adjustment = 0 >
  <cfoutput query='gets'   group="Invoice_ID"  >
  		<cfif wOff eq ''>
	        <cfset W_off =  Pay_Seq_Amount>
	    <cfelse>
	        <cfset W_off = writeoffamount  + Pay_Seq_Amount>
	    </cfif>  
	    <cfset medicare_amount = 0 >
   	    <cfif PayerOutputType EQ 'Medicare'>
                <cfset currentInvoice_ID = gets.Invoice_ID >
                <cfif gets.currentRow LT gets.recordCount>
                    <cfset nextInvoice_ID = gets.Invoice_ID[gets.currentRow + 1]>
                <cfelse>
                    <cfset nextInvoice_ID = "0">
                </cfif>
                <cfif (writeoffamount gt 0 OR Pay_Seq_Amount gt 0) <!--- AND currentInvoice_ID NEQ nextInvoice_ID --->
                AND WriteOffType NEQ '0.00'>
                	<cfset PR_amount = 0 >
                	<cfif gets.Pay_frequency EQ 'Episodic' AND REReplace(LCase(gets.WriteOffType),'[ -]','','all') EQ 'writeoff'>
                        <cfset PR_amount = Val(gets.ContractTotal)>
                        <cfif Val(gets.LUPA_contract) GT 0>
                            <cfset PR_amount = Val(gets.LUPA_contract)>
                        <cfelseif Val(writeoffamount) GT 0 AND Val(writeoffamount) LT W_off>
                            <cfset PR_amount = Val(writeoffamount)>
                        </cfif>
                        <cfset Total_PR_total = Total_PR_total + PR_amount>
                    <cfelseif WriteOffType contains 'PR'>
                        <cfset PR_amount = W_off>
                        <cfset Total_PR_total = Total_PR_total + PR_amount>
	                </cfif> 
	                <cfset Contract_Adjustment = AR_total - gets.paid - Pay_Seq_Amount >
                    <cfset Total_Contract_Adjustment = Total_Contract_Adjustment + Contract_Adjustment>

				    <cfset str = str
				        & """" & #gets.Pt_Last# & """"
				        & "," & """" & #gets.Pt_First# & """"
						& "," & """" & #gets.pat_status# & """"
						& "," & """" & #gets.Pay_Name# & """"
				        & "," & """" & #gets.Invoice_ID# & """"
				        & "," & """" & #DateFormat(gets.PaymentDate,'mm/dd/yyyy')# & """"
						& "," & """" & #DateFormat(gets.START,'mm/dd/yyyy')# & """"
		                & "," & """" & #DateFormat(gets.END,'mm/dd/yyyy')# & """"
						& "," & """" & #DollarFormat(writeoffamount)#  & """"
						& "," & """" & #WriteOffType# & """"
	                    & "," & """" & #DollarFormat(PR_amount)# & """"	                  
	                	& "," & """" & #DollarFormat(Contract_Adjustment)# & """"
						& "," & """" & #gets.Comments# & """"
				        & chr(13) & chr(10)
				   />

                     <cfif Pay_Seq_Amount gt 0 AND WriteOffType NEQ '0.00'>
                        <cfset str = str
                        & """" & "" & """"
                        & "," & """" &""  & """"
                        & "," & """" & "" & """"
                        & "," & """" & "" & """"
                        & "," & """" & "" & """"
                        & "," & """" & "" & """"
                        & "," & """" & "" & """"
                        & "," & """" & "" & """"
                        & "," & """" & #DollarFormat(Pay_Seq_Amount)# & """"
                        & "," & """" & "CO 253"  & """"
                        & "," & """" & "" & """"     
                        & "," & """" & "" & """"
                        & "," & """" & "" & """"
                        & chr(13) & chr(10) />
                    </cfif>

				   <cfset medicare_amount = medicare_amount + W_off />
				   <cfset tot_woff = tot_woff  + W_off>
				</cfif>
		</cfif>
		<cfset pt_row_total = 0 >
		<cfif PayerOutputType NEQ 'Medicare'>
			<cfoutput>
	            <cfquery  name="getpayments" dbtype="query" >
	                SELECT * FROM getallpayments WHERE Invoice_ID =  '#gets.Invoice_ID#'
	                AND (first_wamount  > 0 OR second_wamount > 0 OR third_wamount > 0 OR fourth_wamount > 0  )
	            </cfquery>
	            <cfloop query="getpayments">
 					<cfset first_woffcode = #write_offcode# &" "& #write_offcodesecond# />
					<cfset second_woffcode = #second_write_offcode# &" "& #second_write_offcodesecond# />
					<cfset third_woffcode = #third_write_offcode# &" "& #third_write_offcodesecond# />
					<cfset fourth_woffcode = #fourth_write_offcode# &" "& #fourth_write_offcodesecond# />
					<cfif first_wamount gt 0>
						<cfset adjust_amount = first_wamount  + gets.Pay_Seq_Amount >
						<cfset PR_amount = "0" >
						<cfif write_offcode contains 'PR' OR (PayerOutputType NEQ 'Medicare' AND adjust_contract_balance EQ 'Yes')>
                            <cfset Total_PR_total = Total_PR_total + first_wamount >
                            <cfset PR_amount = first_wamount >
                         </cfif>

		          		<cfset str = str
					        & """" & #gets.Pt_Last# & """"
					        & "," & """" & #gets.Pt_First# & """"
							& "," & """" & #gets.pat_status# & """"
							& "," & """" & #gets.Pay_Name# & """"
					        & "," & """" & #gets.Invoice_ID# & """"
					        & "," & """" & #DateFormat(gets.PaymentDate,'mm/dd/yyyy')# & """"
							& "," & """" & #DateFormat(gets.START,'mm/dd/yyyy')# & """"
			                & "," & """" & #DateFormat(gets.END,'mm/dd/yyyy')# & """"
							& "," & """" & #DollarFormat(adjust_amount)# & """"
							& "," & """" & #first_woffcode#  & """"
							& "," & """" & #DollarFormat(PR_amount)# & """"	    
							& "," & """" & "" & """"
							& "," & """" & #gets.Comments# & """"
					        & chr(13) & chr(10) />
					        <cfset tot_woff = tot_woff  + first_wamount  >

                      

					</cfif>
					<cfif second_wamount gt 0>
						<cfset PR_amount = "0" >
						<cfif second_write_offcode contains 'PR' OR (PayerOutputType NEQ 'Medicare' AND adjust_contract_balance EQ 'Yes')>
	                        <cfset Total_PR_total = Total_PR_total + second_wamount >
	                        <cfset PR_amount = second_wamount >
	                     </cfif>
		          		<cfset str = str
					        & """" & #gets.Pt_Last# & """"
					        & "," & """" & #gets.Pt_First# & """"
							& "," & """" & #gets.pat_status# & """"
							& "," & """" & #gets.Pay_Name# & """"
					        & "," & """" & #gets.Invoice_ID# & """"
					        & "," & """" & #DateFormat(gets.PaymentDate,'mm/dd/yyyy')# & """"
							& "," & """" & #DateFormat(gets.START,'mm/dd/yyyy')# & """"
			                & "," & """" & #DateFormat(gets.END,'mm/dd/yyyy')# & """"
							& "," & """" & #DollarFormat(second_wamount)# & """"
							& "," & """" & #second_woffcode#  & """"
							& "," & """" & #DollarFormat(PR_amount)# & """"	    
							& "," & """" & "" & """"
			                & "," & """" & #gets.Comments# & """"
					        & chr(13) & chr(10) />
					        <cfset tot_woff = tot_woff  + second_wamount  >
					</cfif>
					<cfif third_wamount gt 0>
						<cfset PR_amount = "0" >
						<cfif third_write_offcode contains 'PR' OR (PayerOutputType NEQ 'Medicare' AND adjust_contract_balance EQ 'Yes')>
		                    <cfset Total_PR_total = Total_PR_total + third_wamount >
		                    <cfset PR_amount = third_wamount >
		                </cfif>
		          		<cfset str = str
					        & """" & #gets.Pt_Last# & """"
					        & "," & """" & #gets.Pt_First# & """"
							& "," & """" & #gets.pat_status# & """"
							& "," & """" & #gets.Pay_Name# & """"
					        & "," & """" & #gets.Invoice_ID# & """"
					        & "," & """" & #DateFormat(gets.PaymentDate,'mm/dd/yyyy')# & """"
							& "," & """" & #DateFormat(gets.START,'mm/dd/yyyy')# & """"
			                & "," & """" & #DateFormat(gets.END,'mm/dd/yyyy')# & """"
							& "," & """" & #DollarFormat(third_wamount)# & """"
							 & "," & """" & #third_woffcode#  & """"
							& "," & """" & #DollarFormat(PR_amount)# & """"	    
							& "," & """" & "" & """"			               
							& "," & """" & #gets.Comments# & """"
					        & chr(13) & chr(10) />
					        <cfset tot_woff = tot_woff  + third_wamount  >
					</cfif>
					<cfif fourth_wamount gt 0>
						<cfset PR_amount = "0" >
						<cfif fourth_write_offcode contains 'PR' OR (PayerOutputType NEQ 'Medicare' AND adjust_contract_balance EQ 'Yes')>
                            <cfset Total_PR_total = Total_PR_total + fourth_wamount >
                            <cfset PR_amount = fourth_wamount >
                        </cfif>
		          		<cfset str = str
					        & """" & #gets.Pt_Last# & """"
					        & "," & """" & #gets.Pt_First# & """"
							& "," & """" & #gets.pat_status# & """"
							& "," & """" & #gets.Pay_Name# & """"
					        & "," & """" & #gets.Invoice_ID# & """"
					        & "," & """" & #DateFormat(gets.PaymentDate,'mm/dd/yyyy')# & """"
							& "," & """" & #DateFormat(gets.START,'mm/dd/yyyy')# & """"
			                & "," & """" & #DateFormat(gets.END,'mm/dd/yyyy')# & """"
							& "," & """" & #DollarFormat(fourth_wamount)# & """"
							& "," & """" & #fourth_woffcode#  & """"
							& "," & """" & #DollarFormat(PR_amount)# & """"	    
							& "," & """" & "" & """"			                
							& "," & """" & #gets.Comments# & """"
					        & chr(13) & chr(10) />
					       <cfset tot_woff = tot_woff  + fourth_wamount  >
					</cfif>
				</cfloop>
			</cfoutput>
		</cfif>


  </cfoutput>

  <cfset str = str
        & """" & "" & """"
        & "," & """" &"" & """"
		& "," & """" & "" & """"
		& "," & """" & "" & """"
        & "," & """" & "" & """"
        & "," & """" & "" & """"
		& "," & """" & "" & """"
        & "," & """" & "Total Adjustment" & """"        
		& "," & """" & #DollarFormat(tot_woff)# & """"
		& "," & """" & "" & """"
		& "," & """" & #DollarFormat(Total_PR_total)# & """"
		& "," & """" & #DollarFormat(Total_Contract_Adjustment)# & """"
        & "," & """" & "" & """"
        & chr(13) & chr(10)
   />

  
  <cfset dt = DateFormat(Now(), "yyyy-mm-dd") />
  <cffile action="write" file="#application.datapath##session.AgencyId#_#session.EmployeeId#_#dt#.csv" output="#str#">
  <cfoutput> <br/><br/> <p>&raquo; 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.</p>
  </cfoutput> 
   <br/><p>&laquo; <a href='index.cfm?page=agency&cat=BillingReports&rpt=WriteOff_report'>Click Here</a> to go back to WriteOff report Report.</p> <br/><br/>
 <cfelse>
  <p>No data could be found based on your search criteria.
   <br/><br/>
  Please try again.</p> <br/><br/>
 </cfif>
</div>
</div>

