<?php
	/*
	** File: VALIDATE_ORDER
	** Description: check billing info for validity, add to invoice
	** Here, we're only checking that the credit card number is
	** valid, not authorizing the charge.  Also check that coupons
	** are good.
	**
	** Version: $Revision: 1.24 $
	** Created: 06/14/1999
	** Author: Leon Atkinson, Shannon -jj Behrens
	** Email: leon@clearink.com, shannon.behrens@clearink.com
	** CVS Author: $Author: leon $
	** Last Revised: $Date: 1999/12/21 20:54:27 $
	**
	**
	** Copyright (c) 1999 Working Dogs.  All rights reserved.
	*/

	//insertAddress function
	include(APPLICATION_ROOT . "/modules/include/addressFunctions");

	//get credit card validation function
	include(APPLICATION_ROOT . "/modules/include/validateCard");

	//get calculateTotal
	include(APPLICATION_ROOT . "/modules/include/calculateTotal");

	//get functions for manipulating billing records
	include(APPLICATION_ROOT . "/modules/include/billing");

	//coupon functions
	if(USE_COUPONS)
	{
		include(APPLICATION_ROOT . "/modules/include/coupon");
	}

	//boolean for controlling when if the
	//script may continue
	$continue = TRUE;

	//check that the user has an invoice in progress
	if($UserInfo["Invoice"] <= 0)
	{
		$continue = FALSE;
		$ActionResults[] = L_VALIDATEORDER_NO_INVOICE;
	}

	//get the totals for the invoice
	if($continue)
	{
		$Total = calculateTotal($UserInfo["Invoice"]);
	}

	if(USE_COUPONS)
	{
		//clean up input
		$bill_coupon = trim($bill_coupon);
	}
	
	/*
	** Make sure that we have the necessary information for a valid 
	** order.
	*/
	if($continue)
	{
		//get address from posted variables
		$billingAddress = getAddressFromEnvironment("Ecom_BillTo_");
	
		//check for required fields	
		if(!($AddressIsGood = verifyAddress(
			array("Name_First", 
				"Name_Last", 
				"Street1",	
				"City", 
				"StateProv", 
				"CountryCode", 
				"PostalCode", 
				"Email",
				"Phone1"), 
			$billingAddress, &$ErrorText)))
		{
			$continue = FALSE;
			$SCREEN = "order_billing";
			$ActionResults[] =  $ErrorText;
		}
		
		if($Ecom_Payment_Card_Number == "")
		{
			$continue = FALSE;
			$ActionResults[] =  L_VALIDATEORDER_NEED_CREDITCARD . "<BR>\n";
			$SCREEN = "order_billing";
		}
	}

	
	/*
	** Check that the credit card is not expired 
	*/
	if($continue)
	{
		if(($Ecom_Payment_Card_ExpDate_Year==0) OR ($Ecom_Payment_Card_ExpDate_Month==0))
		{
			$ActionResults[] =  L_VALIDATEORDER_WEIRD_DATES;
			$continue = FALSE;
			$SCREEN = "order_billing";
		}
		elseif((date("Y") > $Ecom_Payment_Card_ExpDate_Year) OR
			((date("Y") == $Ecom_Payment_Card_ExpDate_Year) AND
				(date("m") > $Ecom_Payment_Card_ExpDate_Month)) 
			)
		{
			$continue = FALSE;
			$ActionResults[] =  L_VALIDATEORDER_EXPIRED;
			$SCREEN = "order_billing";
		}
		else 
		{
			$Ecom_Payment_Expiration = 
				$Ecom_Payment_Card_ExpDate_Year . 
				"-" . 
				$Ecom_Payment_Card_ExpDate_Month . 
				"-01 00:00:00";
		}
	}	
	
	


	/*
	** Demo card numbers
	** Visa				4111 1111 1111 1111   
	** MasterCard		5500 0000 0000 0004   
	** American Express	3400 0000 0000 009   
	** Discover			6011 0000 0000 0004   
	**
	** Make sure this number is valid using our validateCard function.
	** If it is, go ahead and remove everything associated with this invoice
	** (in case the user just hit the back button, we don't want multiple 
	** entries).
	*/
	if($continue)
	{
		if(!($Ecom_Payment_Card_Type = validateCard($Ecom_Payment_Card_Number)))
		{
			//card did not validate
			$continue = FALSE;
			$ActionResults[] =  L_VALIDATEORDER_INVALID;
			$SCREEN = 'order_billing';
		}
	}


	/*
	** Validate and set coupons
	*/
	if(USE_COUPONS AND ($bill_coupon != ""))
	{
		if(validateCouponList($bill_coupon, $Total['Grand'], $Reason))
		{
			//coupons are OK, so save fees
			if(!(useCouponList($bill_coupon, $UserInfo['Invoice'])))
			{
				//problems with the coupons, so try again
				$continue = FALSE;
				$ActionResults[] = "An error occurred while using your coupons!";
				$SCREEN="order_billing";
			}
		}
		else
		{
			//problems with the coupons, so try again
			$continue = FALSE;
			$ActionResults[] =  $Reason;
			$SCREEN="order_billing";
		}
	}

	
	if($continue)
	{
		//remove any previous billing records
		destroyInvoiceBilling($UserInfo['Invoice']);

		/*
		** create billing record (and its address record)
		*/
		$Ecom_Payment_NameOnCard = 
			$billingAddress["Name_First"] . " " . $billingAddress["Name_Last"];

		if(!createBilling(
			$UserInfo['Invoice'], 
			$billingAddress, 
			$Ecom_Payment_Card_Type, 
			$Ecom_Payment_NameOnCard,
			$Ecom_Payment_Card_Number, 
			$Ecom_Payment_Expiration))
		{
			$ActionResults[] =  L_VALIDATEORDER_CREATEBILL . "<BR>\n";
			$SCREEN = "order_billing";
		}
	}

?>
