13 use FS::UID qw(adminsuidsetup);
14 use FS::Record qw(qsearch);
18 &untaint_argv; #what it sounds like (eww)
19 use vars qw($opt_v $opt_p $opt_m $opt_e $opt_t $opt_s $opt_f $report_lines $report_template @buf $header);
20 getopts("vpmef:s:"); #switches
22 #we're at now now (and later).
23 my($_finishdate)= $opt_f ? str2time($main::opt_f) : $^T;
24 my($_startdate)= $opt_s ? str2time($main::opt_s) : $^T;
26 # Get the current month
27 my ($ssec,$smin,$shour,$smday,$smon,$syear) =
28 (localtime($_startdate) )[0,1,2,3,4,5];
32 # Get the current month
33 my ($fsec,$fmin,$fhour,$fmday,$fmon,$fyear) =
34 (localtime($_finishdate) )[0,1,2,3,4,5];
38 # Login to the database
39 my $user = shift or die &usage;
42 # Get the needed configuration files
43 my $conf = new FS::Conf;
44 my $lpr = $conf->config('lpr');
45 my $email = $conf->config('email');
46 my $smtpmachine = $conf->config('smtpmachine');
47 my $mail_sender = $conf->exists('invoice_from') ? $conf->config('invoice_from') :
49 my @report_template = $conf->config('report_template')
50 or die "cannot load config file report_template";
52 foreach ( grep /report_lines\(\d+\)/, @report_template ) { #kludgy :/
53 /report_lines\((\d+)\)/;
56 die "no report_lines() functions in template?" unless $report_lines;
57 $report_template = new Text::Template (
59 SOURCE => [ map "$_\n", @report_template ],
60 ) or die "can't create new Text::Template object: $Text::Template::ERROR";
63 my(@cust_credits)=qsearch('cust_credit',{});
64 if (scalar(@cust_credits) == 0)
69 # Open print and email pipes
70 # $lpr and opt_p for printing
71 # $email and opt_m for email
73 if ($lpr && $main::opt_p)
78 if ($email && $main::opt_m)
80 $ENV{MAILADDRESS} = $mail_sender;
81 $header = new Mail::Header ( [
82 "From: Account Processor",
84 "Sender: $mail_sender",
85 "Reply-To: $mail_sender",
86 "Subject: In House Credits",
96 # Now I can start looping
97 foreach my $cust_credit (@cust_credits)
99 my $_date = $cust_credit->getfield('_date');
100 my $amount = $cust_credit->getfield('amount');
102 if ($_date >= $_startdate && $_date <= $_finishdate) {
107 push @buf, sprintf(qq{\n%25s%14.2f\n}, "Credits Offered", $total);
108 push @buf, sprintf(qq{\n%39s\n%39.2f\n}, "=========", $total);
110 sub FS::credit_report::_template::report_lines {
113 scalar(@buf) ? shift @buf : '' ;
118 $FS::credit_report::_template::title = qq~IN HOUSE CREDITS for $smon/$smday/$syear through $fmon/$fmday/$fyear~;
119 $FS::credit_report::_template::title = $opt_t if $opt_t;
120 $FS::credit_report::_template::page = 1;
121 $FS::credit_report::_template::date = $^T;
122 $FS::credit_report::_template::date = $^T;
123 $FS::credit_report::_template::fdate = $_finishdate;
124 $FS::credit_report::_template::fdate = $_finishdate;
125 $FS::credit_report::_template::sdate = $_startdate;
126 $FS::credit_report::_template::sdate = $_startdate;
127 $FS::credit_report::_template::total_pages =
128 int( scalar(@buf) / $report_lines);
129 $FS::credit_report::_template::total_pages++ if scalar(@buf) % $report_lines;
133 push @report, split("\n",
134 $report_template->fill_in( PACKAGE => 'FS::credit_report::_template' )
136 $FS::credit_report::_template::page++;
140 print map "$_\n", @report;
144 print LPR map "$_\n", @report;
145 print LPR "\f" if $opt_e;
146 close LPR || die "Could not close printer: $lpr\n";
150 my $message = new Mail::Internet (
152 'Body' => [ (@report) ],
155 $message->smtpsend( Host => "$smtpmachine" )
156 or die "can't send report to $email via $smtpmachine: $!";
162 foreach $_ ( $[ .. $#ARGV ) { #untaint @ARGV
163 $ARGV[$_] =~ /^([\w\-\/ :\.]*)$/ || die "Illegal argument \"$ARGV[$_]\"";
169 die "Usage:\n\n freeside-credit-report [-v] [-p] [-e] user\n";
174 freeside-credit-report - Prints or emails total credit memos in a given period.
178 freeside-credit-report [-v] [-p] [-m] [-e] [-t "title"] [-s date] [-f date] user
182 Prints or emails total credit memos in a given period.
184 -v: Verbose - Prints records to STDOUT.
186 -p: Print to printer lpr as found in the conf directory.
188 -m: Email output to user found in the Conf email file.
190 -e: Print a final form feed to the printer.
192 -t: supply a title for the top of each page.
194 -s: starting date for inclusion
196 -f: final date for inclusion
198 user: From the mapsecrets file - see config.html from the base documentation
202 $Id: freeside-credit-report,v 1.4.4.1 2002-09-09 22:57:32 ivan Exp $
206 Yes..... Use at your own risk. No guarantees or warrantees of any
207 kind apply to this program. Parts of this program are hacked from
208 other GNU licensed software created mainly by Ivan Kohler.
210 This is released under the GNU Public License. See www.gnu.org
211 for more information regarding this license.
215 L<FS::cust_main>, config.html from the base documentation
219 Jeff Finucane <jeff@cmh.net>
221 based on print-batch by Joel Griffiths <griff@aver-computer.com>