summaryrefslogtreecommitdiff
path: root/FS/bin/freeside-tax-report
blob: 334c4107bc57f5e6ea3b21874cc4b870eff5e006 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
#!/usr/bin/perl -Tw

use strict;
use Date::Parse;
use Time::Local;
use Getopt::Std;
use FS::Conf;
use FS::UID qw(adminsuidsetup);
use FS::Record qw(qsearch);
use FS::cust_pay;
use FS::cust_pay_batch;

# Set the mail program
my $mail_program = "/usr/sbin/sendmail -t -n"; 

&untaint_argv;	#what it sounds like  (eww)
use vars qw($opt_v $opt_p $opt_e $opt_d $opt_s);
getopts("vped:s:");	#switches

#we're at now now (and later).
my($_enddate)= $main::opt_d ? str2time($main::opt_d) : $^T;
my($_startdate)= $main::opt_s ? str2time($main::opt_s) : $^T;

# Get the current month
my ($ssec,$smin,$shour,$smday,$smon,$syear) =
	(localtime($_startdate) )[0,1,2,3,4,5]; 
$smon++;
$syear -= 100 if $syear >= 100;
$syear = "0" . $syear if $syear < 10;

# Get the current month
my ($esec,$emin,$ehour,$emday,$emon,$eyear) =
	(localtime($_enddate) )[0,1,2,3,4,5]; 
$emon++;
$eyear -= 100 if $eyear >= 100;
$eyear = "0" . $eyear if $eyear < 10;

# Login to the database
my $user = shift or die &usage;
adminsuidsetup $user;

# Get the needed configuration files
my $conf = new FS::Conf;
my $lpr = $conf->config('lpr');
my $email = $conf->config('email');

my(@cust_bills)=qsearch('cust_bill',{});
if (scalar(@cust_bills) == 0)
{
	exit 1;
}

if ($main::opt_v)
{
        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~;
}

# Open print and email pipes
# $lpr and opt_p for printing
# $email and opt_e for email

if ($lpr && $main::opt_p)
{
        open(LPR, "|$lpr");
        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~;
}

if ($email && $main::opt_e)
{
	open (MAIL, "|$mail_program");
	print MAIL <<END
To: $email
From: Account Processor
Subject: Sales Taxes Invoiced


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

END
}

my $compped = 0;
my $compped_tax = 0;
my $other = 0;
my $other_tax = 0;
my $total = 0;
my $taxed = 0;
my $untaxed = 0;
my $total_tax = 0;

# Now I can start looping
foreach my $cust_bill (@cust_bills)
{
	my $_date = $cust_bill->getfield('_date');
	my $invnum = $cust_bill->getfield('invnum');
	my $charged = $cust_bill->getfield('charged');
	

	if ($_date >= $_startdate && $_date <= $_enddate) {
		$total += $charged;

		# The following lines were used to produce rather verbose reports
		#my ($sec,$min,$hour,$mday,$mon,$year) =
		#	(localtime($_date) )[0,1,2,3,4,5]; 
		#$mon++;
		#$year -= 100 if $year >= 100;
		#$year = "0" . $year if $year < 10;

		my $invoice_amt =0;
		my $invoice_tax =0;
		my $invoice_compped =0;
		my(@cust_bill_pkgs)= $cust_bill->cust_bill_pkg;
		foreach my $cust_bill_pkg (@cust_bill_pkgs) {

			my $recur = $cust_bill_pkg->getfield('recur');
			my $setup = $cust_bill_pkg->getfield('setup');
			my $pkgnum = $cust_bill_pkg->getfield('pkgnum');
			
			if ($pkgnum == 0) {
				# The following line was used to produce rather verbose reports
				# printf(MAIL qq{\n%10s%15s%14.2f}, "$mon/$mday/$year", "Tax $invnum", $recur+$setup);
				$invoice_tax += $recur;
				$invoice_tax += $setup;
			} else {
				# The following line was used to produce rather verbose reports
				# printf(MAIL qq{\n%10s%15s%14.2f}, "$mon/$mday/$year", "Inv $invnum", $recur+$setup);
				$invoice_amt += $recur;
				$invoice_amt += $setup;
			}

		}

		my(@cust_bill_pays)= $cust_bill->cust_bill_pay;
		foreach my $cust_bill_pay (@cust_bill_pays) {
			my $payby = $cust_bill_pay->cust_pay->payby;
			my $paid = $cust_bill_pay->getfield('amount');
			if ($payby =~ 'COMP') {
				$invoice_compped += $paid;
			}
		}

		if (abs($invoice_compped - ($invoice_amt + $invoice_tax)) < 0.0001){
			$compped += $invoice_amt;
			$compped_tax += $invoice_tax;
		} elsif ($invoice_compped > 0) {
			printf(qq{\nInvoice %10d has inexpliciable complimentary payments of %14.9f\n}, $invnum, $invoice_compped);
			$other += $invoice_amt;
			$other_tax += $invoice_tax;
		} elsif ($invoice_tax > 0) {
			$total_tax += $invoice_tax;
			$taxed += $invoice_amt;
		} else {
			$untaxed += $invoice_amt;
		}

	}

}

