5ea8cc5cbac8e346ac40738c7b8aa3cc9f5a1902
[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 FS::Conf;
10 use FS::UID qw(adminsuidsetup);
11 use FS::Record qw(qsearch qsearchs);
12 use FS::cust_pay;
13 use FS::cust_pay_batch;
14
15 # Set the mail program
16 my $mail_program = "/usr/sbin/sendmail -t -n"; 
17
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
21
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;
25
26 # Get the current month
27 my ($ssec,$smin,$shour,$smday,$smon,$syear) =
28         (localtime($_startdate) )[0,1,2,3,4,5]; 
29 $smon++;
30 $syear += 1900;
31
32 # Get the current month
33 my ($fsec,$fmin,$fhour,$fmday,$fmon,$fyear) =
34         (localtime($_finishdate) )[0,1,2,3,4,5]; 
35 $fmon++;
36 $fyear += 1900;
37
38 # Login to the database
39 my $user = shift or die &usage;
40 adminsuidsetup $user;
41
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";
48 $report_lines = 0;
49 foreach ( grep /report_lines\(\d+\)/, @report_template ) { #kludgy :/
50   /report_lines\((\d+)\)/;
51   $report_lines += $1;
52 }
53 die "no report_lines() functions in template?" unless $report_lines;
54 $report_template = new Text::Template (
55   TYPE   => 'ARRAY',
56   SOURCE => [ map "$_\n", @report_template ],
57 ) or die "can't create new Text::Template object: $Text::Template::ERROR";
58
59
60 my(@cust_pays)=qsearch('cust_pay',{});
61 if (scalar(@cust_pays) == 0)
62 {
63         exit 1;
64 }
65
66 # Open print and email pipes
67 # $lpr and opt_p for printing
68 # $email and opt_m for email
69
70 if ($lpr && $main::opt_p)
71 {
72         open(LPR, "|$lpr");
73 }
74
75 if ($email && $main::opt_m)
76 {
77         open (MAIL, "|$mail_program");
78         print MAIL <<END
79 To: $email
80 From: Account Processor
81 Subject: Receivables
82
83
84 END
85 }
86
87 my $uninvoiced = 0;
88 my $total = 0;
89 my $taxed = 0;
90 my $untaxed = 0;
91 my $total_tax = 0;
92
93 # Now I can start looping
94 foreach my $cust_pay (@cust_pays)
95 {
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');
100         
101
102         if ($_date >= $_startdate && $_date <= $_finishdate && $payby =~ 'CARD') {
103                 $total += $paid;
104
105                 $uninvoiced += $cust_pay->unapplied; 
106                 my @cust_bill_pays = $cust_pay->cust_bill_pay;
107                 foreach my $cust_bill_pay (@cust_bill_pays) {
108                         my $invoice_amt =0;
109                         my $invoice_tax =0;
110                         my(@cust_bill_pkgs)= $cust_bill_pay->cust_bill->cust_bill_pkg;
111                         foreach my $cust_bill_pkg (@cust_bill_pkgs) {
112
113                                 my $recur = $cust_bill_pkg->getfield('recur');
114                                 my $setup = $cust_bill_pkg->getfield('setup');
115                                 my $pkgnum = $cust_bill_pkg->getfield('pkgnum');
116                         
117                                 if ($pkgnum == 0) {
118                                         $invoice_tax += $recur;
119                                         $invoice_tax += $setup;
120                                 } else {
121                                         $invoice_amt += $recur;
122                                         $invoice_amt += $setup;
123                                 }
124
125                         }
126
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;
132                                 } else {
133                                         $total_tax += $invoice_tax;
134                                         $taxed += $invoice_amt;
135                                 }
136                         } else {
137                                 $untaxed += $paid;
138                         }
139
140                 }
141
142         }
143
144 }
145
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);
151
152 sub FS::cc_receipts_report::_template::report_lines {
153   my $lines = shift;
154   map {
155     scalar(@buf) ? shift @buf : '' ;
156   }
157   ( 1 .. $lines );
158 }
159
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;
172
173 my @report;
174 while (@buf) {
175   push @report, split("\n", 
176     $report_template->fill_in( PACKAGE => 'FS::cc_receipts_report::_template' )
177   );
178   $FS::cc_receipts_report::_template::page++;
179 }
180
181 if ($opt_v) {
182   print map "$_\n", @report;
183 }
184 if($lpr && $opt_p)
185 {
186   print LPR map "$_\n", @report;
187   print LPR "\f" if $opt_e;
188   close LPR || die "Could not close printer: $lpr\n";
189 }
190 if($email && $opt_m)
191 {
192   print MAIL map "$_\n", @report;
193   close MAIL || die "Could not close printer: $email\n";
194 }
195
196
197 # subroutines
198 sub untaint_argv {
199   foreach $_ ( $[ .. $#ARGV ) { #untaint @ARGV
200     $ARGV[$_] =~ /^([\w\-\/ :]*)$/ || die "Illegal argument \"$ARGV[$_]\"";
201     $ARGV[$_]=$1;
202   }
203 }
204
205 sub usage {
206   die "Usage:\n\n  freeside-cc-receipts-report [-v] [-p] [-e] user\n";
207 }
208
209 =head1 NAME
210
211 freeside-cc-receipts-report - Prints or emails total credit card receipts in a given period.
212
213 =head1 SYNOPSIS
214
215   freeside-cc-receipts-report [-v] [-p] [-m] [-e] [-t "title"] [-s date] [-f date] user
216
217 =head1 DESCRIPTION
218
219 Prints or emails sales taxes invoiced in a given period.
220
221 -v: Verbose - Prints records to STDOUT.
222
223 -p: Print to printer lpr as found in the conf directory.
224
225 -m: Email output to user found in the Conf email file.
226
227 -e: Print a final form feed to the printer.
228
229 -t: supply a title for the top of each page.
230
231 -s: starting date for inclusion
232
233 -f: final date for inclusion
234
235 user: From the mapsecrets file - see config.html from the base documentation
236
237 =head1 VERSION
238
239 $Id: freeside-cc-receipts-report,v 1.2 2002-03-05 23:13:23 jeff Exp $
240
241 =head1 BUGS
242
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.
246
247 This is released under the GNU Public License. See www.gnu.org
248 for more information regarding this license.
249
250 =head1 SEE ALSO
251
252 L<FS::cust_main>, config.html from the base documentation
253
254 =head1 HISTORY
255
256 griff@aver-computer.com July 99
257
258 $Log: freeside-cc-receipts-report,v $
259 Revision 1.2  2002-03-05 23:13:23  jeff
260 consistency is nice
261
262 Revision 1.2  2002/02/19 14:24:53  jeff
263 might be functional now
264
265 Revision 1.1  2000/09/20 19:25:19  jeff
266 local modifications
267
268 Revision 1.1  2000/05/13 21:57:56  ivan
269 add print_batch script from Joel Griffiths
270
271
272 =cut
273
274