bulk package cancellation
[freeside.git] / httemplate / misc / process / bulk_cancel_pkg.cgi
1 % if ($error) {
2 <% $cgi->redirect(popurl(2)."bulk_cancel_pkg.cgi?".$cgi->query_string ) %>
3 % }
4 <% include('/elements/popup-topreload.html', "Packages Cancelled") %>
5 <%init>
6
7 die "access denied"
8   unless $FS::CurrentUser::CurrentUser->access_right('Bulk change customer packages');
9
10 my $error;
11
12 if (!$error) {
13
14   my %search_hash = ();
15
16   $search_hash{'query'} = $cgi->param('query');
17
18   #scalars
19   for (qw( agentnum cust_status cust_main_salesnum salesnum custnum magic status
20          custom cust_fields pkgbatch zip reasonnum
21          477part 477rownum date 
22       )) 
23   {
24     $search_hash{$_} = $cgi->param($_) if length($cgi->param($_));
25   }
26
27   #arrays
28   for my $param (qw( pkgpart classnum refnum towernum )) {
29     $search_hash{$param} = [ $cgi->param($param) ]
30       if grep { $_ eq $param } $cgi->param;
31   }
32
33   #scalars that need to be passed if empty
34   for my $param (qw( censustract censustract2 )) {
35     $search_hash{$param} = $cgi->param($param) || ''
36       if grep { $_ eq $param } $cgi->param;
37   }
38
39   #location flags (checkboxes)
40   my @loc = grep /^\w+$/, $cgi->param('loc');
41   $search_hash{"location_$_"} = 1 foreach @loc;
42
43   my $report_option = $cgi->param('report_option');
44   $search_hash{report_option} = $report_option if $report_option;
45
46   for my $param (grep /^report_option_any/, $cgi->param) {
47     $search_hash{$param} = $cgi->param($param);
48   }
49
50   ###
51   # parse dates
52   ###
53
54   #false laziness w/report_cust_pkg.html and bulk_pkg_increment_bill.cgi
55   my %disable = (
56     'all'             => {},
57     'one-time charge' => { 'last_bill'=>1, 'bill'=>1, 'adjourn'=>1, 'susp'=>1, 'expire'=>1, 'cancel'=>1, },
58     'active'          => { 'susp'=>1, 'cancel'=>1 },
59     'suspended'       => { 'cancel' => 1 },
60     'cancelled'       => {},
61     ''                => {},
62   );
63
64   foreach my $field (qw( setup last_bill bill adjourn susp expire contract_end change_date cancel active )) {
65
66     $search_hash{$field.'_null'} = scalar( $cgi->param($field.'_null') );
67
68     my($beginning, $ending) = FS::UI::Web::parse_beginning_ending($cgi, $field);
69
70     next if $beginning == 0 && $ending == 4294967295
71        or $disable{$cgi->param('status')}->{$field};
72
73     $search_hash{$field} = [ $beginning, $ending ];
74
75   }
76
77   my $sql_query = FS::cust_pkg->search(\%search_hash);
78   $sql_query->{'select'} = 'cust_pkg.pkgnum';
79
80   ## set suspend info
81   $cgi->param('cancel_reasonnum') =~ /^(\d+)$/ or die "Illegal Reason";
82   my $cancel_reasonnum = $1;
83
84   #my $cancel_date = '';
85   #if ( $cgi->param('cancel_date') ) {
86   #  parse_datetime($cgi->param('cancel_date')) =~ /^(\d+)$/ or die "Illegal date";
87   #  $cancel_date = $1;
88   #}
89
90   foreach my $pkgnum (map { $_->pkgnum } qsearch($sql_query)) {
91     my $cust_pkg = qsearchs('cust_pkg',{'pkgnum'=>$pkgnum});
92
93     $error .= ($error ? ' / ' : '').
94               $cust_pkg->cancel('reason'      => $cancel_reasonnum,
95                                 #'date'        => $cancel_date,
96                                );
97   }
98
99 }
100
101 $cgi->param("error", substr($error, 0, 512)); # arbitrary length believed
102                                               # suited for all supported
103                                               # browsers
104 </%init>