UX: acknowledge actions instead of silently redirecting
[freeside.git] / httemplate / misc / cust_main-suspend.cgi
1 <& /elements/header-popup.html, mt("Customer suspended") &>
2   <script src="<% $fsurl %>elements/js.cookie.js"></script>
3   <SCRIPT TYPE="text/javascript">
4     Cookies.set('freeside_status', <% mt("Customer suspended") |js_string %>,{
5                   expires: 0.0035
6     });
7     topreload();
8   </SCRIPT>
9   </BODY>
10 </HTML>
11 <%init>
12
13 #false laziness w/cust_main-cancel.cgi
14
15 die "access denied"
16   unless $FS::CurrentUser::CurrentUser->access_right('Suspend customer');
17
18 my $custnum;
19 my $adjourn = '';
20 if ( $cgi->param('custnum') =~ /^(\d+)$/ ) {
21   $custnum = $1;
22   $adjourn = $cgi->param('adjourn');
23 } else {
24   my($query) = $cgi->keywords;
25   $query =~ /^(\d+)$/ || die "Illegal custnum";
26   $custnum = $1;
27 }
28
29 #untaint reasonnum / create new reason
30 my ($reasonnum, $error) = $m->comp('process/elements/reason');
31 if (!$reasonnum) {
32   $error ||= 'Reason required';
33 }
34
35 my $cust_main = qsearchs( {
36   'table'     => 'cust_main',
37   'hashref'   => { 'custnum' => $custnum },
38   'extra_sql' => ' AND '. $FS::CurrentUser::CurrentUser->agentnums_sql,
39 } );
40
41 if ( $error ) {
42   # do nothing
43 } elsif ( $cgi->param('now_or_later') ) {
44   $adjourn = parse_datetime($adjourn);
45   if($adjourn) {
46     #warn "setting adjourn dates on custnum#$custnum\n";
47     my @pkgs = $cust_main->unsuspended_pkgs;
48     my @errors = grep {$_} map { $_->suspend(
49       'reason'  => $reasonnum,
50       'date'    => $adjourn,
51     ) } @pkgs;
52     $error = join(' / ', @errors);
53   } else {
54     $error = ("error parsing adjourn date: ".$cgi->param('adjourn'));
55   }
56 } else {
57   warn "suspending $cust_main";
58   $error = $cust_main->suspend(
59     'reason' => $reasonnum,
60   );
61 }
62
63 if ( $error ) {
64   $cgi->param('error', $error);
65   print $cgi->redirect(popurl(1). "suspend_cust.html?". $cgi->query_string );
66 }
67
68 </%init>