fix cancellations
[freeside.git] / httemplate / misc / process / cancel_pkg.html
1 <& /elements/popup-topreload.html, emt("Package $past_method") &>
2 <%once>
3
4 my %past = ( 'cancel'   => 'cancelled',
5              'expire'   => 'expired',
6              'suspend'  => 'suspended',
7              'adjourn'  => 'adjourned',
8              'resume'   => 'scheduled to resume',
9              'uncancel' => 'un-cancelled',
10            );
11
12 #i'm sure this is false laziness with somewhere, at least w/misc/cancel_pkg.html
13 my %right = ( 'cancel'   => 'Cancel customer package immediately',
14               'expire'   => 'Cancel customer package later',
15               'suspend'  => 'Suspend customer package',
16               'adjourn'  => 'Suspend customer package later',
17               'resume'   => 'Unsuspend customer package', #later?
18               'uncancel' => 'Un-cancel customer package',
19             );
20
21 </%once>
22 <%init>
23
24 #untaint method
25 my $method = $cgi->param('method');
26 $method =~ /^(cancel|expire|suspend|adjourn|resume|uncancel)$/
27   or die "Illegal method";
28 $method = $1;
29 my $past_method = $past{$method};
30
31 my $curuser = $FS::CurrentUser::CurrentUser;
32
33 die "access denied"
34   unless $curuser->access_right($right{$method});
35
36 #untaint pkgnum
37 my $pkgnum = $cgi->param('pkgnum');
38 $pkgnum =~ /^(\d+)$/ or die "Illegal pkgnum";
39 $pkgnum = $1;
40
41 my $date = time;
42 if ($method eq 'expire' || $method eq 'adjourn' || $method eq 'resume') {
43   #untaint date
44   $date = $cgi->param('date'); #huh?
45   parse_datetime($cgi->param('date')) =~ /^(\d+)$/ or die "Illegal date";
46   $date = $1;
47   $method = 'cancel'    if $method eq 'expire';
48   $method = 'suspend'   if $method eq 'adjourn';
49   $method = 'unsuspend' if $method eq 'resume';
50 }
51
52 my $resume_date = '';
53 my $options = '';
54 if ( $method eq 'suspend' ) { #or 'adjourn'
55   $resume_date = parse_datetime($cgi->param('resume_date'))
56     if $cgi->param('resume_date');
57
58   $options = { map { $_ => scalar($cgi->param($_)) }
59                  qw( suspend_bill no_suspend_bill )
60              }
61     if $curuser->access_right('Customize billing during suspension');
62 }
63
64 my $cust_pkg = qsearchs( 'cust_pkg', {'pkgnum'=>$pkgnum} );
65
66 #untaint reasonnum, and set up new reason if appropriate
67 my ($reasonnum, $error);
68 if ($method ne 'unsuspend' and $method ne 'uncancel') {
69   ($reasonnum, $error) = $m->comp('elements/reason');
70   if (!$reasonnum) {
71     $error ||= 'Reason required';
72   }
73 }
74
75 #for uncancel
76 my $last_bill =
77   $cgi->param('last_bill') ? parse_datetime($cgi->param('last_bill')) : '';
78 my $bill =
79   $cgi->param('bill')      ? parse_datetime($cgi->param('bill'))      : '';
80
81 my $svc_fatal = ( $cgi->param('svc_not_fatal') ne 'Y' );
82
83 my $only_svcnum = ($method eq 'uncancel') ? [ $cgi->param('only_svcnum') ] : undef;
84
85 $error ||=  $cust_pkg->$method( 'reason'      => $reasonnum,
86                                 'date'        => $date,
87                                 'resume_date' => $resume_date,
88                                 'last_bill'   => $last_bill,
89                                 'bill'        => $bill,
90                                 'svc_fatal'   => $svc_fatal,
91                                 'options'     => $options,
92                                 'only_svcnum' => $only_svcnum,
93                               );
94
95 if ($error) {
96   $cgi->param('error', $error);
97   print $cgi->redirect(popurl(2). "cancel_pkg.html?". $cgi->query_string );
98 }
99
100 </%init>