<?php
	/*
	** File: PURGE_INVOICE
	** Description: Delete an invoice, all SKUs, all billing and 
	** shipping, and all addresses.
	**
	** Version: $Revision: 1.6 $
	** Created: 08/11/1999
	** Author: Leon Atkinson
	** Email: leon@clearink.com
	** CVS Author: $Author: leon $
	** Last Revised: $Date: 2000/01/03 21:41:47 $
	**
	** Log : $Log: purgeInvoice,v $
	** Log : Revision 1.6  2000/01/03 21:41:47  leon
	** Log : Dealt with calling purgeInvoice("") or similar
	** Log :
	** Log : Revision 1.5  1999/10/11 23:04:04  leon
	** Log : Removed all require()'s, added block to stop double include()'s
	** Log :
	** Log : Require wasn't giving any performance advantage because it was always
	** Log : being called with a variable.  Futhermore, using return inside a
	** Log : require()'d file does not work in PHP4.
	** Log :
	** Log : Revision 1.4  1999/08/20 20:03:14  leon
	** Log : Added support to allow checking out if you aren't logged in.  To do this,
	** Log : I added a column to the session table to track the ID of current invoice
	** Log : for the session.  Before, we were getting the current invoice by looking
	** Log : in the invoice table for an inactive invoice for the same user.  This
	** Log : doesn't work for anonymous sessions because they all have user=0.
	** Log :
	** Log : Note that the invoice column is now set or unset by LOGIN_USER and
	** Log : LOGOUT_USER.  If a session creates an invoice, then logs into an
	** Log : account that has an inactive invoice, the invoice the session already
	** Log : has will be preserved.
	** Log :
	** Log : The purgeInvoice function was changed to set Invoice=0 for any session
	** Log : that is associated with the invoice to be purged.
	** Log :
	** Log : Revision 1.3  1999/08/18 22:32:06  leon
	** Log : Returning opening PHP tag to XML-friendly tag
	** Log :
	** Log : Revision 1.2  1999/08/11 23:38:43  leon
	** Log : Fixed creation date on purgeInvoice().  Details, details.
	** Log :
	** Log : Revision 1.1  1999/08/11 23:37:15  leon
	** Log : Added support for automatically purging invoices created by anonymous
	** Log : shoppers that were never completed.  As a side effect, the functionality
	** Log : of the PURGE_INVOICE action modules was wrapped into a function,
	** Log : purgeInvoice().  Two new constants control how often checks are made
	** Log : and how old an invoice has to be to be purged.
	** Log :
	**
	** Copyright (c) 1999 Working Dogs.  All rights reserved.
	*/

	/*
	** Ensure that module isn't included twice
	*/

	$included_flag = 'INCLUDE_' . basename(__FILE__);
	
	if(defined($included_flag))
		//if this module has been included already, then the
		//constant will be defined.  Note the unusual use of
		//the return statement here.  Control will return to
		//the file that included it.
		return;
	
	define($included_flag, TRUE);	


	function purgeInvoice($invoice)
	{
		global $DatabaseLink;

		//clean input
		$invoice = (integer)$invoice;
	
		//get shipping IDs for the invoice
		$AddressList = "0";
	
		$Query = "SELECT Address ";
		$Query .= "FROM shipping ";
		$Query .= "WHERE Invoice = $invoice ";
		$DatabaseResult = mysql_query($Query, $DatabaseLink);
		while($DatabaseRow = mysql_fetch_row($DatabaseResult))
		{
			$AddressList .= ", " . $DatabaseRow[0];
		}
	
		$Query = "SELECT Address ";
		$Query .= "FROM billing ";
		$Query .= "WHERE Invoice = $invoice ";
		$DatabaseResult = mysql_query($Query, $DatabaseLink);
		while($DatabaseRow = mysql_fetch_row($DatabaseResult))
		{
			$AddressList .= ", " . $DatabaseRow[0];
		}
	
		//delete addresses
		$Query = "DELETE FROM address ";
		$Query .= "WHERE ID in ($AddressList) ";
		if(!mysql_query($Query, $DatabaseLink))
		{
			$ActionResults .= mysql_errno() . ": " . mysql_error();
			$ActionResluts .= $Query;
		}
	
	
		//delete shipping
		$Query = "DELETE FROM shipping ";
		$Query .= "WHERE Invoice = $invoice ";
		if(!mysql_query($Query, $DatabaseLink))
		{
			$ActionResults .= mysql_errno() . ": " . mysql_error();
			$ActionResluts .= $Query;
		}
	
		//delete billing
		$Query = "DELETE FROM billing ";
		$Query .= "WHERE Invoice = $invoice ";
		if(!mysql_query($Query, $DatabaseLink))
		{
			$ActionResults .= mysql_errno() . ": " . mysql_error();
			$ActionResluts .= $Query;
		}
	
	
		//delete SKUs
		$Query = "DELETE FROM invoice_sku ";
		$Query .= "WHERE Invoice = $invoice ";
		if(!mysql_query($Query, $DatabaseLink))
		{
			$ActionResults .= mysql_errno() . ": " . mysql_error();
			$ActionResluts .= $Query;
		}
	
		//delete invoice
		$Query = "DELETE FROM invoice ";
		$Query .= "WHERE ID = $invoice ";
		if(!mysql_query($Query, $DatabaseLink))
		{
			$ActionResults .= mysql_errno() . ": " . mysql_error();
			$ActionResluts .= $Query;
		}	
	
		//update column in session table, if it exists
		$Query = "UPDATE session ";
		$Query .= "SET Invoice=NULL ";
		$Query .= "WHERE Invoice=$invoice ";
		if(!mysql_query($Query, $DatabaseLink))
		{
			$ActionResults .= mysql_errno() . ": " . mysql_error();
			$ActionResluts .= $Query;
		}	
	}


?>