<!---
This file is Copyright © 1998-2009 by Home Care Business Services. This work may not be reproduced, in whole or in part, 
using any medium, including, but not limited to, electronic transmission, CD-ROM or published in print, without the express 
permission of Home Care Business Services.

$Revision: 1.2 $
$Id: cleanuparchives.cfm,v 1.2 2009/02/16 20:14:49 arlen Exp $
$Date: 2009/02/16 20:14:49 $
--->
<cfsetting RequestTimeout = "5000"> 

<!--- fix fields before restore --->
<cfquery   name="patients" datasource="#Application.DataSrc#">
	UPDATE #Request.prefix_db_lookup#.archive_pathway_details set Created_By = 0 where Created_by IS NULL
</cfquery>

<cfquery   name="patients" datasource="#Application.DataSrc#">
	UPDATE #Request.prefix_db_lookup#.archive_pathway_details set assessment_tracker_id=0 where assessment_tracker_id is null;
</cfquery>

<!--- we need to unarchive everything. we first removed the already unarchived records 1--->

<cfquery   name="patients" datasource="#Application.DataSrc#">
select DISTINCT(a.Patient_ID) as id from #Request.prefix_db_lookup#.archive_assessments a where unarchive=1 order by Patient_ID;
</cfquery>

<cfset worktodo=ArrayNew(1)>
<cfloop  query=patients>
	<cfset status=ArrayAppend(worktodo,"#id#:#id#")>
</cfloop>

<!--- Work through each patient and each assessment ariving the table entries for each important table. --->

<cfloop  index=ind from=1 to=#ArrayLen(worktodo)#>
	<cfset pid=#gettoken(worktodo[ind],1,":")#>
	<cfset aid=#gettoken(worktodo[ind],2,":")#>

	<cfquery  name="InsertArchived" datasource="#Application.DataSrc#">
	DELETE FROM #Request.prefix_db_lookup#.archive_assessments
	WHERE Patient_ID = #pid#
	</cfquery>

	<!---Assessment Forms Data--->
	<cfquery  name="InsertArchived" datasource="#Application.DataSrc#">
	DELETE FROM  #Request.prefix_db_lookup#.archive_assessment_forms
	WHERE Patient_ID = #pid#
	</cfquery>

	<!---Assessment Tracker Data --->
	<cfquery  name="InsertArchived" datasource="#Application.DataSrc#">
	DELETE FROM #Request.prefix_db_lookup#.archive_assessment_tracker
	WHERE Patient_ID = #pid#
	</cfquery>
	
	<!---Diagnosis data--->
	<cfquery  name="InsertArchived" datasource="#Application.DataSrc#">
	DELETE FROM #Request.prefix_db_lookup#.archive_diagnosis
	WHERE Patient_ID = #pid#
	</cfquery>

	<!--- Goals Data --->
	<cfquery  name="InsertArchived" datasource="#Application.DataSrc#">
	DELETE FROM #Request.prefix_db_lookup#.archive_goals
	WHERE Patient_ID = #pid#
	</cfquery>

	<!--- Pathway Details Data --->
	<cfquery  name="InsertArchived" datasource="#Application.DataSrc#">
	DELETE FROM #Request.prefix_db_lookup#.archive_pathway_details
	WHERE Patient_ID = #pid#
	</cfquery>

	<!--- Patient Visit Details Data --->
	<cfquery  name="InsertArchived" datasource="#Application.DataSrc#">
	DELETE FROM #Request.prefix_db_lookup#.archive_patientvisitdetails
	WHERE Patient_ID = #pid#
	</cfquery>


<cfoutput>Deleted #pid#<br></cfoutput>
</cfloop>


<!--- Now lets unarchive the archive ones --->

<cfquery   name="patients" datasource="#Application.DataSrc#">
select DISTINCT(a.Patient_ID) as id from #Request.prefix_db_lookup#.archive_assessments a where unarchive=0 order by Patient_ID;
</cfquery>
<cfset worktodo=ArrayNew(1)>
<cfloop  query=patients>
	<cfset status=ArrayAppend(worktodo,"#id#:#id#")>
</cfloop>

<!--- Work through each patient and each assessment ariving the table entries for each important table. --->

<cfloop  index=ind from=1 to=#ArrayLen(worktodo)#>
	<cfset pid=#gettoken(worktodo[ind],1,":")#>
	<cfset aid=#gettoken(worktodo[ind],2,":")#>

	<cfif #pid# neq "">
		<cfset url.id = #pid#>
		<cfoutput>Unarchiving #pid#<br></cfoutput>
		<cfinclude template="unarchive.cfm">
	</cfif>
</cfloop>


<!--- We remove the table entries for deleted patients --->

