334c4107bc57f5e6ea3b21874cc4b870eff5e006
[freeside.git] / FS / bin / freeside-tax-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);
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_s ? 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 $smon++;
28 $syear -= 100 if $syear >= 100;
29 $syear = "0" . $syear if $syear < 10;
30
31 # Get the current month
32 my ($esec,$emin,$ehour,$emday,$emon,$eyear) =
33         (localtime($_enddate) )[0,1,2,3,4,5]; 
34 $emon++;
35 $eyear -= 100 if $eyear >= 100;
36 $eyear = "0" . $eyear if $eyear < 10;
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
47 my(@cust_bills)=qsearch('cust_bill',{});
48 if (scalar(@cust_bills) == 0)
49 {
50         exit 1;
51 }
52
53 if ($main::opt_v)
54 {
55         print qq~ S A L E S  T A X E S  I N V O I C E D  for period beginning: $smon/$smday/$syear and ending $emon/$emday/$eyear\n\n~;
56 }
57
58 # Open print and email pipes
59 # $lpr and opt_p for printing
60 # $email and opt_e for email
61
62 if ($lpr && $main::opt_p)
63 {
64         open(LPR, "|$lpr");
65         print LPR qq~ S A L E S  T A X E S  I N V O I C E D  for period beginning: $smon/$smday/$syear and ending $emon/$emday/$eyear\n\n~;
66 }
67
68 if ($email && $main::opt_e)
69 {
70         open (MAIL, "|$mail_program");
71         print MAIL <<END
72 To: $email
73 From: Account Processor
74 Subject: Sales Taxes Invoiced
75
76
77 S A L E S  T A X E S  I N V O I C E D  for period beginning: $smon/$smday/$syear and ending $emon/$emday/$eyear
78
79 END
80 }
81
82 my $compped = 0;
83 my $compped_tax = 0;
84 my $other = 0;
85 my $other_tax = 0;
86 my $total = 0;
87 my $taxed = 0;
88 my $untaxed = 0;
89 my $total_tax = 0;
90
91 # Now I can start looping
92 foreach my $cust_bill (@cust_bills)
93 {
94         my $_date = $cust_bill->getfield('_date');
95         my $invnum = $cust_bill->getfield('invnum');
96         my $charged = $cust_bill->getfield('charged');
97         
98
99         if ($_date >= $_startdate && $_date <= $_enddate) {
100                 $total += $charged;
101
102                 # The following lines were used to produce rather verbose reports
103                 #my ($sec,$min,$hour,$mday,$mon,$year) =
104                 #       (localtime($_date) )[0,1,2,3,4,5]; 
105                 #$mon++;
106                 #$year -= 100 if $year >= 100;
107                 #$year = "0" . $year if $year < 10;
108
109                 my $invoice_amt =0;
110                 my $invoice_tax =0;
111                 my $invoice_compped =0;
112                 my(@cust_bill_pkgs)= $cust_bill->cust_bill_pkg;
113                 foreach my $cust_bill_pkg (@cust_bill_pkgs) {
114
115                         my $recur = $cust_bill_pkg->getfield('recur');
116                         my $setup = $cust_bill_pkg->getfield('setup');
117                         my $pkgnum = $cust_bill_pkg->getfield('pkgnum');
118                         
119                         if ($pkgnum == 0) {
120                                 # The following line was used to produce rather verbose reports
121                                 # printf(MAIL qq{\n%10s%15s%14.2f}, "$mon/$mday/$year", "Tax $invnum", $recur+$setup);
122                                 $invoice_tax += $recur;
123                                 $invoice_tax += $setup;
124                         } else {
125                                 # The following line was used to produce rather verbose reports
126                                 # printf(MAIL qq{\n%10s%15s%14.2f}, "$mon/$mday/$year", "Inv $invnum", $recur+$setup);
127                                 $invoice_amt += $recur;
128                                 $invoice_amt += $setup;
129                         }
130
131                 }
132
133                 my(@cust_bill_pays)= $cust_bill->cust_bill_pay;
134                 foreach my $cust_bill_pay (@cust_bill_pays) {
135                         my $payby = $cust_bill_pay->cust_pay->payby;
136                         my $paid = $cust_bill_pay->getfield('amount');
137                         if ($payby =~ 'COMP') {
138                                 $invoice_compped += $paid;
139                         }
140                 }
141
142                 if (abs($invoice_compped - ($invoice_amt + $invoice_tax)) < 0.0001){
143                         $compped += $invoice_amt;
144                         $compped_tax += $invoice_tax;
145                 } elsif ($invoice_compped > 0) {
146                         printf(qq{\nInvoice %10d has inexpliciable complimentary payments of %14.9f\n}, $invnum, $invoice_compped);
147                         $other += $invoice_amt;
148                         $other_tax += $invoice_tax;
149                 } elsif ($invoice_tax > 0) {
150                         $total_tax += $invoice_tax;
151                         $taxed += $invoice_amt;
152                 } else {
153                         $untaxed += $invoice_amt;
154                 }
155
156         }
157
158 }
159
160 if ($main::opt_v) {
161         printf(qq{\n\n%25s%14.2f\n}, "Complimentary", $compped);
162         printf(qq{%25s%14.2f\n}, "Complimentary Tax", $compped_tax);
163         printf(qq{%25s%14.2f\n}, "Other", $other);
164         printf(qq{%25s%14.2f\n}, "Other Tax", $other_tax);
165         printf(qq{%25s%14.2f\n}, "Untaxed", $untaxed);
166         printf(qq{%25s%14.2f\n}, "Taxed", $taxed);
167         printf(qq{%25s%14.2f\n}, "Tax", $total_tax);
168         printf(qq{\n%39s\n%39.2f\n}, "=========", $total);
169 }
170
171 # Now I need to close LPR and EMAIL if they were open
172 if($lpr && $main::opt_p)
173 {
174         printf(LPR qq{\n\n%25s%14.2f\n}, "Complimentary", $compped);
175         printf(LPR qq{%25s%14.2f\n}, "Complimentary Tax", $compped_tax);
176         printf(LPR qq{%25s%14.2f\n}, "Other", $other);
177         printf(LPR qq{%25s%14.2f\n}, "Other Tax", $other_tax);
178         printf(LPR qq{%25s%14.2f\n}, "Untaxed", $untaxed);
179         printf(LPR qq{%25s%14.2f\n}, "Taxed", $taxed);
180         printf(LPR qq{%25s%14.2f\n}, "Tax", $total_tax);
181         printf(LPR qq{\n%39s\n%39.2f\n}, "=========", $total);
182         close LPR || die "Could not close printer: $lpr\n";
183 }
184 if($email && $main::opt_e)
185 {
186         printf(MAIL qq{\n\n%25s%14.2f\n}, "Complimentary", $compped);
187         printf(MAIL qq{%25s%14.2f\n}, "Complimentary Tax", $compped_tax);
188         printf(MAIL qq{%25s%14.2f\n}, "Other", $other);
189         printf(MAIL qq{%25s%14.2f\n}, "Other Tax", $other_tax);
190         printf(MAIL qq{%25s%14.2f\n}, "Untaxed", $untaxed);
191         printf(MAIL qq{%25s%14.2f\n}, "Taxed", $taxed);
192         printf(MAIL qq{%25s%14.2f\n}, "Tax", $total_tax);
193         printf(MAIL qq{\n%39s\n%39.2f\n}, "=========", $total);
194         close MAIL || die "Could not close printer: $email\n";
195 }
196
197
198 # subroutines
199 sub untaint_argv {
200   foreach $_ ( $[ .. $#ARGV ) { #untaint @ARGV
201     $ARGV[$_] =~ /^([\w\-\/ :]*)$/ || die "Illegal argument \"$ARGV[$_]\"";
202     $ARGV[$_]=$1;
203   }
204 }
205
206 sub usage {
207   die "Usage:\n\n  freeside-tax-report [-v] [-p] [-e] user\n";
208 }
209
210 =head1 NAME
211
212 freeside-tax-report - Prints or emails sales taxes invoiced in a given period.
213
214 =head1 SYNOPSIS
215
216   freeside-tax-report [-v] [-p] [-e] user
217
218 =head1 DESCRIPTION
219
220 Prints or emails sales taxes invoiced in a given period.
221
222 -v: Verbose - Prints records to STDOUT.
223
224 -p: Print to printer lpr as found in the conf directory.
225
226 -e: Email output to user found in the Conf email file.
227
228 user: From the mapsecrets file - see config.html from the base documentation
229
230 =head1 VERSION
231
232 $Id: freeside-tax-report,v 1.1 2002-02-22 23:18:32 jeff Exp $
233
234 =head1 BUGS
235
236 Yes..... Use at your own risk. No guarantees or warrantees of any
237 kind apply to this program. Parts of this program are hacked from
238 other GNU licensed software created mainly by Ivan Kohler.
239
240 This is released under the GNU Public License. See www.gnu.org
241 for more information regarding this license.
242
243 =head1 SEE ALSO
244
245 L<FS::cust_main>, config.html from the base documentation
246
247 =head1 HISTORY
248
249 griff@aver-computer.com July 99
250
251 $Log: freeside-tax-report,v $
252 Revision 1.1  2002-02-22 23:18:32  jeff
253 add some reporting features
254
255 Revision 1.3  2002/02/19 14:24:53  jeff
256 might be functional now
257
258 Revision 1.2  2001/08/20 18:31:49  jeff
259 before-merge-to-freeside_1_4_0-pre1
260
261 Revision 1.1  2000/09/20 19:25:19  jeff
262 local modifications
263
264 Revision 1.1  2000/05/13 21:57:56  ivan
265 add print_batch script from Joel Griffiths
266
267
268 =cut
269
270