1a8d23b6f02bb3313f599844de51fabc4ca6414d
[freeside.git] / httemplate / misc / process / cancel_pkg.html
1 <% header("Package $past{$method}") %>
2   <SCRIPT TYPE="text/javascript">
3     window.top.location.reload();
4   </SCRIPT>
5   </BODY>
6 </HTML>
7 <%once>
8
9 my %past = ( 'cancel'  => 'cancelled',
10              'expire'  => 'expired',
11              'suspend' => 'suspended',
12              'adjourn' => 'adjourned',
13            );
14
15 #i'm sure this is false laziness with somewhere, at least w/misc/cancel_pkg.html
16 my %right = ( 'cancel'  => 'Cancel customer package immediately',
17               'expire'  => 'Cancel customer package later',
18               'suspend' => 'Suspend customer package',
19               'adjourn' => 'Suspend customer package later',
20             );
21
22 </%once>
23 <%init>
24
25 #untaint method
26 my $method = $cgi->param('method');
27 $method =~ /^(cancel|expire|suspend|adjourn)$/ or die "Illegal method";
28 $method = $1;
29
30 die "access denied"
31   unless $FS::CurrentUser::CurrentUser->access_right($right{$method});
32
33 #untaint pkgnum
34 my $pkgnum = $cgi->param('pkgnum');
35 $pkgnum =~ /^(\d+)$/ or die "Illegal pkgnum";
36 $pkgnum = $1;
37
38 #untaint reasonnum
39 my $reasonnum = $cgi->param('reasonnum');
40 $reasonnum =~ /^(-?\d+)$/ or die "Illegal reasonnum";
41 $reasonnum = $1;
42
43 my $date = time;
44 if ($method eq 'expire' || $method eq 'adjourn'){
45   #untaint date
46   $date = $cgi->param('date');
47   str2time($cgi->param('date')) =~ /^(\d+)$/ or die "Illegal date";
48   $date = $1;
49 }
50
51 my $cust_pkg = qsearchs( 'cust_pkg', {'pkgnum'=>$pkgnum} );
52
53 my $oldAutoCommit = $FS::UID::AutoCommit;
54 local $FS::UID::AutoCommit = 0;
55 my $dbh = dbh;
56
57 my $otaker = $FS::CurrentUser::CurrentUser->name;
58 $otaker = $FS::CurrentUser::CurrentUser->username
59   if ($otaker eq "User, Legacy");
60
61 my $error = '';
62 if ($reasonnum == -1) {
63
64   $error = 'Enter a new reason (or select an existing one)'
65     unless $cgi->param('newreasonnum') !~ /^\s*$/;
66
67   my $reason = new FS::reason({ 'reason_type' => $cgi->param('newreasonnumT'),
68                                 'reason'      => $cgi->param('newreasonnum'),
69                               });
70   $error ||= $reason->insert;
71   $reasonnum = $reason->reasonnum
72     unless $error;
73 }
74
75 unless ($error) {
76   if ($method eq 'expire' || $method eq 'adjourn'){
77     my %hash = $cust_pkg->hash;
78     $hash{$method}=$date;
79     my $new = new FS::cust_pkg (\%hash);
80     $error = $new->replace($cust_pkg, 'reason' => $reasonnum);
81   }else{
82     $error = $cust_pkg->$method( 'reason' => $reasonnum );
83   }
84 }
85
86 if ($error) {
87   $cgi->param('error', $error);
88   $dbh->rollback if $oldAutoCommit;
89   print $cgi->redirect(popurl(2). "cancel_pkg.html?". $cgi->query_string );
90 }
91
92 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
93
94 </%init>