fix 'Can't call method "setup" on an undefined value' error when using into rates...
[freeside.git] / FS / bin / freeside-pkg-date-midnight
1 #!/usr/bin/perl -w
2
3 use strict;
4 use Getopt::Std;
5 use FS::UID qw(adminsuidsetup);
6 use FS::Record qw(qsearch qsearchs dbh);
7 use Data::Dumper;
8 use POSIX;
9
10 &untaint_argv;  #what it sounds like  (eww)
11 use vars qw(%opt);
12
13 my $user = shift or die &usage;
14 my $dbh = adminsuidsetup $user;
15
16 my $sql = "select cp.* from cust_pkg cp, part_pkg pp
17     where cp.pkgpart = pp.pkgpart and pp.plan = 'flat' 
18         and cp.cancel is null and cp.bill is not null and cp.bill > 0";
19 my $sth = $dbh->prepare($sql);
20 $sth->execute or die $sth->errstr;
21 my $row;
22 while ( $row = $sth->fetchrow_hashref ) {
23     my $bill = $row->{'bill'}; # interpret in local time zone
24
25     my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) =
26         localtime($bill);
27
28     if ( $hour != 0 || $min != 0 || $sec != 0 ) {
29         $hour = 0;
30         $min = 0;
31         $sec = 0;
32         my $newbill = mktime($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst);
33         $sql = "update cust_pkg set bill = ? where pkgnum = ?";
34         my $sth2 = $dbh->prepare($sql);
35         $sth2->execute($newbill,$row->{'pkgnum'}) or die $sth2->errstr;
36     }
37 }
38
39 $dbh->commit;
40
41 ###
42 # subroutines
43 ###
44
45 sub untaint_argv {
46   foreach $_ ( $[ .. $#ARGV ) { #untaint @ARGV
47     #$ARGV[$_] =~ /^([\w\-\/]*)$/ || die "Illegal arguement \"$ARGV[$_]\"";
48     # Date::Parse
49     $ARGV[$_] =~ /^(.*)$/ || die "Illegal arguement \"$ARGV[$_]\"";
50     $ARGV[$_]=$1;
51   }
52 }
53
54 sub usage {
55   die "Usage:\n  freeside-pkg-date-midnight user \n";
56 }
57
58 ###
59 # documentation
60 ###
61
62 =head1 NAME
63
64 freeside-pkg-date-midnight - change the time portion of next bill dates on all active anniversary packages to midnight
65
66 =head1 SYNOPSIS
67
68   freeside-pkg-date-midnight user
69
70 =head1 DESCRIPTION
71
72 user - name of an internal Freeside user
73
74 =head1 SEE ALSO
75
76 L<FS::cust_pkg>
77
78 =cut
79