Optimize "Customer has a referring customer" condition, RT#74452
[freeside.git] / httemplate / misc / process / bulk_pkg_increment_bill.cgi
1 %if ($error) {
2 %  $cgi->param('error', $error);
3 <% $cgi->redirect(popurl(2). 'bulk_pkg_increment_bill.cgi?'. $cgi->query_string ) %>
4 %} else {
5 <& /elements/header-popup.html, 'Packages Adjusted' &>
6     <SCRIPT TYPE="text/javascript">
7       topreload();
8     </SCRIPT>
9     </BODY></HTML>
10 % }
11 <%init>
12
13 local $FS::UID::AutoCommit = 0;
14 my $dbh = dbh;
15 my $error;
16
17 die "access denied"
18   unless $FS::CurrentUser::CurrentUser->access_right('Bulk change customer packages') 
19      and $FS::CurrentUser::CurrentUser->access_right('Edit customer package dates');
20
21 my $days = $cgi->param('days') or die "missing parameter: days";
22 $days > 0 or $error = "Number of days must be > 0";
23
24 my %search_hash = ();
25
26 $search_hash{'query'} = $cgi->param('query');
27
28 #scalars
29 for (qw( agentnum cust_status cust_main_salesnum salesnum custnum magic status
30          custom cust_fields pkgbatch zip
31          477part 477rownum date 
32     )) 
33 {
34   $search_hash{$_} = $cgi->param($_) if length($cgi->param($_));
35 }
36
37 #arrays
38 for my $param (qw( pkgpart classnum refnum towernum )) {
39   $search_hash{$param} = [ $cgi->param($param) ]
40     if grep { $_ eq $param } $cgi->param;
41 }
42
43 #scalars that need to be passed if empty
44 for my $param (qw( censustract censustract2 )) {
45   $search_hash{$param} = $cgi->param($param) || ''
46     if grep { $_ eq $param } $cgi->param;
47 }
48
49 #location flags (checkboxes)
50 my @loc = grep /^\w+$/, $cgi->param('loc');
51 $search_hash{"location_$_"} = 1 foreach @loc;
52
53 my $report_option = $cgi->param('report_option');
54 $search_hash{report_option} = $report_option if $report_option;
55
56 for my $param (grep /^report_option_any/, $cgi->param) {
57   $search_hash{$param} = $cgi->param($param);
58 }
59
60 ###
61 # parse dates
62 ###
63
64 #false laziness w/report_cust_pkg.html and bulk_change_pkg.cgi
65 my %disable = (
66   'all'             => {},
67   'one-time charge' => { 'last_bill'=>1, 'bill'=>1, 'adjourn'=>1, 'susp'=>1, 'expire'=>1, 'cancel'=>1, },
68   'active'          => { 'susp'=>1, 'cancel'=>1 },
69   'suspended'       => { 'cancel' => 1 },
70   'cancelled'       => {},
71   ''                => {},
72 );
73
74 foreach my $field (qw( setup last_bill bill adjourn susp expire contract_end change_date cancel active )) {
75
76   $search_hash{$field.'_null'} = scalar( $cgi->param($field.'_null') );
77
78   my($beginning, $ending) = FS::UI::Web::parse_beginning_ending($cgi, $field);
79
80   next if $beginning == 0 && $ending == 4294967295
81        or $disable{$cgi->param('status')}->{$field};
82
83   $search_hash{$field} = [ $beginning, $ending ];
84
85 }
86
87 if(!$error) {
88   foreach my $cust_pkg (qsearch(FS::cust_pkg->search(\%search_hash))) {
89     next if ! $cust_pkg->bill;
90     my $new_cust_pkg = FS::cust_pkg->new({ $cust_pkg->hash });
91     $new_cust_pkg->bill($new_cust_pkg->bill + $days*86400);
92     $error = $new_cust_pkg->replace($cust_pkg);
93     
94     if($error) {
95       $cgi->param("error",substr($error, 0, 512));
96       $dbh->rollback;
97       return;
98     }
99   }
100
101   $dbh->commit;
102 }
103
104 </%init>