<?php 

	/* 
	** File: order_view_ibid
	** Description: In invoice_sku, the thing that separates the records is 
	**   the ID, which I am calling ibid (for "in-basket ID"). This page allows
	**   a user to view that particular record based on the ibid. The user 
	**   can view this screen during the order confirmation process (for 
	**   instance, he may want to check to see that a particular item is being
	**   shipped to the correct place).
	** Version: $Revision: 1.13 $
	** Created: 8/3/99
	** Author: Shannon -jj Behrens
	** Email: jj@clearink.com
	** CVS Author: $Author: leon $
	** Last Revised: $Date: 1999/12/08 00:55:11 $
	** Log : $Log: order_view_ibid,v $
	** Log : Revision 1.13  1999/12/08 00:55:11  leon
	** Log : Pulled functions for creating table rows out of addressFunctions and
	** Log : generalized them.  I'm leaving wrappers for the old functions, but I
	** Log : will removed them in the coming weeks.
	** Log :
	** Log : Revision 1.12  1999/11/09 00:28:18  leon
	** Log : Changed use of notes column.  This column now stores an array of variation
	** Log : IDs in serialized form.  Names for attributes and variations are pulled
	** Log : from the database using two new functions in the attribute_functions
	** Log : include module.
	** Log :
	** Log : Revision 1.11  1999/11/04 21:35:44  leon
	** Log : Cleaned up the use of paths.  Paths are now all set in index.php3 as
	** Log : constants.  Example settings for Apache/Unix, Apache/Win32, and IIS.
	** Log :
	** Log : Revision 1.10  1999/10/13 22:19:06  chris
	** Log : Moving text into constants in preparation of localization support.
	** Log :
	** Log : Revision 1.9  1999/10/11 23:04:12  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.8  1999/09/30 18:53:07  jj
	** Log : order_view_ibid was not working for anonymous users. It was broken and needed a cast.
	** Log :
	** Log : Revision 1.7  1999/08/18 22:32:20  leon
	** Log : Returning opening PHP tag to XML-friendly tag
	** Log :
	** Log : Revision 1.6  1999/08/17 17:56:42  jj
	** Log : Modularized everything to do with addresses. Please see include/addressFunctions
	** Log :
	** Log : Revision 1.5  1999/08/11 22:46:50  jj
	** Log : Changed two small things: 1) the order of fields displayed on one of the forms 2) the alignment of one of the fields
	** Log :
	** Log : Revision 1.4  1999/08/11 19:43:32  jj
	** Log : Replaced all instances of eMail with email or Email (as fitting).
	** Log :
	** Log : Revision 1.3  1999/08/10 21:15:26  jj
	** Log : Put the CVS variables back into the code (they were lost when I copied stuff from a live project).
	** Log :
	**
	** Copyright (c) 1999 Working Dogs. All rights reserved. 
	**
	** Expected input: $ibid, $sid
	*/

	/* Address related functions. */
	include(APPLICATION_ROOT . "/modules/include/addressFunctions");

	//get attribute functions
	include(APPLICATION_ROOT . "/modules/include/attribute_functions");


	/* Our halt flag. */		
	$continueReport = TRUE;
	
	/* 
	** First of all, get the record attached to this ibid from the invoice_sku
	** table.  Also be sure to get the userID attached to the SKU.
	*/
	$invoiceSkuQuery = "SELECT v.Invoice, v.Quantity, i.Name, s.Name, " . 
		"v.Notes, v.ListPrice, v.SalePrice, v.Freight, v.Shipping, c.User ";
	$invoiceSkuQuery .= "FROM item i, invoice_sku v, sku s, invoice c ";
	$invoiceSkuQuery .= "WHERE v.ID = " . intval($ibid) . " ";
	$invoiceSkuQuery .= "AND v.SKU = s.ID ";
	$invoiceSkuQuery .= "AND s.Item = i.ID ";
	$invoiceSkuQuery .= "AND v.Invoice = c.ID";
	/* Execute the query. */
	if ( !($invoiceSkuRecords = mysql_query($invoiceSkuQuery, $DatabaseLink)) )
	{
		print("<!-- " . mysql_errno() . ": " . mysql_error() . 
			"   Query was $invoiceSkuQuery -->\n");
		$continueReport = FALSE;
	}
	else
	{
		/* Make sure this is a valid IBID. */ 
		if ( !($invoiceSkuRecord = mysql_fetch_row($invoiceSkuRecords)) )
		{
			print("<P><I>" . L_ORDER_VIEW_IBID_INVALIDIBID . "</I>\n");
			$continueReport = FALSE;
		}
		/* Yehaw! We have a valid IBID! Let's parse it. */
		else
		{
			list($ibidInvoice, $ibidQuantity, $ibidItemName, $ibidSKUName, 
				$ibidNotes, $ibidListPrice, $ibidSalePrice, $ibidFreight, 
				$ibidShipping, $fromIbidUserID) = $invoiceSkuRecord;
				
			$ibidVariation = unserialize($ibidNotes);
		}
	}
	
	
	
	/*
	** If $continueReport is true, in order to have "a little" bit of security,
	** we should verify that this $sid's userID does correspond to this 
	** ibid's userID.
	*/
	if ($continueReport)
	{
		if ($UserInfo["ID"] != intval($fromIbidUserID))
		{
			$continueReport = FALSE;
			print("<I>" . L_ORDER_VIEW_IBID_ITEMNOTBELONGTOUSERID . "</I><BR>\n");
		}
	}
	
	
	
	/* 
	** If everything is okay so far, we can go ahead and get the shipping 
	** information connected to this IBID.
	*/ 
	if ($continueReport)
	{
		$shippingQuery = "SELECT s.Address, m.Name, s.Message ";
		$shippingQuery .= "FROM shipping s, shipping_method m ";
		$shippingQuery .= "WHERE s.ID = " . intval($ibidShipping) . " ";
		$shippingQuery .= "AND s.Method = m.ID";
		/* Execute the query. */
		if ( !($shippingRecords = mysql_query($shippingQuery, $DatabaseLink)) )
		{
			print("<!-- " . mysql_errno() . ": " . mysql_error() . 
				"   Query was $shippingQuery -->\n");
			$continueReport = FALSE;
		}
		else
		{
			/* Make sure that we're able to retrieve a record. Should work. */
			if ( !($shippingRecord = mysql_fetch_row($shippingRecords)) )
			{
				print("<!-- \"$ibidShipping\" is an invalid shipping ID. "
					. "Hmmm, I wonder how that happend. I got it straight "
					. "from the invoice_sku table. -->\n");
					$continueReport = FALSE;
			}
			else
			{
				/* We have valid shipping info, let's parse it. */
				$i = 0;
				$shippingAddress = $shippingRecord[$i++];
				$shippingMethod = $shippingRecord[$i++];
				$shippingMessage = $shippingRecord[$i++];
			}
		}
	}
	
	
	 
 	/*
	** If everything is okay so far, we can go ahead and get the address 
	** and stuff attached to this shipping record via the "Address" column.
	*/
	if ($continueReport)
	{
		if ( !($shipAddressFields = getAddress($shippingAddress)) )
		{
			print(L_ORDER_VIEW_IBID_INVALIDSHIPPINGADDRESSID);
			$continueReport = FALSE;
		}
	}
	
	
	
	/* 
	** Give the user a link back to the order confirmation screen. 
	*/
	print("<BR><B>" . L_ORDER_VIEW_IBID_VIEWINGITEM . "</B>\n");
	print($ibidItemName . /* Item name + (if avail.) sku name. */
		($ibidSKUName == "" ? "" : (": " . $ibidSKUName)) . "\n");
	print("<UL><LI><A HREF=\"" . ScreenURL("order_confirm", TRUE) . 
		"\">" . L_ORDER_VIEW_IBID_RETURNTOMAINORDERSCREEN . "</A></UL>\n");

	
	
	/*	
	** If everything's okay so far, we can output the main invoice_sku
	** information. 
	*/
	if ($continueReport)
	{
		/* Print the data. Put it in a table with no borders. */
		print("<B>" . L_ORDER_VIEW_IBID_ITEMINFORMATION . "</B>\n");
		print("<TABLE>\n");
		printTableRow(L_ORDER_VIEW_IBID_QUANTITY, $ibidQuantity);
		printTableRow(L_ORDER_VIEW_IBID_NAME, $ibidItemName . /* Item name + (if avail.) sku name. */
			($ibidSKUName == "" ? "" : (": " . $ibidSKUName)) );

		if(is_array($ibidVariation))
		{
			$notes = "";
		
			while(list($key, $variation_ID) = each($ibidVariation))
			{
				$variation = getVariationInfo($variation_ID);
				$attribute = getAttributeInfo($variation["Attribute"]);
				
				$notes .= prepareText($attribute["Name"] . ": " . $variation["Name"]) . " ";
			}

			printTableRow(L_ORDER_VIEW_IBID_NOTES, $notes);
		}


		
		printTableRow(L_ORDER_VIEW_IBID_LISTPRICE, $ibidListPrice);
		if ($ibidSalePrice != 0)
		{
			printTableRow(L_ORDER_VIEW_IBID_SALEPRICE, $ibidSalePrice);
		}
		printTableRow(L_ORDER_VIEW_IBID_FREIGHT, $ibidFreight);
		print("</TABLE><BR>\n");
	}
	
	
	
	/*
	** If everything's okay so far, we can output the main shipping information
	** for this ibid. 
	*/ 
	if ($continueReport)
	{
		/* Print the data. Put it in a table with no borders. */
		print("<B>" . L_ORDER_VIEW_IBID_SHIPPINGINFORMATION . "</B>:");
		print("<TABLE>\n");
		printTableRow(L_ORDER_VIEW_IBID_SHIPPINGMETHOD, $shippingMethod);
		print("</TABLE><BR>\n");
	}
	
	
	
	/*
	** If everything's okay so far, we can output the shipping destination 
	** information.  
	*/ 
	if ($continueReport)
	{
		/* Print the data. Put it in a table with no borders. */ 
		print("<B>" . L_ORDER_VIEW_IBID_SHIPPINGDESTINATION . "</B>\n");
		outputAddress($shipAddressFields);
	}		
	
?>	
