<cfparam name='form.scheduleItemId' default='' />

<div style='margin:60px 0px 0px 20px;'>
 <cfquery  name='gets' datasource='#Application.DataSrc#'>
  SELECT SUM(p.hours) AS SumHrs, SUM(p.wages) AS SumPay, p.pay_rate, e.Employee_Id, e.Last_Name, e.First_Name, e.Soc_Sec_No
  FROM #Request.prefix_db_lookup#.Agency a, #Request.prefix_db_agency#.ScheduleItems si, #Request.prefix_db_agency#.payroll p, #Request.prefix_db_agency#.Employee_Payroll ep, #Request.prefix_db_agency#.Agency_Payroll ap, #Request.prefix_db_lookup#.Employee e
  WHERE  a.Agency_Id = '#session.AgencyId#'
  AND    p.ScheduleItems_Id IN (#form.scheduleItemId#)
  AND    si.ScheduleItems_Id = p.ScheduleItems_Id
  AND    ep.EmpPay_Id = si.EmpPay_Id
  AND    ap.Agency_Id = '#session.AgencyId#'
  AND    ap.Payroll_Id = ep.Payroll_Id
  AND    e.Employee_Id = si.Employee_Id
  Group by e.Employee_Id
  ORDER BY p.ScheduleItems_Id
  
 </cfquery>
 <cfif gets.recordcount gt 0>
  <cfset str = """" & chr(13) & chr(10)
  />
  <cfoutput query='gets'>
  <cfset name = gets.First_Name & " " & gets.Last_Name>
  <cfset name1 = #Replace(name, " ", "_", "ALL")#>
  <cfset name2 = #Replace(name1, ",", "_", "ALL")#>
  <cfset str = str
                      & '000000' <!---& """"
                & """" & #gets.pay_date# & """"
                & """" & #gets.Employee_Id# & """"
                & """" &  ---> & #Replace(RJustify( name2, 25 )," ","0","ALL" )# & '0000000000000000000E00' & #Replace(RJustify( gets.pay_rate, 7 )," ","0","all")# & '00' & #Replace(RJustify( gets.SumHrs, 8 )," ","0","all")# & 
				'000000000000000000000000000000000000000000000001' & #gets.Soc_Sec_No# & '000000'
                & 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="#Replace(str, "_", " ", "ALL")#">
  <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=reports&cat=schedules&rpt=Payroll_Report'>Click Here</a> to start a new Payroll Report search.
  <cfquery  name='setExported' datasource='#Application.DataSrc#'>
   UPDATE #Request.prefix_db_agency#.payroll
   SET    exported = 'Y'
   WHERE  ScheduleItems_Id IN (#form.scheduleItemId#)
  </cfquery>
 <cfelse>
  No data could be found based on your search criteria.
   <br/><br/>
  Please try again.
 </cfif>
</div>
