<?php 

	/* 
	** File: edit_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 edit that particular record based on the ibid.
	** Version: $Revision: 1.18 $
	** Created: 6/29/99
	** Author: Shannon -jj Behrens
	** Email: shannon.behrens@clearink.com
	** CVS Author: $Author: leon $
	** Last Revised: $Date: 1999/12/08 00:55:09 $
	** Log : $Log: edit_ibid,v $
	** Log : Revision 1.18  1999/12/08 00:55:09  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.17  1999/12/07 21:45:08  leon
	** Log : All forms should be using StartForm now...
	** Log :
	** Log : Revision 1.16  1999/12/06 21:17:25  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.15  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.14  1999/11/08 22:32:24  leon
	** Log : Added prepareText() function to use everywhere we were using htmlentities.
	** Log :
	** Log : Revision 1.13  1999/11/04 21:35:41  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:01  chris
	** Log : Moving text into constants in preparation of localization support.
	** Log :
	** Log : Revision 1.11  1999/10/13 00:42:27  chris
	** Log : Moving text into constants in preparation of localization support.
	** Log :
	** Log : Revision 1.10  1999/10/11 23:04:09  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/08/18 22:32:15  leon
	** Log : Returning opening PHP tag to XML-friendly tag
	** Log :
	** Log : Revision 1.8  1999/08/17 18:41:01  jj
	** Log : Got rid of dataRow everywhere in favor of functions from addressFunctions
	** Log :
	** Log : Revision 1.7  1999/08/17 17:56:41  jj
	** Log : Modularized everything to do with addresses. Please see include/addressFunctions
	** Log :
	** Log : Revision 1.6  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.5  1999/08/10 18:01:47  jj
	** Log : Created and used an addressEditor function rather than cutting and pasting everytime.
	** Log :
	** Log : Revision 1.4  1999/08/09 22:30:30  jj
	** Log : Any selects for Country now use makeCountrySelect (an include)
	** 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:09  leon
	** Log : Mass update based on code donation from a live project
	** Log :
	**
	** Copyright (c) 1999 Working Dogs. All rights reserved. 
	**
	** Expected input: $ibid
	*/

	/* For creating a form to edit an address. */
	include(APPLICATION_ROOT . "/modules/include/addressFunctions");

	//get attribute functions
	include(APPLICATION_ROOT . "/modules/include/attribute_functions");
	
	$continueReport = TRUE;

	/* 
	** Make sure the user has admin rights. 
	*/
	if( !$UserInfo["Permission"]["Administrate"] )
	{
		print(L_EDITIBID_DENIED . "<BR>\n");
	}
	else
	{
	
	
	
		/* 
		** First of all, get the record attached to this ibid from the 
		** invoice_sku table. 
		*/
		$invoiceSkuQuery = "SELECT v.Invoice, v.SKU, v.ExternalSKU, v.Quantity, " . 
			"i.Name, s.Name, v.Notes, v.ListPrice, v.SalePrice, v.Freight, " . 
			"v.GiftWrap, v.Shipping ";
		$invoiceSkuQuery .= "FROM item i, invoice_sku v, sku s ";
		$invoiceSkuQuery .= "WHERE v.ID = " . intval($ibid) . " ";
		$invoiceSkuQuery .= "AND v.SKU = s.ID ";
		$invoiceSkuQuery .= "AND s.Item = i.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_EDITIBID_INVALIDIBID . "</I>\n");
				$continueReport = FALSE;
			}
			/* Yehaw! We have a valid IBID! Let's parse it. */
			else
			{
				list($ibidInvoice, $ibidSKU, $ibidExternalSKU, $ibidQuantity,
					$ibidItemName, $ibidSKUName, $ibidNotes, $ibidListPrice, 
					$ibidSalePrice, $ibidFreight, $ibidGiftWrap, 
					$ibidShipping) = 
				$invoiceSkuRecord;
				
				$ibidVariation = unserialize($ibidNotes);
			}
		}



		/* 
		** If everything is okay so far, we can go ahead and get the shipping 
		** information connected to this IBID.
		*/ 
		if ($continueReport)
		{
			$shippingQuery = "SELECT Invoice, Address, Method, Message ";
			$shippingQuery .= "FROM shipping WHERE ID = " . $ibidShipping;
			/* 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;
					$shippingInvoice = $shippingRecord[$i++];
					$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("<B>" . L_EDITIBID_EDITINGIBID . intval($ibidInvoice) . "\"</B>\n");
			print("<UL>\n");
			print("<LI><A HREF=\"" . ScreenURL("edit_invoice"));
			print("&invoice_ID=$ibidInvoice\">");
			print(L_EDITIBID_RETURN . "</A> " . L_EDITIBID_MAININVOICESCREEN . "\n");
			print("<LI><A HREF=\"" . ScreenURL("admin_invoice"));
			print("\">" . L_EDITIBID_RETURN . "</A> " . L_EDITIBID_COMPLETELIST . "\n");
			print("</UL>\n");
			print("</TABLE>\n");
		}



		/*	
		** If everything's okay so far, we can output the main invoice_sku
		** information. We'll put it in a form so that it's all editable. 
		*/
		if ($continueReport)
		{
			/* Start the table and the form. */
			//why is $ibid submitted twice? FIX ME
			print(StartForm("edit_ibid", 'post', 'UPDATE_IBID', FALSE, 
				array("ID"=>$ibid, "ibid"=>$ibid)));
			
			print("<TABLE WIDTH=\"100%\" BORDER=1>\n<TR><TD>\n");

			/* Print the data. Put it in a table with no borders. */
			print("<B>" . L_EDITIBID_MAINIBID . "</B>\n");
			print("<TABLE>\n");
			printTableRow(L_EDITIBID_SKU, $ibidSKU);
			printEditRow(L_EDITIBID_QUANTITY, "Quantity", $ibidQuantity);
			printTableRow(L_EDITIBID_EXTERNALSKU, $ibidExternalSKU);
			printTableRow(L_EDITIBID_NAME, /* Item name + SKU name (if present) */
				$ibidItemName . ( $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_EDITIBID_LISTPRICE, $ibidListPrice);
			printEditRow(L_EDITIBID_SALEPRICE, "SalePrice", $ibidSalePrice);
			printEditRow(L_EDITIBID_FREIGHT, "Freight", $ibidFreight);
			
			/* Output GiftWrap status in a select. */
			print("<TR><TD>" . L_EDITIBID_GIFTWRAP . "</TD>\n<TD>");
			print("<SELECT NAME=\"GiftWrap\">\n");
			if ($ibidGiftWrap=='Y')
			{
				print("<OPTION VALUE=\"Y\" SELECTED>Yes\n");
				print("<OPTION VALUE=\"N\">No\n");
			}
			else
			{
				print("<OPTION VALUE=\"Y\">Yes\n");
				print("<OPTION VALUE=\"N\" SELECTED>No\n");
			}
			print("</SELECT></TD>\n</TR>\n");
			
			print("</TABLE>\n");

			/* End the table and the form. */
			print("<INPUT TYPE=\"submit\" VALUE=\"" . L_EDITIBID_SUBMIT . "\">\n");
			print("<INPUT TYPE=\"reset\" VALUE=\"" . L_EDITIBID_RESET . "\">\n");
			print("</FORM>\n");
			print("</TD></TR>\n</TABLE>\n");
		}



		/*
		** If everything's okay so far, we can output the main shipping information
		** for this ibid. We'll put it in a form so that it's all editable. 
		*/ 
		if ($continueReport)
		{
			/* Start the table and the form. */ 
			print(StartForm("edit_ibid", 'post', 'UPDATE_SHIPPING', FALSE, 
				array("ID"=>$ibidShipping, "ibid"=>$ibid)));

			print("<TABLE WIDTH=\"100%\" BORDER=1>\n<TR><TD>\n");

			/* Print the data. Put it in a table with no borders. */
			print("<B>" . L_EDITIBID_SHIPPINGINFO . "</B>:");
			print("<TABLE>\n");
			print("<TR><TD>" . L_EDITIBID_SHIPPINGMETHOD . "</TD>\n<TD>");
			print("<TD><SELECT NAME=\"Method\">\n");
			/* Get the valid list of possible shipping methods. */
			$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($DatabaseRow[0] == $shippingMethod)
				{
					print(" SELECTED");
				}
				print(">" . prepareText($DatabaseRow[1]) . "\n");
			}
			print("</SELECT></TD></TR>\n");
			printEditRow(L_EDITIBID_SHIPPINGMESSAGE, "Message", $shippingMessage);
			print("</TABLE>\n");

			/* End the table and the form. */
			print("<INPUT TYPE=\"submit\" VALUE=\"" . L_EDITIBID_SUBMIT . "\">\n");
			print("<INPUT TYPE=\"reset\" VALUE=\"" . L_EDITIBID_RESET . "\">\n");
			print("</FORM>\n");
			print("</TD></TR>\n</TABLE>\n");
		}



		/*
		** If everything's okay so far, we can output the shipping destination 
		** information.  We'll put it in a form so that it's all editable. 
		*/ 
		if ($continueReport)
		{
			/* Start the table and the form. */ 
			print(StartForm("edit_ibid", 'post', 'UPDATE_ADDRESS', FALSE, 
				array("addressID"=>$shippingAddress, "ibid"=>$ibid)));

			print("<TABLE WIDTH=\"100%\" BORDER=1>\n<TR><TD>\n");
			print("<B>" . L_EDITIBID_SHIPPINGDESTINATION . "</B>\n");

			printAddressFields(
				getAddress($shippingAddress), 
				"input", 
				"ALL");
			
			print("</TD></TR></TABLE>\n");
			print("</FORM>\n");
		}		
	}

?>
		
