
<!---<cfoutput>id=#Form.EmpWorkSch_ID#</cfoutput><br />
<cfoutput>emp=#Form.hdnempdata#</cfoutput><br />
<cfoutput>tracker=#Form.ATID#</cfoutput><br />
<cfabort>--->
<!---<cfif isdefined("Form.EmpWorkSch_ID")>
	<cfset tag_source = Form.EmpWorkSch_ID>
	
	<cfloop list="#tag_source#"  index="i" delimiters=",">
	<cfquery  name="updatetype" datasource="#Application.DataSrc#">
	UPDATE #Request.prefix_db_agency#.Employee_Workschedule SET  ScheduleType = 'Assigned' where EmployeeWorkschedule_ID = '#i#'
	</cfquery> 
	</cfloop>
</cfif>--->

<!---<cfoutput>emp=#Form.hdnempdata#</cfoutput><br /><cfabort>--->

<cfset ErrorFlag = ""> 
<cfif Form.hdnempdata IS ""> 
	<cfset ErrorFlag = "No Dates"> 
<cfelse> 
	<cfif isdefined('Session.Patient.Type')  and Session.Patient.Type eq "assement">
		<cfloop index="x" list="#Form.hdnempdata#" delimiters="*">
			<cfset increment="0">
			<cfset edate="0">
			<cfset eid="0">
			<cfset authid ="">
			<cfloop index="y" list="#x#" delimiters=",">	
				<cfset increment=increment+1>
				<cfif #increment# eq 1>
					<cfset edate=#y#>
						<cfif edate contains "|">
							<cfset inc="0">
							<cfloop index="z" list="#y#" delimiters="|">
								<cfset inc =inc +1>
								<cfif inc eq 1>
									<cfset edate =#z#>
								</cfif>
								<cfif inc eq 2>
									<cfset authid=#z#>
								</cfif>
							</cfloop>
						</cfif>							
				</cfif>
				<cfif #increment# eq 2>
					<cfset eid =#y# >
				</cfif> 
			</cfloop>
			<cfoutput>hdnval1=#Form.hdnempdata# & edate=#edate# & authid=#authid#</cfoutput><br />
			<cfif DateCompare(edate, Form.Certification_Start) IS -1 OR DateCompare(edate, Form.Certification_End) IS 1>
				<cfset ErrorFlag = "Out of Range"> 
			</cfif>
			<!---<cfquery  name="getempid" datasource="#Application.DataSrc#">
					select employee_id from #Request.prefix_db_agency#.Employee_Workschedule where EmployeeWorkschedule_ID =#eid#;
				</cfquery>
				<cfset eid = getempid.employee_id>--->
		</cfloop>
</cfif>

<cfset form.Total_EPay = 00.00>
<cfset form.Total_Units = 0>
<cfset form.Total_EClaim = 0>
<cfset form.Total_QB = 0>

<cfoutput>


