[interchange-cvs] interchange - jon modified 13 files

interchange-cvs at icdevgroup.org interchange-cvs at icdevgroup.org
Mon Jan 24 20:02:59 EST 2005


User:      jon
Date:      2005-01-25 01:02:59 GMT
Modified:  .        MANIFEST
Modified:  code/SystemTag discount.coretag fly_tax.coretag
Modified:           item_list.coretag price.coretag salestax.coretag
Modified:           subtotal.coretag total_cost.coretag
Modified:  dist/test catalog.cfg
Modified:  dist/test/products tests.asc
Modified:  lib/Vend Dispatch.pm Interpolate.pm
Added:     code/UserTag discount_space.tag
Log:
New discount-space functionality by Ethan Rowe <ethan at endpoint.com>.
His notes follow.

Implements discount namespaces, in a manner consistent with the
values-space functionality. The [discount-space] usertag can be used
to change the current discount namespace, causing all subsequent
discount-related tags and calculations to operate from that particular
namespace. Usage:

[discount-space name=<space>]
Changes the current discount namespace to <space>.

[discount-space current=1]
Returns the name of the current namespace.

[discount-space name=<space> clear=1]
Clears all discount information from the specified namespace.

The discount space is initialized, per page process, in a manner
similar to values-space; if a CGI value exists under the variable name
"mv_discount_space", the discount space will be initialized to that
value. Furthermore, a catalog-level variable MV_DISCOUNT_SPACE can be
used to specify the name of an additional CGI variable to check in this
manner. When MV_DISCOUNT_SPACE is defined, it has higher precedence than
mv_discount_space for determining the initial namespace.

The discount-space logic is designed to integrate well with the cart. The
default discount spacename is "main". Setting the catalog variable
MV_DISCOUNT_SPACE to 'mv_cartname' allows the current discount namespace
to match the current cart when the current cart is set via CGI variables.

The discount namespaces are stored in the session at
$Vend::Session->{discount_space}{<spacename>}. Note that, for backwards
compatibility, the default space is always at $Vend::Session->{discount},
and that this will always serve as the master for he "main"
namespace such that $Vend::Session->{discount_space}{main}
= $Vend::Session->{discount}. Reassigning the hashref
for $Vend::Session->{discount_space}{<spacename>}
or $Vend::Session->{discount} can take Interchange internal
variables out of sync, and is thus discouraged (just as performing
a $Session->{scratch} = {} mid-page could cause odd behavior. Use
[discount-space name=<space> clear=1] instead, or assign to the
dereferenced hash (%{$Vend::Session->{discount}} = ()).

For further support, a discount_space parameter has been added to each
of the following tags; these tags all can be affected by discounts,
either in the calculation of a discount, a taxable sum, or an item
price (passing a discount namespace to these tags via the discount_space
parameter will only affect the current discount space within the context
of the tag itself):

[discount]
[fly-tax] (Also added named cart support to this tag in the process)
[item-list] (namespace specified will be used throughout the tag and its subtags)
[salestax]
[subtotal]
[total_cost]

EXAMPLES:

Set the current discount space to the default (which is 'main')
[discount-space]
OR
[discount-space main]

Set the current discount space to space "alt_discount"
[discount name=alt_discount]

Clear discount namespace "garbage"
[discount-space name=garbage clear=1]

Show the current discount namespace
[discount-space current=1]

Use the discount namespace "other" for this loop through the cart
[item-list discount_space=other]
...
[/item-list]

Revision  Changes    Path
2.144     +1 -0      interchange/MANIFEST


rev 2.144, prev_rev 2.143
Index: MANIFEST
===================================================================
RCS file: /var/cvs/interchange/MANIFEST,v
retrieving revision 2.143
retrieving revision 2.144
diff -u -u -r2.143 -r2.144
--- MANIFEST	24 Jan 2005 22:06:31 -0000	2.143
+++ MANIFEST	25 Jan 2005 01:02:58 -0000	2.144
@@ -98,6 +98,7 @@
 code/SystemTag/deliver.coretag
 code/SystemTag/description.coretag
 code/SystemTag/discount.coretag
+code/SystemTag/discount_space.coretag
 code/SystemTag/dump.coretag
 code/SystemTag/either.coretag
 code/SystemTag/error.coretag



1.4       +27 -5     interchange/code/SystemTag/discount.coretag


rev 1.4, prev_rev 1.3
Index: discount.coretag
===================================================================
RCS file: /var/cvs/interchange/code/SystemTag/discount.coretag,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -u -r1.3 -r1.4
--- discount.coretag	6 Jan 2005 17:19:50 -0000	1.3
+++ discount.coretag	25 Jan 2005 01:02:58 -0000	1.4
@@ -1,11 +1,14 @@
-# Copyright 2002 Interchange Development Group (http://www.icdevgroup.org/)
+# Copyright 2002-2005 Interchange Development Group (http://www.icdevgroup.org/)
 # Licensed under the GNU GPL v2. See file LICENSE for details.
-# $Id: discount.coretag,v 1.3 2005/01/06 17:19:50 docelic Exp $
+# $Id: discount.coretag,v 1.4 2005/01/25 01:02:58 jon Exp $
 
 UserTag discount            Order        code
+UserTag discount            AddAttr
+UserTag discount            attrAlias    space discount_space
 UserTag discount            hasEndTag
 UserTag discount            PosNumber    1
 UserTag discount            Routine      <<EOR
+
 # Sets the value of a discount field
 sub {
 	my($code, $opt, $value) = @_;
@@ -16,6 +19,24 @@
 		$opt = {};
 	}
 
+	unless (
+		$::Discounts
+		and $Vend::Session->{discount_space}
+		and $Vend::Session->{discount}
+	) {
+		$::Discounts = 
+			$Vend::Session->{discount_space}{ $Vend::DiscountSpace = 'main' } =
+			$Vend::Session->{discount} ||= {};
+	}
+
+	my $dspace;
+	if ($dspace = $opt->{discount_space}) {
+		$dspace = $Vend::Session->{discount_space}{$dspace} ||= {};
+	}
+	else {
+		$dspace = $::Discounts;
+	}
+
 	if($opt->{subtract}) {
 		$value = <<EOF;
 my \$tmp = \$s - $opt->{subtract};
@@ -30,9 +51,10 @@
 return \$s - \$tmp;
 EOF
 	}
-    $Vend::Session->{discount}{$code} = $value;
-	delete $Vend::Session->{discount}->{$code}
-		unless (defined $value and $value);
+
+	$dspace->{$code} = $value;
+	delete $dspace->{$code}
+		unless defined $value and $value;
 	return '';
 }
 EOR



