<cfparam name="url.Invoice_ID" default="0">
<cfparam name="url.Payment_ID" default="0">
<cfparam name="url.Amnt" default="0">
<cfif url.Invoice_ID NEQ 0 >
	<cfset  url.Invoice_ID = decrypt(#url.Invoice_ID#,#application.enckey#,"AES","Hex") />
	<cfset  url.Payment_ID = decrypt(#url.Payment_ID#,#application.enckey#,"AES","Hex") />
	<cfset  url.Amnt = decrypt(#url.Amnt#,#application.enckey#,"AES","Hex") />
</cfif>


<cfquery  name='Getschedule' datasource='#Application.DataSrc#'>
	 SELECT  
	 pEmployee.Emp_Last,pEmployee.Emp_First,pSchedules.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,
	 pSchedules.Schedule_ID, pSchedules.Total_Units,pSchedules.Assmt_ID,
	 pSchedules.Invoice_ID,pSchedules.Visit_Date,pSchedules.Skill,
	 Payments.WriteOffAmount,pPayer.Pay_Name,
	 pSchedules.Total_Eclaim,Invoices.patientpayer_id,
	  pRevenue_Code.Measure, pRevenue_Code.EClaim_Price ,
	   vp.Visit_Pay_ID, vp.Schedule_ID as vp_Schedule_ID ,vp.Payment_ID, IFNULL(vp.payment,0) AS payment, 
	   IFNULL(vp.write_offamount,0) AS first_wamount,IFNULL(vp.second_write_offamount,0) AS second_wamount,
	   IFNULL(vp.third_write_offamount,0) AS third_wamount, IFNULL(vp.fourth_write_offamount,0) AS fourth_wamount,
	   vp.write_offcode, vp.write_offcodesecond, vp.second_write_offcode,vp.second_write_offcodesecond, vp.third_write_offcode,
	    vp.third_write_offcodesecond, vp.fourth_write_offcode, vp.fourth_write_offcodesecond 
	 FROM   #Request.prefix_db_agency#.pSchedules
	  JOIN  #Request.prefix_db_agency#.Invoices ON  Invoices.Invoice_Id = pSchedules.Invoice_ID
	  JOIN  #Request.prefix_db_agency#.pPayer ON  pPayer.Pay_ID = Invoices.PatientPayer_ID
	  JOIN #Request.prefix_db_agency#.pRevenue_Code ON pRevenue_Code.Rev_CodeID = pSchedules.Rev_ID
	  LEFT OUTER JOIN #Request.prefix_db_agency#.Visits_payments vp ON  vp.Schedule_ID = pSchedules.Schedule_ID 
	  AND vp.Status = 0 
	  JOIN  #Request.prefix_db_agency#.Payments  ON  vp.Payment_ID = Payments.Payment_ID  AND Payments.Deleted  = 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
	  LEFT OUTER JOIN #Request.prefix_db_lookup#.pEmployee  on pEmployee.Emp_ID = pSchedules.Emp_ID AND pSchedules.status = 0
	 WHERE  pSchedules.Status = 0 AND Invoices.Deleted <> 1 
	 AND   pSchedules.Invoice_ID = '#url.Invoice_ID#'
	 AND Payments.Payment_ID =  '#url.Payment_ID#'
	 GROUP BY vp.Visit_Pay_ID
</cfquery>
<cfset Schids = ValueList(Getschedule.Schedule_ID) />
<cfset Getschedules=Getschedule.recordcount>
<cfset data=Trim(Getschedules)-1>
<cfif Getschedule.recordcount EQ 0 >
	<p>Something Went wrong. Please connect with your admin</p><cfabort>
</cfif>


<cfquery  datasource="#Application.DataSrc#" name="Insert_Invoice">
	INSERT INTO #Request.prefix_db_agency#.pat_Invoices 
	(
	  	  Invoice_ID
		 ,PatientPayer_ID
		 ,Agency_ID
		 ,Patient_ID
		 ,Assmt_ID
		 ,DateSent
		 ,MethodSent
	     ,Output
		 ,WhereTreated
		 ,Record_Created_By
	   ,Record_Created_Date
	   ,Record_Modified_By
	   ,Record_Modified_Date
	) 
	VALUES 
	(
	  	'#url.Invoice_ID#'
		,'#Getschedule.PatientPayer_ID#'
		,'#session.agencyid#'
		,'#Getschedule.Patient_ID#'
		,'#Getschedule.Assmt_ID#'
		,NOW()
		,'PDF'
	    ,'PDF'
		,"" 
		,'#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#.pat_Payments
	(
	  pat_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#',
	'#Getschedule.Pay_ID#',
	#CreateODBCDateTime(Now())#,
	'0.00',
	'Electronic',
	'0.00',
	'0.00'	
	,'#Session.EmployeeId#'
    ,#now()#
    ,'#Session.EmployeeId#'
    ,#now()#
   )
</cfquery> --->


<cfdocument format="pdf" orientation = "portrait" overwrite = "yes">
	<cfdocumentsection margintop="1">
		<cfdocumentitem type="header" evalprint="true"></cfdocumentitem> 
		<cfset invoiceid = #Last_Invoice_ID.LastInsertID#>
	    <table border="0" cellpadding="0" style="font-family:Arial; width:100%" cellspacing="0" >
		    <tr>
			     <td align="left" style="padding-left:5px; font-size:16px; font-family:Arial; vertical-align: bottom">
				       <cfoutput>#UCase(getTimeZone.Agency_Name)#</cfoutput></br>
				 </td>
				 <td align="right" style="padding-right:0px; font-family:Arial;font-size:13px;">
				 		<b>Invoice#:</b> <cfoutput> #invoiceid#</cfoutput>
				 </td>
			</tr>
			<tr>
			      <td align="left" style="padding-left:5px; font-size:16px; font-family:Arial;vertical-align: bottom">
				      <cfoutput> #UCase(getTimeZone.Agency_Address1)#</cfoutput>
				 </td>
				 <td align="right" style="padding-right:0px; font-family:Arial;font-size:13px;">
				 		 <b>Date:</b> <cfoutput> #DateFormat(now(), "mmmm dd, yyyy")#</cfoutput>				 		
				 </td>

			  </tr>
		   	<tr>
		      <td align="left" style="padding-left:5px; font-size:16px; font-family:Arial;vertical-align: top">		      
		      		<cfoutput>#UCase(getTimeZone.Agency_City)#</cfoutput>
		 		</td>
			 	<td align="right" style="padding-right:0px; font-family:Arial;font-size:13px;">
			       	 <b>Amount Due: </b> <cfoutput> #DollarFormat(url.Amnt)#</cfoutput>
			 	</td>
		  	</tr>
		  		<tr>
		     	 <td align="left" style="padding-left:5px; font-size:16px; font-family:Arial;vertical-align: top">		      
		      		<cfoutput> #UCase(getTimeZone.Agency_State)# #getTimeZone.Agency_Zip#</cfoutput>
		 		</td>
			 	<td align="right" style="padding-right:0px;font-family:Arial;font-size:13px;">
			       	 <b>Due Date: </b> 30 Days from invoice date
			 	</td>
		  	</tr>
		 	<tr>
	  			<td align="left" style="padding-left:5px; font-size:16px; font-family:Arial;font-weight:bolder;vertical-align: top">	<br />
				  <b>Bill To:
				  <cfoutput>#UCase(Getschedule.Pt_First)#&nbsp;#UCase(Getschedule.Pt_Last)#<br />
				  #UCase(Getschedule.Pt_Street)#<br />
				  #UCase(Getschedule.Pt_City)# #Getschedule.Pt_State# #Getschedule.Pt_Zip#
				  </cfoutput></b>
				</td>
			</tr>
			<tr><td style=" height: 15px;" ></tr>
			<tr>
			    <td align="left" colspan="2" style="font-family:Arial">
				 	<table width="100%"  style="font-family:Arial;" cellpadding="0" cellspacing="0">
						<tr>
							<td style="font-weight:bold;background-color:lightgray;font-size:12.5px; font-family:Arial; border-bottom:solid 1px ##000000; border-left: solid 1px ##000000; border-top: solid 1px ##000000; height: 20px; vertical-align: middle" align="center">Service Date</td>
							<td style="font-weight:bold;background-color:lightgray;font-size:12.5px; font-family:Arial; border-bottom:solid 1px ##000000; border-left: solid 1px ##000000; border-top: solid 1px ##000000; height: 20px; vertical-align: middle" align="center">Decription</td>
							<td style="font-weight:bold;background-color:lightgray;font-size:12.5px; font-family:Arial; border-bottom:solid 1px ##000000; border-left: solid 1px ##000000; border-top: solid 1px ##000000; height: 20px; vertical-align: middle" align="center">Charge</td>
							<td style="font-weight:bold;background-color:lightgray;font-size:12.5px; font-family:Arial; border-bottom:solid 1px ##000000; border-left: solid 1px ##000000; border-top: solid 1px ##000000; height: 20px; vertical-align: middle" align="center">Payment</td>
							<td style="font-weight:bold;background-color:lightgray;font-size:12.5px; font-family:Arial; border-bottom:solid 1px ##000000; border-left: solid 1px ##000000; border-top: solid 1px ##000000; height: 20px; vertical-align: middle" align="center">Adjustment Amount</td>
							<td style="font-weight:bold;background-color:lightgray;font-size:12.5px; font-family:Arial; border-bottom:solid 1px ##000000; border-left: solid 1px ##000000; border-top: solid 1px ##000000; height: 20px; vertical-align: middle" align="center">Adjustment Reason</td>
							<td style="font-weight:bold;background-color:lightgray;font-size:12.5px; font-family:Arial; border-bottom:solid 1px ##000000; border-left: solid 1px ##000000; border-top: solid 1px ##000000; height: 20px; vertical-align: middle;width:30%" align="center">Payer</td>
							<td style="font-weight:bold;background-color:lightgray;font-size:12.5px; font-family:Arial; border-bottom:solid 1px ##000000; border-left: solid 1px ##000000; border-top: solid 1px ##000000; height: 20px; vertical-align: middle;border-right: solid 1px ##000000;" align="center">Payment Due</td>
						</tr>
						<tbody>
							<cfset total_pt_row_total  = 0>
							<cfset enc_Invoice_id = encrypt(#Getschedule.Invoice_ID#,#application.enckey#,"AES","Hex") />
	                    	<cfoutput query='Getschedule'>
	                    	    <cfset enc_payment_id = encrypt(#Payment_ID#,#application.enckey#,"AES","Hex") />
	                    	    <cfset pt_row_total	 = 0 >
	                    		<cfset adjust_reason = "" >
	                    		<cfif write_offcode contains 'PR' AND first_wamount GT 0>
	                               <cfset pt_row_total = pt_row_total + first_wamount >
	                               <cfset adjust_reason = adjust_reason & write_offcode & "<br>">
	                            </cfif>
	                            <cfif second_write_offcode contains 'PR' AND second_wamount GT 0>
	                               <cfset pt_row_total = pt_row_total + second_wamount >
	                               <cfset adjust_reason = adjust_reason & second_write_offcode & "<br>">
	                            </cfif>
	                            <cfif third_write_offcode contains 'PR' AND third_wamount GT 0>
	                               <cfset pt_row_total = pt_row_total + third_wamount >
	                               <cfset adjust_reason = adjust_reason & third_write_offcode & "<br>">
	                            </cfif>
	                            <cfif fourth_write_offcode contains 'PR' AND fourth_wamount GT 0>
	                                <cfset pt_row_total = pt_row_total + fourth_wamount >
	                                <cfset adjust_reason = adjust_reason & fourth_write_offcode & "<br>">
	                            </cfif>
	                            <tr id="trpayment_#Payment_ID#">
	                                 <td align="center" style="font-size:11px; font-family:Arial; border-bottom:solid 1px ##000000; border-left: solid 1px ##000000;; height: 30px; vertical-align: middle">#DateFormat(Visit_Date, 'mm/dd/yy')#</td>
	                                 <td align="center" style="font-size:11px; font-family:Arial; border-bottom:solid 1px ##000000; border-left: solid 1px ##000000;; height: 30px; vertical-align: middle">#Skill#</td>
	                                 <td align="center" style="font-size:11px; font-family:Arial; border-bottom:solid 1px ##000000; border-left: solid 1px ##000000;; height: 30px; vertical-align: middle">#DollarFormat(Total_Eclaim)#</td>
	                                 <td align="center" style="font-size:11px; font-family:Arial; border-bottom:solid 1px ##000000; border-left: solid 1px ##000000;; height: 30px; vertical-align: middle">#DollarFormat(payment)#</td>
	                                 <td align="center" style="font-size:11px; font-family:Arial; border-bottom:solid 1px ##000000; border-left: solid 1px ##000000;; height: 30px; vertical-align: middle">#DollarFormat(pt_row_total)#</td>
	                                 <td align="center" style="font-size:11px; font-family:Arial; border-bottom:solid 1px ##000000; border-left: solid 1px ##000000;; height: 30px; vertical-align: middle">#adjust_reason#</td>
	                                 <td align="center" style="font-size:11px; font-family:Arial; border-bottom:solid 1px ##000000; border-left: solid 1px ##000000;; height: 30px; vertical-align: middle">#Pay_Name#</td>
	                                 <td align="center" style="font-size:11px; font-family:Arial; border-bottom:solid 1px ##000000; border-left: solid 1px ##000000;; height: 30px; vertical-align: middle;border-right: solid 1px ##000000;">#DollarFormat(pt_row_total)#</td>
	                            </tr>
	                           	<cfset total_pt_row_total = total_pt_row_total + pt_row_total>
	                        </cfoutput>
	                        <cfoutput>
	                            <tr>
	                              <td></td>
	                              <td></td>
	                              <td></td>
	                              <td></td>
	                              <td></td>
	                              <td></td>
	                              <td align="center" style="font-weight:bold;background-color:lightgray;">Total</td>
	                              <td align="center" style="font-weight:bold;background-color:lightgray;border-right: solid 1px ##000000;">#DollarFormat(total_pt_row_total)#</td>
	                            </tr> 
	                        </cfoutput>  
						</tbody>
					</table>		
			    </td>
		    </tr>
		    <tr><td style=" height: 25px;" ></tr>
		     <tr>
			     <td align="left" style="padding-left:5px; font-size:11px; font-family:Arial; vertical-align: bottom">
				       Send Payment to <br>
				       <cfoutput>#UCase(getTimeZone.Agency_Name)#<br>
				       #UCase(getTimeZone.Agency_Address1)#<br>
				       #UCase(getTimeZone.Agency_City)#<br>
				      #UCase(getTimeZone.Agency_State)# #getTimeZone.Agency_Zip#<br>
					</cfoutput>
				 </td>
				 <td align="left" style="padding-right:0px; font-family:Arial;font-size:11px;">
				 	Cash or check. please make checks payable to:<br>
				 	<cfoutput> #UCase(getTimeZone.Agency_Address1)#<br>
				       #UCase(getTimeZone.Agency_City)#<br>
				        #UCase(getTimeZone.Agency_State)# #getTimeZone.Agency_Zip#<br>
				 		 To make payment by creditcard please call<br>
				 		 #UCase(getTimeZone.Agency_Name)# at  #getTimeZone.Agency_Contact_Phone#
				 	</cfoutput>
				 </td>
			</tr>
			


	    </table>
		<cfset Session.Schid=""> 
		 <cfquery  datasource="#Application.DataSrc#" name="Update_Flags">
				UPDATE #Request.prefix_db_agency#.pSchedules SET 
				pat_Invoice_ID = #invoiceid#
				WHERE Schedule_ID  in (#Schids#)
				AND   Assmt_ID = #Getschedule.Assmt_ID#
		 </cfquery>
		 <cfquery  datasource="#Application.DataSrc#" name="Update_Flags">
				UPDATE #Request.prefix_db_agency#.pat_Invoices SET 
				InvoiceTotal = '#total_pt_row_total#',
	 			PayerBalance = '#total_pt_row_total#'
				WHERE pat_Invoice_ID  in (#invoiceid#)
		 </cfquery>
		<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>


