<?php
	/*
	** File: CLEAN_INVOICE
	** Description: removes shipping/billing records for active invoice
	** Version: $Revision: 1.5 $
	** Created: 06/30/1999
	** Author: Leon Atkinson
	** Email: leon@clearink.com
	** CVS Author: $Author: leon $
	** Last Revised: $Date: 1999/12/09 23:23:18 $
	**
	** Log : $Log: CLEAN_INVOICE,v $
	** Log : Revision 1.5  1999/12/09 23:23:18  leon
	** Log : Changed ActionResults use so that it's an array instead of a
	** Log : continually-growing string.  Note the little function in standard_library
	** Log : for printing out all the action results.
	** Log :
	** Log : Revision 1.4  1999/11/02 18:23:04  chris
	** Log : Added support for coupons.
	** Log :
	** Log : Revision 1.3  1999/10/04 22:09:49  leon
	** Log : Removed getInvoiceID.  The active invoice is now added to $UserInfo
	** Log : by the check sid functionality in init.
	** Log :
	** Log : Revision 1.2  1999/08/18 22:31:46  leon
	** Log : Returning opening PHP tag to XML-friendly tag
	** Log :
	** Log : Revision 1.1  1999/07/28 21:16:40  leon
	** Log : New actions based on code donation from a live project
	** Log :
	**
	** Copyright (c) 1999 Working Dogs.  All rights reserved.
	*/

	//get shipping IDs for the invoice
	$AddressList = "0";
	
	$Query = "SELECT Address ";
	$Query .= "FROM shipping ";
	$Query .= "WHERE Invoice = " . $UserInfo['Invoice'] . " ";
	$DatabaseResult = mysql_query($Query, $DatabaseLink);
	while($DatabaseRow = mysql_fetch_row($DatabaseResult))
	{
		$AddressList .= ", " . $DatabaseRow[0];
	}

	$Query = "SELECT Address ";
	$Query .= "FROM billing ";
	$Query .= "WHERE Invoice = " . $UserInfo['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 = " . $UserInfo['Invoice'] . " ";
	if(!mysql_query($Query, $DatabaseLink))
	{
		$ActionResults[] =  mysql_errno() . ": " . mysql_error();
		$ActionResluts .= $Query;
	}

	//delete billing
	$Query = "DELETE FROM billing ";
	$Query .= "WHERE Invoice = " . $UserInfo['Invoice'] . " ";
	if(!mysql_query($Query, $DatabaseLink))
	{
		$ActionResults[] =  mysql_errno() . ": " . mysql_error();
		$ActionResluts .= $Query;
	}

	//delete coupons
	$Query = "DELETE FROM invoice_coupon ";
	$Query .= "WHERE Invoice = " . $UserInfo['Invoice'] . " ";
	mysql_query($Query, $DatabaseLink);
	
	//update shipping in invoice_sku
	$Query = "UPDATE invoice_sku ";
	$Query .= "SET shipping = 0 ";
	$Query .= "WHERE Invoice = " . $UserInfo['Invoice'] . " ";
	if(!mysql_query($Query, $DatabaseLink))
	{
		$ActionResults[] =  mysql_errno() . ": " . mysql_error();
		$ActionResluts .= $Query;
	}
	

?>