<?php

	/*
	** File: order_history
	** Description: Create a nice html table listing the invoices for this 
	**   user. Each invoice should have a link to allow the user to view that 
	**   particular invoice. Notice, this file is 'conspicuously' similar to 
	**   admin_invoice, which is the Admin version of this script.
	** Version: $Revision: 1.8 $
	** Created: 7/8/99
	** Author: Shannon -jj Behrens
	** Email: shannon.behrens@clearink.com
	** CVS Author: $Author: leon $
	** Last Revised: $Date: 1999/11/08 22:32:29 $
	** Log : $Log: order_history,v $
	** Log : Revision 1.8  1999/11/08 22:32:29  leon
	** Log : Added prepareText() function to use everywhere we were using htmlentities.
	** Log :
	** Log : Revision 1.7  1999/10/13 22:19:05  chris
	** Log : Moving text into constants in preparation of localization support.
	** Log :
	** Log : Revision 1.6  1999/10/13 00:42:32  chris
	** Log : Moving text into constants in preparation of localization support.
	** Log :
	** Log : Revision 1.5  1999/09/24 01:13:37  jj
	** Log : Fixed bug that would allow users not logged in to see other anonymous user's order history (providing they hacked the URL).
	** Log :
	** Log : Revision 1.4  1999/08/24 00:28:38  jj
	** Log : Re-installed code that would let mortal users see status descriptions.
	** Log :
	** Log : Revision 1.3  1999/08/18 22:32:19  leon
	** Log : Returning opening PHP tag to XML-friendly tag
	** Log :
	** Log : Revision 1.2  1999/08/09 18:27:29  jj
	** Log : Got rid of invoice status "Description" field any place where user could see it. God rid of any mention of batches (since Batch is not in build.sql).
	** Log :
	** Log : Revision 1.1  1999/07/28 19:41:12  leon
	** Log : Mass update based on code donation from a live project
	** Log :
	**
	** Copyright (c) 1999 Working Dogs.  All rights reserved.
	**
	** Expected Input: $sid
	*/
	
	$okayToProceed = TRUE;
	
	
	/*
	** Only properly logged in users can view their order history.
	*/
	$userID = $UserInfo["ID"];
	if ($userID<1)
	{
		$okayToProceed = FALSE;
		print(L_ORDERHISTORY_MUSTBELOGGEDIN . "<BR>\n");
	}
	
	
	
	/*
	** Use the $userID to get the list of invoices attached to this user
	** from the invoice table. 
	*/
	if ($okayToProceed)
	{
		$query = "SELECT n.ID, s.Name, i.Created, i.Description ";
		$query .= "FROM invoice n, status s, invoice_status i ";
		$query .= "WHERE n.ID = i.Invoice AND i.Status = s.ID ";
		$query .= "AND n.User=" . $userID . " ";
		$query .= "AND n.Active='Y' ";
		$query .= "ORDER BY i.Invoice DESC, i.Created DESC";
		/* Execute the query. */ 
		if ( !($databaseResults = mysql_query($query, $DatabaseLink)) )
		{
        		print("<!-- " . mysql_errno() . ": " . mysql_error . 
                		"   Query was: $query -->\n");
		}
		else
		{
			/* If there are no invoices, just say so. */
			if (!mysql_num_rows($databaseResults))
			{
				print("<P>" . L_ORDERHISTORY_NOPREVIOUSORDERS . "\n");
			}
			/* Otherwise, list all the invoices. */ 
			else
			{
				/* Do the headings. */
				print("<TABLE BORDER=\"1\"><TR><TD>\n");
				print("<table>\n");
				print("<tr><td colspan=3>\n");
				print("<b>" . L_ORDERHISTORY_PASTORDERS . "</b>\n");
				print("</td></tr>\n");
				print("<tr>\n");
				print("<td><b>" . L_ORDERHISTORY_INVOICE . "</b></td>\n");
				print("<td><b>" . L_ORDERHISTORY_STATUS . "</b></td>\n");
				print("<td><b>" . L_ORDERHISTORY_DATE . "</b></td>\n");
				print("<TD><B>" . L_ORDERHISTORY_NOTES . "</B></TD>\n");
				print("</tr>\n");

				/* Do the contents. */
				$currentInvoice = -1;
				while ($row = mysql_fetch_row($databaseResults))
				{
					/*
					** If this is just another status record for the same 
					** invoice, skip it. We only want to show the most 
					** current status record.
					*/
					if ($currentInvoice == $row[0])
					{
						continue;
					}
					/* Nope, it's a new invoice. */
					else
					{
						$i = 0;
						$currentInvoice = $row[$i++];
						$status = $row[$i++];
						$date = $row[$i++];
						$description = $row[$i++];
						print("<tr>\n");
						print("<td><a href=\"" . ScreenURL("view_invoice") .
							"&invoice_ID=$currentInvoice\">");
						print("$currentInvoice</a></td>\n");
						print("<td>" . prepareText($status) . "</td>\n");
						print("<td>$date</td>\n");
						print("<TD>" . prepareText($description) . "</TD>\n");
						print("</tr>\n");
					}
				}

				print("</table>\n");
				print("</TD></TR></TABLE>\n");
			}
		}
	}
	
?>
