Optimize "Customer has a referring customer" condition, RT#74452
[freeside.git] / httemplate / misc / process / bulk_suspend_pkg.cgi
1 % if ($error) {
2 <% $cgi->redirect(popurl(2)."bulk_suspend_pkg.cgi?".$cgi->query_string ) %>
3 % }
4 <% include('/elements/popup-topreload.html', "Packages Suspended") %>
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('suspend_reasonnum') =~ /^(\d+)$/ or die "Illegal Reason";
82   my $suspend_reasonnum = $1;
83
84   my $suspend_date = time;
85   parse_datetime($cgi->param('suspend_date')) =~ /^(\d+)$/ or die "Illegal date";
86   $suspend_date = $1;
87
88   my $suspend_resume_date = '';
89   (parse_datetime($cgi->param('suspend_resume_date')) =~ /^(\d+)$/ or die "Illegal resume date") if $cgi->param('suspend_resume_date');
90   $suspend_resume_date = $1;
91
92   foreach my $pkgnum (map { $_->pkgnum } qsearch($sql_query)) {
93     my $cust_pkg = qsearchs('cust_pkg',{'pkgnum'=>$pkgnum});
94
95     $error = $cust_pkg->suspend('reason'      => $suspend_reasonnum,
96                                 'date'        => $suspend_date,
97                                 'resume_date' => $suspend_resume_date,
98                               );
99   }
100
101 }
102
103 $cgi->param("error", substr($error, 0, 512)); # arbitrary length believed
104                                               # suited for all supported
105                                               # browsers
106 </%init>