fix up includes with Apache::ASP
[freeside.git] / FS / bin / freeside-cc-receipts-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 Net::SMTP;
10 use Mail::Header;
11 use Mail::Internet;
12 use FS::Conf;
13 use FS::UID qw(adminsuidsetup);
14 use FS::Record qw(qsearch qsearchs);
15 use FS::cust_pay;
16 use FS::cust_pay_batch;
17
18
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
22
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;
26
27 # Get the current month
28 my ($ssec,$smin,$shour,$smday,$smon,$syear) =
29         (localtime($_startdate) )[0,1,2,3,4,5]; 
30 $smon++;
31 $syear += 1900;
32
33 # Get the current month
34 my ($fsec,$fmin,$fhour,$fmday,$fmon,$fyear) =
35         (localtime($_finishdate) )[0,1,2,3,4,5]; 
36 $fmon++;
37 $fyear += 1900;
38
39 # Login to the database
40 my $user = shift or die &usage;
41 adminsuidsetup $user;
42
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') :
49   'postmaster';
50 my @report_template = $conf->config('report_template')
51   or die "cannot load config file report_template";
52 $report_lines = 0;
53 foreach ( grep /report_lines\(\d+\)/, @report_template ) { #kludgy :/
54   /report_lines\((\d+)\)/;
55   $report_lines += $1;
56 }
57 die "no report_lines() functions in template?" unless $report_lines;
58 $report_template = new Text::Template (
59   TYPE   => 'ARRAY',
60   SOURCE => [ map "$_\n", @report_template ],
61 ) or die "can't create new Text::Template object: $Text::Template::ERROR";
62
63
64 my(@cust_pays)=qsearch('cust_pay',{});
65 if (scalar(@cust_pays) == 0)
66 {
67         exit 1;
68 }
69
70 # Open print and email pipes
71 # $lpr and opt_p for printing
72 # $email and opt_m for email
73
74 if ($lpr && $main::opt_p)
75 {
76         open(LPR, "|$lpr");
77 }
78
79 if ($email && $main::opt_m)
80 {
81   $ENV{MAILADDRESS} = $mail_sender;
82   $header = new Mail::Header ( [
83     "From: Account Processor",
84     "To: $email",
85     "Sender: $mail_sender",
86     "Reply-To: $mail_sender",
87     "Subject: Credit Card Receipts",
88   ] );
89 }
90
91 my $uninvoiced = 0;
92 my $total = 0;
93 my $taxed = 0;
94 my $untaxed = 0;
95 my $total_tax = 0;
96
97 # Now I can start looping
98 foreach my $cust_pay (@cust_pays)
99 {
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');
104         
105
106         if ($_date >= $_startdate && $_date <= $_finishdate && $payby =~ 'CARD') {
107                 $total += $paid;
108
109                 $uninvoiced += $cust_pay->unapplied; 
110                 my @cust_bill_pays = $cust_pay->cust_bill_pay;
111                 foreach my $cust_bill_pay (@cust_bill_pays) {
112                         my $invoice_amt =0;
113                         my $invoice_tax =0;
114                         my(@cust_bill_pkgs)= $cust_bill_pay->cust_bill->cust_bill_pkg;
115                         foreach my $cust_bill_pkg (@cust_bill_pkgs) {
116
117                                 my $recur = $cust_bill_pkg->getfield('recur');
118                                 my $setup = $cust_bill_pkg->getfield('setup');
119                                 my $pkgnum = $cust_bill_pkg->getfield('pkgnum');
120                         
121                                 if ($pkgnum == 0) {
122                                         $invoice_tax += $recur;
123                                         $invoice_tax += $setup;
124                                 } else {
125                                         $invoice_amt += $recur;
126                                         $invoice_amt += $setup;
127                                 }
128
129                         }
130
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;
136                                 } else {
137                                         $total_tax += $invoice_tax;
138                                         $taxed += $invoice_amt;
139                                 }
140                         } else {
141                                 $untaxed += $paid;
142                         }
143
144                 }
145
146         }
147
148 }
149
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);
155
156 sub FS::cc_receipts_report::_template::report_lines {
157   my $lines = shift;
158   map {
159     scalar(@buf) ? shift @buf : '' ;
160   }
161   ( 1 .. $lines );
162 }
163
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;
176
177 my @report;
178 while (@buf) {
179   push @report, split("\n", 
180     $report_template->fill_in( PACKAGE => 'FS::cc_receipts_report::_template' )
181   );
182   $FS::cc_receipts_report::_template::page++;
183 }
184
185 if ($opt_v) {
186   print map "$_\n", @report;
187 }
188 if($lpr && $opt_p)
189 {
190   print LPR map "$_\n", @report;
191   print LPR "\f" if $opt_e;
192   close LPR || die "Could not close printer: $lpr\n";
193 }
194 if($email && $opt_m)
195 {
196   my $message = new Mail::Internet (
197     'Header' => $header,
198     'Body' => [ (@report) ],
199   );
200   $!=0;
201   $message->smtpsend( Host => "$smtpmachine" )
202     or die "can't send report to $email via $smtpmachine: $!";
203 }
204
205
206 # subroutines
207 sub untaint_argv {
208   foreach $_ ( $[ .. $#ARGV ) { #untaint @ARGV
209     $ARGV[$_] =~ /^([\w\-\/ :\.]*)$/ || die "Illegal argument \"$ARGV[$_]\"";
210     $ARGV[$_]=$1;
211   }
212 }
213
214 sub usage {
215   die "Usage:\n\n  freeside-cc-receipts-report [-v] [-p] [-e] user\n";
216 }
217
218 =head1 NAME
219
220 freeside-cc-receipts-report - Prints or emails total credit card receipts in a given period.
221
222 =head1 SYNOPSIS
223
224   freeside-cc-receipts-report [-v] [-p] [-m] [-e] [-t "title"] [-s date] [-f date] user
225
226 =head1 DESCRIPTION
227
228 Prints or emails sales taxes invoiced in a given period.
229
230 -v: Verbose - Prints records to STDOUT.
231
232 -p: Print to printer lpr as found in the conf directory.
233
234 -m: Email output to user found in the Conf email file.
235
236 -e: Print a final form feed to the printer.
237
238 -t: supply a title for the top of each page.
239
240 -s: starting date for inclusion
241
242 -f: final date for inclusion
243
244 user: From the mapsecrets file - see config.html from the base documentation
245
246 =head1 VERSION
247
248 $Id: freeside-cc-receipts-report,v 1.5 2002-09-09 22:57:34 ivan Exp $
249
250 =head1 BUGS
251
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.
255
256 This is released under the GNU Public License. See www.gnu.org
257 for more information regarding this license.
258
259 =head1 SEE ALSO
260
261 L<FS::cust_main>, config.html from the base documentation
262
263 =head1 AUTHOR
264
265 Jeff Finucane <jeff@cmh.net>
266
267 based on print-batch by Joel Griffiths <griff@aver-computer.com>
268
269 =cut
270