10 use FS::UID qw(adminsuidsetup);
11 use FS::Record qw(qsearch qsearchs);
13 use FS::cust_pay_batch;
15 # Set the mail program
16 my $mail_program = "/usr/sbin/sendmail -t -n";
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);
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 @report_template = $conf->config('report_template')
47 or die "cannot load config file report_template";
49 foreach ( grep /report_lines\(\d+\)/, @report_template ) { #kludgy :/
50 /report_lines\((\d+)\)/;
53 die "no report_lines() functions in template?" unless $report_lines;
54 $report_template = new Text::Template (
56 SOURCE => [ map "$_\n", @report_template ],
57 ) or die "can't create new Text::Template object: $Text::Template::ERROR";
60 my(@cust_pays)=qsearch('cust_pay',{});
61 if (scalar(@cust_pays) == 0)
66 # Open print and email pipes
67 # $lpr and opt_p for printing
68 # $email and opt_m for email
70 if ($lpr && $main::opt_p)
75 if ($email && $main::opt_m)
77 open (MAIL, "|$mail_program");
80 From: Account Processor
93 # Now I can start looping
94 foreach my $cust_pay (@cust_pays)
96 my $_date = $cust_pay->getfield('_date');
97 my $invnum = $cust_pay->getfield('invnum');
98 my $paid = $cust_pay->getfield('paid');
99 my $payby = $cust_pay->getfield('payby');
102 if ($_date >= $_startdate && $_date <= $_finishdate && $payby =~ 'CARD') {
105 $uninvoiced += $cust_pay->unapplied;
106 my @cust_bill_pays = $cust_pay->cust_bill_pay;
107 foreach my $cust_bill_pay (@cust_bill_pays) {
110 my(@cust_bill_pkgs)= $cust_bill_pay->cust_bill->cust_bill_pkg;
111 foreach my $cust_bill_pkg (@cust_bill_pkgs) {
113 my $recur = $cust_bill_pkg->getfield('recur');
114 my $setup = $cust_bill_pkg->getfield('setup');
115 my $pkgnum = $cust_bill_pkg->getfield('pkgnum');
118 $invoice_tax += $recur;
119 $invoice_tax += $setup;
121 $invoice_amt += $recur;
122 $invoice_amt += $setup;
127 if ($invoice_tax > 0) {
128 if ($invoice_amt != $paid) {
129 # attempt to prorate partially paid invoices
130 $total_tax += $paid / ($invoice_amt + $invoice_tax) * $invoice_tax;
131 $taxed += $paid / ($invoice_amt + $invoice_tax) * $invoice_amt;
133 $total_tax += $invoice_tax;
134 $taxed += $invoice_amt;
146 push @buf, sprintf(qq{\n%25s%14.2f\n}, "Uninvoiced", $uninvoiced);
147 push @buf, sprintf(qq{%25s%14.2f\n}, "Untaxed", $untaxed);
148 push @buf, sprintf(qq{%25s%14.2f\n}, "Taxed", $taxed);
149 push @buf, sprintf(qq{%25s%14.2f\n}, "Tax", $total_tax);
150 push @buf, sprintf(qq{\n%39s\n%39.2f\n}, "=========", $total);
152 sub FS::cc_receipts_report::_template::report_lines {
155 scalar(@buf) ? shift @buf : '' ;
160 $FS::cc_receipts_report::_template::title = qq~CREDIT CARD RECEIPTS for period $smon/$smday/$syear through $fmon/$fmday/$fyear~;
161 $FS::cc_receipts_report::_template::title = $opt_t if $opt_t;
162 $FS::cc_receipts_report::_template::page = 1;
163 $FS::cc_receipts_report::_template::date = $^T;
164 $FS::cc_receipts_report::_template::date = $^T;
165 $FS::cc_receipts_report::_template::fdate = $_finishdate;
166 $FS::cc_receipts_report::_template::fdate = $_finishdate;
167 $FS::cc_receipts_report::_template::sdate = $_startdate;
168 $FS::cc_receipts_report::_template::sdate = $_startdate;
169 $FS::cc_receipts_report::_template::total_pages =
170 int( scalar(@buf) / $report_lines);
171 $FS::cc_receipts_report::_template::total_pages++ if scalar(@buf) % $report_lines;
175 push @report, split("\n",
176 $report_template->fill_in( PACKAGE => 'FS::cc_receipts_report::_template' )
178 $FS::cc_receipts_report::_template::page++;
182 print map "$_\n", @report;
186 print LPR map "$_\n", @report;
187 print LPR "\f" if $opt_e;
188 close LPR || die "Could not close printer: $lpr\n";
192 print MAIL map "$_\n", @report;
193 close MAIL || die "Could not close printer: $email\n";
199 foreach $_ ( $[ .. $#ARGV ) { #untaint @ARGV
200 $ARGV[$_] =~ /^([\w\-\/ :]*)$/ || die "Illegal argument \"$ARGV[$_]\"";
206 die "Usage:\n\n freeside-cc-receipts-report [-v] [-p] [-e] user\n";
211 freeside-cc-receipts-report - Prints or emails total credit card receipts in a given period.
215 freeside-cc-receipts-report [-v] [-p] [-m] [-e] [-t "title"] [-s date] [-f date] user
219 Prints or emails sales taxes invoiced in a given period.
221 -v: Verbose - Prints records to STDOUT.
223 -p: Print to printer lpr as found in the conf directory.
225 -m: Email output to user found in the Conf email file.
227 -e: Print a final form feed to the printer.
229 -t: supply a title for the top of each page.
231 -s: starting date for inclusion
233 -f: final date for inclusion
235 user: From the mapsecrets file - see config.html from the base documentation
239 $Id: freeside-cc-receipts-report,v 1.3 2002-03-06 00:17:32 ivan Exp $
243 Yes..... Use at your own risk. No guarantees or warrantees of any
244 kind apply to this program. Parts of this program are hacked from
245 other GNU licensed software created mainly by Ivan Kohler.
247 This is released under the GNU Public License. See www.gnu.org
248 for more information regarding this license.
252 L<FS::cust_main>, config.html from the base documentation
256 Jeff Finucane <jeff@cmh.net>
258 based on print-batch by Joel Griffiths <griff@aver-computer.com>