1.2       +2 -0      interchange/code/SystemTag/fly_tax.coretag


rev 1.2, prev_rev 1.1
Index: fly_tax.coretag
===================================================================
RCS file: /var/cvs/interchange/code/SystemTag/fly_tax.coretag,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -u -r1.1 -r1.2
--- fly_tax.coretag	29 Jan 2002 05:52:38 -0000	1.1
+++ fly_tax.coretag	25 Jan 2005 01:02:58 -0000	1.2
@@ -1,3 +1,5 @@
 UserTag fly-tax             Order        area
 UserTag fly-tax             PosNumber    1
+UserTag fly-tax				AddAttr
+UserTag fly-tax				attrAlias	 space discount_space
 UserTag fly-tax             MapRoutine   Vend::Interpolate::fly_tax



1.4       +11 -1     interchange/code/SystemTag/item_list.coretag


rev 1.4, prev_rev 1.3
Index: item_list.coretag
===================================================================
RCS file: /var/cvs/interchange/code/SystemTag/item_list.coretag,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -u -r1.3 -r1.4
--- item_list.coretag	12 Feb 2004 14:07:34 -0000	1.3
+++ item_list.coretag	25 Jan 2005 01:02:58 -0000	1.4
@@ -1,18 +1,28 @@
 UserTag item-list           Order        name
 UserTag item-list           addAttr
 UserTag item-list           attrAlias    cart name
+UserTag item-list           attrAlias    space discount_space
 UserTag item-list           hasEndTag
 UserTag item-list           Routine      <<EOR
 sub {
 	my($cart,$opt,$text) = @_;
 	return if ! $text;
 	my $items = $cart ? ($::Carts->{$cart} ||= []) : $Vend::Items;
+
+	my $oldspace;
+	$oldspace = Vend::Interpolate::switch_discount_space($opt->{discount_space})
+		if defined $opt->{discount_space};
+
 	$items = [ reverse @$items ] if $opt->{reverse};
 	my $obj = { mv_results => $items };
 	$opt->{prefix} = 'item' unless defined $opt->{prefix};
 # LEGACY
 	list_compat($opt->{prefix}, \$text);
 # END LEGACY
-	return labeled_list($opt, $text, $obj);
+
+	# store the output temporarily, as we need to switch back to the old discount space...
+	my $output = labeled_list($opt, $text, $obj);
+	Vend::Interpolate::switch_discount_space($oldspace) if defined $oldspace;
+	return $output;
 }
 EOR



