add some reporting features
[freeside.git] / FS / bin / freeside-cc-receipts-report
1 #!/usr/bin/perl -Tw
2
3 use strict;
4 use Date::Parse;
5 use Time::Local;
6 use Getopt::Std;
7 use FS::Conf;
8 use FS::UID qw(adminsuidsetup);
9 use FS::Record qw(qsearch qsearchs);
10 use FS::cust_pay;
11 use FS::cust_pay_batch;
12
13 # Set the mail program
14 my $mail_program = "/usr/sbin/sendmail -t -n"; 
15
16 &untaint_argv;  #what it sounds like  (eww)
17 use vars qw($opt_v $opt_p $opt_e $opt_d $opt_s);
18 getopts("vped:s:");     #switches
19
20 #we're at now now (and later).
21 my($_enddate)= $main::opt_d ? str2time($main::opt_d) : $^T;
22 my($_startdate)= $main::opt_d ? str2time($main::opt_s) : $^T;
23
24 # Get the current month
25 my ($ssec,$smin,$shour,$smday,$smon,$syear) =
26         (localtime($_startdate) )[0,1,2,3,4,5]; 
27 $syear+=1900;
28 $smon++;
29
30 # Get the current month
31 my ($esec,$emin,$ehour,$emday,$emon,$eyear) =
32         (localtime($_enddate) )[0,1,2,3,4,5]; 
33 $eyear+=1900;
34 $emon++;
35
36 # Login to the database
37 my $user = shift or die &usage;
38 adminsuidsetup $user;
39
40 # Get the needed configuration files
41 my $conf = new FS::Conf;
42 my $lpr = $conf->config('lpr');
43 my $email = $conf->config('email');
44
45 my(@cust_pays)=qsearch('cust_pay',{});
46 if (scalar(@cust_pays) == 0)
47 {
48         exit 1;
49 }
50
51 # Open print and email pipes
52 # $lpr and opt_p for printing
53 # $email and opt_e for email
54
55 if ($lpr && $main::opt_p)
56 {
57         open(LPR, "|$lpr");
58         print LPR qq~ C R E D I T  C A R D  R E C E I P T S  for period beginning: $smon/$smday/$syear and ending $emon/$emday/$eyear\n\n~;
59 }
60
61 if ($email && $main::opt_e)
62 {
63         open (MAIL, "|$mail_program");
64         print MAIL <<END
65 To: $email
66 From: Account Processor
67 Subject: Receivables
68
69
70 C R E D I T  C A R D  R E C E I P T S  for period beginning: $smon/$smday/$syear and ending $emon/$emday/$eyear
71
72 END
73 }
74
75 my $uninvoiced = 0;
76 my $total = 0;
77 my $taxed = 0;
78 my $untaxed = 0;
79 my $total_tax = 0;
80
81 # Now I can start looping
82 foreach my $cust_pay (@cust_pays)
83 {
84         my $_date = $cust_pay->getfield('_date');
85         my $invnum = $cust_pay->getfield('invnum');
86         my $paid = $cust_pay->getfield('paid');
87         my $payby = $cust_pay->getfield('payby');
88         
89
90         if ($_date >= $_startdate && $_date <= $_enddate && $payby =~ 'CARD') {
91                 $total += $paid;
92
93                 $uninvoiced += $cust_pay->unapplied; 
94                 my @cust_bill_pays = $cust_pay->cust_bill_pay;
95                 foreach my $cust_bill_pay (@cust_bill_pays) {
96                         my $invoice_amt =0;
97                         my $invoice_tax =0;
98                         my(@cust_bill_pkgs)= $cust_bill_pay->cust_bill->cust_bill_pkg;
99                         foreach my $cust_bill_pkg (@cust_bill_pkgs) {
100
101                                 my $recur = $cust_bill_pkg->getfield('recur');
102                                 my $setup = $cust_bill_pkg->getfield('setup');
103                                 my $pkgnum = $cust_bill_pkg->getfield('pkgnum');
104                         
105                                 if ($pkgnum == 0) {
106                                         $invoice_tax += $recur;
107                                         $invoice_tax += $setup;
108                                 } else {
109                                         $invoice_amt += $recur;
110                                         $invoice_amt += $setup;
111                                 }
112
113                         }
114
115                         if ($invoice_tax > 0) {
116                                 if ($invoice_amt != $paid) {
117                                         # attempt to prorate partially paid invoices
118                                         $total_tax += $paid / ($invoice_amt + $invoice_tax) * $invoice_tax;
119                                         $taxed += $paid / ($invoice_amt + $invoice_tax) * $invoice_amt;
120                                 } else {
121                                         $total_tax += $invoice_tax;
122                                         $taxed += $invoice_amt;
123                                 }
124                         } else {
125                                 $untaxed += $paid;
126                         }
127
128                 }
129
130         }
131
132 }
133
134 if ($main::opt_v) {
135         printf(qq{\n%25s%14.2f\n}, "Uninvoiced", $uninvoiced);
136         printf(qq{%25s%14.2f\n}, "Untaxed", $untaxed);
137         printf(qq{%25s%14.2f\n}, "Taxed", $taxed);
138         printf(qq{%25s%14.2f\n}, "Tax", $total_tax);
139         printf(qq{\n%39s\n%39.2f\n}, "=========", $total);
140 }
141
142 # Now I need to close LPR and EMAIL if they were open
143 if($lpr && $main::opt_p)
144 {
145         printf(LPR qq{\n%25s%14.2f\n}, "Uninvoiced", $uninvoiced);
146         printf(LPR qq{%25s%14.2f\n}, "Untaxed", $untaxed);
147         printf(LPR qq{%25s%14.2f\n}, "Taxed", $taxed);
148         printf(LPR qq{%25s%14.2f\n}, "Tax", $total_tax);
149         printf(LPR qq{\n%39s\n%39.2f\n}, "=========", $total);
150         close LPR || die "Could not close printer: $lpr\n";
151 }
152 if($email && $main::opt_e)
153 {
154         printf(MAIL qq{\n%25s%14.2f\n}, "Untaxed", $untaxed);
155         printf(MAIL qq{%25s%14.2f\n}, "Taxed", $taxed);
156         printf(MAIL qq{%25s%14.2f\n}, "Tax", $total_tax);
157         printf(MAIL qq{\n%39s\n%39.2f\n}, "=========", $total);
158         close MAIL || die "Could not close printer: $email\n";
159 }
160
161
162 # subroutines
163 sub untaint_argv {
164   foreach $_ ( $[ .. $#ARGV ) { #untaint @ARGV
165     $ARGV[$_] =~ /^([\w\-\/ :]*)$/ || die "Illegal argument \"$ARGV[$_]\"";
166     $ARGV[$_]=$1;
167   }
168 }
169
170 sub usage {
171   die "Usage:\n\n  freeside-tax-report [-v] [-p] [-e] user\n";
172 }
173
174 =head1 NAME
175
176 freeside-tax-report - Prints or emails sales taxes invoiced in a given period.
177
178 =head1 SYNOPSIS
179
180   freeside-tax-report [-v] [-p] [-e] user
181
182 =head1 DESCRIPTION
183
184 Prints or emails sales taxes invoiced in a given period.
185
186 -v: Verbose - Prints records to STDOUT.
187
188 -p: Print to printer lpr as found in the conf directory.
189
190 -e: Email output to user found in the Conf email file.
191
192 user: From the mapsecrets file - see config.html from the base documentation
193
194 =head1 VERSION
195
196 $Id: freeside-cc-receipts-report,v 1.1 2002-02-22 23:18:32 jeff Exp $
197
198 =head1 BUGS
199
200 Yes..... Use at your own risk. No guarantees or warrantees of any
201 kind apply to this program. Parts of this program are hacked from
202 other GNU licensed software created mainly by Ivan Kohler.
203
204 This is released under the GNU Public License. See www.gnu.org
205 for more information regarding this license.
206
207 =head1 SEE ALSO
208
209 L<FS::cust_main>, config.html from the base documentation
210
211 =head1 HISTORY
212
213 griff@aver-computer.com July 99
214
215 $Log: freeside-cc-receipts-report,v $
216 Revision 1.1  2002-02-22 23:18:32  jeff
217 add some reporting features
218
219 Revision 1.2  2002/02/19 14:24:53  jeff
220 might be functional now
221
222 Revision 1.1  2000/09/20 19:25:19  jeff
223 local modifications
224
225 Revision 1.1  2000/05/13 21:57:56  ivan
226 add print_batch script from Joel Griffiths
227
228
229 =cut
230
231