	   <cfset #Request.prefix_db_lookup# = 'hhapowerpath' />
	   <cfset #Request.prefix_db_agency# = 'agency_563' />
	   <cfset agency_id = '563' />
	   <cfquery name="getemployee" datasource="hhapowerpath_prod">
			 SELECT * FROM #Request.prefix_db_lookup#.Employee WHERE Agency_ID = #agency_id# 
			 ORDER BY Employee_ID ASC 
		</cfquery>


		<cfquery name="getemptable" datasource="#Application.DataSrc#">
				SELECT * FROM #Request.prefix_db_lookup#.pEmployee  WHERE Loc_ID = #agency_id# 
		</cfquery>
		<cfif getemptable.recordcount  EQ 0 >
			<cfloop query="getemployee">
					<cfquery name="getauth" datasource="hhapowerpath_prod">
						SELECT * FROM #Request.prefix_db_agency#.employee_auth WHERE eaID = #getemployee.Employee_ID#
					</cfquery>
					<cfquery name="getphy" datasource="hhapowerpath_prod">
							 SELECT * FROM #Request.prefix_db_lookup#.Physician WHERE Employee_ID = #getemployee.Employee_ID#
					</cfquery>
					 <cfquery name="getusername" datasource="#Application.DataSrc#">
								SELECT  * FROM #Request.prefix_db_lookup#.pEmployee WHERE   
								<cfif getemployee.Username NEQ ''> Username="#getemployee.Username#" AND </cfif>
								 Emp_First = '#getemployee.First_Name#' AND Emp_Last = '#getemployee.Last_Name#'  
								 AND Emp_Street1 = <cfqueryparam VALUE="#getemployee.Address1#"  cfsqltype="cf_sql_varchar">
					</cfquery>

				<cfif getusername.recordcount EQ 0 >
					<cftransaction>
			  			<cftry>
			  			    <cfquery name="Maxemp" datasource="#Application.DataSrc#">
									SELECT Max(Emp_ID) AS Emp_ID FROM #Request.prefix_db_lookup#.pEmployee
							</cfquery>
							<cfset Emp_ID = Maxemp.Emp_ID+1 />
							<cfset empstatus = 0>
							<cfif getemployee.Status EQ 'inactive'>
								<cfset empstatus = 1>
							</cfif>
							<cfset encryptedpassword = '' />
							<cfset usernames = '' />
							<cfif getemployee.Password NEQ ''>
								<cfset encryptedpassword = #encrypt(getemployee.Password, "WTq8zYcZfaWVvMncigHqwQ==", "AES", "Base64")# />
							</cfif>
							<cfif getemployee.Username NEQ ''>
								<cfset usernames = #getemployee.Username# />
							</cfif>
							<cfquery name="insertemployee" datasource="#Application.DataSrc#">
									INSERT INTo #Request.prefix_db_lookup#.pEmployee (
									Emp_ID, Loc_ID, Status, Emp_First, Emp_Middle, Emp_Last, Emp_Street1, Emp_Street2, Emp_City, Emp_State, Emp_Zip, Emp_Telephone, Emp_Email, Emp_DOB, Emp_Gender, Emp_Type, Emp_Latitude, Emp_Longitude, Skill_1,Skill_2,Skill_3, Username, Password, Date_Hire, Date_Terminate, Record_Creation_Date, Record_Created_By, Record_Mod_Date, Record_Mod_By)
									VALUES (
									<cfqueryparam VALUE="#Emp_ID#"  cfsqltype="cf_sql_integer">,
									<cfqueryparam VALUE="#getemployee.Agency_ID#"  cfsqltype="cf_sql_integer">,
				                    <cfqueryparam VALUE="#empstatus#"  cfsqltype="cf_sql_integer">,
				                    <cfqueryparam VALUE="#getemployee.First_Name#"  cfsqltype="cf_sql_varchar">,
				                    <cfqueryparam VALUE=""  cfsqltype="cf_sql_varchar">,
				                    <cfqueryparam VALUE="#getemployee.Last_Name#"  cfsqltype="cf_sql_varchar">,
				                    <cfqueryparam VALUE="#getemployee.Address1#"  cfsqltype="cf_sql_varchar">,
				                    <cfqueryparam VALUE="#getemployee.Address2#"  cfsqltype="cf_sql_varchar">,
				                    <cfqueryparam VALUE="#getemployee.City#"  cfsqltype="cf_sql_varchar">,
				                    <cfqueryparam VALUE="#getemployee.State#"  cfsqltype="cf_sql_varchar">,
				                    <cfqueryparam VALUE="#getemployee.Zip#"  cfsqltype="cf_sql_varchar">,
				                    <cfqueryparam VALUE="#getemployee.Home_Phone#"  cfsqltype="cf_sql_varchar">,
				                    <cfqueryparam VALUE="#getemployee.Email#"  cfsqltype="cf_sql_varchar">,
				                    <cfif getemployee.Birthdate NEQ ''>
				                    <cfqueryparam VALUE="#getemployee.Birthdate#"  cfsqltype="cf_sql_date">
				                    <cfelse> NULL </cfif>,
				                    <cfqueryparam VALUE="#getemployee.Gender#"  cfsqltype="cf_sql_varchar">,
				                    <cfqueryparam VALUE="#getemployee.EmployeeType#"  cfsqltype="cf_sql_varchar">,
				                    <cfqueryparam VALUE="#getemployee.Latitude#"  cfsqltype="cf_sql_varchar">,
				                    <cfqueryparam VALUE="#getemployee.Longitude#"  cfsqltype="cf_sql_varchar">,
				                    <cfqueryparam VALUE="#getemployee.Skill#"  cfsqltype="cf_sql_varchar">,
				                    <cfqueryparam VALUE="#getemployee.Skill2#"  cfsqltype="cf_sql_varchar">,
				                    <cfqueryparam VALUE="#getemployee.Skill3#"  cfsqltype="cf_sql_varchar">,
				                    <cfif usernames NEQ '' >		                     
				                    <cfqueryparam VALUE="#usernames#"  cfsqltype="cf_sql_varchar">
				                    <cfelse>NULL </cfif>,
				                    <cfif encryptedpassword  NEQ ''>
				                    	<cfqueryparam VALUE="#encryptedpassword#"  cfsqltype="cf_sql_varchar">
				                    <cfelse> NULL </cfif>,
				                    <cfif getemployee.HireDate NEQ ''>
				                    <cfqueryparam VALUE="#getemployee.HireDate#"  cfsqltype="cf_sql_date">
				                    <cfelse>NULL</cfif>,
				                    <cfif getemployee.Terminatdate NEQ ''>
				                    <cfqueryparam VALUE="#getemployee.Terminatdate#"  cfsqltype="cf_sql_date">
				                    <cfelse>NULL
				                    </cfif>,
				                    <cfqueryparam value="#now()#" cfsqltype="cf_sql_timestamp">,
				                    <cfqueryparam value = "2" CFSQLType ="CF_SQL_INTEGER">,
				                    <cfqueryparam value="#now()#" cfsqltype="cf_sql_timestamp">,
				                    <cfqueryparam value = "2" CFSQLType ="CF_SQL_INTEGER">)
							</cfquery>	
							<cfif getauth.recordcount gt 0 >				
								<cfquery name="insertemployeeauth" datasource="#Application.DataSrc#">
								INSERT INTO #Request.prefix_db_agency#.employee_auth(eaID, eaAuthority, eaSetup, eaSetSchedule, eaSetCareplanItems, eaSetCommSubjects, eaSetEmployees, eaSetIncidentSubjects, eaSetPayers, eaSetPayrollCodes, eaSetPhysicians, eaSetReferralSrcs, eaSetRevenueCodes, eaSetSupplies, eaSetAgyInfoUpdate, eaPayroll, eaBilling, eaOrders, eaAuthorizations, eaDataTransmit, eaType, eaCareplan, eaLock, eaSchedule, eaClientEdit, eaNoteEdit, eaUndo, eaReports, eaStatistics, eaRole, eaPatientSurvey, eaFileEdit, eaonlinechat)
								VALUES (
									<cfqueryparam value = "#Emp_ID#" CFSQLType ="CF_SQL_INTEGER">,
									<cfqueryparam value = "#getauth.eaAuthority#" CFSQLType ="CF_SQL_INTEGER">,
									<cfqueryparam value = "#getauth.eaSetup#" CFSQLType ="CF_SQL_INTEGER">,
									<cfqueryparam value = "#getauth.eaSetSchedule#" CFSQLType ="CF_SQL_INTEGER">,
									<cfqueryparam value = "#getauth.eaSetCareplanItems#" CFSQLType ="CF_SQL_INTEGER">,
									<cfqueryparam value = "#getauth.eaSetCommSubjects#" CFSQLType ="CF_SQL_INTEGER">,
									<cfqueryparam value = "#getauth.eaSetEmployees#" CFSQLType ="CF_SQL_INTEGER">,
									<cfqueryparam value = "#getauth.eaSetIncidentSubjects#" CFSQLType ="CF_SQL_INTEGER">,
									<cfqueryparam value = "#getauth.eaSetPayers#" CFSQLType ="CF_SQL_INTEGER">,
									<cfqueryparam value = "#getauth.eaSetPayrollCodes#" CFSQLType ="CF_SQL_INTEGER">,
									<cfqueryparam value = "#getauth.eaSetPhysicians#" CFSQLType ="CF_SQL_INTEGER">,
									<cfqueryparam value = "#getauth.eaSetReferralSrcs#" CFSQLType ="CF_SQL_INTEGER">,
									<cfqueryparam value = "#getauth.eaSetRevenueCodes#" CFSQLType ="CF_SQL_INTEGER">,
									<cfqueryparam value = "#getauth.eaSetSupplies#" CFSQLType ="CF_SQL_INTEGER">,
									<cfqueryparam value = "#getauth.eaSetAgyInfoUpdate#" CFSQLType ="CF_SQL_INTEGER">,
									<cfqueryparam value = "#getauth.eaPayroll#" CFSQLType ="CF_SQL_INTEGER">,
									<cfqueryparam value = "#getauth.eaBilling#" CFSQLType ="CF_SQL_INTEGER">,
									<cfqueryparam value = "#getauth.eaOrders#" CFSQLType ="CF_SQL_INTEGER">,
									<cfqueryparam value = "#getauth.eaAuthorizations#" CFSQLType ="CF_SQL_INTEGER">,
									<cfqueryparam value = "#getauth.eaDataTransmit#" CFSQLType ="CF_SQL_INTEGER">,
									<cfqueryparam value = "#getauth.eaType#" CFSQLType ="CF_SQL_INTEGER">,
									<cfqueryparam value = "#getauth.eaCareplan#" CFSQLType ="CF_SQL_INTEGER">,
									<cfqueryparam value = "#getauth.eaLock#" CFSQLType ="CF_SQL_INTEGER">,
									<cfqueryparam value = "#getauth.eaSchedule#" CFSQLType ="CF_SQL_INTEGER">,
									<cfqueryparam value = "#getauth.eaClientEdit#" CFSQLType ="CF_SQL_INTEGER">,
									<cfqueryparam value = "#getauth.eaNoteEdit#" CFSQLType ="CF_SQL_INTEGER">,
									<cfqueryparam value = "#getauth.eaUndo#" CFSQLType ="CF_SQL_INTEGER">,
									<cfqueryparam value = "#getauth.eaReports#" CFSQLType ="CF_SQL_INTEGER">,
									<cfqueryparam value = "#getauth.eaStatistics#" CFSQLType ="CF_SQL_INTEGER">,
									<cfqueryparam value = "#getauth.eaRole#" CFSQLType ="CF_SQL_VARCHAR">,
									<cfqueryparam value = "#getauth.eaPatientSurvey#" CFSQLType ="CF_SQL_INTEGER">,
									<cfqueryparam value = "#getauth.eaFileEdit#" CFSQLType ="CF_SQL_INTEGER">,
									<cfqueryparam value = "#getauth.eaonlinechat#" CFSQLType ="CF_SQL_INTEGER">
								)
								</cfquery>	
							<cfelse>
								<cfquery name="insertemployeeauth" datasource="#Application.DataSrc#">
								INSERT INTO #Request.prefix_db_agency#.employee_auth(eaID, eaAuthority)
								VALUES (
									<cfqueryparam value = "#Emp_ID#" CFSQLType ="CF_SQL_INTEGER">,
									<cfqueryparam value = "0" CFSQLType ="CF_SQL_INTEGER">
									
								)
								</cfquery>
							</cfif>				
							<cfif getphy.recordcount gt 0 >
								<cfquery name="insertephy" datasource="#Application.DataSrc#">
								INSERT INTO #Request.prefix_db_lookup#.pPhysicians(Loc_ID, Emp_ID, Status, Username, Password, Phys_First, Phys_Last, Phys_Street, Phys_City, Phys_State, Phys_Zip, Phys_Phone, Phys_Email, Phys_NPI, Phys_Fax, Record_Creation_Date, Record_Created_By, Record_Mod_Date, Record_Mod_By)
								VALUES (
									<cfqueryparam VALUE="#getemployee.Agency_ID#"  cfsqltype="cf_sql_integer">,
									<cfqueryparam value = "#Emp_ID#" CFSQLType ="CF_SQL_INTEGER">,
									<cfqueryparam value = "#empstatus#" CFSQLType ="CF_SQL_INTEGER">,
									<cfif usernames NEQ '' >		                     
				                    <cfqueryparam VALUE="#usernames#"  cfsqltype="cf_sql_varchar">
				                    <cfelse>NULL </cfif>,
				                    <cfif encryptedpassword  NEQ ''>
				                    	<cfqueryparam VALUE="#encryptedpassword#"  cfsqltype="cf_sql_varchar">
				                    <cfelse> NULL </cfif>,
									<cfqueryparam VALUE="#getemployee.First_Name#"  cfsqltype="cf_sql_varchar">,
				                    <cfqueryparam VALUE="#getemployee.Last_Name#"  cfsqltype="cf_sql_varchar">,
				                    <cfqueryparam VALUE="#getemployee.Address1#"  cfsqltype="cf_sql_varchar">,
				                    <cfqueryparam VALUE="#getemployee.City#"  cfsqltype="cf_sql_varchar">,
				                    <cfqueryparam VALUE="#getemployee.State#"  cfsqltype="cf_sql_varchar">,
				                    <cfqueryparam VALUE="#getemployee.Zip#"  cfsqltype="cf_sql_varchar">,
				                    <cfqueryparam VALUE="#getemployee.Home_Phone#"  cfsqltype="cf_sql_varchar">,
				                    <cfqueryparam VALUE="#getemployee.Email#"  cfsqltype="cf_sql_varchar">,
				                    <cfif getphy.Natl_Provider_ID NEQ '' AND getemployee.Employee_ID  NEQ '19917'>
				                    <cfqueryparam VALUE="#getphy.Natl_Provider_ID#"  cfsqltype="cf_sql_varchar">
				                    <cfelse>NULL
				                    </cfif>,
				                    <cfqueryparam VALUE="#getphy.Fax#"  cfsqltype="cf_sql_varchar">,
				                    <cfqueryparam value="#now()#" cfsqltype="cf_sql_timestamp">,
				                    <cfqueryparam value = "2" CFSQLType ="CF_SQL_INTEGER">,
				                    <cfqueryparam value="#now()#" cfsqltype="cf_sql_timestamp">,
				                    <cfqueryparam value = "2" CFSQLType ="CF_SQL_INTEGER">
								)
							</cfquery>
							</cfif>
							<cftransaction action="commit" /> 
						   <cfcatch>
					         <cftransaction action="rollback"><cfdump var="#cfcatch#"><cfabort>
						  </cfcatch>
						</cftry>
				   </cftransaction>
			    </cfif>
				
			</cfloop>
		<cfelse>
			<h3>Some of the employee already existing  </h3>
		</cfif>