* Don't autovifivy @fields array entries.
[interchange.git] / code / SystemTag / discount.coretag
1 # Copyright 2002-2007 Interchange Development Group and others
2
3 # This program is free software; you can redistribute it and/or modify
4 # it under the terms of the GNU General Public License as published by
5 # the Free Software Foundation; either version 2 of the License, or
6 # (at your option) any later version.  See the LICENSE file for details.
7
8 # $Id: discount.coretag,v 1.7 2007-03-30 23:40:49 pajamian Exp $
9
10 UserTag discount            Order        code
11 UserTag discount            AddAttr
12 UserTag discount            attrAlias    space discount_space
13 UserTag discount            hasEndTag
14 UserTag discount            PosNumber    1
15 UserTag discount            Version      $Revision: 1.7 $
16 UserTag discount            Routine      <<EOR
17
18 # Sets the value of a discount field
19 sub {
20         my($code, $opt, $value) = @_;
21
22         # API compatibility
23         if(! ref $opt) {
24                 $value = $opt;
25                 $opt = {};
26         }
27
28         if (! ($::Discounts
29                         and $Vend::Session->{discount_space}
30                         and $Vend::Session->{discount}
31                         and $Vend::DiscountSpaceName)) {
32                 $::Discounts
33                         = $Vend::Session->{discount}
34                         = $Vend::Session->{discount_space}{ $Vend::DiscountSpaceName = 'main' }
35                         ||= ($Vend::Session->{discount} || {});
36         }
37
38         my $dspace;
39         if ($Vend::Cfg->{DiscountSpacesOn} and $dspace = $opt->{discount_space}) {
40                 $dspace = $Vend::Session->{discount_space}{$dspace} ||= {};
41         }
42         else {
43                 $dspace = $::Discounts;
44         }
45
46         if($opt->{subtract}) {
47                 $value = <<EOF;
48 my \$tmp = \$s - $opt->{subtract};
49 \$tmp = 0 if \$tmp < 0;
50 return \$tmp;
51 EOF
52         }
53         elsif ($opt->{level}) {
54                 $value = <<EOF;
55 return (\$s * \$q) if \$q < $opt->{level};
56 my \$tmp = \$s / \$q;
57 return \$s - \$tmp;
58 EOF
59         }
60
61         $dspace->{$code} = $value;
62         delete $dspace->{$code}
63                 unless defined $value and $value;
64         return '';
65 }
66 EOR