<cfloop index="x" list="#Form.hdnempdata#" delimiters="*">
	<cfset increment="0">
	<cfset edate="0">
	<cfset eid="0">
	<cfset authid ="">
	<cfloop index="y" list="#x#" delimiters=",">	
		<cfset increment=increment+1>
			<cfif #increment# eq 1>
				<cfset edate=#y#>
					<cfif edate contains "|">
								<cfset inc="0">
								<cfloop index="z" list="#y#" delimiters="|">
									<cfset inc =inc +1>
									<cfif inc eq 1>
										<cfset edate =#z#>
									</cfif>
									<cfif inc eq 2>
										<cfset authid=#z#>
									</cfif>
								</cfloop>
					</cfif>					
			</cfif>
			<cfif #increment# eq 2>
			<cfset eid=#y#>
			</cfif> 
	</cfloop>
			<!---<cfquery  name="getempid" datasource="#Application.DataSrc#">
			select employee_id from #Request.prefix_db_agency#.Employee_Workschedule where EmployeeWorkschedule_ID =#eid#;
			</cfquery>
			<cfset eid = getempid.employee_id>--->
 
	 <cfif eid neq ''>
	
	  <cfif ErrorFlag neq "Out of Range"> <!--- JDB: Do not insert ScheduleItems if Dates are Out of Range --->
      <!--- Get Rev_Code information ---> 
	  
	  
		<cfset RevenueCode = Form.RevenueCode>
			
			<cfset Rev_CodeID = GetToken(RevenueCode,3,'|')>
		
		<cfset Rev_CodeID= Trim(Rev_CodeID)>
		
      <cfquery  datasource="#Application.DataSrc#" name="GetRev">
       SELECT Revenue_Code.Rev_CodeID, Revenue_Code.Visit_Type, Revenue_Code.Rev_Code, Revenue_Code.HCPCS, Revenue_Code.EClaim_Price, Revenue_Code.Quick_Books_Price, Revenue_Code.Measure, Revenue_Code.Description, Revenue_Code.Payer_ID, Revenue_Code.Tier, Revenue_Code.Units, Revenue_Code.Balance, Payer.Payer_ID, Payer.Output_Type FROM #Request.prefix_db_agency#.Revenue_Code, #Request.prefix_db_agency#.Payer WHERE 
	   Revenue_Code.Rev_CodeID = '#Rev_CodeID#' And 
	   Payer.Payer_ID = Revenue_Code.Payer_ID Order by Revenue_Code.Visit_Type ASC
      </cfquery> 
	  
	  

      <!--- Assign Rev_Code info to Form variables ---> 
      <cfset Form.Visit_Type = GetRev.Visit_Type> 
      <cfset Form.Rev_Code = GetRev.Rev_Code> 
      <cfset Form.HCPCS = GetRev.HCPCS> 
	  <cfset Form.Rev_CodeID = GetRev.Rev_CodeID> 
	  <cfset Rev_Code = Trim(Form.Rev_Code)>
	  
	 <!--- <cfquery  name="GetAuth" datasource="#Application.DataSrc#">
			SELECT  Auth_ID  FROM #Request.prefix_db_agency#.authorizations WHERE  Assessment_ID = #session.cs.AssessmentId# and Revenue_Code ='#Rev_Code#' 
		</cfquery>--->
	  <cfset Form.Bill_Type = GetRev.Description> 
      <cfset Temp_EClaim_Price = GetRev.EClaim_Price> 
      <cfset Temp_Quick_Books_Price = GetRev.Quick_Books_Price> 
      <cfset Temp_Measure = GetRev.Measure> 
      <cfset Form.EClaimed = 0> 
      <cfif Form.Visit_Type IS 'Assessment' AND GetRev.Output_Type IS 'Medicare'> 
       <cfset Form.Output_Type = 'RAP'> 
       <cfelse> 
       <cfset Form.Output_Type = GetRev.Output_Type> 
      </cfif> 
	  
	  <cfquery  name="GetOutput" datasource="#Application.DataSrc#">
	 SELECT Output_Type FROM #Request.prefix_db_agency#.Payer WHERE Agency_ID = #session.AgencyID# AND Payer_ID = '#Form.Payer_ID#'
     </cfquery>
	  
      <!--- Calculate the Units, Total_EClaim and Total_QB values based on items selected ---> 
      <!--- Determine number of minutes (n) apart ---> 
