4 use vars qw( @ISA $conf $unsuspendauto $smtpmachine $invoice_from );
7 use Mail::Internet 1.44;
8 use Business::CreditCard;
10 use FS::Record qw( dbh qsearch qsearchs dbh );
12 use FS::cust_bill_pay;
15 @ISA = qw( FS::Record );
17 #ask FS::UID to run this stuff for us later
18 $FS::UID::callback{'FS::cust_pay'} = sub {
21 $unsuspendauto = $conf->exists('unsuspendauto');
22 $smtpmachine = $conf->config('smtpmachine');
23 $invoice_from = $conf->config('invoice_from');
29 FS::cust_pay - Object methods for cust_pay objects
35 $record = new FS::cust_pay \%hash;
36 $record = new FS::cust_pay { 'column' => 'value' };
38 $error = $record->insert;
40 $error = $new_record->replace($old_record);
42 $error = $record->delete;
44 $error = $record->check;
48 An FS::cust_pay object represents a payment; the transfer of money from a
49 customer. FS::cust_pay inherits from FS::Record. The following fields are
54 =item paynum - primary key (assigned automatically for new payments)
56 =item custnum - customer (see L<FS::cust_main>)
58 =item paid - Amount of this payment
60 =item _date - specified as a UNIX timestamp; see L<perlfunc/"time">. Also see
61 L<Time::Local> and L<Date::Parse> for conversion functions.
63 =item payby - `CARD' (credit cards), `CHEK' (electronic check/ACH),
64 `BILL' (billing), or `COMP' (free)
66 =item payinfo - card number, check #, or comp issuer (4-8 lowercase alphanumerics; think username), respectively
68 =item paybatch - text field for tracking card processing
70 =item closed - books closed flag, empty or `Y'
80 Creates a new payment. To add the payment to the databse, see L<"insert">.
84 sub table { 'cust_pay'; }
88 Adds this payment to the database.
90 For backwards-compatibility and convenience, if the additional field invnum
91 is defined, an FS::cust_bill_pay record for the full amount of the payment
92 will be created. In this case, custnum is optional.
99 local $SIG{HUP} = 'IGNORE';
100 local $SIG{INT} = 'IGNORE';
101 local $SIG{QUIT} = 'IGNORE';
102 local $SIG{TERM} = 'IGNORE';
103 local $SIG{TSTP} = 'IGNORE';
104 local $SIG{PIPE} = 'IGNORE';
106 my $oldAutoCommit = $FS::UID::AutoCommit;
107 local $FS::UID::AutoCommit = 0;
110 if ( $self->invnum ) {
111 my $cust_bill = qsearchs('cust_bill', { 'invnum' => $self->invnum } )
113 $dbh->rollback if $oldAutoCommit;
114 return "Unknown cust_bill.invnum: ". $self->invnum;
116 $self->custnum($cust_bill->custnum );
119 my $cust_main = qsearchs( 'cust_main', { 'custnum' => $self->custnum } );
120 my $old_balance = $cust_main->balance;
122 my $error = $self->check;
123 return $error if $error;
125 $error = $self->SUPER::insert;
127 $dbh->rollback if $oldAutoCommit;
128 return "error inserting $self: $error";
131 if ( $self->invnum ) {
132 my $cust_bill_pay = new FS::cust_bill_pay {
133 'invnum' => $self->invnum,
134 'paynum' => $self->paynum,
135 'amount' => $self->paid,
136 '_date' => $self->_date,
138 $error = $cust_bill_pay->insert;
140 $dbh->rollback if $oldAutoCommit;
141 return "error inserting $cust_bill_pay: $error";
145 if ( $self->paybatch =~ /^webui-/ ) {
146 my @cust_pay = qsearch('cust_pay', {
147 'custnum' => $self->custnum,
148 'paybatch' => $self->paybatch,
150 if ( scalar(@cust_pay) > 1 ) {
151 $dbh->rollback if $oldAutoCommit;
152 return "a payment with webui token ". $self->paybatch. " already exists";
156 #false laziness w/ cust_credit::insert
157 if ( $unsuspendauto && $old_balance && $cust_main->balance <= 0 ) {
158 my @errors = $cust_main->unsuspend;
160 # side-fx with nested transactions? upstack rolls back?
161 warn "WARNING:Errors unsuspending customer ". $cust_main->custnum. ": ".
167 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
173 sub upgrade_replace { #1.3.x->1.4.x
176 local $SIG{HUP} = 'IGNORE';
177 local $SIG{INT} = 'IGNORE';
178 local $SIG{QUIT} = 'IGNORE';
179 local $SIG{TERM} = 'IGNORE';
180 local $SIG{TSTP} = 'IGNORE';
181 local $SIG{PIPE} = 'IGNORE';
183 my $oldAutoCommit = $FS::UID::AutoCommit;
184 local $FS::UID::AutoCommit = 0;
187 my $error = $self->check;
188 return $error if $error;
190 my %new = $self->hash;
191 my $new = FS::cust_pay->new(\%new);
193 if ( $self->invnum ) {
194 my $cust_bill_pay = new FS::cust_bill_pay {
195 'invnum' => $self->invnum,
196 'paynum' => $self->paynum,
197 'amount' => $self->paid,
198 '_date' => $self->_date,
200 $error = $cust_bill_pay->insert;
202 /total cust_bill_pay.amount and cust_credit_bill.amount .* for invnum .* greater than cust_bill.charged/ ) {
204 my $cust_bill = qsearchs( 'cust_bill', { 'invnum' => $self->invnum } );
205 $new->custnum($cust_bill->custnum);
207 $dbh->rollback if $oldAutoCommit;
210 $new->custnum($cust_bill_pay->cust_bill->custnum);
216 $error = $new->SUPER::replace($self);
218 $dbh->rollback if $oldAutoCommit;
222 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
231 Deletes this payment and all associated applications (see L<FS::cust_bill_pay>),
232 unless the closed flag is set.
238 return "Can't delete closed payment" if $self->closed =~ /^Y/i;
240 local $SIG{HUP} = 'IGNORE';
241 local $SIG{INT} = 'IGNORE';
242 local $SIG{QUIT} = 'IGNORE';
243 local $SIG{TERM} = 'IGNORE';
244 local $SIG{TSTP} = 'IGNORE';
245 local $SIG{PIPE} = 'IGNORE';
247 my $oldAutoCommit = $FS::UID::AutoCommit;
248 local $FS::UID::AutoCommit = 0;
251 foreach my $cust_bill_pay ( $self->cust_bill_pay ) {
252 my $error = $cust_bill_pay->delete;
254 $dbh->rollback if $oldAutoCommit;
259 my $error = $self->SUPER::delete(@_);
261 $dbh->rollback if $oldAutoCommit;
265 if ( $conf->config('deletepayments') ne '' ) {
267 my $cust_main = qsearchs('cust_main',{ 'custnum' => $self->custnum });
268 #false laziness w/FS::cust_bill::send & fs_signup_server
269 $ENV{MAILADDRESS} = $invoice_from; #??? well as good as any
270 my $header = new Mail::Header ( [
271 "From: $invoice_from",
272 "To: ". $conf->config('deletepayments'),
273 "Sender: $invoice_from",
274 "Reply-To: $invoice_from",
275 "Date: ". time2str("%a, %d %b %Y %X %z", time),
276 "Subject: FREESIDE NOTIFICATION: Payment deleted",
278 my $message = new Mail::Internet (
281 "This is an automatic message from your Freeside installation\n",
282 "informing you that the following payment has been deleted:\n",
284 'paynum: '. $self->paynum. "\n",
285 'custnum: '. $self->custnum.
286 " (". $cust_main->last. ", ". $cust_main->first. ")\n",
287 'paid: $'. sprintf("%.2f", $self->paid). "\n",
288 'date: '. time2str("%a %b %e %T %Y", $self->_date). "\n",
289 'payby: '. $self->payby. "\n",
290 'payinfo: '. $self->payinfo. "\n",
291 'paybatch: '. $self->paybatch. "\n",
295 $message->smtpsend( Host => $smtpmachine )
296 or $message->smtpsend( Host => $smtpmachine, Debug => 1 )
298 $dbh->rollback if $oldAutoCommit;
299 return "(customer # ". $self->custnum.
300 ") can't send payment deletion email to ".
301 $conf->config('deletepayments').
302 " via server $smtpmachine with SMTP: $!";
306 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
312 =item replace OLD_RECORD
314 Currently unimplemented (accounting reasons).
319 return "Can't (yet?) modify cust_pay records!";
324 Checks all fields to make sure this is a valid payment. If there is an error,
325 returns the error, otherwise returns false. Called by the insert method.
333 $self->ut_numbern('paynum')
334 || $self->ut_numbern('custnum')
335 || $self->ut_money('paid')
336 || $self->ut_numbern('_date')
337 || $self->ut_textn('paybatch')
338 || $self->ut_enum('closed', [ '', 'Y' ])
340 return $error if $error;
342 return "paid must be > 0 " if $self->paid <= 0;
344 return "unknown cust_main.custnum: ". $self->custnum
346 || qsearchs( 'cust_main', { 'custnum' => $self->custnum } );
348 $self->_date(time) unless $self->_date;
350 $self->payby =~ /^(CARD|CHEK|BILL|COMP)$/ or return "Illegal payby";
353 #false laziness with cust_refund::check
354 if ( $self->payby eq 'CARD' ) {
355 my $payinfo = $self->payinfo;
357 $self->payinfo($payinfo);
358 if ( $self->payinfo ) {
359 $self->payinfo =~ /^(\d{13,16})$/
360 or return "Illegal (mistyped?) credit card number (payinfo)";
362 validate($self->payinfo) or return "Illegal credit card number";
363 return "Unknown card type" if cardtype($self->payinfo) eq "Unknown";
365 $self->payinfo('N/A');
369 $error = $self->ut_textn('payinfo');
370 return $error if $error;
379 Returns all applications to invoices (see L<FS::cust_bill_pay>) for this
386 sort { $a->_date <=> $b->_date }
387 qsearch( 'cust_bill_pay', { 'paynum' => $self->paynum } )
393 Returns the amount of this payment that is still unapplied; which is
394 paid minus all payment applications (see L<FS::cust_bill_pay>).
400 my $amount = $self->paid;
401 $amount -= $_->amount foreach ( $self->cust_bill_pay );
402 sprintf("%.2f", $amount );
409 $Id: cust_pay.pm,v 1.21.4.1 2002-10-12 10:15:53 ivan Exp $
413 Delete and replace methods.
417 L<FS::cust_bill_pay>, L<FS::cust_bill>, L<FS::Record>, schema.html from the