############################################################
#
#  sub CheckBuyerLimit
#
#  Check shopping cart against customer spending limit
#
#  Parameters: 0 - Shopping cart ID
#              1 - Destination URL for bouncing page (optional)
#					2 - Frame clear flag (if true the target will be set to top)
#
#  Returns:	0 - status (success - OK, failure - exceeded limit)
#  				1 - HTML to be printed if STATUS is not SUCCESS
#
#  Ryszard Zybert  Jun  2 20:40:47 BST 2000
#
#  Copyright (c) Actinic Software Ltd (2000)
#
############################################################

sub CheckBuyerLimit
	{
	my ($sCartId, $sDestinationURL, $bClearFrame) = @_;
	my $sDigest = $ACTINIC::B2B->Get('UserDigest');			# Get User ID once
	my $nLowerBound = $$::g_pSetupBlob{'MIN_ORDER_VALUE'};
	my $nUpperBound = $$::g_pSetupBlob{'MAX_ORDER_VALUE'};
	my $nBuyerLimit = 0;
	#
	# See what is the limit based on the buyer type (registered/retail)
	#	
	if( $sDigest )
		{
		my ($Status, $Message, $pBuyer) = ACTINIC::GetBuyer($sDigest, ACTINIC::GetPath()); # look up the buyer
		if ($Status != $::SUCCESS)						# error out
			{
			return ($Status, $Message, "");
			}
		#
		# Set the limit for the buyer
		#
		if ($pBuyer->{LimitOrderValue})
			{
			$nBuyerLimit = $pBuyer->{MaximumOrderValue};
			}
		}
	#
	# Now see what is the current order value
	#
	my ($pCartList, @EmptyArray);
	my @Response = $::Session->GetCartObject();
	if ($Response[0] != $::SUCCESS)					# general error
		{
		return (@Response);								# error so return empty string
		}
	my $pCartObject = $Response[2];

	@Response = $pCartObject->SummarizeOrder($::TRUE);# calculate the order total
	if ($Response[0] != $::SUCCESS)
		{
		return (@Response);
		}
	my ($Ignore0, $Ignore1, $nSubTotal, $nShipping, $nTax1, $nTax2, $nTotal, $nShippingTax1, $nShippingTax2, $nHandling, $nHandlingTax1, $nHandlingTax2) = @Response;
	#
	# Now see the if the limit is exceeded
	#
	my $sLimit;
	my $nPromptID;
	if ($nUpperBound > 0 &&
		 $nSubTotal > $nUpperBound)
		{
		@Response = ActinicOrder::FormatPrice($nUpperBound, $::TRUE, $::g_pCatalogBlob);	# Format buyer maximum
		if ($Response[0] != $::SUCCESS)
			{
			return (@Response);
			}
		$sLimit = $Response[2];
		$nPromptID = 2349;
		}
	elsif ($nBuyerLimit > 0 &&
	 		 $nBuyerLimit < $nSubTotal)
	 	{
		@Response = ActinicOrder::FormatPrice($nBuyerLimit, $::TRUE, $::g_pCatalogBlob);	# Format buyer maximum
		if ($Response[0] != $::SUCCESS)
			{
			return (@Response);
			}
		$sLimit = $Response[2];
		$nPromptID = 299;	 	
	 	}
	elsif ($nLowerBound > 0 &&
			 $nSubTotal < $nLowerBound)
		{
		@Response = ActinicOrder::FormatPrice($nLowerBound, $::TRUE, $::g_pCatalogBlob);	# Format buyer minimum
		if ($Response[0] != $::SUCCESS)
			{
			return (@Response);
			}
		$sLimit = $Response[2];				
		$nPromptID = 2348;
		}
	#
	# Format the message and bounce away if the order total is not fine
	#
	if ($sLimit ne "")
		{
		my ($sLocalPage, $sHTML);
		if( !$sDestinationURL )							# if destination page is not given, we get the last page visited
			{
			$sDestinationURL = $::Session->GetLastShopPage();
			}

		if ($$::g_pSetupBlob{UNFRAMED_CHECKOUT} && # if the checkout is unframed,
			 $$::g_pSetupBlob{UNFRAMED_CHECKOUT_URL})
			{
			$sDestinationURL = $$::g_pSetupBlob{UNFRAMED_CHECKOUT_URL};	# return to the given URL
			}

		@Response = ActinicOrder::FormatPrice($nSubTotal, $::TRUE, $::g_pCatalogBlob);	# Format current total
		if ($Response[0] != $::SUCCESS)
			{
			return (@Response);
			}
		my $sTotal = $Response[2];

		my $nDelay = 5;									# Show warning for 5 seconds
		if( !$bClearFrame )
			{
			$nDelay = 2;									# The warning shows up twice, so - cut it to 2 seconds each
			}
		#
		# Take care on frames when bouncing
		#
		my $bClear = ($bClearFrame && ACTINIC::IsCatalogFramed());
		if ($$::g_pSetupBlob{UNFRAMED_CHECKOUT})
			{
			$bClear = (!$bClearFrame && ACTINIC::IsCatalogFramed());
			}
		else
			{
			$bClear = ($bClearFrame && ACTINIC::IsCatalogFramed() && $$::g_pSetupBlob{UNFRAMED_CHECKOUT});
			}

		@Response = ACTINIC::BounceToPageEnhanced($nDelay, ACTINIC::GetPhrase(-1, 1962) . ACTINIC::GetPhrase(-1, $nPromptID, $sTotal, $sLimit) . ACTINIC::GetPhrase(-1, 1970) . ACTINIC::GetPhrase(-1, 2054),
																$$::g_pSetupBlob{CHECKOUT_DESCRIPTION},
																$::g_sWebSiteUrl,
																$::g_sContentUrl, $::g_pSetupBlob, $sDestinationURL, \%::g_InputHash,
																$bClear); 			# bounce back in the broswer
		my ($Status, $Message, $sHTML) = @Response;	# parse the response
		if ($Status != $::SUCCESS)						# error out
			{
			return (@Response);
			}
		return ($::BADDATA, $sHTML);				# return the goods
		}

	return ($::SUCCESS,'');		# OK - either Buyer within the limit or anonymous buyer
	}
