SELECT (i.InvoiceTotal) as InvoiceTotal, (i.PayerBalance) as PayerBalance, DATEDIFF(CURDATE(), i.DateSent) AS age ,i.Seq_Amount, i.invoice_id,pay.Approve, (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, 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) as wOff, p.Patient_ID,p.Pt_Last,p.Pt_First, a.F177 AS START, a.F178 AS END , a.F26 AS TYPE, DATE_FORMAT(p.Pt_DOB,'%m/%d/%Y') AS DateOfBirth,pay.Payment_ID FROM #Request.prefix_db_agency#.Invoices i,#Request.prefix_db_agency#.pPayer pr, #Request.prefix_db_agency#.pPatients p, #Request.prefix_db_agency#.pAssessments a, #Request.prefix_db_agency#.Payments pay WHERE i.Agency_ID = '#session.agencyid#' AND i.Deleted <> 1 AND i.InvoiceTotal > 0 and i.invoice_id =pay.invoice_id AND pay.writeoffamount>0 AND i.DateSent >= #CreateODBCDate(FORM.df)# AND i.DateSent <= #CreateODBCDate(FORM.dt)# AND pr.Pay_ID = '#FORM.pr#' AND p.patient_id = i.patient_id and i.Assmt_ID = a.Assmt_ID AND i.patient_id = '#FORM.pid#' AND p.Patient_ID = '#url.Pid#' AND i.Invoice_Id='#url.Invid#' AND pr.Pay_ID = i.patientpayer_id AND p.Status IN (#Request.patient_status#) AND pr.Status = 0 AND pay.Deleted <> 1 AND a.Status = 0 GROUP BY i.Invoice_Id ORDER BY Patient_ID asc SELECT pr.Pay_ID, pr.Pay_Name FROM #Request.prefix_db_agency#.pPayer pr WHERE pr.Loc_ID = '#session.agencyid#' AND pr.Status = '0' ORDER BY pr.Pay_Name 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#' AND p.Status IN (#Request.patient_status#) ORDER BY Patient_ID Select distinct(Patient_ID) as Patient_ID, Pt_Last,Pt_First from GetI order by Patient_ID asc SELECT * 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#.pSchedules ON pSchedules.Schedule_ID = Visits_payments.Schedule_ID AND pSchedules.Status = 0 AND Payments.Deleted <> 1 WHERE Visits_payments.Status = 0 AND Visits_payments.Payment_ID IN (#ValueList(GetI.Payment_ID)#) AND Visits_payments.Payment_ID = 0
Update WriteOff details
Update WriteOff details

#Pt_Last# #Pt_First#

#invoice_id#

#DateFormat(Start,'mm/dd/yyyy')#    -    #DateFormat(End,'mm/dd/yyyy')#
#DollarFormat(nonmedicare_writeoff)#

#getallpayments.write_offcode# - #getallpayments.write_offamount#
#getallpayments.second_write_offcode# - #getallpayments.second_write_offamount#
#getallpayments.third_write_offcode# - #getallpayments.third_write_offamount#
#getallpayments.fourth_write_offcode# - #getallpayments.fourth_write_offamount#
#WriteOffType#