<cfif #form.StartTime# IS NOT "" AND #form.EndTime# IS NOT "">	
	<cfset diff = #DateDiff("n",Form.StartTime,Form.EndTime)#>	
	<cfset Form.Total_Units = (#diff#/60)*100/25>
	<cfif Form.Total_Units lt 0>
		<cfset Form.Total_Units=Form.Total_Units+96>
	</cfif>
	<cfset Form.Total_Hours = (#diff#/15)*25/100>
	<cfif Form.Total_Hours lt 0>
		<cfset Form.Total_Hours = Form.Total_Hours + 24 />
	</cfif>
	<cfif Form.Rev_CodeID NEQ '' or Form.Rev_CodeID NEQ 0>
		<cfif #GetRev.Measure# IS "1">
			<cfset Form.Total_EClaim = #Total_Hours# * #GetRev.EClaim_Price#>
			<cfset Form.Total_QB = #Total_Hours# * #GetRev.Quick_Books_Price#>
		</cfif>
		<cfif #GetRev.Measure# IS ".25">
        	<cfif #GetRev.Tier# IS 1>
	  			<cfif form.Total_Units GT #GetRev.Units#>
          			<cfset Leftover = form.Total_Units - #GetRev.Units#>
          			<cfset LeftoverCharge = Leftover*#GetRev.Balance#>
              			<cfif LeftoverCharge GT 0>
                  			<cfset form.Total_EClaim = LeftoverCharge + #GetRev.EClaim_Price#>
              			<cfelse>
                  			<cfset form.Total_EClaim =#GetRev.EClaim_Price#>
              			</cfif>
      			<cfelse>
          			<cfset form.Total_EClaim =#GetRev.EClaim_Price#>
      			</cfif>
  			<cfelse>
   				<cfset form.Total_EClaim = #Total_Units# * #GetRev.EClaim_Price#>
   				<cfset form.Total_QB = #Total_Units# * #GetRev.Quick_Books_Price#>
  			</cfif>
        </cfif>
		<cfif #GetRev.Measure# IS "visit">
			<cfset Form.Total_EClaim = 1 * #GetRev.EClaim_Price#>
			<cfset Form.Total_QB = 1 * #GetRev.Quick_Books_Price#>
		</cfif>
	</cfif>
	<cfif isDefined("Form.EmpPay_ID")>
	<cfquery  name="GetEPay" datasource="#Application.DataSrc#">
		SELECT Pay_Rate, Measure 
		FROM   #Request.prefix_db_agency#.Employee_Payroll 
		WHERE  Agency_ID = '#session.AgencyID#' 
		AND    EmpPay_ID = '#Form.EmpPay_ID#'
	</cfquery>
	</cfif>
	<cfif #Form.EmpPay_ID# IS NOT 0>
		<cfif #GetEPay.Measure# IS "visit">
			<cfset Form.Total_EPay = 1 * #GetEPay.Pay_Rate#>
		</cfif>
		<cfif #GetEPay.Measure# IS "hourly">
			<cfset Form.Total_EPay = #Total_Hours# * #GetEPay.Pay_Rate#>
		</cfif>
		<cfif #GetEPay.Measure# IS "salary">
			<cfset Form.Total_EPay = 00.00>
		</cfif>
	</cfif>
<cfelse>
	<cfset Form.Total_EPay = 00.00>
	<cfset Form.Total_Units = 0>
	<cfset Form.Total_EClaim = 0>
	<cfset Form.Total_QB = 0>
</cfif>
			
		
<cfif #GetOutput.Output_Type# IS "Medicare">
	<cfif #Form.Visit_Type# IS '1 - Start of care - further visits planned' OR #Form.Visit_Type# IS '4 - Recertification (follow-up) reassessment'  OR #Form.Visit_Type# IS 'Assessment' >
		<cfset Form.Output_Type = 'RAP'>
	<cfelse>
		<cfset Form.Output_Type = #GetOutput.Output_Type#>
	</cfif>
<cfelse>
	<cfset Form.Output_Type = #GetOutput.Output_Type#>
</cfif>

<cfif #GetRev.Rev_CodeID# IS NOT "">
	<cfset Form.Rev_Code = #GetRev.Rev_Code#>
	<cfset Form.HCPCS = #GetRev.HCPCS#>
	<cfset Form.Bill_Type = #GetRev.Description#>
</cfif>

<cfif #Form.Paperwork# IS NOT "">
	<cfset Paperwork = (#Form.Paperwork#/15)*25/100>
	<cfset Form.Paperwork = Paperwork>
<cfelse>
	<cfset Paperwork = 0>
	<cfset Form.Paperwork = Paperwork>
</cfif>
<cfif #form.Travel# IS NOT "">
	<cfset Travel = (#Form.Travel#/15)*25/100>
	<cfset Form.Travel = Travel>
<cfelse>
	<cfset Travel = 0>
	<cfset Form.Travel = Travel>
</cfif>

<cfif #parameterexists(Form.PayMileage)# >
	<cfset Form.PayMileage = #Form.paymileage#>
<cfelse>
	<cfset Form.PayMileage = 0>
</cfif>

	<cfquery  name="PatientShedule" datasource="#Application.DataSrc#">	
	<cfif eid neq ''>
	<cfsetting requesttimeout="5000">
	   INSERT INTO #Request.prefix_db_agency#.ScheduleItems( <cfif isdefined("Session.Patient.PatientIDValue")>Patient_ID,</cfif> Visit_Date, Employee_ID, EmpPay_ID, <cfif authid neq ''>Auth_ID,</cfif>Record_creation_Date, 
						Record_created_By, Record_Mod_Date, Record_Mod_By, Agency_ID, Visit_Type, <cfif Form.ATID neq ''>Assessment_Tracker_ID,</cfif>
						Assessment_ID, Progress_Note, EClaimed, Rev_Code, HCPCS, Payer_ID, StartTime, EndTime, Rev_CodeID, Total_Units,
						Total_EClaim, Total_QB, Bill_Type, Paperwork, Travel, PayMileage, Output_Type, <cfif isdefined("Form.Telephony_Yes")>Telephony_Yes,</cfif>
						Instructions, Total_EPay, Total_Hours
					  ) 
					  VALUES (
					  <cfif isdefined("Session.Patient.PatientIDValue")>
							#Session.Patient.PatientIDValue#, 
							</cfif>
							#CreateODBCDateTime(edate)#,
							'#eid#',
							'#Form.EmpPay_ID#',
							<cfif authid neq ''>#authid#,</cfif> 
							#Now()#,
							#Session.EmployeeID#, 
							#Now()#, 
							#Session.EmployeeID#,
							'#Session.AgencyID#',
							'#Form.typeofvisit#',
							<cfif Form.ATID neq ''>#Form.ATID#,</cfif>			             
							'#Form.AID#',
							'No',
							'0',
							'#Form.Rev_Code#',
							'#Form.HCPCS#',
							'#Form.payerid#',
							#CreateODBCTime(Form.StartTime)#, 
							#CreateODBCTime(Form.EndTime)#, 
							#Rev_CodeID#, 
							#Form.Total_Units#,
							#Form.Total_EClaim#,
							#Form.Total_QB#,
							'#Form.Bill_Type#',
							'#Form.Paperwork#', 
							'#form.Travel#', 
							'#Form.paymileage#',
							'#Form.Output_Type#',
							<cfif isdefined("Form.Telephony_Yes")>'#Form.Telephony_Yes#',</cfif>
							'#Form.Instructions#',
							#Total_EPay#,
							#Total_Hours#
							 )
	</cfif>
	</cfquery>
	

	 <cfquery  name="NewVisit" datasource="#Application.DataSrc#">
          <!--- SELECT MAX(ScheduleItems_ID) as maxid from #Request.prefix_db_agency#.ScheduleItems --->
          SELECT MAX(ScheduleItems_ID) AS maxId from #Request.prefix_db_agency#.ScheduleItems
     </cfquery>	 
	 
	 <cfif authid neq ''>
	 	<cfquery  name="Authidcount" datasource="#Application.DataSrc#">			
			select count(*) as count from #Request.prefix_db_agency#.authorizations_details where Auth_ID = #authid#
		</cfquery>
		
		<cfif Authidcount.count neq 0 and Authidcount.count neq ''>			
			<cfquery  name="GetAuth_Details_ID" datasource="#Application.DataSrc#">
				select Auth_Details_ID  from #Request.prefix_db_agency#.authorizations_details where Auth_ID=#authid# and ScheduleItems_ID is null order by Auth_Details_ID asc limit 1 
			</cfquery>
			<!---<cfif GetAuth_Details_ID.recordcount gt 0>--->
				
				<cfquery  name="Updateschid" datasource="#Application.DataSrc#">
					update #Request.prefix_db_agency#.authorizations_details SET ScheduleItems_ID= '#NewVisit.maxId#' where Auth_Details_ID='#GetAuth_Details_ID.Auth_Details_ID#' and Auth_ID=#authid# and ScheduleItems_ID is null
				</cfquery>
				
			<!---<cfelse>
				<script language="javascript" type="text/javascript">				
					window.onload = function()
					{
						alert('Authorized scheduled has complete you cannot add !');
						window.location.href="../schedules/index.cfm?page=schedules&cat=&rpt=&view=month";												
					}
				</script>
			</cfif>--->
		</cfif>
		
	 </cfif>
	
       <cfif eid neq ''>
	   
	   
       <!--- <cf_telephonyid result="theitem" action="updateitem" employeeid="#eid#" visitdate=#edate# id=#NewVisit.maxId#> 
		<cf_telephonyid result="encodefinal" action="encode" employeeid="#eid#" visitdate=#edate# item=#theitem#> 
		--->
		
		<cfset siti = NewVisit.maxId />
 <cfif LEN(siti) eq 7>
  <cfset siti = '000' & siti />
 <cfelseif LEN(siti) eq 8>
  <cfset siti = '00' & siti />
 <cfelseif LEN(siti) eq 9>
  <cfset siti = '0' & siti />
 </cfif>
		
        <cfquery  datasource="#Application.DataSrc#" name="upd">
          update #Request.prefix_db_agency#.ScheduleItems set telephony_id = '#siti#' where scheduleitems_id = #NewVisit.maxId#
         </cfquery> 
	  </cfif> 
	
	
	</cfif>
	
	</cfif>
			
</cfloop>
</cfoutput>

<cflocation url="index.cfm?page=schedules&cat=&rpt=schedule_complete_page" addtoken="no">

</cfif>
	
<cfif ErrorFlag IS ""> 
   <!-- <cflocation url="index.cfm?view=day"> --->
    <cfelse> 
    <cfswitch expression="#ErrorFlag#"> 
      <cfcase value="No Dates"> 
      <h3>Scheduling Problem</h3> 
      <p><strong>You must select at least one date to schedule.</strong></p> 
      <p><strong>Please click the Back button in your browser to correct the problem.</strong></p> 
      </cfcase> 
      <cfcase value="Too Many"> 
      <h3>Scheduling Problem</h3> 
      <p><strong>For an Admission Visit, you can only specify one visit date.</strong></p> 
      <p><strong>Please click the Back button in your browser to correct the problem.</strong></p> 
      </cfcase> 
      <cfcase value="Out of Range"> 
      <h3>Scheduling Problem</h3> 
      <p><strong>One of the dates you specified is outside the Certification Period.</strong></p> 
      <p><strong>Please click the Back button in your browser to correct the problem.</strong></p> 
      </cfcase> 
    </cfswitch> 
 </cfif>