8 use FS::UID qw(adminsuidsetup);
9 use FS::Record qw(qsearch);
11 use FS::cust_pay_batch;
13 # Set the mail program
14 my $mail_program = "/usr/sbin/sendmail -t -n";
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
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;
24 # Get the current month
25 my ($ssec,$smin,$shour,$smday,$smon,$syear) =
26 (localtime($_startdate) )[0,1,2,3,4,5];
28 $syear -= 100 if $syear >= 100;
29 $syear = "0" . $syear if $syear < 10;
31 # Get the current month
32 my ($esec,$emin,$ehour,$emday,$emon,$eyear) =
33 (localtime($_enddate) )[0,1,2,3,4,5];
35 $eyear -= 100 if $eyear >= 100;
36 $eyear = "0" . $eyear if $eyear < 10;
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');
47 my(@cust_bills)=qsearch('cust_bill',{});
48 if (scalar(@cust_bills) == 0)
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~;
58 # Open print and email pipes
59 # $lpr and opt_p for printing
60 # $email and opt_e for email
62 if ($lpr && $main::opt_p)
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~;
68 if ($email && $main::opt_e)
70 open (MAIL, "|$mail_program");
73 From: Account Processor
74 Subject: Sales Taxes Invoiced
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
91 # Now I can start looping
92 foreach my $cust_bill (@cust_bills)
94 my $_date = $cust_bill->getfield('_date');
95 my $invnum = $cust_bill->getfield('invnum');
96 my $charged = $cust_bill->getfield('charged');
99 if ($_date >= $_startdate && $_date <= $_enddate) {
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];
106 #$year -= 100 if $year >= 100;
107 #$year = "0" . $year if $year < 10;
111 my $invoice_compped =0;
112 my(@cust_bill_pkgs)= $cust_bill->cust_bill_pkg;
113 foreach my $cust_bill_pkg (@cust_bill_pkgs) {
115 my $recur = $cust_bill_pkg->getfield('recur');
116 my $setup = $cust_bill_pkg->getfield('setup');
117 my $pkgnum = $cust_bill_pkg->getfield('pkgnum');
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;
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;
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;
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;
153 $untaxed += $invoice_amt;
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);
171 # Now I need to close LPR and EMAIL if they were open
172 if($lpr && $main::opt_p)
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";
184 if($email && $main::opt_e)
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";
200 foreach $_ ( $[ .. $#ARGV ) { #untaint @ARGV
201 $ARGV[$_] =~ /^([\w\-\/ :]*)$/ || die "Illegal argument \"$ARGV[$_]\"";
207 die "Usage:\n\n freeside-tax-report [-v] [-p] [-e] user\n";
212 freeside-tax-report - Prints or emails sales taxes invoiced in a given period.
216 freeside-tax-report [-v] [-p] [-e] user
220 Prints or emails sales taxes invoiced in a given period.
222 -v: Verbose - Prints records to STDOUT.
224 -p: Print to printer lpr as found in the conf directory.
226 -e: Email output to user found in the Conf email file.
228 user: From the mapsecrets file - see config.html from the base documentation
232 $Id: freeside-tax-report,v 1.1 2002-02-22 23:18:32 jeff Exp $
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.
240 This is released under the GNU Public License. See www.gnu.org
241 for more information regarding this license.
245 L<FS::cust_main>, config.html from the base documentation
249 griff@aver-computer.com July 99
251 $Log: freeside-tax-report,v $
252 Revision 1.1 2002-02-22 23:18:32 jeff
253 add some reporting features
255 Revision 1.3 2002/02/19 14:24:53 jeff
256 might be functional now
258 Revision 1.2 2001/08/20 18:31:49 jeff
259 before-merge-to-freeside_1_4_0-pre1
261 Revision 1.1 2000/09/20 19:25:19 jeff
264 Revision 1.1 2000/05/13 21:57:56 ivan
265 add print_batch script from Joel Griffiths