add some reporting features
[freeside.git] / FS / bin / freeside-credit-report
1 #!/usr/bin/perl -Tw
2
3 use strict;
4 use Date::Parse;
5 use Time::Local;
6 use Getopt::Std;
7 use FS::Conf;
8 use FS::UID qw(adminsuidsetup);
9 use FS::Record qw(qsearch);
10 use FS::cust_credit;
11
12 # Set the mail program
13 my $mail_program = "/usr/sbin/sendmail -t -n"; 
14
15 &untaint_argv;  #what it sounds like  (eww)
16 use vars qw($opt_v $opt_p $opt_e $opt_d $opt_s);
17 getopts("vped:s:");     #switches
18
19 #we're at now now (and later).
20 my($_enddate)= $main::opt_d ? str2time($main::opt_d) : $^T;
21 my($_startdate)= $main::opt_s ? str2time($main::opt_s) : $^T;
22
23 # Get the current month
24 my ($ssec,$smin,$shour,$smday,$smon,$syear) =
25         (localtime($_startdate) )[0,1,2,3,4,5]; 
26 $syear+=1900;
27 $smon++;
28
29 # Get the current month
30 my ($esec,$emin,$ehour,$emday,$emon,$eyear) =
31         (localtime($_enddate) )[0,1,2,3,4,5]; 
32 $eyear+=1900;
33 $emon++;
34
35 # Login to the database
36 my $user = shift or die &usage;
37 adminsuidsetup $user;
38
39 # Get the needed configuration files
40 my $conf = new FS::Conf;
41 my $lpr = $conf->config('lpr');
42 my $email = $conf->config('email');
43
44 my(@cust_credits)=qsearch('cust_credit',{});
45 if (scalar(@cust_credits) == 0)
46 {
47         exit 1;
48 }
49
50 # Open print and email pipes
51 # $lpr and opt_p for printing
52 # $email and opt_e for email
53
54 if ($lpr && $main::opt_p)
55 {
56         open(LPR, "|$lpr");
57         print LPR qq~ I N  H O U S E  C R E D I T S for period beginning: $smon/$smday/$syear and ending $emon/$emday/$eyear\n\n~;
58 }
59
60 if ($email && $main::opt_e)
61 {
62         open (MAIL, "|$mail_program");
63         print MAIL <<END
64 To: $email
65 From: Account Processor
66 Subject: In House Credits
67
68
69 I N  H O U S E  C R E D I T S  for period beginning: $smon/$smday/$syear and ending $emon/$emday/$eyear
70
71 END
72 }
73
74 my $total = 0;
75
76 # Now I can start looping
77 foreach my $cust_credit (@cust_credits)
78 {
79         my $_date = $cust_credit->getfield('_date');
80         my $amount = $cust_credit->getfield('amount');
81         my $credited = $cust_credit->getfield('credited');
82         
83
84         if ($_date >= $_startdate && $_date <= $_enddate) {
85                 $total += $amount;
86
87                 my ($sec,$min,$hour,$mday,$mon,$year) =
88                         (localtime($_date) )[0,1,2,3,4,5]; 
89                 $mon++;
90
91         }
92
93 }
94
95 if ($main::opt_v) {
96         printf(qq{\n\n%25s%14.2f\n}, "Credits Offered", $total);
97         printf(qq{\n%39s\n%39.2f\n}, "=========", $total);
98 }
99
100 # Now I need to close LPR and EMAIL if they were open
101 if($lpr && $main::opt_p)
102 {
103         printf(LPR qq{\n\n%25s%14.2f\n}, "Credits Offered", $total);
104         printf(LPR qq{\n%39s\n%39.2f\n}, "=========", $total);
105         close LPR || die "Could not close printer: $lpr\n";
106 }
107 if($email && $main::opt_e)
108 {
109         printf(MAIL qq{\n\n%25s%14.2f\n}, "Credits Offered", $total);
110         printf(MAIL qq{\n%39s\n%39.2f\n}, "=========", $total);
111         close MAIL || die "Could not close printer: $email\n";
112 }
113
114
115 # subroutines
116 sub untaint_argv {
117   foreach $_ ( $[ .. $#ARGV ) { #untaint @ARGV
118     $ARGV[$_] =~ /^([\w\-\/ :]*)$/ || die "Illegal argument \"$ARGV[$_]\"";
119     $ARGV[$_]=$1;
120   }
121 }
122
123 sub usage {
124   die "Usage:\n\n  freeside-credit-report [-v] [-p] [-e] user\n";
125 }
126
127 =head1 NAME
128
129 freeside-credit-report - Prints or emails in house credits offered in a given period.
130
131 =head1 SYNOPSIS
132
133   freeside-credit-report [-v] [-p] [-e] user
134
135 =head1 DESCRIPTION
136
137 Prints or emails in house credits offered in a given period.
138
139 -v: Verbose - Prints records to STDOUT.
140
141 -p: Print to printer lpr as found in the conf directory.
142
143 -e: Email output to user found in the Conf email file.
144
145 user: From the mapsecrets file - see config.html from the base documentation
146
147 =head1 VERSION
148
149 $Id: freeside-credit-report,v 1.1 2002-02-22 23:18:32 jeff Exp $
150
151 =head1 BUGS
152
153 Yes..... Use at your own risk. No guarantees or warrantees of any
154 kind apply to this program. Parts of this program are hacked from
155 other GNU licensed software created mainly by Ivan Kohler.
156
157 This is released under the GNU Public License. See www.gnu.org
158 for more information regarding this license.
159
160 =head1 SEE ALSO
161
162 L<FS::cust_main>, config.html from the base documentation
163
164 =head1 HISTORY
165
166 griff@aver-computer.com July 99
167
168 $Log: freeside-credit-report,v $
169 Revision 1.1  2002-02-22 23:18:32  jeff
170 add some reporting features
171
172 Revision 1.1  2002/02/19 14:24:53  jeff
173 might be functional now
174
175 Revision 1.1  2000/09/20 19:25:19  jeff
176 local modifications
177
178 Revision 1.1  2000/05/13 21:57:56  ivan
179 add print_batch script from Joel Griffiths
180
181
182 =cut
183
184