<?php
	/*
	** File: admin_invoice
	** Description: Create a nice html table listing the invoices. Each invoice
	**   should have a link to allow the admin to edit that particular invoice.
	** Version: $Revision: 1.12 $
	** Created: 6/28/1999
	** Author: Shannon -jj Behrens
	** Email: shannon.behrens@clearink.com
	** CVS Author: $Author: leon $
	** Last Revised: $Date: 1999/12/07 21:45:06 $
	** Log : $Log: admin_invoice,v $
	** Log : Revision 1.12  1999/12/07 21:45:06  leon
	** Log : All forms should be using StartForm now...
	** Log :
	** Log : Revision 1.11  1999/11/08 22:32:21  leon
	** Log : Added prepareText() function to use everywhere we were using htmlentities.
	** Log :
	** Log : Revision 1.10  1999/11/04 21:35:37  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.9  1999/10/18 23:26:04  jj
	** Log : Made it so that only admins can use dump_invoices.php3
	** Log :
	** Log : Revision 1.8  1999/10/18 19:02:35  jj
	** Log : Setup GUI for linking to dump_invoices script.
	** Log :
	** Log : Revision 1.7  1999/10/12 23:14:47  leon
	** Log : Moved localization constants into 'english' module.  Added constant to
	** Log : control whether unregistered shoppers may checkout.
	** Log :
	** Log : Revision 1.6  1999/10/12 00:39:07  chris
	** Log : Moving text into constants in preparation of localization support.
	** Log :
	** Log : Revision 1.5  1999/08/18 22:32:12  leon
	** Log : Returning opening PHP tag to XML-friendly tag
	** Log :
	** Log : Revision 1.4  1999/08/09 18:27:28  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.3  1999/07/28 19:27:29  leon
	** Log : Mass update based on code donation from a live project
	** Log :
	**
	** Copyright (c) 1999 Working Dogs.  All rights reserved.
	**
	** Expected input: possibly $limitStart 
	**   To facilitate listing only a limited number of invoices per page, 
	**   we'll use $limitStart to denote which invoice our list should start
	**   with. Naturally, we'll have "prev" and "next" links.
	*/

	/*
	** This is the number of invoices we'll list per page.
	*/
	define("MAX_INVOICES_PER_PAGE", 25);

	/* Goes FALSE if anything serious happens. */
	$proceedWithReport = TRUE;

	/* Make sure the user has admin rights. */
	if( !$UserInfo["Permission"]["Administrate"] )
	{
		print(L_ADMININVOICE_DENIED . "<BR>\n");
	}
	else
	{
		/* 
		** Give the user "prev" and "next" links, as well as a text box to 
		** jump to a specific invoice.
		*/
		/* Start the section. */
		print("<TABLE BORDER=\"1\" WIDTH=\"100%\"><TR><TD>\n");
		print("<B>" . L_ADMININVOICE_INVOICENAVIGATION . "</B><BR>\n");
		print("<UL>\n");

		/* "prev" and "next" links (all based on the value of $limitStart) */
		print("<LI>");
		/* Don't let $limitStart go below 0. */
		$limitStart = isset($limitStart) ? $limitStart : 0;
		if ($limitStart == 0)
		{
			print(L_ADMININVOICE_PREV . "\n");
		}
		else
		{
			print("<A HREF=\"" . ScreenURL("admin_invoice"));
			print("&limitStart=" . intval($limitStart - MAX_INVOICES_PER_PAGE));
			print("\">" . L_ADMININVOICE_PREV . "</A>\n");
		}
		print(" | ");
		print("<A HREF=\"" . ScreenURL("admin_invoice"));
		print("&limitStart=" . intval($limitStart + MAX_INVOICES_PER_PAGE));
		print("\">" . L_ADMININVOICE_NEXT . "</A>");
		print(" Group\n");

		/* Give the user a text box so that he can manually choose an invoice. */
		print(StartForm("edit_invoice", 'post', '', FALSE));


		print("<LI>" . L_ADMININVOICE_JUMPTOINVOICE);
		print("<INPUT TYPE=\"text\" NAME=\"invoice_ID\" SIZE=\"7\">\n");
		print("<INPUT TYPE=\"submit\" VALUE=\"" . L_ADMININVOICE_GO . "\">\n");
		print("</FORM>\n");

		/* 
		** Give the user two "date boxes" so that he can download an invoice dump
		** of all invoices between two dates. Only the days are important here, the 
		** other side will discard the hours, minutes, and seconds.
		*/
		include(APPLICATION_ROOT . "/modules/include/DateSelector");
		$startTime = time() - 60*60*24*1; 
		$endTime = time();

		/* Figure out the path to the dump_invoices.php3 script (in admin dir). */
		//note the StartForm can't be used here
		$https = USE_SSL ? "https" : "http";
		$path = ereg_replace("/[^/\\]*$", "/admin/dump_invoices.php3", #"
			$SERVER_NAME . $SCRIPT_NAME);
		print("<FORM METHOD=\"POST\" ACTION=\"$https://$path\">\n");
		print("<INPUT TYPE=\"hidden\" NAME=\"sid\" VALUE=\"$sid\">\n");
		
		print("<LI>" . L_ADMININVOICE_DOWNLOADINVOICESBETWEEN . "<BR>\n");
		
		/* Output the GUI. */
		print("<TABLE>\n");
		print("<TR><TD>" . L_ADMININVOICE_STARTINGDATE . "</TD>\n<TD>");
		DateSelector("start", $startTime);
		/* Will output html names startMonth, startDay, startYear */
		print("</TD></TR>\n");
		print("<TR><TD>" . L_ADMININVOICE_ENDINGDATE . "</TD>\n<TD>");
		DateSelector("end", $endTime);
		/* Will output html names endMonth, endDay, endYear */   
		print("</TD></TR>\n");
		print("</TABLE>\n");
		
		print("<INPUT TYPE=\"submit\" VALUE=\"" . L_ADMININVOICE_DOWNLOADINVOICES . 
			"\">\n");
		print("</FORM>\n");

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



		/* 
		** Get the list of invoices. 
		*/
		$query = "SELECT i.Invoice, s.Name, i.Created, i.Description ";
		$query .= "FROM status s, invoice_status i, invoice n ";
		$query .= "WHERE i.Status = s.ID AND i.Invoice = n.ID AND n.Active != 'N' ";
		$query .= "ORDER BY i.Invoice DESC, i.Created DESC ";
		$query .= "LIMIT " . intval($limitStart) . ", " . 
			(intval($limitStart) + MAX_INVOICES_PER_PAGE);
		/* Execute the query */ 
		if ( !($invoiceRecords = mysql_query($query, $DatabaseLink)) )
		{
			print("<!-- " . mysql_errno() . ": " . mysql_error . 
				"   Query was: $query -->\n");
			$proceedWithReport = FALSE;
		}
		else 
		{
			/* If there are no invoices, just say so. */
			if (!mysql_num_rows($invoiceRecords))
			{
				print("<TABLE BORDER=\"1\" WIDTH=\"100%\"><TR><TD>\n");
				print("<B>" . L_ADMININVOICE_INVOICEREPORT . "</B>\n");
				print("<BLOCKQUOTE>" . L_ADMININVOICE_NOMOREINVOICES . "</BLOCKQUOTE>\n");
				print("</TD></TR>\n</TABLE>\n");
				$proceedWithReport = FALSE; /* Not a serious error, however. */
			}
			/* 
			** Otherwise, we'll just leave the records in $invoiceRecords for 
			** a while.
			*/
		}



		/* 
		** If everything's okay so far, we can output the list of invoices.
		** Since there are multiple invoice_status records for every invoice, we'll
		** only output the most current. That means we'll have to skip several of 
		** the records.
		*/
		if ($proceedWithReport)
		{
			/* Start the table and do the headings. */
			print("<TABLE BORDER=\"1\" WIDTH=\"100%\"><TR><TD>\n");
			print("<TABLE>\n");
			print("<TR><TD COLSPAN=\"3\">\n");
			print("<B>" . L_ADMININVOICE_INVOICEREPORT . "</B>\n");
			print("</TD></TR>\n");
			print("<TR>\n");
			print("<TD><B>" . L_ADMININVOICE_INVOICE . "</B></TD>\n");
			print("<TD><B>" . L_ADMININVOICE_STATUS . "</B></TD>\n");
			print("<TD><B>" . L_ADMININVOICE_DATE . "</B></TD>\n");
			print("<TD><B>" . L_ADMININVOICE_NOTES . "</B></TD>\n");
			print("</TR>\n");

			$currentInvoice = -1;
			while ($row = mysql_fetch_row($invoiceRecords))
			{
				/* 
				** If this is just another status record for the same invoice, 
				** skip it. 
				*/
				if ($currentInvoice == $row[0])
				{
					continue;
				}
				/* Nope, it's a new invoice. */
				else
				{
					$currentInvoice = $row[0];

					/* Parse the row. */
					$i = 0;
					$ID = $row[$i++];
					$Status = $row[$i++];
					$Created = $row[$i++];
					$Description = $row[$i++];

					/* Output it. */
					print("<TR>\n");
					print("<TD><A HREF=\"" . ScreenURL("edit_invoice") .
						"&invoice_ID=$ID\">");
					print("$ID</A></TD>\n");
					print("<TD>" . prepareText($Status) . "</TD>\n");
					print("<TD>$Created</TD>\n");
					print("<TD>" . prepareText($Description) . "</TD>\n");
					print("</TR>\n");
				}
			}

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