<cfquery   name="deletedpatients" datasource="#Application.DataSrc#">
	select DISTINCT(p.Patient_ID) as pid from #Request.prefix_db_agency#.patient p where p.Status = 'deleted' order by Patient_ID
</cfquery>

<cfloop query=deletedpatients>
	<cfoutput>Deleting deleted patient #pid#<br></cfoutput>
	<cfquery  name="InsertArchived" datasource="#Application.DataSrc#">
	DELETE FROM #Request.prefix_db_lookup#.archive_assessments
	WHERE Patient_ID = #pid#
	</cfquery>

	<!---Assessment Forms Data--->
	<cfquery  name="InsertArchived" datasource="#Application.DataSrc#">
	DELETE FROM  #Request.prefix_db_lookup#.archive_assessment_forms
	WHERE Patient_ID = #pid#
	</cfquery>

	<!---Assessment Tracker Data --->
	<cfquery  name="InsertArchived" datasource="#Application.DataSrc#">
	DELETE FROM #Request.prefix_db_lookup#.archive_assessment_tracker
	WHERE Patient_ID = #pid#
	</cfquery>
	
	<!---Diagnosis data--->
	<cfquery  name="InsertArchived" datasource="#Application.DataSrc#">
	DELETE FROM #Request.prefix_db_lookup#.archive_diagnosis
	WHERE Patient_ID = #pid#
	</cfquery>

	<!--- Goals Data --->
	<cfquery  name="InsertArchived" datasource="#Application.DataSrc#">
	DELETE FROM #Request.prefix_db_lookup#.archive_goals
	WHERE Patient_ID = #pid#
	</cfquery>

	<!--- Pathway Details Data --->
	<cfquery  name="InsertArchived" datasource="#Application.DataSrc#">
	DELETE FROM #Request.prefix_db_lookup#.archive_pathway_details
	WHERE Patient_ID = #pid#
	</cfquery>

	<!--- Patient Visit Details Data --->
	<cfquery  name="InsertArchived" datasource="#Application.DataSrc#">
	DELETE FROM #Request.prefix_db_lookup#.archive_patientvisitdetails
	WHERE Patient_ID = #pid#
	</cfquery>

</cfloop>

<!--- We remove the table entries for other patients as we should be done --->

<cfquery   name="otherpatients" datasource="#Application.DataSrc#">
	SELECT distinct(a.patient_id) as pid FROM #Request.prefix_db_lookup#.archive_pathway_details a  left outer join #Request.prefix_db_agency#.patient p on a.Patient_ID = p.Patient_ID where p.Status = 'active' or p.Status = 'inactive' order by a.Patient_ID
</cfquery>

<cfloop query=otherpatients>
	<cfoutput>Deleting active/inactive patient #pid#<br></cfoutput>
	<cfquery  name="InsertArchived" datasource="#Application.DataSrc#">
	DELETE FROM #Request.prefix_db_lookup#.archive_assessments
	WHERE Patient_ID = #pid#
	</cfquery>

	<!---Assessment Forms Data--->
	<cfquery  name="InsertArchived" datasource="#Application.DataSrc#">
	DELETE FROM  #Request.prefix_db_lookup#.archive_assessment_forms
	WHERE Patient_ID = #pid#
	</cfquery>

	<!---Assessment Tracker Data --->
	<cfquery  name="InsertArchived" datasource="#Application.DataSrc#">
	DELETE FROM #Request.prefix_db_lookup#.archive_assessment_tracker
	WHERE Patient_ID = #pid#
	</cfquery>
	
	<!---Diagnosis data--->
	<cfquery  name="InsertArchived" datasource="#Application.DataSrc#">
	DELETE FROM #Request.prefix_db_lookup#.archive_diagnosis
	WHERE Patient_ID = #pid#
	</cfquery>

	<!--- Goals Data --->
	<cfquery  name="InsertArchived" datasource="#Application.DataSrc#">
	DELETE FROM #Request.prefix_db_lookup#.archive_goals
	WHERE Patient_ID = #pid#
	</cfquery>

	<!--- Pathway Details Data --->
	<cfquery  name="InsertArchived" datasource="#Application.DataSrc#">
	DELETE FROM #Request.prefix_db_lookup#.archive_pathway_details
	WHERE Patient_ID = #pid#
	</cfquery>

	<!--- Patient Visit Details Data --->
	<cfquery  name="InsertArchived" datasource="#Application.DataSrc#">
	DELETE FROM #Request.prefix_db_lookup#.archive_patientvisitdetails
	WHERE Patient_ID = #pid#
	</cfquery>

</cfloop>


<cfquery  name="InsertArchived" datasource="#Application.DataSrc#">
	DELETE FROM #Request.prefix_db_lookup#.archive_assessments	WHERE Patient_ID IS NULL
</cfquery>


