<?php

	/*
	** File: order_confirm
	** Description: show final order info, ask for confirmation
	** Version: $Revision: 1.28 $
	** Created: 05/01/1999
	** Author: Shannon -jj Behrens, Leon Atkinson
	** Email: jj@clearink.com, leon@clearink.com
	** CVS Author: $Author: leon $
	** Last Revised: $Date: 1999/12/17 22:08:32 $
	** Log : $Log: order_confirm,v $
	** Log : Revision 1.28  1999/12/17 22:08:32  leon
	** Log : Added function to print invoice totals, consequently updated printTableRow
	** Log : to allow passing of styles.
	** Log :
	** Log : Revision 1.27  1999/12/16 21:58:45  leon
	** Log : Refactored calculateTotal.  Now with fewer queries! Gift Wrap was being
	** Log : refered to as GiftWrap in the array returned by calculateTotal, so I changed
	** Log : it in the few places it was.  That allowed me to use a general strategy
	** Log : for storing fee totals.
	** Log :
	** Log : Revision 1.26  1999/12/08 00:55:10  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.25  1999/12/06 21:17:27  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.24  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.23  1999/11/08 22:32:28  leon
	** Log : Added prepareText() function to use everywhere we were using htmlentities.
	** Log :
	** Log : Revision 1.22  1999/11/05 18:17:53  leon
	** Log : Reorganized coupon code.
	** Log :
	** Log : Revision 1.21  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.20  1999/11/02 20:49:44  leon
	** Log : Added constant to control use of coupons
	** Log :
	** Log : Revision 1.19  1999/11/02 18:23:14  chris
	** Log : Added support for coupons.
	** Log :
	** Log : Revision 1.18  1999/10/23 00:20:09  jj
	** Log : Credit card expiration dates are now stored as datetime's.
	** Log :
	** Log : Revision 1.17  1999/10/13 22:19:05  chris
	** Log : Moving text into constants in preparation of localization support.
	** Log :
	** Log : Revision 1.16  1999/10/13 00:42:31  chris
	** Log : Moving text into constants in preparation of localization support.
	** Log :
	** Log : Revision 1.15  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.14  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.13  1999/09/10 00:21:09  jj
	** Log : Changed include("addressfunctions") -> require("addressfunctions");
	** Log :
	** Log : Revision 1.12  1999/09/09 22:19:38  jj
	** Log : No longer any need to use the variable $addressFunctionsDefined.
	** Log :
	** Log : Revision 1.11  1999/09/07 21:42:05  jj
	** Log : Added code to display the first line of the address on order_confirm so that users can confirm their shipping destinations.
	** Log :
	** Log : Revision 1.10  1999/08/18 22:32:19  leon
	** Log : Returning opening PHP tag to XML-friendly tag
	** Log :
	** Log : Revision 1.9  1999/08/18 16:23:12  jj
	** Log : These screens now use addressFunctions for all of their address needs.
	** Log :
	** Log : Revision 1.8  1999/08/17 17:56:42  jj
	** Log : Modularized everything to do with addresses. Please see include/addressFunctions
	** Log :
	** Log : Revision 1.7  1999/08/11 19:43:32  jj
	** Log : Replaced all instances of eMail with email or Email (as fitting).
	** Log :
	** Log : Revision 1.6  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");

	//we need to calculate the total below
	include(APPLICATION_ROOT . "/modules/include/calculateTotal");

	if(USE_COUPONS)
	{
		include(APPLICATION_ROOT . "/modules/include/coupon");
	}
	
	$continueScript = TRUE;


	//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Main

	/*
	** It's possible the $bill_FirstName, etc. may already be set (if we just
	** came from VALIDATE_ORDER). It's also possible that it may not be set
	** (if we just returned from order_view_ibid). Hence, if $bill_FirstName,
	** etc. are not set, we'll have to get them again.
	*/
	if (isset($Ecom_BillTo_Postal_Name_First))
	{
		//get address from posted variables
		$billingAddress = getAddressFromEnvironment("Ecom_BillTo_");
	}
	else /* retrieve the stuff from the database */
	{
		//get billing record
		$Query = "SELECT Address, Method, CreditCardNumber, CreditCardExpiration ";
		$Query .= "FROM billing ";
		$Query .= "WHERE Invoice = " . $UserInfo['Invoice'] . " ";

		if(!($DatabaseResult = mysql_query($Query, $DatabaseLink)))
		{
			print("<!-- " . mysql_errno() . ": " . mysql_error() . 
				"   Query was $query -->\n");
			$continueScript = FALSE;
		}
		else
		{
			
			if(!($DatabaseRow = mysql_fetch_object($DatabaseResult)))
			{
				print("Unable to get the address and billing information " . 
					"attached to this invoice.<BR>\n");
				$continueScript = FALSE;
			}
			else
			{ 
				$AddressID = $DatabaseRow->Address;
			
				$Ecom_Payment_Card_Type = $DatabaseRow->Method;
				$Ecom_Payment_Card_Number = $DatabaseRow->CreditCardNumber;
				$Ecom_Payment_Expiration = $DatabaseRow->CreditCardExpiration;
			}
		}
		
		//get the address
		//Yes, we could do both in one query, but isn't it
		//nice to have less lines of code?  Plus, we really should
		//have a getBilling() function.
		$billingAddress = getAddress($AddressID);
	}



	/*
	** Give the user the billing contact information.
	*/
	if ($continueScript)
	{
		/* Output all of the billing contact information. */
		print("<BR><B>" . L_ORDERCONFIRM_BILLINGCONTACTINFO . "</B><BR>\n");
		print("<TABLE>\n");
		
		outputAddress($billingAddress, "ALL", FALSE);
		
		/* Billing method. */
		//this ought to become a function, too
		$Query = "SELECT Name ";
		$Query .= "FROM billing_method ";
		$Query .= "WHERE ID = '$Ecom_Payment_Card_Type'";

		$DatabaseResult = mysql_query($Query, $DatabaseLink);
		$DatabaseRow = mysql_fetch_row($DatabaseResult);
		
		//note mis-use of printTableRow
		//change later
		printTableRow(L_ORDERCONFIRM_BILLINGMETHOD, $DatabaseRow[0]);
		printTableRow(L_ORDERCONFIRM_CREDITCARDNUMBER, "xxxx xxxx xxxx " . 
			substr($Ecom_Payment_Card_Number,-4,4));
		$dateTime = split("[-]", $Ecom_Payment_Expiration);/* create month/year */
		printTableRow(L_ORDERCONFIRM_EXPIRATIONDATE, 
			($dateTime[1] . "/" . $dateTime[0]) );
			
		/* Finish up this section. */
		print("</TABLE><BR>\n");
	}
		
	
	
	/*
	** Output a table listing the invoice's contents. Each item should have
	** a link so that the user can check out the shipping information for that
	** particular item. It should also have the first part of the address for 
	** the shipping destination (for quick user verification).
	*/
	if ($continueScript)
	{
		/* Start this section. */
		print("<B>" . L_ORDERCONFIRM_INVOICECONTENTS . "</B><BR>\n");

		/* 
		** We'll have to get the list of sku's from the database. Note, in order
		** to get one field from the address, we'll need to join address via 
		** first joining shipping.
		*/
		$skuesQuery = "SELECT v.ID, v.Quantity, i.Name, s.Name, v.Notes, h.Address ";
		$skuesQuery .= "FROM item i, invoice_sku v, sku s, shipping h ";
		$skuesQuery .= "WHERE v.Invoice = " . $UserInfo['Invoice'] . " ";
		$skuesQuery .= "AND v.SKU = s.ID ";
		$skuesQuery .= "AND s.Item = i.ID ";
		$skuesQuery .= "AND h.ID = v.Shipping ";

		if ( !($skuesRecords = mysql_query($skuesQuery, $DatabaseLink)) )
		{
			print("<!-- " . mysql_errno() . ": " . mysql_error() . 
				"   Query was: $skuesQuery -->\n");
			$continueReport = FALSE;
		}
		/* Retrieve the invoice skues, if any. */ 
		else
		{	/* No skues in this invoice. */
			if (!mysql_num_rows($skuesRecords))
			{ 
				print("<P><I>" . L_ORDERCONFIRM_NOITEMS . "</I>\n");
			}
			/* Make a table full of the skues. */
			else
			{
				/* Start the table, and the headings. */ 
				print("<TABLE>\n<TR>\n");
				print("<TD><B>" . L_ORDERCONFIRM_NAME . "</B></TD>\n");
				print("<TD><B>" . L_ORDERCONFIRM_QUANTITY . "</B></TD>\n");
				print("<TD><B>" . L_ORDERCONFIRM_NOTES . "</B></TD>\n");
				print("<TD><B>" . L_ORDERCONFIRM_DESTINATION . "</B></TD>\n");
				print("</TR>\n");

				/* Output a row for each sku. */ 
				while ($skuRow = mysql_fetch_row($skuesRecords))
				{
					/* Parse the row. */
					list($skuID, $skuQuantity, $skuItemName, $skuSkuName, 
						$skuNotes, $AddressID) = $skuRow;

					//get address information
					$destination = getAddress($AddressID);

					$sku_Variation = unserialize($skuNotes);

					/* Output the row. */
					print("<TR>\n");
					/* 
					** Give the user a link to view this ibid in more detail. 
					** I'm using ibid to represent a particular ID in the 
					** invoice_sku table. Ibid -> "in-basket ID".
					*/
					print("<TD><A HREF=\"" . 
						ScreenURL("order_view_ibid", TRUE));
					print("&ibid=$skuID\">\n");
					print(prepareText($skuItemName) . // Item name + sku name 
						( $skuSkuName == "" ? "" :     // if there is one.
							": " . prepareText($skuSkuName) ) );
					print("</A></TD>\n");
					print("<TD ALIGN=\"CENTER\">$skuQuantity</TD>\n");

					print("<TD>\n");

					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("&nbsp;");
					print("</TD>\n");
					
					print("<TD>" . prepareText($destination["Street1"]) . "</TD>\n");
					print("</TR>\n");
				}

				/* Finish up the table. */ 
				print("</TABLE><BR>\n");			
			}
		}
	}
	
	
	
	/*
	** Output the totals for this invoice. 
	*/
	if ($continueScript)
	{
		/* Start the section. */
		print("<P>\n");

		print("<B>" . L_ORDERCONFIRM_INVOICETOTALS . "</B><BR>\n");

		print("<TABLE>\n");

		$Total = calculateTotal($UserInfo['Invoice']);

		showTotals($Total);

		/* Finish up the section. */
		print("</TABLE>\n");

		print("<BR>\n");
	}
	
	
	
	/*
	** Give the user the big submit button. 
	*/
	if ($continueScript)
	{
		/* Start the form here so that it doesn't interrupt our layout. */
		print(StartForm("order_thankyou", 'post', 'SUBMIT_ORDER', TRUE, 
			array("Ecom_SchemaVersion"=>"http://www.ecml.org/version1.0")));

		
		/* Start the section. */
		print("<B>" . L_ORDERCONFIRM_CONFIRMORDER . "?</B><BR>\n");

		/* Sub-table: button on left, cancel link on right. */
		print("<TABLE WIDTH=\"100%\"><TR>\n");
		print("<TD><INPUT TYPE=\"SUBMIT\" VALUE=\"" . L_ORDERCONFIRM_CONFIRMORDER . "\"></TD>\n");
		print("<TD><A HREF=\"" . ScreenURL("order_info", TRUE) . 
			"&amp;ACTION=CLEAN_INVOICE_FULL\">" . L_ORDERCONFIRM_RESTARTINVOICE . "</A></TD>\n");
		print("</TR></TABLE><BR>\n");

		/* Finish up this section. */
		print("</FORM>\n");
	}
	
?>
