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
|
#!/usr/bin/perl -Tw
use strict;
use Date::Parse;
use Time::Local;
use Getopt::Std;
use Text::Template;
use Net::SMTP;
use Mail::Header;
use Mail::Internet;
use FS::Conf;
use FS::UID qw(adminsuidsetup);
use FS::Record qw(qsearch);
use FS::cust_credit;
&untaint_argv; #what it sounds like (eww)
use vars qw($opt_v $opt_p $opt_m $opt_e $opt_t $opt_s $opt_f $report_lines $report_template @buf $header);
getopts("vpmef:s:"); #switches
#we're at now now (and later).
my($_finishdate)= $opt_f ? str2time($main::opt_f) : $^T;
my($_startdate)= $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 += 1900;
# Get the current month
my ($fsec,$fmin,$fhour,$fmday,$fmon,$fyear) =
(localtime($_finishdate) )[0,1,2,3,4,5];
$fmon++;
$fyear += 1900;
# 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 $smtpmachine = $conf->config('smtpmachine');
my $mail_sender = $conf->exists('invoice_from') ? $conf->config('invoice_from') :
'postmaster';
my @report_template = $conf->config('report_template')
or die "cannot load config file report_template";
$report_lines = 0;
foreach ( grep /report_lines\(\d+\)/, @report_template ) { #kludgy :/
/report_lines\((\d+)\)/;
$report_lines += $1;
}
die "no report_lines() functions in template?" unless $report_lines;
$report_template = new Text::Template (
TYPE => 'ARRAY',
SOURCE => [ map "$_\n", @report_template ],
) or die "can't create new Text::Template object: $Text::Template::ERROR";
my(@cust_credits)=qsearch('cust_credit',{});
if (scalar(@cust_credits) == 0)
{
exit 1;
}
# Open print and email pipes
# $lpr and opt_p for printing
# $email and opt_m for email
if ($lpr && $main::opt_p)
{
open(LPR, "|$lpr");
}
if ($email && $main::opt_m)
{
$ENV{MAILADDRESS} = $mail_sender;
$header = new Mail::Header ( [
"From: Account Processor",
"To: $email",
"Sender: $mail_sender",
"Reply-To: $mail_sender",
"Subject: In House Credits",
] );
}
my $uninvoiced = 0;
my $total = 0;
my $taxed = 0;
my $untaxed = 0;
my $total_tax = 0;
# Now I can start looping
foreach my $cust_credit (@cust_credits)
{
my $_date = $cust_credit->getfield('_date');
my $amount = $cust_credit->getfield('amount');
if ($_date >= $_startdate && $_date <= $_finishdate) {
$total += $amount;
}
}
push @buf, sprintf(qq{\n%25s%14.2f\n}, "Credits Offered", $total);
push @buf, sprintf(qq{\n%39s\n%39.2f\n}, "=========", $total);
sub FS::credit_report::_template::report_lines {
my $lines = shift;
map {
scalar(@buf) ? shift @buf : '' ;
}
( 1 .. $lines );
}
$FS::credit_report::_template::title = qq~IN HOUSE CREDITS for $smon/$smday/$syear through $fmon/$fmday/$fyear~;
$FS::credit_report::_template::title = $opt_t if $opt_t;
$FS::credit_report::_template::page = 1;
$FS::credit_report::_template::date = $^T;
$FS::credit_report::_template::date = $^T;
$FS::credit_report::_template::fdate = $_finishdate;
$FS::credit_report::_template::fdate = $_finishdate;
$FS::credit_report::_template::sdate = $_startdate;
$FS::credit_report::_template::sdate = $_startdate;
$FS::credit_report::_template::total_pages =
int( scalar(@buf) / $report_lines);
$FS::credit_report::_template::total_pages++ if scalar(@buf) % $report_lines;
my @report;
while (@buf) {
push @report, split("\n",
$report_template->fill_in( PACKAGE => 'FS::credit_report::_template' )
);
$FS::credit_report::_template::page++;
}
if ($opt_v) {
print map "$_\n", @report;
}
if($lpr && $opt_p)
{
print LPR map "$_\n", @report;
print LPR "\f" if $opt_e;
close LPR || die "Could not close printer: $lpr\n";
}
if($email && $opt_m)
{
my $message = new Mail::Internet (
'Header' => $header,
'Body' => [ (@report) ],
);
$!=0;
$message->smtpsend( Host => "$smtpmachine" )
or die "can't send report to $email via $smtpmachine: $!";
}
# subroutines
sub untaint_argv {
foreach $_ ( $[ .. $#ARGV ) { #untaint @ARGV
$ARGV[$_] =~ /^([\w\-\/ :\.]*)$/ || die "Illegal argument \"$ARGV[$_]\"";
$ARGV[$_]=$1;
}
}
sub usage {
die "Usage:\n\n freeside-credit-report [-v] [-p] [-e] user\n";
}
=head1 NAME
freeside-credit-report - Prints or emails total credit memos in a given period.
=head1 SYNOPSIS
freeside-credit-report [-v] [-p] [-m] [-e] [-t "title"] [-s date] [-f date] user
=head1 DESCRIPTION
Prints or emails total credit memos in a given period.
-v: Verbose - Prints records to STDOUT.
-p: Print to printer lpr as found in the conf directory.
-m: Email output to user found in the Conf email file.
-e: Print a final form feed to the printer.
-t: supply a title for the top of each page.
-s: starting date for inclusion
-f: final date for inclusion
user: From the mapsecrets file - see config.html from the base documentation
=head1 VERSION
$Id: freeside-credit-report,v 1.5 2002-09-09 22:57:34 ivan 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 AUTHOR
Jeff Finucane <jeff@cmh.net>
based on print-batch by Joel Griffiths <griff@aver-computer.com>
=cut
|