<cfset invoiceTotal=0>

<cfset schid = Session.Schid>
<cfif isdefined('schid') and schid neq ''>

<cfquery  name="Getdata" datasource="#Application.DataSrc#">
 SELECT 
			pEmployee.Emp_Last,
			pPayer.Pay_ID, "" AS terms,
			pPatients.Pt_City,
			pPatients.Pt_State, 
			pPatients.Pt_Zip,
			pPatients.Pt_Street,
			pPatients.Patient_ID, 
			pPatients.Pt_Last, 
			pPatients.Pt_First,
			pPayer.Pay_Street,
			pPayer.Pay_Name,
			pSchedules.Schedule_ID,
			pSchedules.Visit_Date,
			pSchedules.StartTime,
			pSchedules.EndTime,
			 0 AS Total_Hours,pSchedules.Assmt_ID,
			pRevenue_Code.Rev_Code,
			pSchedules.Skill,
			pRevenue_Code.HCPCS,
			pSchedules.Total_EClaim, 
			pSchedules.Invoice_ID,
			pAssessments.F26 AS Assessment_Reason_M0100,
			pAssessments.F177 AS Certification_Start,
			pAssessments.F178 AS Certification_End,
			pRevenue_Code.EClaim_Price, 
			pRevenue_Code.Description
			FROM 
			#Request.prefix_db_agency#.pSchedules  
			LEFT OUTER JOIN #Request.prefix_db_lookup#.pEmployee  on pEmployee.Emp_ID = pSchedules.Emp_ID AND pSchedules.status = 0
			<!--- JOIN #Request.prefix_db_agency#.pAuths ON pSchedules.Auth_ID   = pAuths.Auth_ID AND pAuths.status = 0 --->
			JOIN #Request.prefix_db_agency#.pPtPayer ON   pPtPayer.PtPayer_ID = pSchedules.PtPayer_ID  AND pPtPayer.status = 0
			JOIN #Request.prefix_db_agency#.pPayer  ON pPtPayer.Payer_ID = pPayer.Pay_ID AND pPayer.status = 0
			JOIN #Request.prefix_db_agency#.pPatients ON pPatients.Patient_ID = pSchedules.Patient_ID AND pPatients.status < 2
			JOIN #Request.prefix_db_agency#.pAssessments ON pAssessments.Assmt_ID = pSchedules.Assmt_ID  AND pAssessments.status = 0
			JOIN #Request.prefix_db_agency#.pRevenue_Code  on pRevenue_Code.Rev_CodeID = pSchedules.Rev_ID
			WHERE	 
			   pSchedules.Visit_Date < NOW()
				AND pPayer.Pay_ID = '#url.ID#'
 				AND pSchedules.EClaimed = 0
				AND pSchedules.Missed  = 0
			 AND pSchedules.Schedule_ID IN (#schid#)
			Group by Schedule_ID	
			ORDER BY Pt_Last, Pt_First, pSchedules.Assmt_ID, Visit_Date
</cfquery>

<cfquery  name="GetAssessmentTracker" datasource="#Application.DataSrc#">
    SELECT 
			pEmployee.Emp_Last,
			pPayer.Pay_ID,
			pPatients.Pt_City,
			pPatients.Pt_State, 
			pPatients.Pt_Zip,
			pPatients.Pt_Street,
			pPatients.Patient_ID, 
			pPatients.Pt_Last, 
			pPatients.Pt_First,
			pPayer.Pay_Street,
			pPayer.Pay_Name,
			pSchedules.Schedule_ID,
			pSchedules.Visit_Date,
			pSchedules.StartTime,
			pSchedules.EndTime,
			 0 AS Total_Hours,pSchedules.Assmt_ID,
			pRevenue_Code.Rev_Code,
			pSchedules.Skill,
			pRevenue_Code.HCPCS,
			pSchedules.Total_EClaim, 
			pSchedules.Invoice_ID,
			pAssessments.F26 AS Assessment_Reason_M0100,
			pAssessments.F177 AS Certification_Start,
			pAssessments.F178 AS Certification_End,
			pRevenue_Code.EClaim_Price, 
			pRevenue_Code.Description
			FROM 
			#Request.prefix_db_agency#.pSchedules  
			LEFT OUTER JOIN #Request.prefix_db_lookup#.pEmployee  on pEmployee.Emp_ID = pSchedules.Emp_ID AND pSchedules.status = 0
			<!--- JOIN #Request.prefix_db_agency#.pAuths ON pSchedules.Auth_ID   = pAuths.Auth_ID AND pAuths.status = 0 --->
			JOIN #Request.prefix_db_agency#.pPtPayer ON   pPtPayer.PtPayer_ID = pSchedules.PtPayer_ID  AND pPtPayer.status = 0
			JOIN #Request.prefix_db_agency#.pPayer  ON pPtPayer.Payer_ID = pPayer.Pay_ID AND pPayer.status = 0
			JOIN #Request.prefix_db_agency#.pPatients ON pPatients.Patient_ID = pSchedules.Patient_ID AND pPatients.status < 2
			JOIN #Request.prefix_db_agency#.pAssessments ON pAssessments.Assmt_ID = pSchedules.Assmt_ID  AND pAssessments.status = 0
			JOIN #Request.prefix_db_agency#.pRevenue_Code  on pRevenue_Code.Rev_CodeID = pSchedules.Rev_ID
			WHERE	 
			   pSchedules.Visit_Date < NOW()
				AND pPayer.Pay_ID = '#url.ID#'
 				AND pSchedules.EClaimed = 0
				AND pSchedules.Missed  = 0
			 AND pSchedules.Schedule_ID IN (#schid#)
			Group by pSchedules.Assmt_ID	
			ORDER BY Pt_Last, Pt_First, pSchedules.Assmt_ID, Visit_Date
</cfquery>

<cfset recordcount=GetAssessmentTracker.recordcount>
<cfset data=Trim(recordcount)-1>
<cfif GetAssessmentTracker.recordcount EQ 0 >
	<p>Something Went wrong. Please connect with your admin</p><cfabort>
</cfif>

 <cfdocument format="pdf" orientation = "portrait" overwrite = "yes">
	<cfdocumentsection margintop="1">
	<cfdocumentitem type="header" evalprint="true">
	</cfdocumentitem> 
	<cfset invoiceid = 0>
	<cfset chk_Patient_Id = ''>
	<cfloop query="GetAssessmentTracker">
	<cfset data1=#GetAssessmentTracker.Assmt_ID#>
		<cfset GetData_Schedule_ID = 0>
		<cfloop query="Getdata">
		  <cfset data2=#GetData.Assmt_ID#>
			<cfif data1 eq data2>
			 <cfif GetData_Schedule_ID eq 0 >
					  <cfset GetData_Schedule_ID = #GetData.Schedule_ID#>
					  <cfelse>
					  <cfset GetData_Schedule_ID = GetData_Schedule_ID&','&GetData.Schedule_ID>
					  </cfif>
					 <cfif Getdata.Total_EClaim neq '' and Getdata.Total_EClaim gt 0>
					  <cfset invoiceTotal = invoiceTotal + Getdata.Total_EClaim>
					</cfif>
			 </cfif> 
		 </cfloop>
	  <cfif chk_Patient_Id neq GetAssessmentTracker.Patient_ID>
<cfquery  datasource="#Application.DataSrc#" name="Insert_Invoice">
  INSERT INTO #Request.prefix_db_agency#.Invoices (
	  PatientPayer_ID
	 ,Agency_ID
	 ,Patient_ID
	 ,Assmt_ID
	 ,DateSent
	 ,MethodSent
     ,Output
	 ,InvoiceTotal
	 ,PayerBalance
	 ,WhereTreated
	 ,Record_Created_By
   ,Record_Created_Date
   ,Record_Modified_By
   ,Record_Modified_Date

  ) VALUES (
	   '#GetData.Pay_ID#'
	  ,'#session.agencyid#'
	  ,'#GetData.Patient_ID#'
	  ,'#GetData.Assmt_ID#'
		,NOW()
		,'PDF'
    ,'PDF'
		,#invoiceTotal#
		,#invoiceTotal#
		,"" 
		,'#Session.EmployeeId#'
    ,#now()#
    ,'#Session.EmployeeId#'
    ,#now()#

  )
 </cfquery>
  <cfquery  datasource="#Application.DataSrc#" name="Last_Invoice_ID">
		SELECT LAST_INSERT_ID() as LastInsertID
	 </cfquery>
	 
	<cfquery datasource="#Application.DataSrc#" name="Insert_Payments">
		 INSERT INTO #Request.prefix_db_agency#.Payments (
		  Invoice_ID
		 ,PatientPayer_ID
		 ,PaymentDate
		 ,Amount
		 ,PaymentMethod
		 ,WriteOffAmount
		 ,WriteOffType
		 ,Record_Created_By
	   ,Record_Created_Date
	   ,Record_Modified_By
	   ,Record_Modified_Date
	  ) VALUES (
	  	'#Last_Invoice_ID.LastInsertID#',
		 '#GetData.Pay_ID#',
		 #CreateODBCDateTime(Now())#,
		 '0.00',
		 'Electronic',
		 '0.00',
		 '0.00'	
		  ,'#Session.EmployeeId#'
	    ,#now()#
	    ,'#Session.EmployeeId#'
	    ,#now()#
	 
	  )
	</cfquery>
 
	<cfset invoiceid = #Last_Invoice_ID.LastInsertID#>
  <table border="0" cellpadding="0" style="font-family:Arial; width:95%" cellspacing="0" >
	    <tr>
		     <td align="left" style="padding-left:5px; font-size:16px; font-family:Arial; font-weight:bolder; vertical-align: bottom">
			       <cfoutput>#UCase(getTimeZone.Agency_Name)#</cfoutput></br>
			 </td>
			 <td align="right" style="padding-right:13px; font-size:26px; font-family:Arial; font-weight:bolder">
			       Invoice
			 </td>
		  </tr>
		  <tr>
		      <td align="left" style="padding-left:5px; font-size:16px; font-family:Arial;font-weight:bolder;vertical-align: bottom">
			      <cfoutput> #UCase(getTimeZone.Agency_Address1)#</cfoutput>
			 </td>
		  </tr>
	   	<tr>
	      <td align="left" style="padding-left:5px; font-size:16px; font-family:Arial;font-weight:bolder;vertical-align: top">		      
		      <cfoutput>#UCase(getTimeZone.Agency_City)# #UCase(getTimeZone.Agency_State)# #getTimeZone.Agency_Zip#</cfoutput>
		 		</td>
		 		<td align="right" style="padding-right:13px; font-family:Arial">
		      <table width="60%"  style="font-family:Arial;" cellpadding="0" cellspacing="0">
				<tr>
					<td style="font-size:12px; font-family:Arial;width: 30%; border-bottom:solid 1px #000000; border-left: solid 1px #000000; border-top: solid 1px #000000; height: 20px; vertical-align: middle" align="center">
							Date			
					</td>
					<td style="font-size:12px; font-family:Arial;width: 30%; border: solid 1px #000000; height: 20px; vertical-align: middle" align="center">
						Invoice#		
					</td>
				</tr>
				<tr>
				    <td align="center" style="font-size:11px; font-family:Arial;width: 30%; border-bottom:solid 1px #000000; border-left: solid 1px #000000;; height: 30px; vertical-align: middle">
					 <cfoutput>#DateFormat(now(),"mm/dd/yyyy")#</cfoutput>
					</td>
					<td align="center" style="font-size:11px; font-family:Arial;width: 30%; border-bottom:solid 1px #000000; border-left: solid 1px #000000; border-right: solid 1px #000000; height: 30px; vertical-align: middle">
					 <cfoutput>#Last_Invoice_ID.LastInsertID#</cfoutput>
					</td>
			</tr>
			</table>
		 </td>
	  </tr>
	  <tr>
	  <td align="left">
	     <table width="95%" cellpadding="0" cellspacing="0">
				<tr>
					<td align="left" style="padding-left: 18px; border: solid 1px #000000;font-size:12px; font-family:Arial; height: 30px;">
					  Bill To
					</td>
				</tr>
				<tr>
					<td align="left" style="padding-left: 3px; border-bottom:solid 1px #000000; border-left: solid 1px #000000; border-right: solid 1px #000000; font-family:Arial; height: 125px; vertical-align: top"><br />
					<cfoutput>&nbsp;&nbsp;#UCase(GetAssessmentTracker.Pt_First)#&nbsp;#UCase(GetAssessmentTracker.Pt_Last)#<br />
					  &nbsp;&nbsp;#UCase(GetAssessmentTracker.Pt_Street)#<br />
					  &nbsp;&nbsp;#UCase(GetAssessmentTracker.Pt_City)# #GetAssessmentTracker.Pt_State# #GetAssessmentTracker.Pt_Zip#</cfoutput>
					</td>
				</tr>
			</table>
		</td>
	  </tr>
	   <!--- <tr>
		  <td align="right" colspan="2" style="padding-right:8px; font-family:Arial">
			 <table width="17%" style="border-bottom: 0px" cellpadding="0" cellspacing="0">
					<tr>
						<td align="center" style="font-family:Arial;font-size:12px; border:solid 1px #000000; height: 25px">
						 Payment Terms
						</td>
					</tr>
					<tr>
						<td align="center" style="padding-left: 3px; font-size: 11px; border-left: solid 1px black; border-right: solid 1px black;height: 25px"> <cfoutput>#(GetData.terms)#</cfoutput>
						</td>
					</tr>
				</table>
			</td>
		  </tr>--->
		   <tr>
		  <td align="left" colspan="2" style="font-family:Arial">
			 <table width="99%" cellpadding="0" cellspacing="0">
					<tr>
						<td align="center" style="font-size: 12px; font-family:Arial; width: 10%; border-bottom:solid 1px #000000; border-left: solid 1px #000000; border-top: solid 1px #000000;height: 25px">
						 Visit Type
						</td>
						<td align="center" style="font-size: 12px; font-family:Arial; width: 10%; border:solid 1px #000000; height: 25px">
						 Quantity
						</td>
						<td align="center" style="font-size: 12px; font-family:Arial; width: 10%; border-bottom:solid 1px #000000; border-right: solid 1px #000000; border-top: solid 1px #000000; height: 25px">
						 Date
						</td>
						<td align="center" style="font-size: 12px; font-family:Arial; width: 10%; border-bottom:solid 1px #000000; border-right: solid 1px #000000; border-top: solid 1px #000000; height: 25px">
						 Rate
						</td>
						<td align="center" style="font-size: 12px; font-family:Arial; width: 12%; border-bottom:solid 1px #000000; border-right: solid 1px #000000; border-top: solid 1px #000000; height: 25px">
						 Time In
						</td>
						<td align="center" style="font-size: 12px; font-family:Arial; width: 12%; border-bottom:solid 1px #000000; border-right: solid 1px #000000; border-top: solid 1px #000000; height: 25px">
						 Time Out
						</td>
						<td align="center" style="font-size: 12px; font-family:Arial; width: 17%; border-bottom:solid 1px #000000; border-right: solid 1px #000000; border-top: solid 1px #000000; height: 25px">
						 Amount
						</td>
					</tr>
					<cfloop query="Getdata">
					<tr>
					  <td align="center" style="padding-right: 5px; font-family:Arial; padding-left: 3px; font-size: 11px;  border-left: solid 1px black; height: 25px">
						   <cfoutput>#Trim(GetData.Skill)#<cfif GetData.Skill NEQ GetData.Description> - #Trim(GetData.Description)#</cfif></cfoutput><br> 
						 </td>
						 <cfset diff = #DateDiff("n",GetData.StartTime,GetData.EndTime)#> 
						 <cfset TotalHours = (#diff#/15)*25/100>
						 <cfif TotalHours lt 0>
								<cfset TotalHours = TotalHours + 24 />
						 </cfif>
						<td align="center" style=" padding-right: 5px; font-family:Arial; font-size: 11px; border-left: solid 1px black;border-right: solid 1px black; height: 25px">
						    <cfoutput> #NumberFormat(TotalHours,'_____.__')# Hour(s)</cfoutput><br> 
						</td>
						<td align="center" style="padding-right: 5px; font-family:Arial; font-size: 11px; border-right: solid 1px black; height: 25px">
						   <cfoutput>#DateFormat(GetData.Visit_Date,"mm/dd/yyyy")#</cfoutput><br> 
						 </td>
						<td align="center" style="padding-right: 5px; font-family:Arial; font-size: 11px; border-right: solid 1px black; height: 25px ">
						   <cfoutput>#Trim(GetData.EClaim_Price)#</cfoutput><br> 
						 </td>
						<td align="center" style="padding-right: 5px; font-family:Arial; font-size: 11px; border-right: solid 1px black; height: 25px ">
						  <cfoutput>#timeformat(GetData.StartTime,'hh:mm:ss tt')#</cfoutput><br>
				        </td>
						<td align="center" style="padding-right: 5px; font-family:Arial; font-size: 11px; border-right: solid 1px black; height: 25px ">
						   <cfoutput>#timeformat(GetData.EndTime,'hh:mm:ss tt')#</cfoutput><br>
				        </td>
						<td align="center" style="padding-right: 5px; font-family:Arial; font-size: 11px; border-right: solid 1px black; height: 25px ">
					         <cfoutput>
					           #NumberFormat(GetData.Total_EClaim,'_____.__')#
                   </cfoutput><br> 
						</td>
						
					</tr>
					<!---</cfif>--->
					</cfloop>
					<tr>
					    <td align="right" style="font-family:Arial; padding-right: 10px; font-size: 20px; font-weight: bold; border-bottom:solid 1px black; border-left: solid 1px black;border-right: solid 1px black;border-top:solid 1px black;" colspan="6">
						    Total
						</td>
						<td align="right" style="font-family:Arial; font-size: 16px; border-bottom:solid 1px black; border-right: solid 1px black; border-top:solid 1px black;" colspan="2">
						 <cfoutput>
						   <div align="center">$#NumberFormat(invoiceTotal,'_____.__')#</div>
						 </cfoutput>
						  <cfset invoiceTotal = 0>
						</td>
					</tr>
					<tr>
					   <td colspan="5">
					      <table width="100%"cellpadding="0" cellspacing="0">
								<tr>
									<td align="center" style="font-family:Arial; font-size: 12px; border-bottom:solid 1px black; border-right: solid 1px black; border-left: solid 1px black; height: 25px ; width: 25%;">
									 Phone#
									</td>
									<td align="center" style="font-family:Arial; font-size: 12px; border-bottom:solid 1px black; border-right: solid 1px black;height: 25px; width: 25%;">
									 Fax#
									</td>
									<td align="center" style="font-family:Arial; font-size: 12px; border-bottom:solid 1px black; border-right: solid 1px black;height: 25px; width: 50%;">
									 E-mail
									</td>
								</tr>
								<tr>
									<td align="center" style="font-family:Arial; font-size: 11px; border-bottom:solid 1px black; border-right: solid 1px black; border-left: solid 1px black; height: 30px">
									<cfoutput><cfset Phone=#REReplace(#getTimeZone.Agency_Contact_Phone#,"-","","ALL")#>
									<cfset areaCode = Left(Phone, 3)>
									<cfset firstThree = Mid(Phone, 4,3)>
									<cfset lastFour = Right(Phone, 4)>
									(#areaCode#) #firstThree#-#lastFour#</cfoutput>
									  
									</td>
									<td align="center" style="font-family:Arial; font-size: 11px; border-bottom:solid 1px black; border-right: solid 1px black; height: 30px">
									<cfoutput><cfset Fax=#REReplace(#getTimeZone.Agency_Fax#,"-","","ALL")#>
									<cfset areaCode = Left(Fax, 3)>
									<cfset firstThree = Mid(Fax, 4,3)>
									<cfset lastFour = Right(Fax, 4)>
									(#areaCode#) #firstThree#-#lastFour#</cfoutput>
									</td>
									<td align="center" style="font-family:Arial; font-size: 11px; border-bottom:solid 1px black; border-right: solid 1px black; height: 30px">
									 <cfoutput>#getTimeZone.Agency_Contact_Email#</cfoutput>
									</td>
								</tr>
							</table>
					   </td>
					</tr>
				</table>
			</td>
		  </tr>
  </table>
<cfset Session.Schid=""> 

<cfif data gt 0>
		<cfdocumentitem type="pagebreak"></cfdocumentitem>
		<cfset data=data-1>
</cfif>
</cfif>

 <cfquery  datasource="#Application.DataSrc#" name="Update_Flags">
		UPDATE #Request.prefix_db_agency#.pSchedules SET 
		EClaimed = 1,
		Invoice_ID = #invoiceid#,
		Export_Date = #CreateODBCDateTime(Now())#
		WHERE Schedule_ID  in (#GetData_Schedule_ID#)
		AND   Assmt_ID = #GetAssessmentTracker.Assmt_ID#
 </cfquery>
<cfset chk_Patient_Id = #GetAssessmentTracker.Patient_ID#>

</cfloop>

 <cfdocumentitem type="footer">
		<table width="100%" cellpadding="0" cellpadding="0" border="0" align="center">
		<tr>
		<td align="center" style="font-family: Arial; font-size: 13px;">
		<cfoutput>Page #cfdocument.currentpagenumber# </cfoutput>
		</td>
		</tr>
		</table>
 </cfdocumentitem>
</cfdocumentsection>
</cfdocument>

</cfif>
