add print_batch script from Joel Griffiths
[freeside.git] / FS / bin / freeside-print-batch
1 #!/usr/bin/perl -Tw
2
3 use strict;
4 #use Date::Format;
5 use Time::Local;
6 use Getopt::Std;
7 use FS::Conf;
8 use FS::UID qw(adminsuidsetup);
9 use FS::Record qw(qsearch);
10 use FS::cust_pay;
11 use FS::cust_pay_batch;
12
13 # Get the currennt time and date
14 my $time = time;
15 my ($sec,$min,$hour,$mday,$mon,$year) =
16         (localtime($time) )[0,1,2,3,4,5]; 
17 my $_date =
18         timelocal($sec,$min,$hour,$mday,$mon,$year);
19
20 # Set the mail program
21 my $mail_program = "/usr/sbin/sendmail -t -n"; 
22
23 &untaint_argv;  #what it sounds like  (eww)
24 use vars qw($opt_v $opt_p $opt_e $opt_a $opt_d);
25 getopts("vpead");       #switches
26
27 # Login to the database
28 my $user = shift or die &usage;
29 adminsuidsetup $user;
30
31 # Get the needed configuration files
32 my $conf = new FS::Conf;
33 my $lpr = $conf->config('lpr');
34 my $email = $conf->config('email');
35
36 my(@batch)=qsearch('cust_pay_batch',{});
37 if (scalar(@batch) == 0)
38 {
39         exit 1;
40 }
41
42 # Open print and email pipes
43 # $lpr and opt_p for printing
44 # $email and opt_e for email
45
46 if ($lpr && $main::opt_p)
47 {
48         open(LPR, "|$lpr");
49         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~;
50 }
51
52 if ($email && $main::opt_e)
53 {
54         open (MAIL, "|$mail_program");
55         print MAIL <<END
56 To: $email
57 From: Account Processor
58 Subject: CREDIT CARD PAYMENTS DUE
59
60
61 C R E D I T  C A R D  P A Y M E N T S  D U E $mon/$mday/$year
62 END
63 }
64
65 # Now I can start looping
66 foreach my $cust_pay_batch (@batch)
67 {
68         my $state = $cust_pay_batch->getfield('state');
69         my $zip = $cust_pay_batch->getfield('zip');
70         my $amount = $cust_pay_batch->getfield('amount');
71         my $last = $cust_pay_batch->getfield('last');
72         my $address1 = $cust_pay_batch->getfield('address1');
73         my $address2 = $cust_pay_batch->getfield('address2');
74         my $first = $cust_pay_batch->getfield('first');
75         my $city = $cust_pay_batch->getfield('city');
76         my $cardnum = $cust_pay_batch->getfield('cardnum');
77         my $payname = $cust_pay_batch->getfield('payname');
78         my $exp = $cust_pay_batch->getfield('exp');
79         my $invnum = $cust_pay_batch->getfield('invnum');
80         my $custnum = $cust_pay_batch->getfield('custnum');
81
82         # Need a carriage return in address before address2
83         # if it exists. Otherwise address will just be address1
84         my $address = $address1;
85         $address .= "\n$address2" if ($address2);
86
87         # Only print to the screen in verbose mode
88         if ($main::opt_v)
89         {
90                 printf("Invoice %d for %s %s\tCustomer Number: %d\n",
91                         $invnum,
92                         $first,
93                         $last,
94                         $custnum);
95
96                 printf("\t%s\n", $address);
97                 printf("\t%s, %s, %s\n\n",
98                         $city,
99                         $state,
100                         $zip);
101
102                 printf("\tCard Number: %s\tExp:%s\n",
103                         $cardnum,
104                         $exp);
105                 printf("\t\tName: %s\n", $payname);
106                 printf("\t\tAmount: %.2f\n\n\n", $amount);
107         }
108
109         if ($lpr && $main::opt_p)
110         {
111                 printf(LPR "Invoice %d for %s %s\tCustomer Number: %d\n",
112                         $invnum,
113                         $first,
114                         $last,
115                         $custnum);
116
117                 printf(LPR "\t%s\n", $address);
118                 printf(LPR "\t%s, %s, %s\n\n",
119                         $city,
120                         $state,
121                         $zip);
122
123                 printf(LPR "\tCard Number: %s\tExp:%s\n",
124                         $cardnum,
125                         $exp);
126                 printf(LPR "\t\tName: %s\n", $payname);
127                 printf(LPR "\t\tAmount: %.2f\n\n\n", $amount);
128         }
129
130         if ($email && $main::opt_e)
131         {
132                 printf(MAIL "Invoice %d for %s %s\tCustomer Number: %d\n",
133                         $invnum,
134                         $first,
135                         $last,
136                         $custnum);
137
138                 printf(MAIL "\t%s\n", $address);
139                 printf(MAIL "\t%s, %s, %s\n\n",
140                         $city,
141                         $state,
142                         $zip);
143
144                 printf(MAIL "\tCard Number: %s\tExp:%s\n",
145                         $cardnum,
146                         $exp);
147                 printf(MAIL "\t\tName: %s\n", $payname);
148                 printf(MAIL "\t\tAmount: %.2f\n\n\n", $amount);
149         }
150
151         # Now I want to delete the records from cust_pay_batch
152         # and mark the records in cust_pay as paid today if
153         # the delete (-d) command line option is set.
154         if($main::opt_a)
155         {
156                 my $payment=new FS::cust_pay {
157                         'invnum' => $invnum,
158                         'paid' => $amount,
159                         '_date' => $_date,
160                         'payby' => "CARD",
161                         'payinfo' => $cardnum,
162                         'paybatch' => "AUTO",
163                 };
164                 
165                 my $pay_error=$payment->insert;
166                 if ($pay_error)
167                         {
168                                 # warn might be better if you get root's mail
169                                 # NEED TO TEST THIS BEFORE DELETE IF WARN IS USED
170                                 die "Could not update cust_pay for invnum $invnum. $pay_error\n";
171                         }
172         }
173                 
174         # This just deletes the records
175         # Must be last in the foreach loop
176         if($main::opt_d)
177         {
178                 my $del_error = $cust_pay_batch->delete;
179                 if ($del_error)
180                 {
181                         die "Could not delete cust_pay_batch for invnum $invnum. $del_error\n";
182                 }
183         }
184
185 }
186
187 # Now I need to close LPR and EMAIL if they were open
188 if($lpr && $main::opt_p)
189 {
190         close LPR || die "Could not close printer: $lpr\n";
191 }
192
193 if($email && $main::opt_e)
194 {
195         close MAIL || die "Could not close printer: $lpr\n";
196 }
197
198
199 # subroutines
200 sub untaint_argv {
201   foreach $_ ( $[ .. $#ARGV ) { #untaint @ARGV
202     $ARGV[$_] =~ /^([\w\-\/]*)$/ || die "Illegal argument \"$ARGV[$_]\"";
203     $ARGV[$_]=$1;
204   }
205 }
206
207 sub usage {
208   die "Usage:\n\n  freeside-print-batch [-v] [-p] [-e] [-a] [-d] user\n";
209 }
210
211 =head1 NAME
212
213 freeside-print-batch - Prints or emails cust_pay_batch. Also deletes
214               old records and adds payment to cust_pay.
215                                                         Usually run after the bill command.
216
217 =head1 SYNOPSIS
218
219   freeside-print-batch [-v] [-p] [-e] [-a] [-d] user
220
221 =head1 DESCRIPTION
222
223 Prints or emails cust_pay_batch. Can enter payment and delete
224 printed records. Usually run as a cron job.
225
226 -v: Verbose - Prints records to STDOUT.
227
228 -p: Print to printer lpr as found in the conf directory.
229
230 -e: Email output to user found in the Conf email file.
231
232 -a: Automatically pays all records in cust_pay_batch. Use -d with this option usually.
233
234 -d:     Delete - Pays account and deletes record from cust_pay_batch.
235
236 user: From the mapsecrets file - see config.html from the base documentation
237
238 =head1 VERSION
239
240 $Id: freeside-print-batch,v 1.1 2000-05-13 21:57:56 ivan Exp $
241
242 =head1 BUGS
243
244 Yes..... Use at your own risk. No guarantees or warrantees of any
245 kind apply to this program. Parts of this program are hacked from
246 other GNU licensed software created mainly by Ivan Kohler.
247
248 This is released under the GNU Public License. See www.gnu.org
249 for more information regarding this license.
250
251 =head1 SEE ALSO
252
253 L<FS::cust_main>, config.html from the base documentation
254
255 =head1 HISTORY
256
257 griff@aver-computer.com July 99
258
259 $Log: freeside-print-batch,v $
260 Revision 1.1  2000-05-13 21:57:56  ivan
261 add print_batch script from Joel Griffiths
262
263
264 =cut
265
266