<?php

	/*
	** File: listInvoiceContents
	** Description: Given an invoice ID, returns a string listing all of the items
	**   in the invoice and where each of those items are going to be shipped to.
	** Version: $Revision: 1.1 $
	** Created: 12/29/99
	** Author: Shannon -jj Behrens
	** Email: jj@clearink.com
	** CVS Author: $Author: jj $
	** Last Revised: $Date: 1999/12/30 00:39:18 $
	** Log : $Log: listInvoiceContents,v $
	** Log : Revision 1.1  1999/12/30 00:39:18  jj
	** Log : Added include module to list shipping destinations for confirmation email.
	** Log :
	**
	** Copyright (c) 1999 Working Dogs.  All rights reserved.
	*/
	
	/*
	** Function: listInvoiceContents
	** Input: $invoice, &$contents
	** Output: Returns TRUE on success. The contents of the invoice will be 
	**   returned in the $contents parameter. Returns FALSE on failure. The 
	**   cause for the failure will then be in the $contents parameter.
	** Description: [in the $contents parameter] returns a string listing all
	**   of the items in a particular invoice including where those items 
	**   are going to be shipped to.  This is most useful for inclusion in 
	**   thank you emails.
	*/
	function listInvoiceContents($invoice, &$contents)
	{
		global $DatabaseLink;
		$contents = "";

		//get the list of items in this invoice (and their corresponding shipping 
		//record ID's)
		$query = "SELECT item.Name, sku.Name, invoice_sku.Shipping ";
		$query .= "FROM item, sku, invoice_sku ";
		$query .= "WHERE invoice_sku.Invoice = " . intval($invoice) . " ";
		$query .= "AND invoice_sku.SKU = sku.ID ";
		$query .= "AND sku.Item = item.ID ";
		$query .= "ORDER BY invoice_sku.Shipping";
		$records = mysql_query($query, $DatabaseLink);
		if ( !$records || !mysql_num_rows($records) )
		{
			$contents = mysql_errno() . ": " . mysql_error();
			return FALSE;
		}

		//print out each item 
		$currShippingID = $shippingID;
		while ($row = mysql_fetch_row($records))
		{
			list($itemName, $skuName, $shippingID) = $row;
			$contents .= "\t$itemName" . 
				(($skuName != "") ? ": $skuName" : "") . "\n";
			
			//if this is a new shipping record, get the details so that we can 
			//output the shipping destination address
			if ($shippingID != $currShippingID)
			{
				$query = 
					"SELECT a.Name_Prefix, a.Name_First, a.Name_Middle, a.Name_Last, " . 
					"a.Name_Suffix, a.Company, a.Street1, a.Street2, a.Street3, a.City, " .
					"a.StateProv, a.PostalCode, a.CountryCode ";
				$query .= "FROM address a, shipping s ";
				$query .= "WHERE s.ID = $shippingID ";
				$query .= "AND a.ID = s.Address";
				$addressRecords = mysql_query($query, $DatabaseLink);
				if ( !$addressRecords || !(mysql_num_rows($addressRecords)==1) )
				{
					$contents = mysql_errno() . ": " . mysql_error();
					return FALSE;
				}
				else
				{
					$obj = mysql_fetch_object($addressRecords);
					$contents .= "\tBeing shipped to:\n\n";
					$contents .= "\t\t" . 
						$obj->Name_Prefix . " " .
						$obj->Name_First . " " . 
						$obj->Name_Middle . " " . 
						$obj->Name_Last . " " . 
						$obj->Name_Suffix . "\n";
					if ($obj->Company != "")
					{
						$contents .= "\t\t" . $obj->Company . "\n";
					}
					if ($obj->Street1 != "")
					{
						$contents .= "\t\t" . $obj->Street1 . "\n";
					}
					if ($obj->Street2 != "")
					{
						$contents .= "\t\t" . $obj->Street2 . "\n";
					}
					if ($obj->Street3 != "")
					{
						$contents .= "\t\t" . $obj->Street3 . "\n";
					}
					$contents .= "\t\t" . 
						$obj->City . ", " . 
						$obj->StateProv . "  " . 
						$obj->PostalCode . "\n";
					$contents .= "\t\t" . $obj->CountryCode . "\n\n";
				}
			}
			$currShippingID = $shippingID;
		}

		return TRUE;
	}

?>
