summaryrefslogtreecommitdiff
path: root/FS/bin/freeside-print-batch
blob: 5efa4ccb3a29b904e8e6e93d55488bc2a8d76817 (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
#!/usr/bin/perl -Tw

use strict;
#use Date::Format;
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;

# Get the currennt time and date
my $time = time;
my ($sec,$min,$hour,$mday,$mon,$year) =
	(localtime($time) )[0,1,2,3,4,5]; 
my $_date =
	timelocal($sec,$min,$hour,$mday,$mon,$year);

# 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_a $opt_d);
getopts("vpead");	#switches

# 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(@batch)=qsearch('cust_pay_batch',{});
if (scalar(@batch) == 0)
{
	exit 1;
}

# 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~C R E D I T  C A R D  P A Y M E N T S  D U E $mon/$mday/$year\n\n~;
}

if ($email && $main::opt_e)
{
	open (MAIL, "|$mail_program");
	print MAIL <<END
To: $email
From: Account Processor
Subject: CREDIT CARD PAYMENTS DUE


C R E D I T  C A R D  P A Y M E N T S  D U E $mon/$mday/$year
END
}

# Now I can start looping
foreach my $cust_pay_batch (@batch)
{
	my $state = $cust_pay_batch->getfield('state');
	my $zip = $cust_pay_batch->getfield('zip');
	my $amount = $cust_pay_batch->getfield('amount');
	my $last = $cust_pay_batch->getfield('last');
	my $address1 = $cust_pay_batch->getfield('address1');
	my $address2 = $cust_pay_batch->getfield('address2');
	my $first = $cust_pay_batch->getfield('first');
	my $city = $cust_pay_batch->getfield('city');
	my $cardnum = $cust_pay_batch->getfield('cardnum');
	my $payname = $cust_pay_batch->getfield('payname');
	my $exp = $cust_pay_batch->getfield('exp');
	my $invnum = $cust_pay_batch->getfield('invnum');
	my $custnum = $cust_pay_batch->getfield('custnum');

	# Need a carriage return in address before address2
	# if it exists. Otherwise address will just be address1
	my $address = $address1;
	$address .= "\n$address2" if ($address2);

	# Only print to the screen in verbose mode
	if ($main::opt_v)
	{
		printf("Invoice %d for %s %s\tCustomer Number: %d\n",
			$invnum,
			$first,
			$last,
			$custnum);

		printf("\t%s\n", $address);
		printf("\t%s, %s, %s\n\n",
			$city,
			$state,
			$zip);

		printf("\tCard Number: %s\tExp:%s\n",
			$cardnum,
			$exp);
		printf("\t\tName: %s\n", $payname);
		printf("\t\tAmount: %.2f\n\n\n", $amount);
	}

	if ($lpr && $main::opt_p)
	{
		printf(LPR "Invoice %d for %s %s\tCustomer Number: %d\n",
			$invnum,
			$first,
			$last,
			$custnum);

		printf(LPR "\t%s\n", $address);
		printf(LPR "\t%s, %s, %s\n\n",
			$city,
			$state,
			$zip);

		printf(LPR "\tCard Number: %s\tExp:%s\n",
			$cardnum,
			$exp);
		printf(LPR "\t\tName: %s\n", $payname);
		printf(LPR "\t\tAmount: %.2f\n\n\n", $amount);
	}

	if ($email && $main::opt_e)
	{
		printf(MAIL "Invoice %d for %s %s\tCustomer Number: %d\n",
			$invnum,
			$first,
			$last,
			$custnum);

		printf(MAIL "\t%s\n", $address);
		printf(MAIL "\t%s, %s, %s\n\n",
			$city,
			$state,
			$zip);

		printf(MAIL "\tCard Number: %s\tExp:%s\n",
			$cardnum,
			$exp);
		printf(MAIL "\t\tName: %s\n", $payname);
		printf(MAIL "\t\tAmount: %.2f\n\n\n", $amount);
	}

	# Now I want to delete the records from cust_pay_batch
	# and mark the records in cust_pay as paid today if
	# the delete (-d) command line option is set.
	if($main::opt_a)
	{
		my $payment=new FS::cust_pay {
			'invnum' => $invnum,
			'paid' => $amount,
			'_date' => $_date,
			'payby' => "CARD",
			'payinfo' => $cardnum,
			'paybatch' => "AUTO",
		};
		
		my $pay_error=$payment->insert;
		if ($pay_error)
			{
				# warn might be better if you get root's mail
				# NEED TO TEST THIS BEFORE DELETE IF WARN IS USED
				die "Could not update cust_pay for invnum $invnum. $pay_error\n";
			}
	}
		
	# This just deletes the records
	# Must be last in the foreach loop
	if($main::opt_d)
	{
		my $del_error = $cust_pay_batch->delete;
		if ($del_error)
		{
			die "Could not delete cust_pay_batch for invnum $invnum. $del_error\n";
		}
	}

}

# Now I need to close LPR and EMAIL if they were open
if($lpr && $main::opt_p)
{
	close LPR || die "Could not close printer: $lpr\n";
}

if($email && $main::opt_e)
{
	close MAIL || die "Could not close printer: $lpr\n";
}


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

sub usage {
  die "Usage:\n\n  freeside-print-batch [-v] [-p] [-e] [-a] [-d] user\n";
}

=head1 NAME

freeside-print-batch - Prints or emails cust_pay_batch. Also deletes
              old records and adds payment to cust_pay.
							Usually run after the bill command.

=head1 SYNOPSIS

  freeside-print-batch [-v] [-p] [-e] [-a] [-d] user

=head1 DESCRIPTION

Prints or emails cust_pay_batch. Can enter payment and delete
printed records. Usually run as a cron job.

B<-v>: Verbose - Prints records to STDOUT.

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

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

B<-a>: Automatically pays all records in cust_pay_batch. Use -d with this option usually.

B<-d>:	Delete - Pays account and deletes record from cust_pay_batch.

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

=head1 VERSION

$Id: freeside-print-batch,v 1.2 2001-02-21 01:48:07 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 HISTORY

griff@aver-computer.com July 99

$Log: freeside-print-batch,v $
Revision 1.2  2001-02-21 01:48:07  ivan
stupid pod errors

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


=cut