1.5       +6 -0      interchange/code/SystemTag/price.coretag


rev 1.5, prev_rev 1.4
Index: price.coretag
===================================================================
RCS file: /var/cvs/interchange/code/SystemTag/price.coretag,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -u -r1.4 -r1.5
--- price.coretag	14 Aug 2003 22:32:06 -0000	1.4
+++ price.coretag	25 Jan 2005 01:02:58 -0000	1.5
@@ -1,15 +1,21 @@
 UserTag price               Order        code
 UserTag price               addAttr
 UserTag price               attrAlias    base mv_ib
+UserTag price				attrAlias	 space discount_space
 UserTag price               PosNumber    1
 UserTag price               Routine   <<EOR
 sub {
 	my ($code, $ref) = @_;
 	$ref->{code} ||= $code;
 
+	my $oldspace;
+	$oldspace = Vend::Interpolate::switch_discount_space($ref->{discount_space})
+		if defined $ref->{discount_space};
+
 	my $amount = Vend::Data::item_price($ref);
 	$amount = discount_price($code, $amount, $ref->{quantity})
 			if $ref->{discount};
+	Vend::Interpolate::switch_discount_space($oldspace) if defined $oldspace;
 	return currency( $amount, $ref->{noformat} );
 }
 EOR



1.5       +1 -0      interchange/code/SystemTag/salestax.coretag


rev 1.5, prev_rev 1.4
Index: salestax.coretag
===================================================================
RCS file: /var/cvs/interchange/code/SystemTag/salestax.coretag,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -u -r1.4 -r1.5
--- salestax.coretag	7 May 2003 01:07:28 -0000	1.4
+++ salestax.coretag	25 Jan 2005 01:02:58 -0000	1.5
@@ -1,5 +1,6 @@
 UserTag salestax            Order        name noformat
 UserTag salestax            attrAlias    cart name
+UserTag salestax            attrAlias    space discount_space
 UserTag salestax            addAttr
 UserTag salestax            PosNumber    2
 UserTag salestax            Routine <<EOR



1.4       +3 -1      interchange/code/SystemTag/subtotal.coretag


rev 1.4, prev_rev 1.3
Index: subtotal.coretag
===================================================================
RCS file: /var/cvs/interchange/code/SystemTag/subtotal.coretag,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -u -r1.3 -r1.4
--- subtotal.coretag	7 May 2003 01:07:28 -0000	1.3
+++ subtotal.coretag	25 Jan 2005 01:02:58 -0000	1.4
@@ -1,10 +1,12 @@
 UserTag subtotal            Order        name noformat
 UserTag subtotal            attrAlias    cart name
+UserTag subtotal			attrAlias	 space discount_space
 UserTag subtotal            addAttr
 UserTag subtotal            PosNumber    2
 UserTag subtotal            Routine <<EOR
 sub {
 	my($cart, $noformat, $opt) = @_;
-	return currency( subtotal($cart), $noformat, undef, $opt);
+	return currency( subtotal($cart, $opt->{discount_space}),
+		$noformat, undef, $opt);
 }
 EOR



1.4       +2 -1      interchange/code/SystemTag/total_cost.coretag


rev 1.4, prev_rev 1.3
Index: total_cost.coretag
===================================================================
RCS file: /var/cvs/interchange/code/SystemTag/total_cost.coretag,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -u -r1.3 -r1.4
--- total_cost.coretag	7 May 2003 01:07:28 -0000	1.3
+++ total_cost.coretag	25 Jan 2005 01:02:58 -0000	1.4
@@ -1,10 +1,11 @@
 UserTag total-cost          Order        name noformat
 UserTag total-cost          attrAlias    cart name
+UserTag total-cost			attrAlias	 space discount_space
 UserTag total-cost          PosNumber    2
 UserTag total-cost          addAttr
 UserTag total-cost          Routine <<EOR
 sub {
 	my($cart, $noformat, $opt) = @_;
-	return currency( total_cost($cart), $noformat, undef, $opt);
+	return currency( total_cost($cart, $opt->{discount_space}), $noformat, undef, $opt);
 }
 EOR



1.1                  interchange/code/UserTag/discount_space.tag


