<cfwindow
	name="AddVisit3"
	title="New Visit - Added"
	height="500"
	width="800"
	resizable="false"
	center="true"
	closable="false"
	draggable="true"
	modal="true"
	bodystyle="background-color:white;"
	initshow="true">

	<table border="0" cellspacing="0" cellpadding="0" width="98%" align="center">
		<tr><td colspan="3" style="padding-top:5px; padding-right:2px" align="right" >
			<img src="../../images/vizard_close.png" style="cursor:pointer;" onclick="window.location='/patientadmin_new/index.cfm?page=patient&cat=Patient'" name="imgclose" id="imgclose" />
		</td></tr>
		<tr>
			<td style="height:5px">
			</td>
		</tr>
		<tr>
			<td colspan="3" width="98%" style=" background-image:url(../../images/vizard_title_bg.gif); height:13px;" >

			</td>
		</tr>
		<tr>
			<td style="height:5px">
			</td>
		</tr>
	</table>

	<cfif Not isDefined('session.AgencyId') or session.AgencyId eq ''>
		<div style='margin:20px;'>
			Your session has Timed Out.
			<br/><br/>
			Please <a href='/'>Sign In</a> again to continue.
		</div>
		<cfexit>
	</cfif>

	<cfif IsDefined("form.pid")>
		<cfif #form.pid# EQ 0>
			<h1>The information you provided in the form was incomplete or incorrect.  Please take a moment to review the following list:<br /></h1>  Patient Id Is Missing.
			<br />
			<FORM>
				<INPUT type="button" value="Go Back" onClick="history.back()">
			</FORM>
			<cfabort>
		</cfif>
	</cfif>

	<cfset form.Total_EPay = 00.00>
	<cfset form.Total_Units = 0>
	<cfset form.Total_EClaim = 0>
	<cfset form.Total_QB = 0>

	<cfparam name="form.EmpPay_ID" default="">
	<cfparam name="url.close" default=-1>

	<cfparam name="url.thedate" default=''>
	<cfparam name="form.thedate" default=''>

	<cfif NOT isDefined("form.Rev_CodeID")>
		<h2>Your Session has timed out. Please Login again.<br /></h2>
		<FORM>
			<INPUT type="button" value="Go Back" onClick="history.back()">
		</FORM>
	<cfelseif form.Rev_CodeID eq 0>
		<h1>The information you provided in the form was incomplete or incorrect.  Please take a moment to review the following list:<br /></h1>
		Revenue Code is required.
		<br />
		<FORM>
			<INPUT type="button" value="Go Back" onClick="history.back()">
		</FORM>
		<cfabort>
	<cfelse>
		<cfset Session.CS.AssessmentTrackerID=#form.Assessment_Tracker_ID# >
		<cfset Session.CS.AssessmentID=#form.Assessment_ID# >

		<cfquery  name="GetRev" datasource="#Application.DataSrc#">
			SELECT *
			FROM   #Request.prefix_db_agency#.Revenue_Code
			WHERE  Rev_CodeId = '#form.Rev_CodeId#'
			AND    Agency_Id = '#session.AgencyId#'
		</cfquery>

		<cfif GetRev.RecordCount eq 1>
			<cfif isdefined("session.rollover") and  session.rollover eq 1 and isdefined("session.Visit_Type")>
				<cfset form.visit_type = #session.Visit_Type#>
			<cfelse>
				<cfset form.visit_type = #getrev.visit_type#>
			</cfif>
		<cfelse>
			<cfset form.visit_type = #form.visit_type#>
		</cfif>

		<cfif len(trim(form.visit_type)) eq 0>
			<cfquery  name="GetVT" datasource="#Application.DataSrc#">
				SELECT Assessment_Reason_M0100 as vt
				FROM   #Request.prefix_db_agency#.Assessment_New
				WHERE  assessment_tracker_id = '#Session.CS.AssessmentTrackerID#'
				AND    assessment_id = '#Session.CS.AssessmentID#'
				AND   (Agency_ID = '#session.AgencyID#' or Agency_ID = 1)
			</cfquery>
			<cfif GetVT.recordcount><cfset form.visit_type=#getvt.vt#></cfif>
		</cfif>

		<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>

		<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>

		<cfif #form.StartTime# IS NOT "" AND #form.EndTime# IS NOT "">
			<cfif form.StartTimeAMPM eq 'AM' and LEFT(form.StartTime, 3) eq '12:'>
				<cfset form.StartTime = '00:' & Right(form.StartTime, 2) />
			</cfif>

			<cfif form.EndTimeAMPM eq 'AM' and LEFT(form.EndTime, 3) eq '12:'>
				<cfset form.EndTime = '00:' & Right(form.EndTime, 2) />
			</cfif>

			<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 isdefined('GetRev.Total_Units')>
							<cfif GetRev.Total_Units GT GetRev.Units>
								<cfset Leftover = #GetRev.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>
						</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 #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>

		<cfif form.scheduleitems_id eq 0>
			<cfset form.Record_Created_By = #Session.EmployeeID#>
			<cfset form.Record_Creation_Date = #Now()#>
		</cfif>

		<cfset form.Record_Mod_By = #Session.EmployeeID#>
		<cfset form.Record_Mod_Date = #Now()#>
		<cfset Payer_ID = #form.Payer_ID#>

		<cfset thehour = GetToken(form.StartTime,1,':')>
		<cfset theminute = GetToken(form.StartTime,2,':')>
		<cfset form.starttime = "#thehour#:#theminute#">
		<cfset thehour = GetToken(form.EndTime,1,':')>
		<cfset theminute = GetToken(form.EndTime,2,':')>
		<cfset form.endtime = "#thehour#:#theminute#">

		<cfsetting requesttimeout=50000>

		<cfquery  name="InsertSchedule" datasource="#Application.DataSrc#">
			INSERT INTO #Request.prefix_db_agency#.ScheduleItems (
				Patient_ID, Visit_Date, Employee_ID, EmpPay_ID, Record_creation_Date, Record_created_By, Record_Mod_Date, Record_Mod_By
				,Agency_ID, Visit_Type, Assessment_Tracker_ID,Assessment_ID, Progress_Note,Payer_ID, StartTime, EndTime, Rev_CodeID,
				Paperwork,Travel,Mileage,Telephony_Yes,Total_Units,Total_EClaim,Total_QB,Visit_Notes,Missed,PayMileage,Bill_Type,
				Rev_Code,HCPCS,Output_Type,Instructions,Total_Hours,Total_EPay<cfif isdefined('form.Auth_ID') and form.Auth_ID neq ''>,Auth_ID</cfif> 
				)
			VALUES
			(
				'#Session.CS.PatientID#', #CreateODBCDateTime(form.Visit_Date)#,'#form.Employee_ID#', '<cfif EmpPay_ID eq "">0<cfelse>#Form.EmpPay_ID#</cfif>',#Now()#,
				'#Session.EmployeeID#', #Now()#,'#Session.EmployeeID#','#Session.AgencyID#','#Form.Visit_Type#',
				'#Session.CS.AssessmentTrackerID#','#Session.CS.AssessmentID#','#Form.Progress_Note#','#Form.Payer_ID#',
				<cfif #Form.StartTime# eq '4:00'>{t '16:00:00'},<cfelse>#CreateODBCTime(Form.StartTime)#,</cfif> #CreateODBCTime(Form.EndTime)#, '#Form.Rev_CodeID#','#Form.Paperwork#',
				'#Form.Travel#','#Form.Mileage#','#Form.Telephony_Yes#','#form.Total_Units#','#form.Total_EClaim#','#form.Total_QB#',
				'#Form.Visit_Notes#','#Form.Missed#','#Form.PayMileage#','#Form.Bill_Type#','#Form.Rev_Code#','#form.HCPCS#',
				'#form.Output_Type#','#Form.Visit_Instructions#','#form.Total_Hours#','#form.Total_EPay#'<cfif isdefined('form.Auth_ID') and form.Auth_ID neq ''>,'#form.Auth_ID#'</cfif>)
		</cfquery>

		<cfquery  datasource="#Application.DataSrc#" name="upd">
			select MAX(scheduleitems_id) as maxid from #Request.prefix_db_agency#.ScheduleItems where agency_id = '#session.agencyid#' and employee_id = '#form.employee_id#'
		</cfquery>

		<cfif (session.IsAgencyLogin eq 1 and Session.AgencySurvey eq 1) OR (session.IsAgencyLogin eq 0 and session.eaPatientSurvey eq 1)>
			<cfif form.survotype neq 5>
				<cfset Today = DateFormat(form.Visit_Date,'mm/dd/yyyy')>
				<cfset Cvt_time = TimeFormat(Form.EndTime,'hh:mm tt')>
				<cfset Survo_Time = ''>

				<cfif form.survotype eq 1>
					<cfset Survo_Time = TimeFormat(DateAdd("n", 15, Cvt_time),'hh:mm tt')>
				<cfelseif  form.survotype eq 2>
					<cfset Survo_Time = TimeFormat(DateAdd("h", 1, Cvt_time),'hh:mm tt')>
				<cfelseif  form.survotype eq 3>
					<cfset Today = DateFormat(DateAdd("d", 1, Today),'mm/dd/yyyy')>
					<cfset Survo_Time = Cvt_time>
				<cfelseif  form.survotype eq 4>
					<cfset Today = DateFormat(DateAdd("w", 7, Today),'mm/dd/yyyy')>
					<cfset Survo_Time = Cvt_time>
				</cfif>

				<cfset cfc_VisitDate = Today>
				<cfset cfc_Hrs = TimeFormat(Survo_Time,'hh') >
				<cfset cfc_Mins = TimeFormat(Survo_Time,'mm') >
				<cfset cfc_Meridiem = TimeFormat(Survo_Time,'tt') >
				<cfset cfc_Patient_ID = Session.CS.PatientID >
				<cfset cfc_Questions =  form.resQuestions>
				<cfset cfc_QuestionsType =  form.resQuestionsType>

				<cfinvoke component="Survo" method="insScheduleCallType4" returnvariable="GetR">
					<cfinvokeargument name="VisitDate" value="#cfc_VisitDate#" >
					<cfinvokeargument name="Hrs" value="#cfc_Hrs#" >
					<cfinvokeargument name="Mins" value="#cfc_Mins#" >
					<cfinvokeargument name="Meridiem" value="#cfc_Meridiem#" >
					<cfinvokeargument name="Patient_ID" value="#cfc_Patient_ID#" >
					<cfinvokeargument name="Questions" value="#cfc_Questions#" >
					<cfinvokeargument name="QuestionsType" value="#cfc_QuestionsType#" >
					<cfinvokeargument name="SchID" value="#upd.maxid#" >
				</cfinvoke>
			</cfif>
		</cfif>

		<cfset form.scheduleitems_id = #upd.maxid#>

		<cf_telephonyid result="itemfinal" action="updateitem" employeeid="#form.employee_id#" visitdate=#form.visit_date# id=#form.scheduleitems_id#>
		<cf_telephonyid result="encodefinal" action="encode" employeeid="#form.employee_id#" visitdate=#form.visit_date# item=#itemfinal#>

		<cfset siti = form.scheduleitems_id />
		<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 = '#form.scheduleitems_id#'
		</cfquery>

		<cfif url.close eq 1>
            <cfset form.pid   = encrypt(#form.pid#,"54kczhzgHTj4a8n+SRZPyg==","AES","Hex") />
            <cfset form.assid   = encrypt(#form.assid#,"54kczhzgHTj4a8n+SRZPyg==","AES","Hex") />
            <cfset form.sid   = encrypt(#form.sid#,"54kczhzgHTj4a8n+SRZPyg==","AES","Hex") />
			<cflocation url="index.cfm?page=patient&cat=patient_editVisit&id=#form.pid#&restrict=#form.restrict#&formonly=#form.formonly#&assid=#form.assid#&sid=#form.sid#" addtoken="no">
		<cfelseif url.close eq 0>
			<script>
				<cfif findnocase('QB_compliancebyweek',form.previous)>
					<cfif FindNoCase('?', FORM.previous) GT 0>
						<cfset VARIABLES.DestinationURL = GetToken(FORM.previous, 1, '?') & '?'>
						<cfset VARIABLES.URLQuerystring = GetToken(FORM.previous, 2, '?')>

						<cfloop list="#VARIABLES.URLQuerystring#" index="thisQuerystringItem" delimiters="&">
							<cfset VARIABLES.thisVarName = GetToken(thisQuerystringItem, 1, '=')>
							<cfset VARIABLES.thisVarValue = GetToken(thisQuerystringItem, 2, '=')>

							<cfif VARIABLES.thisVarName IS "d">
							<cfelse>
								<cfset VARIABLES.DestinationURL = VARIABLES.DestinationURL & '&' & thisQuerystringItem>
							</cfif>
						</cfloop>
					<cfelse>
						<cfset VARIABLES.DestinationURL = FORM.previous>
					</cfif>

					<cfif url.thedate neq ''>
						<cfset VARIABLES.DestinationURL = VARIABLES.DestinationURL & '&d=' & url.thedate>
					<cfelseif form.thedate neq ''>
						<cfset VARIABLES.DestinationURL = VARIABLES.DestinationURL & '&d=' & form.thedate>
					</cfif>

					<cfoutput>window.location='#VARIABLES.DestinationURL#';</cfoutput>
				<cfelse>
					<cfoutput>window.location='#form.previous#';</cfoutput>
				</cfif>
			</script>
		<cfelse>
			<table border="0" cellpadding="0" cellspacing="0" width="98%">
				<tr>
					<td style="padding-left:5px">
						<h1>Visit Updated</h1>
						<br>
					</td>
				</tr>
				<tr>
					<td style="padding-left:5px">
						<cfquery  name="Treatment_Auth_Calc" datasource="#Application.DataSrc#">
							SELECT Assessment_New.Assessment_ID,
							Assessment_New.Assessment_Reason_M0100
							FROM #Request.prefix_db_agency#.Assessment_New
							WHERE Assessment_New.Assessment_ID='#session.cs.AssessmentId#'
						</cfquery>

						<cfset thereason = Treatment_Auth_Calc.Assessment_Reason_M0100/>
						<cfif thereason neq '' and  thereason contains "Start of Care">
							<cfset thereason = 'startofcarewizard' />
						<cfelseif thereason neq '' and  thereason contains "Resumption of Care">
							<cfset thereason = 'resumptionofcarewizard' />
						<cfelseif thereason neq '' and  thereason contains "Recertification">
							<cfset thereason = 'recertificationwizard' />
						<cfelseif thereason neq '' and  thereason contains "5 - Other follow-up">
							<cfset thereason = 'Other_followup' />
						<cfelseif thereason neq '' and  thereason contains "6 - Transferred to an inpatient facility - patient not discharged from agency">
							<cfset thereason = 'transfernotdischargewizard' />
						<cfelseif thereason neq '' and  thereason contains "7 - Transferred to an inpatient facility - patient discharged from agency">
							<cfset thereason = 'transferdischargewizard' />
						<cfelseif thereason neq '' and  thereason contains "death">
							<cfset thereason = 'deathwizard' />
						<cfelseif thereason neq '' and  ((thereason contains "Hospice Comprehensive") or (thereason contains  "Hospice Comprehensive"))>
							<cfset thereason = 'hospicewizard' />
						<cfelseif thereason neq '' and thereason eq "9 - Discharge from agency">
							<cfset thereason = 'dischargefromagencywizard' />
						<cfelseif thereason neq '' and  thereason contains "SN Evaluation">
							<cfset thereason = 'snevaluationwizard' />
						<cfelseif thereason neq '' and  thereason contains "Ped - Pediatric Evaluation">
							<cfset thereason = 'pediatricevaluationwizard' />
						<cfelseif thereason neq '' and  thereason contains "PT Evaluation">
							<cfset thereason = 'PTEvaluationWizard' />
						<cfelseif thereason neq '' and  thereason contains "OT Evaluation">
							<cfset thereason = 'otevaluationwizard' />

						<cfelseif thereason neq '' and  thereason contains "ST Adult Evaluation">
							<cfset thereason = 'stadultwizard' />
						<cfelseif thereason neq '' and  thereason contains "ST Pediatric Evaluation">
							<cfset thereason = 'stpediatricwizard' />
						<cfelseif thereason neq '' and  thereason contains "MSW Adult Evaluation">
							<cfset thereason = 'mswadultwizard' />
						<cfelseif thereason neq '' and  thereason contains "MSW Pediatric Evaluation">
							<cfset thereason = 'mswpedwizard' />
						<cfelseif thereason neq '' and  thereason contains "SN Adult Evaluation">
							<cfset thereason = 'snadultwizard' />
						<cfelseif thereason neq '' and  thereason contains "SN Pediatric Evaluation">
							<cfset thereason = 'snpediatricwizard' />
						<cfelseif thereason neq '' and  thereason contains "PT Pediatric Evaluation">
							<cfset thereason = 'ptpediatricwizard' />
						<cfelseif thereason neq '' and  thereason contains "PT Adult Evaluation">
							<cfset thereason = 'ptadultwizard' />
						<cfelseif thereason neq '' and  thereason contains "OT Adult Evaluation">
							<cfset thereason = 'otadultwizard' />
						<cfelseif thereason neq '' and  thereason contains "OT Pediatric Evaluation">
							<cfset thereason = 'otpediatricwizard' />
						<cfelseif thereason neq '' and  thereason contains "Psychiatric Evaluation">
							<cfset thereason = 'psychiatricevalwizard' />
						<cfelseif thereason neq '' and  thereason contains "Chaplain Comprehensive Evaluation">
							<cfset thereason = 'chaplaincompwizard' />
						<cfelseif thereason neq '' and  thereason eq "Discharge Non-OASIS">
							<cfset thereason = 'dischargenoasiswizard' />
						<cfelseif thereason neq '' and  thereason contains "Hospice SN">
							<cfset thereason = 'snhospicewizard' />
						<cfelseif thereason neq '' and  thereason contains "Other follow-up">

						<cfelseif thereason neq '' and  thereason contains "NO ASSESSMENT">

						</cfif>

						<cfoutput>
							<a href="/patientadmin_new/index.cfm?a=1&a=1&px=y&page=patient&px=y&cat=#thereason#">
								Start assessment data entry </a>
						</cfoutput>
						<br><br>
					</td>
				</tr>
				<tr>
					<td style="padding-left:5px">
						<cfoutput>
							<a href="/patientadmin_new/index.cfm?page=patient&cat=Patient">
								Edit Visits in Patient Chart</a>
						</cfoutput>
						<br><br>
					</td>
				</tr>
				<tr>
					<td style="padding-left:5px">
						<cfoutput><a href="index.cfm?view=day&mmid=3">Go to Schedules</a>
						</cfoutput>
						<br><br>
					</td>
				</tr>
				<tr>
					<td style="padding-left:5px">
						<cfoutput>
							<a href="../dashboard/index.cfm?page=dashboard&cat=&rpt=visit_compliance">Compliance for Unbilled Visits</a>
						</cfoutput>
						<br>
					</td>
				</tr>
			</table>
		</cfif>
	</cfif>
</cfwindow>