<?php
	/*
	** File: admin_coupon
	** Description: administrate coupons
	** Version: $Revision: 1.6 $
	** Created: 11/2/99
	** Author: Chris Bellew
	** Email: chris@clearink.com
	** CVS Author: $Author: leon $
	** Last Revised: $Date: 1999/12/07 21:45:05 $
	** Log : $Log: admin_coupon,v $
	** Log : Revision 1.6  1999/12/07 21:45:05  leon
	** Log : All forms should be using StartForm now...
	** Log :
	** Log : Revision 1.5  1999/11/15 19:43:18  leon
	** Log : localized coupon modules.
	** Log :
	** Log : Revision 1.4  1999/11/08 22:32:21  leon
	** Log : Added prepareText() function to use everywhere we were using htmlentities.
	** Log :
	** Log : Revision 1.3  1999/11/04 21:35:36  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.2  1999/11/02 20:35:43  leon
	** Log : Cleaned up coupon code a bit.
	** Log :
	** Log : Revision 1.1  1999/11/02 18:23:12  chris
	** Log : Added support for coupons.
	** Log :
	**
	** Copyright (c) 1999 Working Dogs.  All rights reserved.
	*/
	
	// We need this to draw pretty boxes
	include(APPLICATION_ROOT . "/modules/include/box");


	
	/* Make sure the user has admin rights. */
	if( !$UserInfo["Permission"]["Administrate"] )
	{
		print(L_ADMINCOUPON_DENIED . "<BR>\n");
	}
	else
	{
		startBox();
	
	
		/*
		** open coupon information table, and column headers
		*/
		print("<TABLE BORDER=\"0\">\n");

		print("<TR>\n");
		print("<TH>".L_ADMINCOUPON_NAME."</TH>\n");
		print("<TH>".L_ADMINCOUPON_DISCOUNT."</TH>\n");
		print("<TH>".L_ADMINCOUPON_MINIMUM."</TH>\n");
		print("<TH>".L_ADMINCOUPON_DATES."</TH>\n");
		print("<TH>".L_ADMINCOUPON_COMBINE."</TH>\n");
		print("<TH>".L_ADMINCOUPON_ORDER."</TH>\n");
		print("<TH>".L_ADMINCOUPON_ACTION."</TH>\n"); // for delete link
		print("</TR>\n");
		

		/*
		** Get all coupons.
		*/
		$Query = "SELECT ID, Name, DollarOff, PercentageOff, MinAmountPurchased, ";
		$Query .= "DATE_FORMAT(StartDate, '%Y/%m/%d'), DATE_FORMAT(EndDate, '%Y/%m/%d'), ";
		$Query .= "NeverExpires, Combineable, DisplayPrecedence ";
		$Query .= "FROM coupon ";
		$Query .= "ORDER BY DisplayPrecedence, Name ";
		
		if ( !($DatabaseResult = mysql_query($Query, $DatabaseLink)) )
		{
			print("<!-- " . mysql_errno() . ":  " . mysql_error() . 
				"   Query was $Query -->\n");
			$continueScript = FALSE;
		}
		else
		{
			while($DatabaseRow = mysql_fetch_row($DatabaseResult))
			{
				$coupons["ID"][] = $DatabaseRow[0];
				$coupons["Name"][] = $DatabaseRow[1];
				$coupons["DollarOff"][] = $DatabaseRow[2];
				$coupons["PercentageOff"][] = $DatabaseRow[3];
				$coupons["MinAmountPurchased"][] = $DatabaseRow[4];
				$coupons["StartDate"][] = $DatabaseRow[5];
				$coupons["EndDate"][] = $DatabaseRow[6];
				$coupons["NeverExpires"][] = $DatabaseRow[7];
				$coupons["Combineable"][] = $DatabaseRow[8];
				$coupons["DisplayPrecedence"][] = $DatabaseRow[9];
	
			}
		}
	
		/* Output the coupons. */
		for($index=0; $index < mysql_num_rows($DatabaseResult); $index++)
		{
			$ID = $coupons["ID"][$index];
			$Name = $coupons["Name"][$index];
			$DollarOff = $coupons["DollarOff"][$index];
			$PercentageOff = $coupons["PercentageOff"][$index];
			$MinAmountPurchased = $coupons["MinAmountPurchased"][$index];
			$StartDate = $coupons["StartDate"][$index];
			$EndDate = $coupons["EndDate"][$index];
			$NeverExpires = $coupons["NeverExpires"][$index];
			$Combineable = $coupons["Combineable"][$index];
			$DisplayPrecedence = $coupons["DisplayPrecedence"][$index];

			print("<TR>\n");
			
			/* 
			** Coupon Name
			*/			
			print("<TD>");
			print("<A HREF=\"" . ScreenURL("edit_coupon", FALSE, 
				array("coupon"=>$ID)) . "\">" . 
				prepareText($Name) . "</A>\n");
			print("</TD>\n");

			/* 
			** Dollar Amount Off or Percentage Amount Off
			*/
			print("<TD>");
			
			if($DollarOff > 0.00)
			{
				print(formatMoney($DollarOff));
			}
			else
			{
				print(prepareText($PercentageOff) . "%");
			}

			/* 
			** Minimum Amount Purchased
			*/			
			print("<TD>");
			print(formatMoney($MinAmountPurchased));
			print("</TD>\n");

			/* 
			** Start and End Dates
			*/		
			print("<TD>");

			print($StartDate);
			
			print(" - ");
			
			if($NeverExpires)
			{
				//never expires
				print(L_ADMINCOUPON_NEVER_EXPIRES);
			}
			else
			{
				//show date range
				print($EndDate);
			}
			
			print("</TD>\n");

			
			/*
			** Can be combined with others
			*/
			print("<TD ALIGN=\"CENTER\">");
			if($Combineable)
			{
				print(L_ADMINCOUPON_YES);
			}
			print("</TD>\n");


			/*
			** Execution Order
			*/
			print("<TD ALIGN=\"CENTER\">$DisplayPrecedence</TD>\n");


			/*
			** Delete link
			*/
			print("<TD ALIGN=\"CENTER\">");
			print("<A HREF=\"" . ScreenURL('admin_coupon', FALSE, 
				array("ACTION"=>"DELETE_COUPON", "coupon"=>$ID)) . 
				"\">" . L_ADMINCOUPON_DELETE . "</A>");
			print("</TD>\n");
			
			print("</TR>\n");
		} /* Done outputting list of items. */
	

		
		/*
		** end coupon information table
		*/
		print("</TABLE>\n");

		/*
		** Explanations
		*/
		print("Click on the Coupon Name to edit that coupon.<BR>\n");
		
		print("<A HREF=\"" . ScreenURL('admin_coupon', FALSE, array("ACTION"=>"ADD_COUPON")) . 
			"\">".L_ADMINCOUPON_ADD."</A><BR>\n");

		
		/*
		** end outside table
		*/
		endBox();
		

	} // end if user is admin
?>