rev 1.1, prev_rev 1.0
Index: discount_space.tag
===================================================================
# The discount-space is rather equivalent to the values-space functionality.
# Interchange keeps discount information in a single hash at $Vend::Session->{discount}.
# This is fine except when you start using multiple shopping carts to represent different
# portions of the store and fundamentally different transactions; any common item codes
# will result in one cart's discounts leaking into that of the other cart...
#
# Consequently, we can use a discount space to give a different namespace to various discounts.
# This can be used in parallel with mv_cartname for different shopping carts.
# Set up a master hash of different discount namespaces in the session. Treat the default one
# as 'main' (like Interchange does with the cart). When discount space is called and a name
# is given, point the $Vend::Session->{discount} to the appropriate hashref held in this master
# hash.

# Some options:
# clear - this will empty the discounts for the space specified, after switching to that space.
# current - this will not change the namespace; it simply returns the current space name.

UserTag discount_space  order  name
UserTag discount_space  AttrAlias  space   name
UserTag discount_space  AddAttr
UserTag discount_space  Routine <<EOF
sub {
	my ($namespace, $opt) = @_;
	$namespace ||= 'main';
#::logDebug("Tag discount-space called for namespace '$namespace'! Clear: '$opt->{clear}' Current: '$opt->{current}'");

	unless ($Vend::Session->{discount} and $Vend::Session->{discount_space}) {
		# Initialize the discount space hash, and just assign whatever's in
		# the current discount hash to it as the 'main' entry.
		# Furthermore, instantiate the discount hash if it doesn't already exist, otherwise
		# the linkage between that hashref and the discount_space hashref might break...
		$Vend::Session->{discount_space}{main} = $Vend::Session->{discount} ||= {};
#::logDebug('Tag discount-space: initializing discount_space hash; first call to this tag for this session.');
		$Vend::DiscountSpace = 'main';
		$::Discounts = $Vend::Session->{discount};
	}

	return $Vend::DiscountSpace if $opt->{current};

	$::Discounts = $Vend::Session->{discount_space}{$namespace} ||= {};
	$Vend::DiscountSpace = $namespace;
#::logDebug("Tag discount-space: set discount space to '$namespace'");

	%$::Discounts = () if $opt->{clear};

	return undef;
}
EOF



2.10      +1 -0      interchange/dist/test/catalog.cfg


rev 2.10, prev_rev 2.9
Index: catalog.cfg
===================================================================
RCS file: /var/cvs/interchange/dist/test/catalog.cfg,v
retrieving revision 2.9
retrieving revision 2.10
diff -u -u -r2.9 -r2.10
--- catalog.cfg	25 Jan 2005 00:51:22 -0000	2.9
+++ catalog.cfg	25 Jan 2005 01:02:59 -0000	2.10
@@ -101,6 +101,7 @@
 SpecialPage     violation      special/violation
 
 TaxShipping     OH
+Variable        TAXRATE  HAPPYLAND=0.75
 
 UseModifier     size color
 



2.18      +63 -8     interchange/dist/test/products/tests.asc


rev 2.18, prev_rev 2.17
Index: tests.asc
===================================================================
RCS file: /var/cvs/interchange/dist/test/products/tests.asc,v
retrieving revision 2.17
retrieving revision 2.18
diff -u -u -r2.17 -r2.18
--- tests.asc	13 Apr 2004 01:10:14 -0000	2.17
+++ tests.asc	25 Jan 2005 01:02:59 -0000	2.18
@@ -2627,20 +2627,75 @@
 %%
 Test value, value-extended, and values-space tags.
 %%%
-999999
+000156
 %%
