package search on null dates, RT#73715
[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 for my $param (qw(agentnum magic status classnum pkgpart)) {
29   $search_hash{$param} = $cgi->param($param)
30     if $cgi->param($param);
31 }
32
33 ###
34 # parse dates
35 ###
36
37 #false laziness w/report_cust_pkg.html
38 # and, now, w/bulk_change_pkg.cgi
39 my %disable = (
40   'all'             => {},
41   'one-time charge' => { 'last_bill'=>1, 'bill'=>1, 'adjourn'=>1, 'susp'=>1, 'expire'=>1, 'cancel'=>1, },
42   'active'          => { 'susp'=>1, 'cancel'=>1 },
43   'suspended'       => { 'cancel' => 1 },
44   'cancelled'       => {},
45   ''                => {},
46 );
47
48 foreach my $field (qw( setup last_bill bill adjourn susp expire contract_end change_date cancel active )) {
49
50   my($beginning, $ending) = FS::UI::Web::parse_beginning_ending($cgi, $field);
51
52   next if $beginning == 0 && $ending == 4294967295
53        or $disable{$cgi->param('status')}->{$field};
54
55   $search_hash{$field} = [ $beginning, $ending ];
56
57 }
58
59 if(!$error) {
60   foreach my $cust_pkg (qsearch(FS::cust_pkg->search(\%search_hash))) {
61     next if ! $cust_pkg->bill;
62     my $new_cust_pkg = FS::cust_pkg->new({ $cust_pkg->hash });
63     $new_cust_pkg->bill($new_cust_pkg->bill + $days*86400);
64     $error = $new_cust_pkg->replace($cust_pkg);
65     
66     if($error) {
67       $cgi->param("error",substr($error, 0, 512));
68       $dbh->rollback;
69       return;
70     }
71   }
72
73   $dbh->commit;
74 }
75
76 </%init>