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