<?php
	/*
	** File: order_address
	** Description: allow user to choose muliple shipping addresses
	** Version: $Revision: 1.21 $
	** Created: 06/25/1999
	** Author: Leon Atkinson
	** Email: leon@clearink.com
	** CVS Author: $Author: leon $
	** Last Revised: $Date: 2000/02/05 00:23:47 $
	** Log : $Log: order_address,v $
	** Log : Revision 1.21  2000/02/05 00:23:47  leon
	** Log : Removing number_format in favor of formatMoney
	** Log :
	** Log : Revision 1.20  2000/02/02 18:59:50  leon
	** Log : Fixing bugs reported by Ted Henigson.
	** Log :
	** Log : Revision 1.19  1999/12/06 21:17:26  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.18  1999/11/09 00:28:16  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.17  1999/11/08 22:32:27  leon
	** Log : Added prepareText() function to use everywhere we were using htmlentities.
	** Log :
	** Log : Revision 1.16  1999/11/04 21:35:43  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.15  1999/11/02 20:49:43  leon
	** Log : Added constant to control use of coupons
	** Log :
	** Log : Revision 1.14  1999/11/02 18:23:13  chris
	** Log : Added support for coupons.
	** Log :
	** Log : Revision 1.13  1999/10/25 22:41:34  leon
	** Log : Made constant for privacy statement, printed statement on relavant pages.
	** Log :
	** Log : Revision 1.12  1999/10/13 22:19:04  chris
	** Log : Moving text into constants in preparation of localization support.
	** Log :
	** Log : Revision 1.11  1999/10/13 00:42:30  chris
	** Log : Moving text into constants in preparation of localization support.
	** Log :
	** Log : Revision 1.10  1999/10/11 23:04:10  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.9  1999/10/04 22:09:56  leon
	** Log : Removed getInvoiceID.  The active invoice is now added to $UserInfo
	** Log : by the check sid functionality in init.
	** Log :
	** Log : Revision 1.8  1999/09/10 00:21:09  jj
	** Log : Changed include("addressfunctions") -> require("addressfunctions");
	** Log :
	** Log : Revision 1.7  1999/09/09 22:19:38  jj
	** Log : No longer any need to use the variable $addressFunctionsDefined.
	** Log :
	** Log : Revision 1.6  1999/08/18 22:32:18  leon
	** Log : Returning opening PHP tag to XML-friendly tag
	** Log :
	** Log : Revision 1.5  1999/08/18 16:23:12  jj
	** Log : These screens now use addressFunctions for all of their address needs.
	** Log :
	** Log : Revision 1.4  1999/08/17 17:56:42  jj
	** Log : Modularized everything to do with addresses. Please see include/addressFunctions
	** Log :
	** Log : Revision 1.3  1999/08/10 23:16:42  jj
	** Log : Used $UserInfo for getting the user's g information rather than hitting the database.
	** Log :
	** Log : Revision 1.2  1999/08/09 22:30:31  jj
	** Log : Any selects for Country now use makeCountrySelect (an include)
	** Log :
	** Log : Revision 1.1  1999/07/28 19:41:11  leon
	** Log : Mass update based on code donation from a live project
	** Log :
	**
	** Copyright (c) 1999 Working Dogs.  All rights reserved.
	*/

	// Cool functions for getting addresses
	include(APPLICATION_ROOT . "/modules/include/addressFunctions");

	include(APPLICATION_ROOT . "/modules/include/attribute_functions");

	//turn selected SKUs around
	//this allows automatically checking checkboxes later
	for($index=0; $index < count($SelectedSKU); $index++)
	{
		$SKUSelected[($SelectedSKU[$index])] = TRUE;
	}


	//get items from invoice
	$Query = "SELECT ss.ID, ss.ExternalSKU, s.Name, ss.ListPrice, ";
	$Query .= "ss.SalePrice, i.Name, ss.Quantity, ss.Freight, ss.GiftWrap, s.ID, ss.Notes, ss.Shipping ";
	$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'] . " ";
	$DatabaseResult = mysql_query($Query, $DatabaseLink);
	
	if(mysql_numrows($DatabaseResult))
	{
		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]);
			$basket["Shipping"][] = $DatabaseRow[11];
			
		}
	}
		

	//get shipping addresses
	$Query = "SELECT s.ID, a.Street1 ";
	$Query .= "FROM shipping s, address a ";
	$Query .= "WHERE s.Address = a.ID ";
	$Query .= "AND s.Invoice = " . $UserInfo['Invoice'] . " ";
	$DatabaseResult = mysql_query($Query, $DatabaseLink);
	while($DatabaseRow = mysql_fetch_row($DatabaseResult))
	{
		$ShippingName[($DatabaseRow[0])] = $DatabaseRow[1];
	}	
	$ShippingName[0] = "Not Assigned";




	print("<BR>" . L_ORDERADDRESS_GROUPED . "<BR><BR>\n");

	//list all SKUs on order

	print(StartForm("order_address", 'post', 'SET_SHIPPING', TRUE, 
			array("Ecom_SchemaVersion"=>"http://www.ecml.org/version1.0")));


	print("<TABLE BORDER=\"0\">\n");

	print("<TR>\n");
	print("<TD>" . L_ORDERADDRESS_SELECT . "</TD>\n");
	print("<TD>" . L_ORDERADDRESS_ITEM . "</TD>\n");
	print("<TD>" . L_ORDERADDRESS_DESTINATION . "</TD>\n");
	print("</TR>\n");

	
	for($index=0; $index < count($basket["invoice_sku"]); $index++)
	{
		//grabbing these values from the array will make
		//the code more readable hopefully
		$basket_ID = $basket["invoice_sku"][$index];
		$sku_ExternalSKU = $basket["ExternalSKU"][$index];
		$sku_Name = $basket["Name"][$index];
		$item_Name = $basket["ItemName"][$index];	
		$sku_Variation = $basket["Notes"][$index];
		$sku_Shipping = $basket["Shipping"][$index];


		print("<TR>\n");

		print("<TD>");
		if($sku_Shipping == 0)
		{
			print("<INPUT TYPE=\"checkbox\" NAME=\"SelectedSKU[]\" VALUE=\"$basket_ID\"");
			if(isset($SKUSelected[$basket_ID]) && $SKUSelected[$basket_ID])
			{
				print(" CHECKED");
			}
			print(">");
		}
		print("</TD>\n");
		
		print("<TD>");
		print("$item_Name ($sku_ExternalSKU) $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("</TD>\n");

		print("<TD>$ShippingName[$sku_Shipping]</TD>\n");
		
		print("</TR>\n");
	}

	print("</TABLE>\n");

	print("<BR>\n");

	print("<TABLE BORDER=\"0\">\n");


	/* 
	** Get the user's shipping information. Rather than have the text fields
	** start blank, we'll see if they already had values (this is the second
	** time around for the user) or we'll give them default values based on 
	** the user's address.
	*/
	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. 
	*/ 	
	print("<TR>\n");
	print("<TD COLSPAN=\"2\"><B>" . L_ORDERADDRESS_SHIPITEMSTO . "</B></TD>\n");
	print("</TR>\n");

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

	/* Shipping method field. */
	print("<TR>\n");
	print("<TD>" . L_ORDERADDRESS_SHIPPINGMETHOD . "</TD>\n");
	print("<TD>");
	print("<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) && ($DatabaseRow[0] == $ship_Method))
		{
			print(" SELECTED");
		}
		print(">" . prepareText($DatabaseRow[1]) . "\n");
	}
	print("</SELECT>");
	print("</TD>\n");
	print("</TR>\n");

	print("</TABLE>\n");




	print("<INPUT TYPE=\"SUBMIT\" VALUE=\"" . L_ORDERADDRESS_SETSHIPPINGINFO . "\"><BR>\n");

	print("</FORM>\n");


	print(L_ORDERADDRESS_YOUMAYWISHTO);
	print("<A HREF=\"" . ScreenURL("order_address", TRUE) . 
		"&amp;ACTION=CLEAN_INVOICE\">");
	print(L_ORDERADDRESS_STARTOVER . "</A>.<BR>\n");

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