-[the test] [perl]
-# Make this come out right
-return 'The expected result as a regex.';
-[/perl]
+[calcn]
+
+@{$Scratch->{shipping_line} = []} = @{$Config->{Shipping_line}};
+
+@{$Config->{Shipping_line}} = grep { ! $_->[6] } @{$Scratch->{shipping_line}};
+
+%{$Session->{discount}} = () if defined $Session->{discount};
+%{$Session->{discount_space}{main}} = () if defined $Session->{discount_space}{main};
+%{$Session->{discount_space}{all_discount}} = () if defined $Session->{discount_space}{all_discount};
+
+@{$Session->{carts}{main}} = map {
+                {
+                        code => "TEST$_",
+                        mv_cache_price => $_,
+                        mv_ip => $_ - 1,
+                }
+        } (1..3);
+
+$Values->{state} = 'MA';
+$Values->{zip} = '02461';
+$Values->{country} = 'US';
+
+undef;
+
+[/calcn][discount ALL_ITEMS] $s + 1 [/discount][discount code=ALL_ITEMS discount_space=all_discount] $s + 5 [/discount][comment]default space subtotal (every line is one dollar): [/comment][subtotal noformat=1]
+[comment]all_discount space subtotal (every line is five dollars): [/comment][subtotal noformat=1 discount_space=all_discount][discount-space all_discount][discount code=TEST2] $s -3 [/discount][discount code=TEST2 space=main] $s + 17 [/discount]
+[comment]main subtotal: [/comment][subtotal noformat=1 discount_space=main]
+[comment]main tax: [/comment][salestax noformat=1 discount_space=main]
+[comment]Switch to [/comment][discount-space][discount-space current=1]
+[comment]main total cost: [/comment][total-cost noformat=1]
+[comment]Switch to [/comment][discount-space all_discount][discount-space current=1]
+[comment]all_discount subtotal: [/comment][subtotal noformat=1]
+[comment]all_discount tax: [/comment][salestax noformat=1]
+[comment]Switch to [/comment][discount-space][discount-space current=1]
+[comment]all_discount total cost: [/comment][total-cost noformat=1 discount_space=all_discount]
+[comment]main items: [/comment][item-list][item-code] [item-price noformat] [item-discount noformat] [item-subtotal noformat] [item-discount-subtotal noformat][item-alternate except_last] -- [/item-alternate][/item-list]
+[comment]all_discount items: [/comment][item-list discount_space=all_discount][item-code] [item-price noformat] [item-discount noformat] [item-subtotal noformat] [item-discount-subtotal noformat][item-alternate except_last] -- [/item-alternate][/item-list]
+[comment]discount space is: [/comment][discount-space current=1]
+[comment]Fly tax discount space main: [/comment][fly-tax area=HAPPYLAND discount_space=main] [subtotal noformat=1 discount_space=main]
+[comment]Fly tax discount space all_discount: [/comment][fly-tax area=HAPPYLAND discount_space=all_discount] [subtotal noformat=1 discount_space=all_discount][calcn]
+@{$Config->{Shipping_line}} = @{$Scratch->{shipping_line}};
+delete @{$Values}{('state','zip','country')};
+undef;
+[/calcn]
 %%
-The expected result as a regex.
+3
+15
+20
+1.00
+main
+21
+all_discount
+12
+0.60
+main
+12.6
+TEST1 0 -1 0 1 -- TEST2 0 -18 0 18 -- TEST3 0 -1 0 1
+TEST1 0 -5 0 5 -- TEST2 0 -2 0 2 -- TEST3 0 -5 0 5
+main
+15 20
+9 12
 %%
-The NOT expected result.
+
 %%
 
 %%
-Skeleton test.
+Tests for discount-space support: discount-space tag, discount_space attributes for total-cost, subtotal, fly-tax, salestax, discount, item-list
 %%%
 999999
 %%



1.47      +28 -2     interchange/lib/Vend/Dispatch.pm


rev 1.47, prev_rev 1.46
Index: Dispatch.pm
===================================================================
RCS file: /var/cvs/interchange/lib/Vend/Dispatch.pm,v
retrieving revision 1.46
retrieving revision 1.47
diff -u -u -r1.46 -r1.47
--- Dispatch.pm	19 Jan 2005 15:55:13 -0000	1.46
+++ Dispatch.pm	25 Jan 2005 01:02:59 -0000	1.47
@@ -1,6 +1,6 @@
 # Vend::Dispatch - Handle Interchange page requests
 #
-# $Id: Dispatch.pm,v 1.46 2005/01/19 15:55:13 ton Exp $
+# $Id: Dispatch.pm,v 1.47 2005/01/25 01:02:59 jon Exp $
 #
 # Copyright (C) 2002-2003 Interchange Development Group
 # Copyright (C) 2002 Mike Heins <mike at perusion.net>
@@ -26,7 +26,7 @@
 package Vend::Dispatch;
 
 use vars qw($VERSION);
-$VERSION = substr(q$Revision: 1.46 $, 10);
+$VERSION = substr(q$Revision: 1.47 $, 10);
 
 use POSIX qw(strftime);
 use Vend::Util;
