<?php
	/*
	** File: billing
	** Description: functions for dealing with billing records
	** Version: $Revision: 1.2 $
	** Created: 12/21/1999
	** Author: Leon Atkinson
	** Email: leon@clearink.com
	** CVS Author: $Author: leon $
	** Last Revised: $Date: 1999/12/21 20:41:48 $
	** Log : $Log: billing,v $
	** Log : Revision 1.2  1999/12/21 20:41:48  leon
	** Log : Fixed bug in billing that wasn't saving the address.  Moved coupon
	** Log : functionality into coupon module.
	** Log :
	**
	** Copyright (c) 1999 Working Dogs.  All rights reserved.
	*/

	$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);	

	//we need this module in order to create and destroy addresses
	include(APPLICATION_ROOT . "/modules/include/addressFunctions");


	/*
	** Function: createBilling
	** Input: INTEGER invoice, ARRAY address, STRING $method, 
	**	STRING owner, STRING number, STRING expires
	** Output:
	** Description:
	*/
	function createBilling($invoice, $address, $method, $owner, $number, $expires)
	{
		global $DatabaseLink;

		// Create the address row
		if($AddressID = insertAddress($address))
		{
			//create billing row 
			$Query = "INSERT INTO billing ";
			$Query .= "(Invoice, Address, Method, ";
			$Query .= "CreditCardOwner, CreditCardNumber, CreditCardExpiration) ";
			$Query .= "VALUES ( ";
			$Query .= $invoice . ", ";
			$Query .= "$AddressID, ";
			$Query .= "'$method', ";
			$Query .= "'$owner', ";
			$Query .= "'$number', ";
			$Query .= "'$expires' ";
			$Query .= ") ";
			if(!($DatabaseResult = mysql_query($Query, $DatabaseLink)))
			{
				//PANIC
				//The query didn't work!
				//(A call to the future log functionality should go here)
				print("\n\n<!-- ($Query) failed!\n" . 
					mysql_errno() . ": " . mysql_error() . "-->\n\n");
	
				return(FALSE);
			}
		}
		else
		{
			//PANIC
			//We couldn't create an address
			//(A call to the future log functionality should go here)
			print("\n\n<!--createBilling couldn't create an address-->\n\n");

			return(FALSE);
		}
	
		return(TRUE);
	}
	
	
	/*
	** Function: destroyInvoiceBilling
	** Input: INTEGER invoice
	** Output: BOOLEAN
	** Description: removes all records from the billing table for an invoice, 
	** but also removes the address record associated with it.  This function
	** does not check that the billing record belongs to an invoice in progress.
	** The calling process is assumed to be responsible.
	*/
	function destroyInvoiceBilling($invoice)
	{
		global $DatabaseLink;

		/*
		** get address IDs for the invoice's billing records and
		** destroy them.
		*/

		//query db
		$Query = "SELECT Address ";
		$Query .= "FROM billing ";
		$Query .= "WHERE Invoice = $invoice ";
		if(!($DatabaseResult = mysql_query($Query, $DatabaseLink)))
		{
			//PANIC
			//The query didn't work!
			//(A call to the future log functionality should go here)
			print("\n\n<!-- ($Query) failed!\n" . 
				mysql_errno() . ": " . mysql_error() . "-->\n\n");

			return(FALSE);
		}
		else 
		{
			//pull each id from results and add to comma-separated list
			while($DatabaseRow = mysql_fetch_object($DatabaseResult))
			{
				$addressList .= $DatabaseRow->Address . ",";
			}
		}

		//make sure there are any rows to destroy
		if(mysql_num_rows($DatabaseResult) > 0)
		{
			//remove trailing comma
			$addressList = substr($addressList, 0, strlen($addressList)-1);
	
			//pass the list to remove
			destroyAddressList($addressList);
		}
		
		/* 
		** Delete the billing records, if any
		*/
		$Query = "DELETE FROM billing ";
		$Query .= "WHERE Invoice = $invoice ";
		if(!mysql_query($Query, $DatabaseLink))
		{
			//PANIC
			//The query didn't work!
			//(A call to the future log functionality should go here)
			print("\n\n<!-- ($Query) failed!\n" . 
				mysql_errno() . ": " . mysql_error() . "-->\n\n");

			return(FALSE);
		}

		return(TRUE);
	}


?>