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