@@ -1309,6 +1309,32 @@
 	if(my $vspace = $CGI::values{mv_values_space}) {
 		$::Values = $Vend::Session->{values_repository}{$vspace} ||= {};
 		$Vend::ValuesSpace = $vspace;
+	}
+
+	if (
+		my $dspace = defined $::Variable->{MV_DISCOUNT_SPACE}
+			? $CGI::values{$::Variable->{MV_DISCOUNT_SPACE}} || $CGI::values{mv_discount_space}
+			: $CGI::values{mv_discount_space}
+	) {
+		# The 'main' is the default space; this allows us to easily tie the discount space
+		# to the cart namespace.
+#::logDebug("Dispatch: discount space is '$dspace'");
+		if ($dspace eq 'main') {
+			$::Discounts = $Vend::Session->{discount_space}{$dspace} = $Vend::Session->{discount} ||= {};
+		}
+		else {
+			$::Discounts = $Vend::Session->{discount_space}{$dspace} ||= {};
+		}
+		$Vend::DiscountSpace = $dspace;
+	}
+	else {
+		$Vend::DiscountSpace = 'main';
+		if (defined $Vend::Session->{discount}) {
+			$::Discounts = $Vend::Session->{discount_space}{main} = $Vend::Session->{discount};
+		}
+		else {
+			$::Discounts = undef;
+		}
 	}
 
 	if($Vend::Cfg->{CookieLogin} and ! $Vend::Session->{logged_in}) {



2.229     +88 -26    interchange/lib/Vend/Interpolate.pm


rev 2.229, prev_rev 2.228
Index: Interpolate.pm
===================================================================
RCS file: /var/cvs/interchange/lib/Vend/Interpolate.pm,v
retrieving revision 2.228
retrieving revision 2.229
diff -u -u -r2.228 -r2.229
--- Interpolate.pm	29 Dec 2004 20:06:46 -0000	2.228
+++ Interpolate.pm	25 Jan 2005 01:02:59 -0000	2.229
@@ -1,6 +1,6 @@
 # Vend::Interpolate - Interpret Interchange tags
 # 
-# $Id: Interpolate.pm,v 2.228 2004/12/29 20:06:46 ton Exp $
+# $Id: Interpolate.pm,v 2.229 2005/01/25 01:02:59 jon Exp $
 #
 # Copyright (C) 2002-2003 Interchange Development Group
 # Copyright (C) 1996-2002 Red Hat, Inc.
@@ -28,7 +28,7 @@
 require Exporter;
 @ISA = qw(Exporter);
 
-$VERSION = substr(q$Revision: 2.228 $, 10);
+$VERSION = substr(q$Revision: 2.229 $, 10);
 
 @EXPORT = qw (
 
@@ -120,6 +120,7 @@
 							$item
 							$CGI_array
 							$CGI
+							$Discounts
 							$Document
 							%Db
 							$DbSearch
@@ -1034,7 +1035,7 @@
 				if defined $comp;
 	}
 	elsif($base eq 'discount') {
-		$op =	qq%$Vend::Session->{discount}->{$term}%;
+		$op =	qq%$::Discounts->{$term}%;
 		$op = "q{$op}" unless defined $noop;
 		$op .=	qq%	$operator $comp%
 				if defined $comp;
@@ -2649,6 +2650,23 @@
 	return '';
 }
 
+# Sets the discount namespace.
+sub switch_discount_space {
+	my $dspace = shift || 'main';
+	my $oldspace;
+#::logDebug("switch_discount_space: called for space '$dspace'; current space is $Vend::DiscountSpace.");
+	unless ($Vend::Session->{discount} and $Vend::Session->{discount_space}) {
+		$::Discounts = $Vend::Session->{discount_space}{main} = $Vend::Session->{discount} ||= {};
+		$Vend::DiscountSpace = 'main';
+#::logDebug('switch_discount_space: initialized discount space hash.');
+	}
+	if ($dspace ne ($oldspace = $Vend::DiscountSpace)) {
+		$::Discounts = $Vend::Session->{discount_space}{$Vend::DiscountSpace = $dspace};
+#::logDebug("switch_discount_space: changed discount space from '$oldspace' to $Vend::DiscountSpace");
+	}
+	return $oldspace;
+}
+
 sub tag_calc {
 	my($body) = @_;
 	my $result;
@@ -4864,10 +4882,13 @@
 
 	($code, $extra) = ($item->{code}, $item->{mv_discount});
 
-	$Vend::Session->{discount} = {}
-		if $extra and ! $Vend::Session->{discount};
+	if ($extra and ! $::Discounts) {
+		my $dspace = $Vend::DiscountSpace ||= 'main';
+		$Vend::Session->{discount_space}{main} = $Vend::Session->{discount} ||= {};
+		$::Discounts = $Vend::Session->{discount_space}{$dspace} ||= {};
+	}
 
-	return $price unless $Vend::Session->{discount};
+	return $price unless $::Discounts;
 
 	$quantity = $item->{quantity};
 
@@ -4882,7 +4903,7 @@
 	my ($discount, $return);
 
 	for($code, 'ALL_ITEMS') {
-		next unless $discount = $Vend::Session->{discount}->{$_};
+		next unless $discount = $::Discounts->{$_};
 		$Vend::Interpolate::s = $return ||= $subtotal;
         $return = $ready_safe->reval($discount);
 		if($@) {
@@ -4910,11 +4931,11 @@
 	my(@formulae);
 
 	# Check for individual item discount
-	push(@formulae, $Vend::Session->{discount}->{$item->{code}})
-		if defined $Vend::Session->{discount}->{$item->{code}};
+	push(@formulae, $::Discounts->{$item->{code}})
+		if defined $::Discounts->{$item->{code}};
 	# Check for all item discount
-	push(@formulae, $Vend::Session->{discount}->{ALL_ITEMS})
-		if defined $Vend::Session->{discount}->{ALL_ITEMS};
+	push(@formulae, $::Discounts->{ALL_ITEMS})
+		if defined $::Discounts->{ALL_ITEMS};
 	push(@formulae, $item->{mv_discount})
 		if defined $item->{mv_discount};
 
@@ -5124,18 +5145,22 @@
 
 
 sub taxable_amount {
-	my($cart) = @_;
+	my($cart, $dspace) = @_;
     my($taxable, $i, $code, $item, $tmp, $quantity);
 
-	return subtotal($cart || undef) unless $Vend::Cfg->{NonTaxableField};
+	return subtotal($cart || undef, $dspace || undef) unless $Vend::Cfg->{NonTaxableField};
 
-	my($save);
+	my($save, $oldspace);
 
     if ($cart) {
         $save = $Vend::Items;
         tag_cart($cart);
     }
 
+	if (defined $dspace and $dspace ne $Vend::DiscountSpace) {
+		$oldspace = switch_discount_space($dspace);
+	}
+
     $taxable = 0;
 
     foreach $i (0 .. $#$Vend::Items) {
@@ -5143,7 +5168,7 @@
 		next if is_yes( $item->{mv_nontaxable} );
 		next if is_yes( item_field($item, $Vend::Cfg->{NonTaxableField}) );
 		$tmp = item_subtotal($item);
-		unless (defined $Vend::Session->{discount}) {
+		unless ($::Discounts) {
 			$taxable += $tmp;
 		}
 		else {
@@ -5151,11 +5176,11 @@
 		}
     }
 
-	if (defined $Vend::Session->{discount}->{ENTIRE_ORDER}) {
+	if (defined $::Discounts->{ENTIRE_ORDER}) {
 		$Vend::Interpolate::q = tag_nitems();
 		$Vend::Interpolate::s = $taxable;
 		my $cost = $Vend::Interpolate::ready_safe->reval(
-							 $Vend::Session->{discount}{ENTIRE_ORDER},
+							 $::Discounts->{ENTIRE_ORDER},
 						);
 		if($@) {
 			logError
@@ -5167,13 +5192,17 @@
 
 	$Vend::Items = $save if defined $save;
 
+	# Restore initial discount namespace if appropriate.
+	switch_discount_space($oldspace)
+		if $dspace and $oldspace ne $Vend::DiscountSpace;
+
 	return $taxable;
 }
 
 
 
 sub fly_tax {
-	my ($area) = @_;
+	my ($area, $opt) = @_;
 
 	if(my $country_check = $::Variable->{TAXCOUNTRY}) {
 		$country_check =~ /\b$::Values->{country}\b/
@@ -5206,12 +5235,26 @@
 	}
 #::logDebug("flytax rate=$rate");
 	return 0 unless $rate;
+
+	my ($oldcart, $oldspace);
+	if ($opt->{cart}) {
+		$oldcart = $Vend::Items;
+		tag_cart($opt->{cart});
+	}
+	if ($opt->{discount_space}) {
+		$oldspace = switch_discount_space($opt->{discount_space});
+	}
+
 	my $amount = taxable_amount();
 #::logDebug("flytax before shipping amount=$amount");
 	$amount   += tag_shipping()
 		if $taxable_shipping =~ m{(^|[\s,])$area([\s,]|$)}i;
 	$amount   += tag_handling()
 		if $taxable_handling =~ m{(^|[\s,])$area([\s,]|$)}i;
+
+	$Vend::Items = $oldcart if defined $oldcart;
+	switch_discount_space($oldspace) if defined $oldspace;
+
 #::logDebug("flytax amount=$amount return=" . $amount*$rate);
 	return $amount * $rate;
 }
@@ -5361,7 +5404,7 @@
 
 	$opt ||= {};
 
-	my($save);
+	my($save, $oldspace);
 	### If the user has assigned to salestax,
 	### we use their value come what may, no rounding
 	if($Vend::Session->{assigned}) {
@@ -5375,6 +5418,10 @@
         tag_cart($cart);
     }
 
+	if (defined $opt->{discount_space} and $opt->{discount_space} ne $Vend::DiscountSpace) {
+		$oldspace = switch_discount_space( $opt->{discount_space} );
+	}
+
 #::logDebug("salestax entered, cart=$cart");
 	my $tax_hash;
 	my $cost;
@@ -5396,6 +5443,7 @@
 # if we have a cost from previous routines, return it
 	if(defined $cost) {
 		$Vend::Items = $save if $save;
+		switch_discount_space($oldspace) if $opt->{discount_space} and $oldspace ne $Vend::DiscountSpace;
 		return Vend::Util::round_to_frac_digits($cost);
 	}
 
@@ -5405,6 +5453,9 @@
 
 #::logDebug("got to tax function: " . uneval($tax_hash));
 	my $amount = taxable_amount();
+	# Restore the original discount namespace if appropriate; no other routines need the discount info.
+	switch_discount_space($oldspace) if $opt->{discount_space} and $oldspace ne $Vend::DiscountSpace;
+
 	my($r, $code);
 	# Make it upper case for state and overseas postal
 	# codes, zips don't matter
@@ -5457,8 +5508,8 @@
 # Returns just subtotal of items ordered, with discounts
 # applied
 sub subtotal {
-	my($cart) = @_;
-
+	my($cart, $dspace) = @_;
+	
 	### If the user has assigned to salestax,
 	### we use their value come what may, no rounding
 	if($Vend::Session->{assigned}) {
@@ -5474,8 +5525,11 @@
 	}
 
 	levies() unless $Vend::Levying;
+	
+	my $oldspace = switch_discount_space($dspace) if $dspace;
+	
+	my $discount = defined $::Discounts;
 
-	my $discount = defined $Vend::Session->{discount};
     $subtotal = 0;
 	$tmp = 0;
 
@@ -5489,8 +5543,8 @@
         else { $subtotal += $tmp }
 	}
 
-	if (defined $Vend::Session->{discount}->{ENTIRE_ORDER}) {
-		$formula = $Vend::Session->{discount}->{ENTIRE_ORDER};
+	if (defined $::Discounts->{ENTIRE_ORDER}) {
+		$formula = $::Discounts->{ENTIRE_ORDER};
 		$formula =~ s/\$q\b/tag_nitems()/eg; 
 		$formula =~ s/\$s\b/$subtotal/g; 
 		$cost = $Vend::Interpolate::ready_safe->reval($formula);
@@ -5503,6 +5557,10 @@
 	}
 	$Vend::Items = $save if defined $save;
 	$Vend::Session->{latest_subtotal} = $subtotal;
+
+	# Switch to original discount space if one exists.
+	switch_discount_space($oldspace) if $dspace and $oldspace ne $Vend::DiscountSpace;
+
     return $subtotal;
 }
 
@@ -5511,8 +5569,11 @@
 # Returns the total cost of items ordered.
 
 sub total_cost {
-	my($cart) = @_;
-    my($total, $i, $save);
+	my ($cart, $dspace) = @_;
+	my ($total, $i, $save, $oldspace);
+
+	$oldspace = switch_discount_space($dspace)
+		if defined $dspace and $dspace ne $Vend::DiscountSpace;
 
 	if ($cart) {
 		$save = $Vend::Items;
@@ -5538,6 +5599,7 @@
 	}
 	$Vend::Items = $save if defined $save;
 	$Vend::Session->{latest_total} = $total;
+	switch_discount_space($oldspace) if defined $oldspace;
     return $total;
 }
 








More information about the interchange-cvs mailing list