consistency is nice
[freeside.git] / FS / bin / freeside-credit-report
1 #!/usr/bin/perl -Tw
2
3
4 use strict;
5 use Date::Parse;
6 use Time::Local;
7 use Getopt::Std;
8 use Text::Template;
9 use FS::Conf;
10 use FS::UID qw(adminsuidsetup);
11 use FS::Record qw(qsearch);
12 use FS::cust_credit;
13
14 # Set the mail program
15 my $mail_program = "/usr/sbin/sendmail -t -n"; 
16
17 &untaint_argv;  #what it sounds like  (eww)
18 use vars qw($opt_v $opt_p $opt_m $opt_e $opt_t $opt_s $opt_f $report_lines $report_template @buf);
19 getopts("vpmef:s:");    #switches
20
21 #we're at now now (and later).
22 my($_finishdate)= $opt_f ? str2time($main::opt_f) : $^T;
23 my($_startdate)= $opt_s ? str2time($main::opt_s) : $^T;
24
25 # Get the current month
26 my ($ssec,$smin,$shour,$smday,$smon,$syear) =
27         (localtime($_startdate) )[0,1,2,3,4,5]; 
28 $smon++;
29 $syear += 1900;
30
31 # Get the current month
32 my ($fsec,$fmin,$fhour,$fmday,$fmon,$fyear) =
33         (localtime($_finishdate) )[0,1,2,3,4,5]; 
34 $fmon++;
35 $fyear += 1900;
36
37 # Login to the database
38 my $user = shift or die &usage;
39 adminsuidsetup $user;
40
41 # Get the needed configuration files
42 my $conf = new FS::Conf;
43 my $lpr = $conf->config('lpr');
44 my $email = $conf->config('email');
45 my @report_template = $conf->config('report_template')
46   or die "cannot load config file report_template";
47 $report_lines = 0;
48 foreach ( grep /report_lines\(\d+\)/, @report_template ) { #kludgy :/
49   /report_lines\((\d+)\)/;
50   $report_lines += $1;
51 }
52 die "no report_lines() functions in template?" unless $report_lines;
53 $report_template = new Text::Template (
54   TYPE   => 'ARRAY',
55   SOURCE => [ map "$_\n", @report_template ],
56 ) or die "can't create new Text::Template object: $Text::Template::ERROR";
57
58
59 my(@cust_credits)=qsearch('cust_credit',{});
60 if (scalar(@cust_credits) == 0)
61 {
62         exit 1;
63 }
64
65 # Open print and email pipes
66 # $lpr and opt_p for printing
67 # $email and opt_m for email
68
69 if ($lpr && $main::opt_p)
70 {
71         open(LPR, "|$lpr");
72 }
73
74 if ($email && $main::opt_m)
75 {
76         open (MAIL, "|$mail_program");
77         print MAIL <<END
78 To: $email
79 From: Account Processor
80 Subject: In House Credits
81
82
83 END
84 }
85
86 my $uninvoiced = 0;
87 my $total = 0;
88 my $taxed = 0;
89 my $untaxed = 0;
90 my $total_tax = 0;
91
92 # Now I can start looping
93 foreach my $cust_credit (@cust_credits)
94 {
95         my $_date = $cust_credit->getfield('_date');
96         my $amount = $cust_credit->getfield('amount');
97
98         if ($_date >= $_startdate && $_date <= $_finishdate) {
99                 $total += $amount;
100         }
101 }
102
103 push @buf, sprintf(qq{\n%25s%14.2f\n}, "Credits Offered", $total);
104 push @buf, sprintf(qq{\n%39s\n%39.2f\n}, "=========", $total);
105
106 sub FS::credit_report::_template::report_lines {
107   my $lines = shift;
108   map {
109     scalar(@buf) ? shift @buf : '' ;
110   }
111   ( 1 .. $lines );
112 }
113
114 $FS::credit_report::_template::title = qq~IN HOUSE CREDITS for $smon/$smday/$syear through $fmon/$fmday/$fyear~;
115 $FS::credit_report::_template::title = $opt_t if $opt_t;
116 $FS::credit_report::_template::page = 1;
117 $FS::credit_report::_template::date = $^T;
118 $FS::credit_report::_template::date = $^T;
119 $FS::credit_report::_template::fdate = $_finishdate;
120 $FS::credit_report::_template::fdate = $_finishdate;
121 $FS::credit_report::_template::sdate = $_startdate;
122 $FS::credit_report::_template::sdate = $_startdate;
123 $FS::credit_report::_template::total_pages = 
124   int( scalar(@buf) / $report_lines);
125 $FS::credit_report::_template::total_pages++ if scalar(@buf) % $report_lines;
126
127 my @report;
128 while (@buf) {
129   push @report, split("\n", 
130     $report_template->fill_in( PACKAGE => 'FS::credit_report::_template' )
131   );
132   $FS::credit_report::_template::page++;
133 }
134
135 if ($opt_v) {
136   print map "$_\n", @report;
137 }
138 if($lpr && $opt_p)
139 {
140   print LPR map "$_\n", @report;
141   print LPR "\f" if $opt_e;
142   close LPR || die "Could not close printer: $lpr\n";
143 }
144 if($email && $opt_m)
145 {
146   print MAIL map "$_\n", @report;
147   close MAIL || die "Could not close printer: $email\n";
148 }
149
150
151 # subroutines
152 sub untaint_argv {
153   foreach $_ ( $[ .. $#ARGV ) { #untaint @ARGV
154     $ARGV[$_] =~ /^([\w\-\/ :]*)$/ || die "Illegal argument \"$ARGV[$_]\"";
155     $ARGV[$_]=$1;
156   }
157 }
158
159 sub usage {
160   die "Usage:\n\n  freeside-credit-report [-v] [-p] [-e] user\n";
161 }
162
163 =head1 NAME
164
165 freeside-credit-report - Prints or emails total credit memos in a given period.
166
167 =head1 SYNOPSIS
168
169   freeside-credit-report [-v] [-p] [-m] [-e] [-t "title"] [-s date] [-f date] user
170
171 =head1 DESCRIPTION
172
173 Prints or emails total credit memos in a given period.
174
175 -v: Verbose - Prints records to STDOUT.
176
177 -p: Print to printer lpr as found in the conf directory.
178
179 -m: Email output to user found in the Conf email file.
180
181 -e: Print a final form feed to the printer.
182
183 -t: supply a title for the top of each page.
184
185 -s: starting date for inclusion
186
187 -f: final date for inclusion
188
189 user: From the mapsecrets file - see config.html from the base documentation
190
191 =head1 VERSION
192
193 $Id: freeside-credit-report,v 1.2 2002-03-05 23:13:23 jeff Exp $
194
195 =head1 BUGS
196
197 Yes..... Use at your own risk. No guarantees or warrantees of any
198 kind apply to this program. Parts of this program are hacked from
199 other GNU licensed software created mainly by Ivan Kohler.
200
201 This is released under the GNU Public License. See www.gnu.org
202 for more information regarding this license.
203
204 =head1 SEE ALSO
205
206 L<FS::cust_main>, config.html from the base documentation
207
208 =head1 HISTORY
209
210 griff@aver-computer.com July 99
211
212 $Log: freeside-credit-report,v $
213 Revision 1.2  2002-03-05 23:13:23  jeff
214 consistency is nice
215
216
217 =cut
218
219