<cfparam name="url.out" default="" />
<cfquery datasource="#Application.DataSrc#" name="GetVisitType">
	SELECT Payer.PayName, ScheduleItems.Visit_Type AS TYPE, ScheduleItems.Output_Type, 
	ScheduleItems.Visit_Date, ScheduleItems.Missed, ScheduleItems.Payer_ID, SUM(ScheduleItems.Total_Hours) as TotalHours, SUM(ScheduleItems.Total_EPay) as PAY,ScheduleItems.Payer_ID,
	COUNT(ScheduleItems.Total_Hours) as TotalVisits, SUM(an.Homebound_Status) AS MCSUM, SUM(ScheduleItems.Total_EClaim) AS TotalBilled,
	Payer.Output_Type AS OTYPE
	FROM #Request.prefix_db_agency#.ScheduleItems, #Request.prefix_db_agency#.Payer, #Request.prefix_db_agency#.Assessment_New an
	WHERE ScheduleItems.Agency_ID = #session.AgencyID#
	AND ScheduleItems.Assessment_ID = an.Assessment_ID
	And ScheduleItems.Payer_ID = #url.ID#
	AND ScheduleItems.Missed ="No"
	And ScheduleItems.Deleted <> 1
	AND ScheduleItems.Visit_Date BETWEEN '#Session.Start#' AND '#Session.End#'
	AND ScheduleItems.Payer_ID = Payer.Payer_ID
	AND ScheduleItems.Output_Type = '#url.out#'
	GROUP BY TYPE
	Order by TYPE
</cfquery>
<div class="portlet box blue">
	<div class="portlet-title">
		<div class="caption">
			<i class="fa fa-gear"></i> Lists Visit Type Export
		</div>
		<div class="tools">
			<a href="javascript:;" class="collapse"></a>
		</div>
	</div>		
	<div class="portlet-body">
		<cfif GetVisitType.recordcount gt 0>
			<cfset str = """" & "Visit Type" & """"
				& "," & """" & "Total Visits" & """"
				& "," & """" & "Total Hours" & """"
				& "," & """" & "Total Paid" & """"
				& "," & """" & "Total Billed" & """"
				& chr(13)
				& chr(10)
			/>
			<cfoutput query='GetVisitType'>
				<cfset str = str
					& """" & #TYPE# & """"
					& "," & """" & #TotalVisits# & """"
					& "," & """" & #TotalHours# & """"	  
					& "," & """" & #DecimalFormat(PAY)# & """"
					& "," & """" & #LSCurrencyFormat(TotalBilled, "local")# & """"					
					& chr(13)
					& chr(10)
				/>
			</cfoutput>
			<cfset dt = DateFormat(Now(), "yyyy-mm-dd") />
			<cffile action="write" file="#application.datapath##session.AgencyId#_#dt#.csv" output="#str#">
			<cfoutput>
				<p>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.</p>
			</cfoutput> 
		<cfelse>
			<h4>No data could be found based on your search criteria.</h4>
			<p>Please try again.</p>
		</cfif>
	</div>
</div>


