10 use FS::UID qw(adminsuidsetup);
11 use FS::Record qw(qsearch);
14 # Set the mail program
15 my $mail_program = "/usr/sbin/sendmail -t -n";
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
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;
25 # Get the current month
26 my ($ssec,$smin,$shour,$smday,$smon,$syear) =
27 (localtime($_startdate) )[0,1,2,3,4,5];
31 # Get the current month
32 my ($fsec,$fmin,$fhour,$fmday,$fmon,$fyear) =
33 (localtime($_finishdate) )[0,1,2,3,4,5];
37 # Login to the database
38 my $user = shift or die &usage;
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";
48 foreach ( grep /report_lines\(\d+\)/, @report_template ) { #kludgy :/
49 /report_lines\((\d+)\)/;
52 die "no report_lines() functions in template?" unless $report_lines;
53 $report_template = new Text::Template (
55 SOURCE => [ map "$_\n", @report_template ],
56 ) or die "can't create new Text::Template object: $Text::Template::ERROR";
59 my(@cust_credits)=qsearch('cust_credit',{});
60 if (scalar(@cust_credits) == 0)
65 # Open print and email pipes
66 # $lpr and opt_p for printing
67 # $email and opt_m for email
69 if ($lpr && $main::opt_p)
74 if ($email && $main::opt_m)
76 open (MAIL, "|$mail_program");
79 From: Account Processor
80 Subject: In House Credits
92 # Now I can start looping
93 foreach my $cust_credit (@cust_credits)
95 my $_date = $cust_credit->getfield('_date');
96 my $amount = $cust_credit->getfield('amount');
98 if ($_date >= $_startdate && $_date <= $_finishdate) {
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);
106 sub FS::credit_report::_template::report_lines {
109 scalar(@buf) ? shift @buf : '' ;
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;
129 push @report, split("\n",
130 $report_template->fill_in( PACKAGE => 'FS::credit_report::_template' )
132 $FS::credit_report::_template::page++;
136 print map "$_\n", @report;
140 print LPR map "$_\n", @report;
141 print LPR "\f" if $opt_e;
142 close LPR || die "Could not close printer: $lpr\n";
146 print MAIL map "$_\n", @report;
147 close MAIL || die "Could not close printer: $email\n";
153 foreach $_ ( $[ .. $#ARGV ) { #untaint @ARGV
154 $ARGV[$_] =~ /^([\w\-\/ :]*)$/ || die "Illegal argument \"$ARGV[$_]\"";
160 die "Usage:\n\n freeside-credit-report [-v] [-p] [-e] user\n";
165 freeside-credit-report - Prints or emails total credit memos in a given period.
169 freeside-credit-report [-v] [-p] [-m] [-e] [-t "title"] [-s date] [-f date] user
173 Prints or emails total credit memos in a given period.
175 -v: Verbose - Prints records to STDOUT.
177 -p: Print to printer lpr as found in the conf directory.
179 -m: Email output to user found in the Conf email file.
181 -e: Print a final form feed to the printer.
183 -t: supply a title for the top of each page.
185 -s: starting date for inclusion
187 -f: final date for inclusion
189 user: From the mapsecrets file - see config.html from the base documentation
193 $Id: freeside-credit-report,v 1.3 2002-03-06 00:17:32 ivan Exp $
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.
201 This is released under the GNU Public License. See www.gnu.org
202 for more information regarding this license.
206 L<FS::cust_main>, config.html from the base documentation
210 Jeff Finucane <jeff@cmh.net>
212 based on print-batch by Joel Griffiths <griff@aver-computer.com>