prevent reload loops on process pages, #71249
[freeside.git] / httemplate / edit / process / cust_pkg_discount.html
1 % if ($error) {
2 %   $cgi->param('error', $error);
3 %   $cgi->redirect(popurl(3). 'edit/cust_pkg_discount.html?'. $cgi->query_string );
4 % } else {
5
6     <% header("Discount applied") %>
7       <SCRIPT TYPE="text/javascript">
8         topreload();
9       </SCRIPT>
10     </BODY>
11     </HTML>
12
13 % }
14 <%init>
15
16 my $curuser = $FS::CurrentUser::CurrentUser;
17 my $can_discount = $curuser->access_right('Discount customer package');
18 my $can_waive_setup = $curuser->access_right('Waive setup fee');
19
20 #this search is really for security wrt agent virt...
21 #maybe move it to the cust_pkg_discount->insert call?
22 my $cust_pkg = qsearchs({
23   #'select'    => 'cust_pkg.*',
24   'table'     => 'cust_pkg',
25   'addl_from' => 'LEFT JOIN cust_main USING ( custnum )',
26   'hashref'   => { 'pkgnum' => scalar($cgi->param('pkgnum')), },
27   'extra_sql' => ' AND '. $curuser->agentnums_sql,
28 });
29 die 'unknown pkgnum' unless $cust_pkg;
30
31 my $error;
32 my %discountnum = (setup => '', recur => '');
33 if ( $cgi->param('setup_discountnum') == -2 ) {
34
35   die "access denied" unless $can_waive_setup; # UI protects against this
36   # waive setup fee (not really a discount but treated as one in the UI)
37   if ( !$cust_pkg->get('setup') and !$cust_pkg->waive_setup ) {
38     $cust_pkg->set('waive_setup' => 'Y');
39     $error = $cust_pkg->replace;
40   }
41
42 } else {
43   if ( $cgi->param('setup_discountnum') =~ /^(-?\d+)$/ ) {
44     $discountnum{setup} = $1;
45   }
46   if ( $cust_pkg->waive_setup ) {
47     $cust_pkg->set('waive_setup', '');
48     $error = $cust_pkg->replace;
49   }
50 }
51
52 if ( $cgi->param('recur_discountnum') =~ /^(-?\d+)$/ ) {
53
54   $discountnum{recur} = $1;
55
56 }
57
58 my @active_discounts = $cust_pkg->cust_pkg_discount_active;
59
60 foreach my $setuprecur (qw(setup recur)) {
61
62   if ( $cust_pkg->get('setup') and $setuprecur eq 'setup' ) {
63     # no point allowing setup discounts to be edited for a previously setup
64     # package
65     next;
66   }
67
68   my ($active) = grep { $_->setuprecur eq $setuprecur } @active_discounts;
69
70   if ( $active ) {
71     if ( $active->discount ne $discountnum{$setuprecur} ) {
72       $active->set('disabled' => 'Y');
73       $error ||= $active->replace;
74       undef $active;
75     } else {
76       # it's the same discountnum; don't touch it
77       next;
78     }
79   }
80
81   if ( $discountnum{$setuprecur} ) {
82     die "access_denied" unless $can_discount;
83     my $cust_pkg_discount = FS::cust_pkg_discount->new({
84       'pkgnum'      => $cust_pkg->pkgnum,
85       'discountnum' => $discountnum{$setuprecur},
86       'setuprecur'  => $setuprecur,
87       'months_used' => 0,
88       'end_date'    => '', #XXX
89       #for the create a new discount case
90       '_type'       => scalar($cgi->param($setuprecur.'_discountnum__type')),
91       'amount'      => scalar($cgi->param($setuprecur.'_discountnum_amount')),
92       'percent'     => scalar($cgi->param($setuprecur.'_discountnum_percent')),
93     });
94     if ( $setuprecur eq 'setup' ) {
95       $cust_pkg_discount->set('setup' => 'Y');
96       $cust_pkg_discount->set('months' => 1);
97     } else {
98       if ( $cgi->param($setuprecur.'_discountnum_months') =~ /^(\w+)$/ ) {
99         $cust_pkg_discount->set('months' => $1);
100       }
101     }
102
103     $error ||= $cust_pkg_discount->insert;
104
105   }
106 } # foreach $setuprecur
107
108 </%init>