13 use FS::UID qw(adminsuidsetup);
14 use FS::Record qw(qsearch qsearchs);
16 use FS::cust_pay_batch;
19 &untaint_argv; #what it sounds like (eww)
20 use vars qw($opt_v $opt_p $opt_m $opt_e $opt_t $opt_s $opt_f $report_lines $report_template @buf $header);
21 getopts("vpmef:s:"); #switches
23 #we're at now now (and later).
24 my($_finishdate)= $opt_f ? str2time($main::opt_f) : $^T;
25 my($_startdate)= $opt_s ? str2time($main::opt_s) : $^T;
27 # Get the current month
28 my ($ssec,$smin,$shour,$smday,$smon,$syear) =
29 (localtime($_startdate) )[0,1,2,3,4,5];
33 # Get the current month
34 my ($fsec,$fmin,$fhour,$fmday,$fmon,$fyear) =
35 (localtime($_finishdate) )[0,1,2,3,4,5];
39 # Login to the database
40 my $user = shift or die &usage;
43 # Get the needed configuration files
44 my $conf = new FS::Conf;
45 my $lpr = $conf->config('lpr');
46 my $email = $conf->config('email');
47 my $smtpmachine = $conf->config('smtpmachine');
48 my $mail_sender = $conf->exists('invoice_from') ? $conf->config('invoice_from') :
50 my @report_template = $conf->config('report_template')
51 or die "cannot load config file report_template";
53 foreach ( grep /report_lines\(\d+\)/, @report_template ) { #kludgy :/
54 /report_lines\((\d+)\)/;
57 die "no report_lines() functions in template?" unless $report_lines;
58 $report_template = new Text::Template (
60 SOURCE => [ map "$_\n", @report_template ],
61 ) or die "can't create new Text::Template object: $Text::Template::ERROR";
64 my(@cust_pays)=qsearch('cust_pay',{});
65 if (scalar(@cust_pays) == 0)
70 # Open print and email pipes
71 # $lpr and opt_p for printing
72 # $email and opt_m for email
74 if ($lpr && $main::opt_p)
79 if ($email && $main::opt_m)
81 $ENV{MAILADDRESS} = $mail_sender;
82 $header = new Mail::Header ( [
83 "From: Account Processor",
85 "Sender: $mail_sender",
86 "Reply-To: $mail_sender",
87 "Subject: Credit Card Receipts",
97 # Now I can start looping
98 foreach my $cust_pay (@cust_pays)
100 my $_date = $cust_pay->getfield('_date');
101 my $invnum = $cust_pay->getfield('invnum');
102 my $paid = $cust_pay->getfield('paid');
103 my $payby = $cust_pay->getfield('payby');
106 if ($_date >= $_startdate && $_date <= $_finishdate && $payby =~ 'CARD') {
109 $uninvoiced += $cust_pay->unapplied;
110 my @cust_bill_pays = $cust_pay->cust_bill_pay;
111 foreach my $cust_bill_pay (@cust_bill_pays) {
114 my(@cust_bill_pkgs)= $cust_bill_pay->cust_bill->cust_bill_pkg;
115 foreach my $cust_bill_pkg (@cust_bill_pkgs) {
117 my $recur = $cust_bill_pkg->getfield('recur');
118 my $setup = $cust_bill_pkg->getfield('setup');
119 my $pkgnum = $cust_bill_pkg->getfield('pkgnum');
122 $invoice_tax += $recur;
123 $invoice_tax += $setup;
125 $invoice_amt += $recur;
126 $invoice_amt += $setup;
131 if ($invoice_tax > 0) {
132 if ($invoice_amt != $paid) {
133 # attempt to prorate partially paid invoices
134 $total_tax += $paid / ($invoice_amt + $invoice_tax) * $invoice_tax;
135 $taxed += $paid / ($invoice_amt + $invoice_tax) * $invoice_amt;
137 $total_tax += $invoice_tax;
138 $taxed += $invoice_amt;
150 push @buf, sprintf(qq{\n%25s%14.2f\n}, "Uninvoiced", $uninvoiced);
151 push @buf, sprintf(qq{%25s%14.2f\n}, "Untaxed", $untaxed);
152 push @buf, sprintf(qq{%25s%14.2f\n}, "Taxed", $taxed);
153 push @buf, sprintf(qq{%25s%14.2f\n}, "Tax", $total_tax);
154 push @buf, sprintf(qq{\n%39s\n%39.2f\n}, "=========", $total);
156 sub FS::cc_receipts_report::_template::report_lines {
159 scalar(@buf) ? shift @buf : '' ;
164 $FS::cc_receipts_report::_template::title = qq~CREDIT CARD RECEIPTS for period $smon/$smday/$syear through $fmon/$fmday/$fyear~;
165 $FS::cc_receipts_report::_template::title = $opt_t if $opt_t;
166 $FS::cc_receipts_report::_template::page = 1;
167 $FS::cc_receipts_report::_template::date = $^T;
168 $FS::cc_receipts_report::_template::date = $^T;
169 $FS::cc_receipts_report::_template::fdate = $_finishdate;
170 $FS::cc_receipts_report::_template::fdate = $_finishdate;
171 $FS::cc_receipts_report::_template::sdate = $_startdate;
172 $FS::cc_receipts_report::_template::sdate = $_startdate;
173 $FS::cc_receipts_report::_template::total_pages =
174 int( scalar(@buf) / $report_lines);
175 $FS::cc_receipts_report::_template::total_pages++ if scalar(@buf) % $report_lines;
179 push @report, split("\n",
180 $report_template->fill_in( PACKAGE => 'FS::cc_receipts_report::_template' )
182 $FS::cc_receipts_report::_template::page++;
186 print map "$_\n", @report;
190 print LPR map "$_\n", @report;
191 print LPR "\f" if $opt_e;
192 close LPR || die "Could not close printer: $lpr\n";
196 my $message = new Mail::Internet (
198 'Body' => [ (@report) ],
201 $message->smtpsend( Host => "$smtpmachine" )
202 or die "can't send report to $email via $smtpmachine: $!";
208 foreach $_ ( $[ .. $#ARGV ) { #untaint @ARGV
209 $ARGV[$_] =~ /^([\w\-\/ :]*)$/ || die "Illegal argument \"$ARGV[$_]\"";
215 die "Usage:\n\n freeside-cc-receipts-report [-v] [-p] [-e] user\n";
220 freeside-cc-receipts-report - Prints or emails total credit card receipts in a given period.
224 freeside-cc-receipts-report [-v] [-p] [-m] [-e] [-t "title"] [-s date] [-f date] user
228 Prints or emails sales taxes invoiced in a given period.
230 -v: Verbose - Prints records to STDOUT.
232 -p: Print to printer lpr as found in the conf directory.
234 -m: Email output to user found in the Conf email file.
236 -e: Print a final form feed to the printer.
238 -t: supply a title for the top of each page.
240 -s: starting date for inclusion
242 -f: final date for inclusion
244 user: From the mapsecrets file - see config.html from the base documentation
248 $Id: freeside-cc-receipts-report,v 1.4 2002-03-07 19:50:23 jeff Exp $
252 Yes..... Use at your own risk. No guarantees or warrantees of any
253 kind apply to this program. Parts of this program are hacked from
254 other GNU licensed software created mainly by Ivan Kohler.
256 This is released under the GNU Public License. See www.gnu.org
257 for more information regarding this license.
261 L<FS::cust_main>, config.html from the base documentation
265 Jeff Finucane <jeff@cmh.net>
267 based on print-batch by Joel Griffiths <griff@aver-computer.com>