default to a session cookie instead of setting an explicit timeout, weird timezone...
[freeside.git] / httemplate / edit / process / change-cust_pkg.html
1 % if ($error) {
2 %   $cgi->param('error', $error);
3 %   $cgi->redirect(popurl(3). 'misc/change_pkg.cgi?'. $cgi->query_string );
4 % } else {
5
6     <& /elements/header-popup.html, emt("Package changed") &>
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
18 die "access denied"
19   unless $curuser->access_right('Change customer package');
20
21 my $cust_pkg = qsearchs({
22   'table'     => 'cust_pkg',
23   'addl_from' => 'LEFT JOIN cust_main USING ( custnum )',
24   'hashref'   => { 'pkgnum' => scalar($cgi->param('pkgnum')), },
25   'extra_sql' => ' AND '. $curuser->agentnums_sql,
26 });
27 die 'unknown pkgnum' unless $cust_pkg;
28
29 my %change = map { $_ => scalar($cgi->param($_)) }
30                  qw( locationnum pkgpart quantity );
31
32 $change{'keep_dates'} = 1;
33
34 if ( $cgi->param('locationnum') == -1 ) {
35   my $cust_location = FS::cust_location->new({
36     'custnum' => $cust_pkg->custnum,
37     map { $_ => scalar($cgi->param($_)) }
38         FS::cust_main->location_fields
39   });
40   $change{'cust_location'} = $cust_location;
41 }
42
43 my $error;
44
45 # Discounts:
46 # setup_discountnum and change_discountnum may contain one of the following:
47 # - "-1" Represents the 'other' option.  Results in a new entry to the
48 #        discount table.
49 # - "-2" Represents the "waive setup fee" option. Sets cust_pkg.waive_setup = Y
50 # - int  Represents the id for a discount row: discount.discountnum
51 # my %discount;
52 # $change{waive_setup} = '';
53 # for my $type (qw|setup recur|) {
54 #   my $dnum = $cgi->param("${type}_discountnum");
55
56 #   if ($dnum eq '-2' && $type eq 'setup') {
57 #     $change{waive_setup} = 'Y';
58 #   } elsif ($val =~ /^-?\d+$/) {
59 #     $discount{$type} = {discountnum => $dnum};
60 #     if ($dnum eq '-1') {
61 #       $discount{$type}->{amount}  = $cgi->param("${type}_discountnum_amount");
62 #       $discount{$type}->{percent} = $cgi->param("${type}_discountnum_percent");
63 #     }
64 #   } else {
65 #     # Shouldn't happen without funny business
66 #     $error = "Bad value ${type}_discountnum ($val)";
67 #   }
68 # }
69
70
71 $change{waive_setup} = '';
72 for my $type (qw|setup_discountnum recur_discountnum|) {
73   my $dnum = $cgi->param($type);
74
75   if ($dnum eq '-2' && $type eq 'setup_discountnum') {
76     # Waive Discount
77     $change{waive_setup} = 'Y';
78   } elsif ($dnum =~ /^-?\d+$/) {
79     # Set discountnum
80     $change{$type} = $dnum;
81     $change{"${type}_amount"}  = $cgi->param("${type}_amount");
82     $change{"${type}_percent"} = $cgi->param("${type}_percent");
83     $change{"${type}_months"}  = $cgi->param("${type}_months");
84   } elsif ($dnum eq '') {
85     # Set discount as no discount
86     $change{"${type}"} = 0;
87   } else {
88     $error = "Bad value ${type}_discountnum ($dnum)";
89   }
90 }
91
92 my $now = time;
93 if (defined($cgi->param('contract_end'))) {
94   $change{'contract_end'} = parse_datetime($cgi->param('contract_end'));
95 }
96
97 unless ($error) {
98   if ( $cgi->param('delay') ) {
99     my $date = parse_datetime($cgi->param('start_date'));
100     if (!$date) {
101       $error = "Invalid change date '".$cgi->param('start_date')."'.";
102     } elsif ( $date < $now ) {
103       $error = "Change date ".$cgi->param('start_date')." is in the past.";
104     } else {
105       # schedule the change
106       $change{'start_date'} = $date;
107       $error = $cust_pkg->change_later(\%change);
108     }
109   } else {
110
111     # for now, can't change usageprice with change_later
112     my @old_cust_pkg_usageprice = $cust_pkg->cust_pkg_usageprice;
113
114     # build new usageprice array
115     # false laziness with /edit/process/quick-cust_pkg.cgi
116     my @cust_pkg_usageprice = ();
117     foreach my $quantity_param ( grep { $cgi->param($_) && $cgi->param($_) > 0 }
118                                    grep /^usagepricenum(\d+)_quantity$/,
119                                      $cgi->param
120                                )
121     {
122       $quantity_param =~ /^usagepricenum(\d+)_quantity$/ or die 'unpossible';
123       my $num = $1;
124       push @cust_pkg_usageprice, new FS::cust_pkg_usageprice {
125         usagepricepart => scalar($cgi->param("usagepricenum${num}_usagepricepart")),
126         quantity       => scalar($cgi->param($quantity_param)),
127       };
128     }
129
130     # Need to figure out if usagepricepart quantities changed
131     my %oldup = map { $_->usagepricepart, $_->quantity } @old_cust_pkg_usageprice;
132     my %newup = map { $_->usagepricepart, $_->quantity } @cust_pkg_usageprice;
133     my $usagechanged = 0;
134     foreach my $up (keys %oldup) {
135       last if $usagechanged;
136       $usagechanged = 1 unless $oldup{$up} == $newup{$up};
137     }
138     foreach my $up (keys %newup) {
139       last if $usagechanged;
140       $usagechanged = 1 unless $oldup{$up} == $newup{$up};
141     }
142     $change{'cust_pkg_usageprice'} = \@cust_pkg_usageprice;
143
144     # special case: if there's a package change scheduled, and it matches
145     # the parameters the user requested this time, then change to the existing
146     # future package.
147     if ( $cust_pkg->change_to_pkgnum ) {
148       my $change_to = FS::cust_pkg->by_key($cust_pkg->change_to_pkgnum);
149       if (
150         $change_to->pkgpart      == $change{'pkgpart'} and
151         $change_to->locationnum  == $change{'locationnum'} and
152         $change_to->quantity     == $change{'quantity'} and
153         $change_to->contract_end == $change{'contract_end'} and
154         $change_to->waive_setup  == $change{'waive_setup'} and
155         !$usagechanged
156       ) {
157         %change = ( 'cust_pkg' => $change_to );
158       }
159     }
160
161     # do a package change right now
162     my $pkg_or_error = $cust_pkg->change( \%change );
163     $error = ref($pkg_or_error) ? '' : $pkg_or_error;
164   }
165 }
166
167 </%init>