<?php 

	/* 
	** File: 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.
	** Version: $Revision: 1.15 $
	** Created: 7/9/99
	** Author: Shannon -jj Behrens
	** Email: shannon.behrens@clearink.com
	** CVS Author: $Author: leon $
	** Last Revised: $Date: 1999/12/08 00:55:12 $
	** Log : $Log: view_ibid,v $
	** Log : Revision 1.15  1999/12/08 00:55:12  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.14  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.13  1999/11/04 21:35:45  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.12  1999/10/13 22:19:08  chris
	** Log : Moving text into constants in preparation of localization support.
	** Log :
	** Log : Revision 1.11  1999/10/12 23:14:50  leon
	** Log : Moved localization constants into 'english' module.  Added constant to
	** Log : control whether unregistered shoppers may checkout.
	** Log :
	** Log : Revision 1.10  1999/10/12 20:09:42  leon
	** Log : Pulled text into constants in preparation for localization.
	** 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/08/18 22:32:22  leon
	** Log : Returning opening PHP tag to XML-friendly tag
	** Log :
	** Log : Revision 1.7  1999/08/17 18:41:02  jj
	** Log : Got rid of dataRow everywhere in favor of functions from addressFunctions
	** 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 20:27:37  jj
	** Log : Included GiftWrap status as a field while viewing individual ibid's (edit_ibid, view_ibid).
	** Log :
	** Log : Revision 1.4  1999/08/10 00:04:48  jj
	** Log : Made use of UserInfo["ID"] instead of doing a query to figure out the user ID.
	** Log :
	** Log : Revision 1.3  1999/08/09 18:41:33  jj
	** Log : Changed a few field labels to make them "nicer".
	** Log :
	** Log : Revision 1.2  1999/07/30 16:40:57  jj
	** Log : Used wrong fields for name of product when I originally wrote these.  Bug fix.
	** Log :
	** Log : Revision 1.1  1999/07/28 19:41:12  leon
	** Log : Mass update based on code donation 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");

	
	$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, " . 
		"v.GiftWrap, 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_VIEWIBID_INVALID."</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, $ibidGiftWrap, $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"] != $fromIbidUserID)
		{
			$continueReport = FALSE;
			print("<I>".L_VIEWIBID_NOT_YOURS."</I>");
		}
	}
	
	
	
	/* 
	** 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's okay so far, we'll output a nice little table
	** that gives the user a link back to the page where he can edit the 
	** invoice and a link back to the list of invoices.
	*/ 
	if ($continueReport) 
	{
		print("<TABLE WIDTH=\"100%\" BORDER=1>\n<TR><TD>\n");
		print("<UL>\n");
		print("<LI><A HREF=\"" . ScreenURL("view_invoice"));
		print("&invoice_ID=$ibidInvoice\">");
		print(L_VIEWIBID_RETURN_INVOICE . "</A>\n");
		print("<LI><A HREF=\"" . ScreenURL("order_history"));
		print("\">".L_VIEWIBID_RETURN_HISTORY."</A>\n");
		print("</UL>\n");
		print("</TABLE><BR>\n");
	}
		
	

	/*	
	** If everything's okay so far, we can output the main invoice_sku
	** information. 
	*/
	if ($continueReport)
	{
		/* Start the table. */ 
		print("<TABLE WIDTH=\"100%\" BORDER=1>\n<TR><TD>\n");
		
		/* Print the data. Put it in a table with no borders. */
		print("<B>".L_VIEWIBID_MAIN_INFO."</B>\n");
		print("<TABLE>\n");

		//TRICKY
		//This uses an address function to output table rows.
		//This should change.
		printTableRow(L_VIEWIBID_QUANTITY, $ibidQuantity);
		printTableRow(L_VIEWIBID_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_EDITIBID_NOTES, $notes);
		}


		printTableRow(L_VIEWIBID_LISTPRICE, $ibidListPrice);
		if ($ibidSalePrice != 0)
		{
			printTableRow(L_VIEWIBID_SALEPRICE, $ibidSalePrice);
		}
		printTableRow(L_VIEWIBID_FREIGHT, $ibidFreight);
		printTableRow(L_VIEWIBID_GIFTWRAP, ($ibidGiftWrap=="Y" ? "Yes" : "No"));


		print("</TABLE><BR>\n");
		
		/* End the table. */
		print("</TD></TR>\n</TABLE><BR>\n");
	}
	
	
	
	/*
	** If everything's okay so far, we can output the main shipping information
	** for this ibid. 
	*/ 
	if ($continueReport)
	{
		/* Start the table. */ 
		print("<TABLE WIDTH=\"100%\" BORDER=1>\n<TR><TD>\n");
		
		/* Print the data. Put it in a table with no borders. */
		print("<B>".L_VIEWIBID_SHIPINFO."</B>:");
		print("<TABLE>\n");
		printTableRow(L_VIEWIBID_SHIP_METHOD, $shippingMethod);
		printTableRow(L_VIEWIBID_SHIP_MESSAGE, $shippingMessage);
		print("</TABLE><BR>\n");
		
		/* End the table. */
		print("</TD></TR>\n</TABLE><BR>\n");
	}
	
	
	
	/*
	** If everything's okay so far, we can output the shipping destination 
	** information.  
	*/ 
	if ($continueReport)
	{
		if ( !($address = getAddress($shippingAddress)) )
		{
			print("<!-- \"$shippingAddress\" is an invalid address ID. " . 
				"Hmmm, I wonder how that happend. I got it straight from " . 
				"the shipping table. -->");
			$continueReport = FALSE;
		}
		else
		{
			print("<TABLE WIDTH=\"100%\" BORDER=1>\n<TR><TD>\n");
			print("<B>".L_VIEWIBID_DESTINATION."</B>\n");
			outputAddress($address);
			print("</TD></TR>\n</TABLE><BR>\n");
		}
	}
	
?>
		
