<?php

	/*
	** File: order_info
	** Description: get shipping info
	** Version: $Revision: 1.30 $
	** Created: 04/23/1999
	** Author: Leon Atkinson
	** Email: leon@clearink.com
	** CVS Author: $Author: leon $
	** Last Revised: $Date: 2000/02/05 00:23:48 $
	** Log : $Log: order_info,v $
	** Log : Revision 1.30  2000/02/05 00:23:48  leon
	** Log : Removing number_format in favor of formatMoney
	** Log :
	** Log : Revision 1.29  2000/02/04 18:57:24  leon
	** Log : Removing delete link from basket contents.  It was piggy-backed unto
	** Log : the code for updating the rest of the basket stuff, but it wasn't
	** Log : working.  It was causing the loss of all gift wrap selections.
	** Log :
	** Log : Revision 1.28  1999/12/06 21:17:28  leon
	** Log : ECML compliance
	** Log : Overhauled address functions to use ECML form field names.  Ditto with
	** Log : the billing fields on the checkout forms.  Note the change to build.sql.
	** Log : Yes, the address table changed to accomodate new fields.  The billing
	** Log : table changed too.
	** Log :
	** Log : Revision 1.27  1999/11/09 00:28:17  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.26  1999/11/08 22:32:29  leon
	** Log : Added prepareText() function to use everywhere we were using htmlentities.
	** Log :
	** Log : Revision 1.25  1999/11/05 18:17:54  leon
	** Log : Reorganized coupon code.
	** Log :
	** Log : Revision 1.24  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.23  1999/11/02 20:49:44  leon
	** Log : Added constant to control use of coupons
	** Log :
	** Log : Revision 1.22  1999/11/02 18:23:15  chris
	** Log : Added support for coupons.
	** Log :
	** Log : Revision 1.21  1999/10/26 17:31:37  leon
	** Log : Removed constants defining variations.  Instead functions wrap the tables.
	** Log : They use caching to reduce the number of database queries.
	** Log :
	** Log : Revision 1.20  1999/10/25 22:41:36  leon
	** Log : Made constant for privacy statement, printed statement on relavant pages.
	** Log :
	** Log : Revision 1.19  1999/10/14 20:42:21  leon
	** Log : Added formatMoney function, cleaned up references to GIFTWRAP_COST.
	** Log :
	** Log : Revision 1.18  1999/10/13 22:19:06  chris
	** Log : Moving text into constants in preparation of localization support.
	** Log :
	** Log : Revision 1.17  1999/10/13 00:42:33  chris
	** Log : Moving text into constants in preparation of localization support.
	** Log :
	** Log : Revision 1.16  1999/10/11 23:04:11  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.15  1999/10/04 22:09:58  leon
	** Log : Removed getInvoiceID.  The active invoice is now added to $UserInfo
	** Log : by the check sid functionality in init.
	** Log :
	** Log : Revision 1.14  1999/08/18 22:32:20  leon
	** Log : Returning opening PHP tag to XML-friendly tag
	** Log :
	** Log : Revision 1.13  1999/08/17 17:56:42  jj
	** Log : Modularized everything to do with addresses. Please see include/addressFunctions
	** Log :
	** Log : Revision 1.12  1999/08/11 20:05:29  jj
	** Log : Fixed any references to the gift wrap variation so that it uses the global constant.
	** Log :
	** Log : Revision 1.11  1999/08/10 23:16:42  jj
	** Log : Used $UserInfo for getting the user's address information rather than hitting the database.
	** Log :
	** Log : Revision 1.10  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.
	*/

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

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


	$continueScript = TRUE;

	/*
	** Get items from invoice.
	*/
	$Query = "SELECT ss.ID, ss.ExternalSKU, s.Name, ss.ListPrice, " . 
		"ss.SalePrice, i.Name, ss.Quantity, ss.Freight, ss.GiftWrap, s.ID, " . 
		"ss.Notes ";
	$Query .= "FROM invoice_sku ss, sku s, item i ";
	$Query .= "WHERE ss.SKU = s.ID ";
	$Query .= "AND s.Item = i.ID ";
	$Query .= "AND ss.Invoice = " . $UserInfo['Invoice'] . " ";
	if ( !($DatabaseResult = mysql_query($Query, $DatabaseLink)) )
	{
		print("<!-- " . mysql_errno() . ":  " . mysql_error() . 
			"   Query was $Query -->\n");
		$continueScript = FALSE;
	}
	else
	{
		if ( !mysql_numrows($DatabaseResult) )
		{
			/* The user must have removed the last item from his basket. */
			print("<BR>" . L_ORDERINFO_NOMOREITEMS . "<BR>\n");
			print("<P>" . L_ORDERINFO_DELETETHISINVOICE . " ");
			print("<A HREF=\"" . ScreenURL("contents") . 
				"&ACTION=PURGE_INVOICE\">" . L_ORDERINFO_HERE . "</A>.\n");
			$continueScript = FALSE;
		}
		else
		{
			while($DatabaseRow = mysql_fetch_row($DatabaseResult))
			{
				$basket["invoice_sku"][] = $DatabaseRow[0];
				$basket["ExternalSKU"][] = $DatabaseRow[1];
				$basket["Name"][] = $DatabaseRow[2];
				$basket["Price"][] = $DatabaseRow[3];
				$basket["SalePrice"][] = $DatabaseRow[4];

				$basket["ItemName"][] = $DatabaseRow[5];
				$basket["Quantity"][] = $DatabaseRow[6];
				$basket["Freight"][] = $DatabaseRow[7];
				$basket["GiftWrap"][] = $DatabaseRow[8];
				$basket["SKU"][] = $DatabaseRow[9];
				$basket["Notes"][] = unserialize($DatabaseRow[10]);
			}
		}
	}
		
	
	
	/* 
	** Get sku giftwrap-ability.
	*/
	if ($continueScript)
	{
		$Query = "SELECT SKU ";
		$Query .= "FROM sku_variation ";
		$Query .= "WHERE Variation = " . getVariationID(getAttributeID("Gift Wrap"), "Available") . " ";
		$Query .= "AND SKU in (0";
		for($index = 0; $index < count($basket["SKU"]); $index++)
		{
			$Query .= ", " . $basket["SKU"][$index];
		}		
		$Query .= ") ";
		if ( !($DatabaseResult = mysql_query($Query, $DatabaseLink)) )
		{
			print("<!-- " . mysql_errno() . ":  " . mysql_error() . 
				"   Query was: $Query. -->\n");
			$continueScript = FALSE;
		}
		else 
		{
			while($DatabaseRow = mysql_fetch_row($DatabaseResult))
			{
				$GiftWrap[($DatabaseRow[0])] = TRUE;
			}
		}
	}
	
	
	
	/*
	** Output the table with all the items in it.
	*/
	if ($continueScript)
	{
		print(StartForm("order_info", 'post', 'UPDATE_INVOICE_SKU', TRUE));
	
		print("<BR><TABLE BORDER=\"0\">\n");
		print("<TR>\n");
		print("<TH>" . L_ORDERINFO_ITEM . "</TH>\n");
		print("<TH>" . L_ORDERINFO_PRICE . "<SUP>*</SUP></TH>\n");
		print("<TH>" . L_ORDERINFO_QUANTITY . "</TH>\n");
		print("<TH>" . L_ORDERINFO_GIFTWRAP . "<SUP>*</SUP></TH>\n");
		print("<TH>" . L_ORDERINFO_SUBTOTAL . "</TH>\n");
		print("</TR>\n");

		print("<TR>\n");
		print("<TD COLSPAN=\"5\"><HR COLOR=\"#800000\"></TD>\n");
		print("</TR>\n");

		/* Output the items. */
		for($index=0; $index < count($basket["invoice_sku"]); $index++)
		{
			//grabbing these values from the array will make
			//the code more readable hopefully (you're right, it does :P )
			$basket_ID = $basket["invoice_sku"][$index];
			$sku_ExternalSKU = $basket["ExternalSKU"][$index];
			$sku_Name = $basket["Name"][$index];
			$sku_Price = $basket["Price"][$index];
			$sku_SalePrice = $basket["SalePrice"][$index];
			$item_Name = $basket["ItemName"][$index];
			$basket_Quantity = $basket["Quantity"][$index];
			$sku_Freight = $basket["Freight"][$index];
			$sku_GiftWrap = $basket["GiftWrap"][$index];
			$sku_ID = $basket["SKU"][$index];
			$sku_Variation = $basket["Notes"][$index];
			if($sku_SalePrice > 0)
			{
				$sku_Price = $sku_SalePrice;
			}
			print("<TR>\n");

			/* 
			** Item description
			*/			
			print("<TD>");
			print("<FONT SIZE=\"2\">");
			print(prepareText($item_Name));
			if ($sku_Name != "")
			{
				print(": " . prepareText($sku_Name));
			}

			if(is_array($sku_Variation))
			{
				while(list($key, $variation_ID) = each($sku_Variation))
				{
					print("<BR>\n");
					
					$variation = getVariationInfo($variation_ID);
					$attribute = getAttributeInfo($variation["Attribute"]);
					
					print(prepareText($attribute["Name"] . ": " . $variation["Name"]));
				}
			}

			print("</FONT>");
			print("</TD>\n");

			/* 
			** Price (with extra shipping)
			*/			
			print("<TD>" . formatMoney($sku_Price));
			if($sku_Freight > 0)
			{
				print(" (" . formatMoney($sku_Freight) . ")");
			}
			print("</TD>\n");
	
			/* 
			** Quantity
			*/			
			print("<TD ALIGN=\"center\">");
			print("<INPUT TYPE=\"text\" SIZE=\"3\" ");
			print("NAME=\"inputBasket[$basket_ID]\" ");
			print("VALUE=\"$basket_Quantity\">");
			print("</TD>\n");

			/* 
			** Gift Wrap Checkbox
			*/			
			print("<TD ALIGN=\"center\">");
			if(isset($GiftWrap[$sku_ID]))
			{
				print("<INPUT TYPE=\"checkbox\" ");
				print("NAME=\"inputGiftWrap[$basket_ID]\" ");
				print("VALUE=\"Y\"");
				if($sku_GiftWrap == 'Y')
				{
					print(" CHECKED");
				}
				print(">");
			}
			else
			{
				print("N/A");
			}
			print("</TD>\n");

			/*
			** Subtotal
			*/
			$SubTotal = $basket_Quantity * ($sku_Price + $sku_Freight);
			if($sku_GiftWrap == 'Y')
			{
				$SubTotal += $basket_Quantity * GIFTWRAP_COST;
			}
			print("<TD ALIGN=\"right\">" . formatMoney($SubTotal) . 
				"</TD>\n");
			print("</TR>\n");
			print("<TR>\n");
			print("<TD COLSPAN=\"5\"><HR COLOR=\"#800000\"></TD>\n");
			print("</TR>\n");
		} /* Done outputting list of items. */
		
		/* Small print stuff. */
		print("<TR>\n");
		print("<TD COLSPAN=\"5\">");
		print("<FONT SIZE=\"1\">* " . L_ORDERINFO_CHARGESINPARENTHESES . "</FONT><BR>\n");
		print("<FONT SIZE=\"1\">* " . L_ORDEREXPRESS_GIFTWRAPIS . ":  " . formatMoney(GIFTWRAP_COST) . "</FONT>");
		print("</TD>\n");
		print("</TR>\n");

		/* Submit button. */
		print("<TR>\n");
		print("<TD COLSPAN=\"5\" ALIGN=\"right\">");
		print("<INPUT TYPE=\"submit\" VALUE=\"" . L_ORDERINFO_SUBMITCHANGES . "\">\n");
		print("</TD>\n");
		print("</TR>\n");

		/* Finish up the table and the form. */
		print("</TABLE>\n");
		print("</FORM>\n");
	} /* Done outputting the table of items. */


		
	/*
	** Done updating quantities, below this, we can start doing shipping 
	** information. 
	*/
	if ($continueScript)
	{
		print(L_ORDERINFO_PLEASEFILLOUTSHIPPINGADDRESS . "<BR>\n");
	}



	/* 
	** Get default shipping address information:
	** If this is the second time around (user omitted something), use those
	** values, otherwise, try to use $UserInfo (if available).
	*/
	if($continueScript)
	{	
		if(isset($Ecom_ShipTo_Postal_Name_First))
		{
			$shipAddress = getAddressFromEnvironment("Ecom_ShipTo_");
		}
		elseif(isset($UserInfo))
		{
			$shipAddress = $UserInfo["Address"];
		}
	}


	
	/* 
	** Okay, time to actually get the shipping information. 
	*/ 			
	if ($continueScript)
	{
		/* Start the form, and do the main shipping address part. */
		print(StartForm("order_billing", 'post', 'SET_SHIPPING_ALL', TRUE, 
			array("Ecom_SchemaVersion"=>"http://www.ecml.org/version1.0")));

		print("<P>\n");
		
		print("<TABLE BORDER=\"0\">\n");
		print("<TR><TD COLSPAN=\"2\"><B>" . L_ORDERINFO_SHIPPINGINFORMATION . "</B></TD></TR>\n");

		printAddressFields(
			$shipAddress, 
			"Ecom_ShipTo_", 
			"ALL", 
			0, 
			0, 
			FALSE);


		/* Get the shipping method. */		
		print("<TR><TD>" . L_ORDERINFO_SHIPPINGMETHOD . "</TD>\n");
		print("<TD><SELECT NAME=\"ship_Method\">\n");
		$Query = "SELECT ID, Name ";
		$Query .= "FROM shipping_method ";
		$Query .= "ORDER BY DisplayPrecedence ";
		$DatabaseResult = mysql_query($Query, $DatabaseLink);
		while($DatabaseRow = mysql_fetch_row($DatabaseResult))
		{
			print("<OPTION VALUE=\"$DatabaseRow[0]\"");
			if(isset($ship_Method) AND ($DatabaseRow[0] == $ship_Method))
			{
				print(" SELECTED");
			}
			print(">" . prepareText($DatabaseRow[1]) . "\n");
		}
		print("</SELECT></TD></TR>\n");

		/* Finish up. */
		print("<TR><TD COLSPAN=\"2\">\n");
		print("<INPUT TYPE=\"SUBMIT\" " . 
			"VALUE=\"" . L_ORDERINFO_SENDALLITEMS . "\">\n");		
		print("</TD></TR>\n");
		print("</TABLE>\n");
		print("</FORM>\n");
	
		/* Multiple address link. */
		print(L_ORDERINFO_YOUMAYWISHTO . " ");
		print("<A HREF=\"" . ScreenURL("order_address", TRUE) . 
			"&amp;ACTION=CLEAN_INVOICE\">");
		print(L_ORDERINFO_SENDITEMS . "</A>.<BR>\n");
	}

	print("<BR>\n");
	print(PRIVACY_STATEMENT);

?>
