<?php
	/*
	** File: quick_order
	** Description: list all SKUs organized by department and item, allow
	** user to add any to basket
	** Version: $Revision: 1.11 $
	** Created: 08/09/1999
	** Author: Leon Atkinson
	** Email: leon@clearink.com
	** CVS Author: $Author: leon $
	** Last Revised: $Date: 2000/02/05 00:32:17 $
	** Log : $Log: quick_order,v $
	** Log : Revision 1.11  2000/02/05 00:32:17  leon
	** Log : Missing parentheses
	** Log :
	** Log : Revision 1.10  2000/02/05 00:23:49  leon
	** Log : Removing number_format in favor of formatMoney
	** Log :
	** Log : Revision 1.9  1999/12/07 21:45:11  leon
	** Log : All forms should be using StartForm now...
	** Log :
	** Log : Revision 1.8  1999/11/08 22:32:30  leon
	** Log : Added prepareText() function to use everywhere we were using htmlentities.
	** Log :
	** Log : Revision 1.7  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.6  1999/10/26 22:32:21  leon
	** Log : Added "Add to Wishlist" button to item and quick_order pages.
	** Log :
	** Log : Revision 1.5  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.4  1999/10/12 22:39:38  leon
	** Log : Added support for language modules.  Note that while the structure is in
	** Log : place, efforts to move text out of screen and action modules is still
	** Log : underway.
	** Log :
	** Log : Revision 1.3  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.2  1999/08/18 22:32:21  leon
	** Log : Returning opening PHP tag to XML-friendly tag
	** Log :
	** Log : Revision 1.1  1999/08/09 23:01:41  leon
	** Log : Added new quick_order screen, added it to side nav, added link to attribute
	** Log : admin back to admin menu, and removed references to batches from build.sql
	** Log :
	**
	** Copyright (c) 1999 Working Dogs.  All rights reserved.
	*/

	//get item functions
	include(APPLICATION_ROOT . "/modules/include/item_functions");

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



	//get departments and items
	$Query = "SELECT d.ID, d.Name, i.ID, i.Name ";
	$Query .= "FROM department d, department_item di, item i ";
	$Query .= "WHERE d.ID = di.Department ";
	$Query .= "AND di.Item = i.ID ";
	$Query .= "ORDER BY d.DisplayPrecedence, d.Name, i.DisplayPrecedence, i.Name ";
	$DatabaseResult = mysql_query($Query, $DatabaseLink);


	while($DatabaseRow = mysql_fetch_row($DatabaseResult))
	{
		$department_ID[] = $DatabaseRow[0];
		$department_Name[] = $DatabaseRow[1];
		$item_ID[] = $DatabaseRow[2];
		$item_Name[] = $DatabaseRow[3];
	}
	

	//get sku giftwrap-ability
	$Query = "SELECT SKU ";
	$Query .= "FROM sku_variation ";
	$Query .= "WHERE Variation = " . getVariationID(getAttributeID("Gift Wrap"), "Available") . " ";
	$DatabaseResult = mysql_query($Query, $DatabaseLink);
	while($DatabaseRow = mysql_fetch_row($DatabaseResult))
	{
		$GiftWrap[($DatabaseRow[0])] = TRUE;
	}



	print("<TABLE BORDER=\"0\">\n");
	$last_Department = "";
	for($item_index=0; $item_index < count($item_ID); $item_index++)
	{
		//if a new department, show header
		if($department_Name[$item_index] != $last_Department)
		{
			print("<TR>\n");
			print("<TD COLSPAN=\"10\">&nbsp;</TD>\n");
			print("</TR>\n");

			print("<TR>\n");
			print("<TD COLSPAN=\"10\"><H3>$department_Name[$item_index]</H3></TD>\n");
			print("</TR>\n");
	
			$last_Department = $department_Name[$item_index];		
		}
		
		print("<TR>\n");
		print("<TD COLSPAN=\"10\"><B>$item_Name[$item_index]</B></TD>\n");
		print("</TR>\n");
	
		
		//get item info
		$Item = getItemInfo($item_ID[$item_index]);



		$BackorderedItems = FALSE;
		
		
		for($index = 0; $index < count($Item["SKU"]["ID"]); $index++)
		{

			print("<TR>\n");

			print(StartForm('quick_order', 'post', 'ADD_BASKET', FALSE, 
				array('sku'=>$Item["SKU"]["ID"][$index])));

			//gift wrap status		
			print("<TD VALIGN=\"center\">");
			if( isset($GiftWrap[($Item["SKU"]["ID"][$index])]) )
			{
				print("<FONT SIZE=\"1\">Gift Wrap<BR>Available</FONT>");
			}
			else
			{
				print("&nbsp;");
			}
			print("</TD>\n");

			print("<TD VALIGN=\"center\">");
			print(prepareText($Item["SKU"]["Name"][$index]));
			print("(" .prepareText($Item["SKU"]["ExternalSKU"][$index]). ")");
			print("</TD>\n");

			print("<TD VALIGN=\"center\">");
			if($Item["SKU"]["SalePrice"][$index] > 0)
			{
				print(" " . formatMoney($Item["SKU"]["SalePrice"][$index]));
			}
			else
			{
				print(" " . formatMoney($Item["SKU"]["ListPrice"][$index]));
			}

			if($Item["SKU"]["Shipping"][$index] > 0)
			{
				print(" (" . formatMoney($Item["SKU"]["Shipping"][$index]) . ")");
			}

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



			
			// get SKU attributes with more than one variation
			
			//get list of attributes as comma-separated list
			//this allows us to drop it right into the SQL
			$MultiAttributes = "0"; //seed with dummy value
			
			$Query = "SELECT v.Attribute, COUNT(v.Attribute) ";
			$Query .= "FROM sku_variation sv, variation v ";
			$Query .= "WHERE sv.Variation = v.ID ";
			$Query .= "AND sv.SKU = " . $Item["SKU"]["ID"][$index] . " ";
			$Query .= "GROUP BY v.Attribute ";
			$Query .= "ORDER BY 2 DESC ";
			$DatabaseResult = mysql_query($Query, $DatabaseLink);
			while($DatabaseRow = mysql_fetch_row($DatabaseResult))
			{
				if($DatabaseRow[1] > 1)
				{
					$MultiAttributes .= ", $DatabaseRow[0]";
				}
			}
			
			
			//get full variation info
			$Query = "SELECT a.ID, a.Name, v.ID, v.Name ";
			$Query .= "FROM sku_variation sv, variation v, attribute a ";
			$Query .= "WHERE sv.SKU = " . $Item["SKU"]["ID"][$index] . " ";
			$Query .= "AND sv.Variation = v.ID ";
			$Query .= "AND v.Attribute = a.ID ";
			$Query .= "AND a.ID in ($MultiAttributes) ";
			$Query .= "ORDER BY a.DisplayPrecedence, a.Name ";
			if ( !($DatabaseResult = mysql_query($Query, $DatabaseLink)) )
			{
				print("<!-- " . mysql_errno() . ": " . mysql_error() . 
					"Query was $Query -->\n");
			}
			else
			{
				if(mysql_num_rows($DatabaseResult))
				{
					$last_attr = 0;
					while($DatabaseRow = mysql_fetch_row($DatabaseResult))
					{
						$tAttributeID = $DatabaseRow[0];
						$tAttributeName = $DatabaseRow[1];
						$tVariationID = $DatabaseRow[2];
						$tVariationName = $DatabaseRow[3];

						if($last_attr != $tAttributeID)
						{
							if($last_attr != 0)
							{
								print("</SELECT>\n");
								print("</TD>\n");
							}

							print("<TD VALIGN=\"center\">\n");
							print("<SELECT NAME=\"inputVariation[]\">\n");
							print("<OPTION VALUE=\"$tVariationID\" SELECTED>");
							print(prepareText($tAttributeName));
							print("\n");

							$last_attr = $tAttributeID;
						} 

						print("<OPTION VALUE=\"$tVariationID\">");
						print(prepareText($tVariationName));
						print("\n");
					}
					print("</SELECT>\n");
					print("</TD>\n");
				}
			}	


			//submit button for adding item to basket
			print("<TD VALIGN=\"center\">");

			//check inventory levels
			if($Item["SKU"]["Available"][$index] + $Item["SKU"]["Reserve"][$index]  > 0)
			{
				print("<INPUT TYPE=\"SUBMIT\" NAME=\"basket_button\" VALUE=\"Add\">");
			}
			elseif($Item["SKU"]["Backorder"][$index] == 'Y')
			{
				print("<INPUT TYPE=\"SUBMIT\" NAME=\"basket_button\" VALUE=\"Backorder *\">");
				$BackorderedItems = TRUE;
			}
			else
			{
				print("<B>Out of Stock</B>");
			}

			//user must be logged in to use wishlist
			if($UserInfo["ID"] > 0)
			{
				print("<INPUT TYPE=\"SUBMIT\" NAME=\"wishlist_button\" VALUE=\"Add To Wishlist\">");
			}
			
			print("</TD>\n");
			
			print("</FORM>\n");
			
			print("</TR>\n");


		}


	}

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

?>