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