10 use FS::UID qw(adminsuidsetup);
11 use FS::Record qw(qsearch);
13 use FS::cust_bill_pay;
16 # Set the mail program
17 my $mail_program = "/usr/sbin/sendmail -t -n";
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);
21 getopts("vpmef:s:"); #switches
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;
27 # Get the current month
28 my ($ssec,$smin,$shour,$smday,$smon,$syear) =
29 (localtime($_startdate) )[0,1,2,3,4,5];
33 # Get the current month
34 my ($fsec,$fmin,$fhour,$fmday,$fmon,$fyear) =
35 (localtime($_finishdate) )[0,1,2,3,4,5];
39 # Login to the database
40 my $user = shift or die &usage;
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 @report_template = $conf->config('report_template')
48 or die "cannot load config file report_template";
50 foreach ( grep /report_lines\(\d+\)/, @report_template ) { #kludgy :/
51 /report_lines\((\d+)\)/;
54 die "no report_lines() functions in template?" unless $report_lines;
55 $report_template = new Text::Template (
57 SOURCE => [ map "$_\n", @report_template ],
58 ) or die "can't create new Text::Template object: $Text::Template::ERROR";
61 my(@cust_bills)=qsearch('cust_bill',{});
62 if (scalar(@cust_bills) == 0)
67 # Open print and email pipes
68 # $lpr and opt_p for printing
69 # $email and opt_m for email
71 if ($lpr && $main::opt_p)
76 if ($email && $main::opt_m)
78 open (MAIL, "|$mail_program");
81 From: Account Processor
82 Subject: Sales Taxes Invoiced
97 # Now I can start looping
98 foreach my $cust_bill (@cust_bills)
100 my $_date = $cust_bill->getfield('_date');
101 my $invnum = $cust_bill->getfield('invnum');
102 my $charged = $cust_bill->getfield('charged');
104 if ($_date >= $_startdate && $_date <= $_finishdate) {
107 # The following lines were used to produce rather verbose reports
108 #my ($sec,$min,$hour,$mday,$mon,$year) =
109 # (localtime($_date) )[0,1,2,3,4,5];
111 #$year -= 100 if $year >= 100;
112 #$year = "0" . $year if $year < 10;
116 my $invoice_comped =0;
117 my(@cust_bill_pkgs)= $cust_bill->cust_bill_pkg;
118 foreach my $cust_bill_pkg (@cust_bill_pkgs) {
120 my $recur = $cust_bill_pkg->getfield('recur');
121 my $setup = $cust_bill_pkg->getfield('setup');
122 my $pkgnum = $cust_bill_pkg->getfield('pkgnum');
125 # The following line was used to produce rather verbose reports
126 # push @buf, ('', sprintf(qq{%10s%15s%14.2f}, "$mon/$mday/$year", "Tax $invnum", $recur+$setup));
127 $invoice_tax += $recur;
128 $invoice_tax += $setup;
130 # The following line was used to produce rather verbose reports
131 # push @buf, ('', sprintf(qq{%10s%15s%14.2f}, "$mon/$mday/$year", "Inv $invnum", $recur+$setup));
132 $invoice_amt += $recur;
133 $invoice_amt += $setup;
138 my(@cust_bill_pays)= $cust_bill->cust_bill_pay;
139 foreach my $cust_bill_pay (@cust_bill_pays) {
140 my $payby = $cust_bill_pay->cust_pay->payby;
141 my $paid = $cust_bill_pay->getfield('amount');
142 if ($payby =~ 'COMP') {
143 $invoice_comped += $paid;
147 if (abs($invoice_comped - ($invoice_amt + $invoice_tax)) < 0.0001){
148 $comped += $invoice_amt;
149 $comped_tax += $invoice_tax;
150 } elsif ($invoice_comped > 0) {
151 push @buf, sprintf(qq{\nInvoice %10d has inexpliciable complimentary payments of %14.9f\n}, $invnum, $invoice_comped);
152 $other += $invoice_amt;
153 $other_tax += $invoice_tax;
154 } elsif ($invoice_tax > 0) {
155 $total_tax += $invoice_tax;
156 $taxed += $invoice_amt;
158 $untaxed += $invoice_amt;
165 push @buf, ('', sprintf(qq{%25s%14.2f}, "Complimentary", $comped));
166 push @buf, sprintf(qq{%25s%14.2f}, "Complimentary Tax", $comped_tax);
167 push @buf, sprintf(qq{%25s%14.2f}, "Other", $other);
168 push @buf, sprintf(qq{%25s%14.2f}, "Other Tax", $other_tax);
169 push @buf, sprintf(qq{%25s%14.2f}, "Untaxed", $untaxed);
170 push @buf, sprintf(qq{%25s%14.2f}, "Taxed", $taxed);
171 push @buf, sprintf(qq{%25s%14.2f}, "Tax", $total_tax);
172 push @buf, ('', sprintf(qq{%39s}, "========="), sprintf(qq{%39.2f}, $total));
174 sub FS::tax_report::_template::report_lines {
177 scalar(@buf) ? shift @buf : '' ;
182 $FS::tax_report::_template::title = qq~SALES TAXES INVOICED for $smon/$smday/$syear through $fmon/$fmday/$fyear~;
183 $FS::tax_report::_template::title = $opt_t if $opt_t;
184 $FS::tax_report::_template::page = 1;
185 $FS::tax_report::_template::date = $^T;
186 $FS::tax_report::_template::date = $^T;
187 $FS::tax_report::_template::fdate = $_finishdate;
188 $FS::tax_report::_template::fdate = $_finishdate;
189 $FS::tax_report::_template::sdate = $_startdate;
190 $FS::tax_report::_template::sdate = $_startdate;
191 $FS::tax_report::_template::total_pages =
192 int( scalar(@buf) / $report_lines);
193 $FS::tax_report::_template::total_pages++ if scalar(@buf) % $report_lines;
197 push @report, split("\n",
198 $report_template->fill_in( PACKAGE => 'FS::tax_report::_template' )
200 $FS::tax_report::_template::page++;
204 print map "$_\n", @report;
208 print LPR map "$_\n", @report;
209 print LPR "\f" if $opt_e;
210 close LPR || die "Could not close printer: $lpr\n";
214 print MAIL map "$_\n", @report;
215 close MAIL || die "Could not close printer: $email\n";
221 foreach $_ ( $[ .. $#ARGV ) { #untaint @ARGV
222 $ARGV[$_] =~ /^([\w\-\/ :]*)$/ || die "Illegal argument \"$ARGV[$_]\"";
228 die "Usage:\n\n freeside-tax-report [-v] [-p] [-e] user\n";
233 freeside-tax-report - Prints or emails sales taxes invoiced in a given period.
237 freeside-tax-report [-v] [-p] [-m] [-e] [-t "title"] [-s date] [-f date] user
241 Prints or emails sales taxes invoiced in a given period.
243 -v: Verbose - Prints records to STDOUT.
245 -p: Print to printer lpr as found in the conf directory.
247 -m: Email output to user found in the Conf email file.
249 -e: Print a final form feed to the printer.
251 -t: supply a title for the top of each page.
253 -s: starting date for inclusion
255 -f: final date for inclusion
257 user: From the mapsecrets file - see config.html from the base documentation
261 $Id: freeside-tax-report,v 1.3 2002-03-06 00:17:32 ivan Exp $
265 Yes..... Use at your own risk. No guarantees or warrantees of any
266 kind apply to this program. Parts of this program are hacked from
267 other GNU licensed software created mainly by Ivan Kohler.
269 This is released under the GNU Public License. See www.gnu.org
270 for more information regarding this license.
274 L<FS::cust_main>, config.html from the base documentation
278 Jeff Finucane <jeff@cmh.net>
280 based on print-batch by Joel Griffiths <griff@aver-computer.com>