if ($main::opt_v) {
	printf(qq{\n\n%25s%14.2f\n}, "Complimentary", $compped);
	printf(qq{%25s%14.2f\n}, "Complimentary Tax", $compped_tax);
	printf(qq{%25s%14.2f\n}, "Other", $other);
	printf(qq{%25s%14.2f\n}, "Other Tax", $other_tax);
	printf(qq{%25s%14.2f\n}, "Untaxed", $untaxed);
	printf(qq{%25s%14.2f\n}, "Taxed", $taxed);
	printf(qq{%25s%14.2f\n}, "Tax", $total_tax);
	printf(qq{\n%39s\n%39.2f\n}, "=========", $total);
}

# Now I need to close LPR and EMAIL if they were open
if($lpr && $main::opt_p)
{
	printf(LPR qq{\n\n%25s%14.2f\n}, "Complimentary", $compped);
	printf(LPR qq{%25s%14.2f\n}, "Complimentary Tax", $compped_tax);
	printf(LPR qq{%25s%14.2f\n}, "Other", $other);
	printf(LPR qq{%25s%14.2f\n}, "Other Tax", $other_tax);
	printf(LPR qq{%25s%14.2f\n}, "Untaxed", $untaxed);
	printf(LPR qq{%25s%14.2f\n}, "Taxed", $taxed);
	printf(LPR qq{%25s%14.2f\n}, "Tax", $total_tax);
	printf(LPR qq{\n%39s\n%39.2f\n}, "=========", $total);
	close LPR || die "Could not close printer: $lpr\n";
}
if($email && $main::opt_e)
{
	printf(MAIL qq{\n\n%25s%14.2f\n}, "Complimentary", $compped);
	printf(MAIL qq{%25s%14.2f\n}, "Complimentary Tax", $compped_tax);
	printf(MAIL qq{%25s%14.2f\n}, "Other", $other);
	printf(MAIL qq{%25s%14.2f\n}, "Other Tax", $other_tax);
	printf(MAIL qq{%25s%14.2f\n}, "Untaxed", $untaxed);
	printf(MAIL qq{%25s%14.2f\n}, "Taxed", $taxed);
	printf(MAIL qq{%25s%14.2f\n}, "Tax", $total_tax);
	printf(MAIL qq{\n%39s\n%39.2f\n}, "=========", $total);
	close MAIL || die "Could not close printer: $email\n";
}


# subroutines
sub untaint_argv {
  foreach $_ ( $[ .. $#ARGV ) { #untaint @ARGV
    $ARGV[$_] =~ /^([\w\-\/ :]*)$/ || die "Illegal argument \"$ARGV[$_]\"";
    $ARGV[$_]=$1;
  }
}

sub usage {
  die "Usage:\n\n  freeside-tax-report [-v] [-p] [-e] user\n";
}

=head1 NAME

freeside-tax-report - Prints or emails sales taxes invoiced in a given period.

=head1 SYNOPSIS

  freeside-tax-report [-v] [-p] [-e] user

=head1 DESCRIPTION

Prints or emails sales taxes invoiced in a given period.

-v: Verbose - Prints records to STDOUT.

-p: Print to printer lpr as found in the conf directory.

-e: Email output to user found in the Conf email file.

user: From the mapsecrets file - see config.html from the base documentation

=head1 VERSION

$Id: freeside-tax-report,v 1.1 2002-02-22 23:18:32 jeff Exp $

=head1 BUGS

Yes..... Use at your own risk. No guarantees or warrantees of any
kind apply to this program. Parts of this program are hacked from
other GNU licensed software created mainly by Ivan Kohler.

This is released under the GNU Public License. See www.gnu.org
for more information regarding this license.

=head1 SEE ALSO

L<FS::cust_main>, config.html from the base documentation

=head1 HISTORY

griff@aver-computer.com July 99

$Log: freeside-tax-report,v $
Revision 1.1  2002-02-22 23:18:32  jeff
add some reporting features

Revision 1.3  2002/02/19 14:24:53  jeff
might be functional now

Revision 1.2  2001/08/20 18:31:49  jeff
before-merge-to-freeside_1_4_0-pre1

Revision 1.1  2000/09/20 19:25:19  jeff
local modifications

Revision 1.1  2000/05/13 21:57:56  ivan
add print_batch script from Joel Griffiths


=cut